Skip to content

Commit

Permalink
Docs: Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Jan 4, 2025
1 parent a9cf7ec commit c20e935
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
14 changes: 13 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,17 @@
"string.h": "c",
"cstdio": "c",
"wchar.h": "c"
}
},
"cSpell.words": [
"BLAS",
"dlsym",
"dlvsym",
"hsearch",
"LAPACK",
"libsee",
"memcpy",
"memset",
"PCRE",
"tsearch"
]
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
> _See where you use LibC the most._ <br/>
> _Trace calls failing tests. Then - roast!_
One-liner, to download and compile the script and run your vavorite query:
One-liner, to download and compile the script and run your favorite query:

```bash
gcc -g -O2 -fno-builtin -fPIC -nostdlib -nostartfiles -shared -o libsee.so libsee.c
```

LibSee overrides LibC symbols using `LD_PRELOAD`, profiling the most commonly used functions, and, optionally, fuzzing their behaviour for testing.
LibSee overrides LibC symbols using `LD_PRELOAD`, profiling the most commonly used functions, and, optionally, fuzzing their behavior for testing.
The library yields a few binaries when compiled:

```bash
libsee.so # Profiles LibC calls
libsee_and_knee.so # Correct LibC behaviour, but fuzzed!
libsee_and_knee.so # Correct LibC behavior, but fuzzed!
```

## Tricks Used
Expand All @@ -39,11 +39,11 @@ Feel free to suggest PRs covering the rest:
- [x] [algorithms](https://en.cppreference.com/w/c/algorithm)
- [x] [date and time](https://en.cppreference.com/w/c/chrono)
- [x] [input/output](https://en.cppreference.com/w/c/io)
- [x] [wide-character strings](https://en.cppreference.com/w/c/string/wide)
- [ ] [concurrency and atomics](https://en.cppreference.com/w/c/thread)
- [ ] retrieving error numbers
- [ ] [numerics](https://en.cppreference.com/w/c/numeric)
- [ ] [wide-character strings](https://en.cppreference.com/w/c/string/wide)
- [ ] [multibyte strings](https://en.cppreference.com/w/c/string/multibyte)
- [ ] [multi-byte strings](https://en.cppreference.com/w/c/string/multibyte)
- [ ] [wide-character IO](https://en.cppreference.com/w/c/io)
- [ ] [localization](https://en.cppreference.com/w/c/locale)
- [ ] anything newer than C 11
Expand Down
52 changes: 28 additions & 24 deletions libsee.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,25 +480,27 @@ void syscall_print(char const *buf, size_t count) {
// The system call number is passed in x8, and the arguments are in x0, x1, and x2.
long syscall_write = (long)64; // System call number for write in AArch64 Linux
long file_descriptor = (long)1;
asm volatile("mov x0, %1\n" // First argument: file descriptor
"mov x1, %2\n" // Second argument: buffer address
"mov x2, %3\n" // Third argument: buffer size
"mov x8, %4\n" // System call number: SYS_write (64)
"svc #0\n" // Make the system call
"mov %0, x0" // Store the return value
: "=r"(ret)
: "r"(file_descriptor), "r"(buf), "r"((long)count), "r"(syscall_write)
: "x0", "x1", "x2", "x8", "memory");
asm volatile( //
"mov x0, %1\n" // First argument: file descriptor
"mov x1, %2\n" // Second argument: buffer address
"mov x2, %3\n" // Third argument: buffer size
"mov x8, %4\n" // System call number: SYS_write (64)
"svc #0\n" // Make the system call
"mov %0, x0" // Store the return value
: "=r"(ret)
: "r"(file_descriptor), "r"(buf), "r"((long)count), "r"(syscall_write)
: "x0", "x1", "x2", "x8", "memory");
#elif defined(__x86_64__) || defined(__i386__)
// Inline assembly syntax for making a system call in x86-64 Linux.
// Uses the syscall instruction, passing the system call number in rax,
// and the call arguments in rdi, rsi, and rdx, respectively.
long syscall_write = (long)1; // System call number for write in x86-64 Linux
unsigned int file_descriptor = (unsigned int)1;
asm volatile("syscall"
: "=a"(ret)
: "a"(syscall_write), "D"(file_descriptor), "S"(buf), "d"(count)
: "rcx", "r11", "memory");
asm volatile( //
"syscall"
: "=a"(ret)
: "a"(syscall_write), "D"(file_descriptor), "S"(buf), "d"(count)
: "rcx", "r11", "memory");
(void)ret;
#endif
(void)buf;
Expand Down Expand Up @@ -539,18 +541,20 @@ void reopen_stdout(void) {
void close_stdout(void) {
long ret;
#ifdef __aarch64__
asm volatile("mov x0, 1\n" // File descriptor for stdout
"mov x8, 57\n" // Syscall number for 'close' in AArch64
"svc #0\n"
"mov %0, x0"
: "=r"(ret)
: // No inputs besides the syscall number and FD
: "x0", "x8", "memory");
asm volatile( //
"mov x0, 1\n" // File descriptor for stdout
"mov x8, 57\n" // Syscall number for 'close' in AArch64
"svc #0\n"
"mov %0, x0"
: "=r"(ret)
: // No inputs besides the syscall number and FD
: "x0", "x8", "memory");
#elif defined(__x86_64__)
asm volatile("syscall"
: "=a"(ret)
: "a"(3), "D"(1) // Inputs: syscall number for 'close', FD for stdout
: "rcx", "r11", "memory");
asm volatile( //
"syscall"
: "=a"(ret)
: "a"(3), "D"(1) // Inputs: syscall number for 'close', FD for stdout
: "rcx", "r11", "memory");
#endif
(void)ret;
}
Expand Down

0 comments on commit c20e935

Please sign in to comment.