theseus-os/Theseus

Undefined behaviour in aarch64 `logger`

tsoutsman opened this issue · 2 comments

1

pub static mut LOGGER: Logger = unsafe { MaybeUninit::uninit().assume_init() };

From the assume_init docs:

It is up to the caller to guarantee that the MaybeUninit really is in an initialized state. Calling this when the content is not yet fully initialized causes immediate undefined behavior.

We should either assume_init in log or wrap the logger in an Option.

2

let mutable = unsafe { (self as *const Self).cast_mut().as_mut().unwrap() };

From the Nomicon:

  • Transmuting an & to &mut is Undefined Behavior. While certain usages may appear safe, note that the Rust optimizer is free to assume that a shared reference won't change through its lifetime and thus such transmutation will run afoul of those assumptions. So:
    • Transmuting an & to &mut is always Undefined Behavior.
    • No you can't do it.
    • No you're not special.

We should use a mutex or unsafe cell.

@NathanRoyer can chime in here.

I think this will likely be removed once we have UEFI support for aarch64, since we plan to merge the code in /aarch64 into the main repo.

Thank you @tsoutsman for noticing this. Fixed in #784.