I have encountered UB in kernel code, as a result the code only works with debug output.
If I print the contents of a variable the code works, if I don't the code doesn't work, it looks like a UB occurs when allocating memory for that variable.
This code is kernel and runs in an emulator, so I have no way to run miri or anything like that.
Any tips on how I can try to discover the cause of the UB?
Not sure what exactly you mean by kernel, but Linux supports sanitizers : CONFIG_UBSAN=y
.
Otherwise, you might be able to build with sanitizers with -Z sanitizer=address
(or other sanitizers).
Disclaimer: I haven't used sanitizers in Rust yet.
It appears you are writing your own kernel and have a Heisenbug situation.
The best tool I know of for this kind of work is binary analysis. cargo asm
can be helpful to sift through the generated code. Use your emulator’s remote debugging capability with gdb
or lldb
.
Not sure how much work it would be to implement for your own kernel, but the Linux kernel supports kernel variants of the classic asan and ubsan sanitizers.
I don't know if they work with Rustc or just Clang, and I don't know how much of the implementation is in the kernel vs the compiler. I presume at minimum you need to provide functions for printing/logging reports as well as ways to tell the sanitizer what memory is allocated/freed.
But it might be worth investigating.