Sanitizing static freestanding nolibc programs?

231 views Asked by At

I'm developing a static freestanding nolibc/nostdlib program for Linux and would like to use the C compiler's memory, address and undefined behavior sanitizers to improve my code.

I couldn't get it to work when I tried it though:

clang -static -ffreestanding -nostdlib -fno-omit-frame-pointer -fsanitize=undefined -g -o program program.c

This causes the compiler to emit instrumentation code that calls functions such as __ubsan_handle_type_mismatch_v1@plt. It compiles and links successfully but the program segfaults near those references when run. More specifically, in my memory allocator:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) up
(gdb) disas
Dump of assembler code for function lone_reallocate:
...
   0x00000000002116d0 <+160>:   bl      0x206470 <__ubsan_handle_pointer_overflow@plt>
=> 0x00000000002116d4 <+164>:   b       0x2116d8 <lone_reallocate+168>
   0x00000000002116d8 <+168>:   ldur    x8, [x29, #-72]
...

I assume these functions are missing due to the lack of libc support. When I tried to use the -static-libsan option, I got numerous undefined sybol errors:

error: undefined symbol: __aarch64_cas8_acq_rel
error: undefined symbol: pthread_self
error: undefined symbol: dl_iterate_phdr
error: undefined symbol: abort
...

How can I make this work? Do I have to implement those functions? If so, how? I couldn't find any documentation on this matter.

Can operating systems and kernels use these sanitizers? If they can, then whatever method they use would also be applicable to my use case.

0

There are 0 answers