simdeez icon indicating copy to clipboard operation
simdeez copied to clipboard

`simd_runtime_generate!` creates implicit unsafe fn, allowing unsafe ops in seemingly safe code

Open jonas-schievink opened this issue 4 years ago • 3 comments

This program has undefined behavior but does not use unsafe:

extern crate simdeez;

use simdeez::avx2::*;
use simdeez::scalar::*;
use simdeez::sse2::*;
use simdeez::sse41::*;

simd_runtime_generate! {
    fn unsafe_deref(p: *const u8) -> u8 {
        *p
    }
}

fn main() {
    unsafe_deref_runtime_select(6 as *const u8);
}

jonas-schievink avatar Jun 28 '20 11:06 jonas-schievink

Good point, the macros should probably create unsafe functions, since all the intrinsics are unsafe. Do you agree with that as the solution?

jackmott avatar Jun 28 '20 12:06 jackmott

Most methods it creates are already unsafe, just not the runtime_select one. Making that unsafe as well would avoid this issue, so that seems fine.

I think there's a deeper issue with the fn unsafe_deref being sort of magically made into an unsafe fn though. I see this is needed to use pretty much any part of this library since so much is unsafe due to the use of the SIMD intrinsics, so I'm not sure how to solve that.

jonas-schievink avatar Jun 28 '20 12:06 jonas-schievink

@jonas-schievink : can you check this PR/branch and check if that does what you requested+

verpeteren avatar Jan 22 '23 19:01 verpeteren