zyantific / zydis-rs

Zydis Rust Bindings
MIT License
83 stars 14 forks source link

mismatched types on arm #26

Closed williballenthin closed 2 years ago

williballenthin commented 2 years ago

When cross compiling for armv7/aarch64 a crate that depends on zydis-rs, the compilation fails with a mismatched types error:

error[E0308]: mismatched types
   --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/zydis-3.1.1/src/ffi.rs:202:17
    |
202 |                 buffer as *mut i8,
    |                 ^^^^^^^^^^^^^^^^^ expected `u8`, found `i8`
    |
    = note: expected raw pointer `*mut u8`
               found raw pointer `*mut i8`

error[E0308]: mismatched types
   --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/zydis-3.1.1/src/ffi.rs:245:17
    |
245 |                 buffer.as_ptr() as *const i8,
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `i8`
    |
    = note: expected raw pointer `*const u8`
               found raw pointer `*const i8`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

error: could not compile `zydis` due to 3 previous errors
warning: build failed, waiting for other jobs to finish...
error: build failed

https://github.com/williballenthin/lancelot/runs/4215259537?check_suite_focus=true

Line 202 is https://github.com/zyantific/zydis-rs/blob/2089243f0610089e3f70ccf766a4a8413f41c746/src/ffi.rs#L200-L204

We see the buffer parameters to ZyanStringInitCustomBuffer should be a *mut c_char: https://github.com/zyantific/zydis-rs/blob/2089243f0610089e3f70ccf766a4a8413f41c746/src/ffi.rs#L1143-L1147

and the buffer above is casted to a *mut i8. This is correct on x64 but not arm:

https://github.com/rust-lang/rust/blob/eab2d7519a3f1c11ddaff3d19f8b7727354c6362/library/std/src/os/raw/mod.rs#L48-L55

I think this can be fixed on lines 202 and 245 with a change like here: https://github.com/jeaye/ncurses-rs/pull/100/files

williballenthin commented 2 years ago

I will attempt this fix on a fork and see how it goes, reporting back here.

williballenthin commented 2 years ago

PR in #27. With this patch, my project builds on both x86 and ARM: https://github.com/williballenthin/lancelot/runs/4215615280?check_suite_focus=true