-
Notifications
You must be signed in to change notification settings - Fork 141
Description
I started to write a PR to just show how using #[target_feature] would look like but I encountered too much undefined behavior in the process (fixing it was taking more time than actually dealing with target feature).
I've only taken a look at a tiny fraction of the aesni crate, but it seems that references to uninitialized memory are created pretty much everywhere (all references must point to initialized memory [0]):
* https://github.com/RustCrypto/block-ciphers/blob/master/aes/aesni/src/aes128/expand.rs#L21
* https://github.com/RustCrypto/block-ciphers/blob/master/aes/aesni/src/aes128/expand.rs#L36
The way I fixed these was by using ptr::write to write to uninitialized memory without creating a reference.
There are also reads memory out-of-bounds: https://github.com/RustCrypto/block-ciphers/blob/master/aes/aesni/src/aes192/expand.rs#L51
The way I fixed this was by creating a stack buffer of appropriate size in the mod.rs file which I initialized with mem::uninitialized, and then using ptr::write to write the GenericArray into the head of the buffer, so that the vector load never reads memory out of bounds - the memory read is only partially initialized though, and reading vector lanes with uninitialized memory should be done with care (so that no UB is introduced).
[0] &T as *const T and &mut T as *mut T might not end up being undefined behavior because they might end up being atomic operations in the memory model that create a raw pointer directly without creating a reference (so that no reference to uninitialized memory is ever created by these expressions, preventing them from invoking undefined behavior).