zyantific / zydis-rs

Zydis Rust Bindings
MIT License
83 stars 14 forks source link

type mismatch for ZydisMnemonic and ZydisMnemonics #18

Closed williballenthin closed 5 years ago

williballenthin commented 5 years ago

I'd like to inspect the mnemonic of decoded instructions in order to find specific instructions. This is not as easy as I'd like because of a type mismatch between the mnemonic constants and the mnemonic field.

A ZydisDecodedInstruction_ has a field mnemonic with type ZydisMnemonic:

image

ZydisMnemonic is an alias for u16:

image

The mnemonic constants exported have type ZydisMnemonics:

image

And, unfortunately, ZydisMnemonics is an alias for c_uint:

image

This prevents me from directly comparing the mnemonic field of a decoded instruction to a constant exported by Zydis:

image

image

Would you consider changing the type of ZydisMnemonic or ZydisMnemonics so that they are the same?

williballenthin commented 5 years ago

Of course, I can cast the mnemonic field as an i32; however, it seems like I shouldn't have to do this manually.

image

th0rex commented 5 years ago

Yes, this is an artifact of the currently released version on crates.io using bindgen and using a released zydis version. Zydis v2.0.3 (the latest release) used to typedef ZydisU16 ZydisMnemonic; and then have a seperate enum ZydisMnemonics with all the values (see here for more context). Thus bindgen also generates two different types for that.

On the current master branch, we've moved away from bindgen for various reasons, and also are tracking the zydis development branch, instead of a tagged release, so the problem you're having doesn't exist there anymore. I'm holding off a release on crates.io until a new zydis version gets officially released. I think in the meantime your only option is to either life with the casts, or use these bindings from git directly (theres an example in the readme). There have been some breaking changes however, all enums are now properly scoped. ZYDIS_MNEMONIC_JMP would turn into Mnemonic::JMP, etc. As far as I remember all the other breaking changes should only involve the formatter, and especially only formatter hooks.

williballenthin commented 5 years ago

thanks for the explanation, @th0rex!

I think for the time being I'll cast things explicitly, but definitely intend to upgrade once the new release is out.

williballenthin commented 5 years ago

as a short updated, i've updated my project to use zydis-rs master, and things are much nicer here. it was easy to switch over, and the types are better. nice work!