Page 1 of 1

Clang KASAN unsupported option '-fsanitize=kernel-address' for target 'x86_64-pc-none-elf'

Posted: Tue Dec 24, 2024 5:57 am
by songziming
Hi,

I want to add KASAN to my kernel, the compiling command line is

Code: Select all

clang -DC_FILE -std=c11 -c -Ikernel_new -Ikernel_new/arch_x86_64 -ffunction-sections -fdata-sections -target x86_64-pc-none-elf -flto -Wall -Wextra -Wshadow -Werror=implicit -g -gdwarf-5 -DDEBUG -fstack-protector -fno-omit-frame-pointer -fsanitize=undefined -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-3dnow -mno-fma -fsanitize=kernel-address -mllvm -asan-mapping-offset=0xdfffe00000000000 -mllvm -asan-globals=false -ffreestanding -fno-builtin -MT build/kernel_new/arch_x86_64/apic/apic_init.c.ko -MMD -MP -MF build/kernel_new/arch_x86_64/apic/apic_init.c.ko.d -o build/kernel_new/arch_x86_64/apic/apic_init.c.ko kernel_new/arch_x86_64/apic/apic_init.c
But I got an error:

Code: Select all

clang: error: unsupported option '-fsanitize=kernel-address' for target 'x86_64-pc-none-elf'
If I remove `-target x86_64-pc-none-elf`, kernel compiles without problem, but hangs during boot (page fault, im still working on it).

Is it safe to remove target triplet? What if I want to cross compile my kernel for another arch?

My kernel is 64-bit, the compiler clang is built from source using default settings.

Code: Select all

cmake ../llvm-project/llvm \
    -DCMAKE_INSTALL_PREFIX=/opt/llvm \
    -DLLVM_ENABLE_PROJECTS="clang;lld;compiler-rt" \
    -DLLVM_TARGETS_TO_BUILD=X86 \
    -DCMAKE_BUILD_TYPE=Release

Re: Clang KASAN unsupported option '-fsanitize=kernel-address' for target 'x86_64-pc-none-elf'

Posted: Sat Dec 28, 2024 3:12 am
by eekee
If you remove the target triplet, it's going to assume some target which is probably the OS you're running on, not "none". (Or are you using a cross-compiler? I'm don't remember if clang needs to be built as a cross-compiler.)

Given that the triplet's OS type is "none", I'm not surprised '-fsanitize=kernel-address' is unsupported. Clang can't know what's a kernel address and what's userspace when it doesn't know what the OS is or even if there's an OS at all. It might work for some architectures which enforce higher half = kernel space, but I don't think x86_64 is one of those.