c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

Generated Rust for inline assembly violates borrow-checking rules

Open oinoom opened this issue 3 years ago • 0 comments

For the following example:

int six(void) {
    int out = 0;
    int six = 6;

    asm("add %0, %1\n\t"
        : "=r"(out)
        : "r"(six), "0"(out));

    return out;
}

the following rust code is generated

#[no_mangle]
pub unsafe extern "C" fn rust_six() -> libc::c_int {
    let mut out: libc::c_int = 0 as libc::c_int;
    let mut six: libc::c_int = 6 as libc::c_int;
    let fresh9 = &mut out;
    let fresh10;
    let fresh11 = out;
    asm!(
        "add {0}, {1}\n\t", inlateout(reg) c2rust_asm_casts::AsmCast::cast_in(fresh9,
        fresh11) => fresh10, inlateout(reg) six => _, options(preserves_flags, pure,
        readonly)
    );
    c2rust_asm_casts::AsmCast::cast_out(fresh9, fresh11, fresh10);
    return out;
}

which results in the following violations

error[E0503]: cannot use `out` because it was mutably borrowed
  --> src/asm.rs:71:19
     |
69 |     let fresh9 = &mut out;
     |                  -------- borrow of `out` occurs here
70 |     let fresh10;
71 |     let fresh11 = out;
     |                   ^^^ use of borrowed `out`
72 |     asm!(
73 |         "add {0}, {1}\n\t", inlateout(reg) c2rust_asm_casts::AsmCast::cast_in(fresh9,
     |                                                                               ------ borrow later used here

oinoom avatar May 10 '22 19:05 oinoom