rust-lang / miri

An interpreter for Rust's mid-level intermediate representation
Apache License 2.0
4.31k stars 329 forks source link

Shims ICE when a scalar argument isn't actually a scalar #3842

Open RalfJung opened 2 weeks ago

RalfJung commented 2 weeks ago

For instance, consider:

extern "C" {
    fn pipe(pipefd: [i32; 2]) -> i32;
}

fn main() {
    let mut fds: [i32; 2] = [0; 2]; 
    assert_eq!(unsafe { pipe(fds) }, 0); 
}

I think to fix this properly we need to completely re-do the way we handle shim arguments: every OpTy needs to be transmuted to the right type before we do anything else with it. And we can't just just call OpTy::transmute as that can't be used for arbitrary transmute, e.g. changing ABIs can fail spectacularly.

Once this is consistently done everywhere, we can also remove the ScalarSizeMismatch error from the interpreter. This was added because otherwise ICEs can be triggered by using an incorrect scalar type in the signature of a function that has a Miri shim, but as we have seen, ICEs can still be triggered -- and once we use our own hard-coded types everywhere, ScalarSizeMismatch can no longer be triggered at all.

We probably want a macro that helps with that.

RalfJung commented 2 weeks ago

I think we want a new function like get_arg or so that preprocesses the OpTy. Given an op: OpTy and ty: Ty, we do a check like