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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
songziming
Member
Member
Posts: 71
Joined: Fri Jun 28, 2013 1:48 am
Contact:

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

Post 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
Reinventing the Wheel, code: https://github.com/songziming/wheel
User avatar
eekee
Member
Member
Posts: 892
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

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

Post 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.
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
Post Reply