Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete bindings with Bindgen #1

Merged
merged 3 commits into from
Aug 21, 2017
Merged

Conversation

tmfink
Copy link
Member

@tmfink tmfink commented Aug 1, 2017

This wraps the entire capstone interface by generating bindings with bindgen. I think bindgen is the way to go because capstone will change over time and we need to be able to change the bindings quickly without manual effort.

For now, bindgen's wierd union type is used, but a pull request is in the pipeline to use Rust untagged unions.

I would love to hear feedback.

For example, I changed around the names of some features and added some new ones. The use_bundled_capstone_bindings feature does not require bindgen to run at build time and eliminates the dependency on Clang >= 3.9.

Todo:

  • Make CI pass
  • Builds on own
  • Builds with downstream client (capstone-rs)
  • Documentation on how to build (trade-offs with features)
  • Decide on features (including defaults)
  • Create pre-generated bindings for architectures/platforms separately

@m4b
Copy link
Collaborator

m4b commented Aug 1, 2017

@tmfink this looks totally awesome! I guess I'm sort of an old timer and like writing the bindings by hand, but you're right bindgen is the way to go for the Future.

So before merging:

  1. need to figure out why the CI is failing.
  2. Also I can't seem to build this for m4b/capstone-rs, modulo feature renames.

Also I'm fine with feature renames, but in general should try to avoid in the future. So make sure these are the names you want!

Otherwise this is great work!

@tmfink
Copy link
Member Author

tmfink commented Aug 1, 2017

Good to hear that you are receptive! I made no attempt to keep function/type names the same (bindgen likes to do its own thing), so I would not be surprised if m4b/capstone-rs does not work with this code (yet).

I'm receptive to changing the name of features, but I did not think that build_src clearly described that capstone should be built. I figured changing feature names before a stable release would be best.

@m4b
Copy link
Collaborator

m4b commented Aug 2, 2017

That's a pretty huge problem if the symbol names are different...

I would expect modulo feature names and some other minor details for capstone-rs to work completely out of the box?

How do I use the bindings I wrote; it's not quite clear to me from the feature names/ I can't get it working?

@m4b
Copy link
Collaborator

m4b commented Aug 2, 2017

So I can't even compile the branch...

   Compiling capstone-sys v0.2.0 (file:///home/m4b/git/capstone-sys)
warning: the option `Z` is unstable and should only be used on the nightly compiler, but it is currently accepted for backwards compatibility; this will soon change, see issue #31847 for more details

error: failed to run custom build command for `capstone-sys v0.2.0 (file:///home/m4b/git/capstone-sys)`
process didn't exit successfully: `/home/m4b/git/capstone-sys/target/debug/build/capstone-sys-bdbd61958ebe7050/build-script-build` (exit code: 101)
--- stderr
thread 'main' panicked at 'Must specify exactly one of features "use_bundled_capstone" or
                    "use_system_capstone"; (use_bundled_capstone, use_system_capstone) = (false, false)', build.rs:132
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Also when I try to actually use this in capstone-rs it just hangs.

So I didn't realize that bindgen pulls down a bunch of deps, which is a huge bummer.

Did you need to delete the handwritten bindings? Seems they could have been easily put in a feature flag, made bindgen dep be optional (like cmake), and then I wouldn't have to update capstone-rs. As it stands, I simply don't have the time to update capstone-rs to new bindings (and test) and etc.

m4b
m4b previously requested changes Aug 2, 2017
Copy link
Collaborator

@m4b m4b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't merge this until:

  1. The CI passes
  2. It builds
  3. It builds with a downstream client, capstone-rs for example
  4. The bindgen feature flag thing is decided on
  5. What the scope of the symbol changes are; it worries me that capstone function names and arguments would differ; that seems directly like an ABI breaking change, which would be terrifying if bindgen does that...

Cargo.toml Outdated

[features]
# use bundled capstone and bindings by default
default = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to set the default feature here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving the comment was a mistake; my intention was to require exactly one of the use_bundled_capstone or use_system_capstone features.

#![allow(non_camel_case_types)]
extern crate libc;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you should have deleted these bindings, at least for time being could put behind a feature flag.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured that if you prefer, you could make a new "bindgen-wip" branch. Once you felt that it was in good enough condition, you could merge the changes to master.

It would be confusing to a consumer of this library that the interface is different based on which features are specified.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp, good point. Also I believe it technically breaks cargo. So it's bindgen or nothing then...

@@ -12,11 +12,17 @@ repository = "https://github.com/m4b/capstone-sys"
libc = "0.2"

[build-dependencies]
bindgen = "0.26.3"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be optional with undeleted bindings; just need to add a feature flag on the bindings and reexport iiuc

@tmfink
Copy link
Member Author

tmfink commented Aug 2, 2017

The function names/arguments should not be different because bindgen creates them directly from the headers. Assuming the function definitions were originally defined correctly, they should match.

However, the variables will be often be different. For example, currently you define CS_ARCH_ARM as

pub type cs_arch = u16;
pub const CS_ARCH_ARM: cs_arch = 0;

wheras bindgen generates:

#[repr(u32)]
pub enum cs_arch {                                                                                                                       
    CS_ARCH_ARM = 0,
    // ...
}

There are several correct ways to define these. I think that by the C standard, the compiler could use either 16 or 32 bit types.

@m4b
Copy link
Collaborator

m4b commented Aug 2, 2017

Ok, yea, I figured enums, etc might be slightly different, but you had me worried when I though you meant the function symbols were different.

@m4b
Copy link
Collaborator

m4b commented Aug 2, 2017

I don't have a lot of time to give to testing this, but I added you as collaborator; feel free to push a new branch, say bindgen, and mess around. As I said though, I'd like to see all the points above before I'd feel comfortable merging

@tmfink
Copy link
Member Author

tmfink commented Aug 8, 2017

Thanks for the access! I'll also need to make another branch of capstone-rs that uses capstone-sys.

I would also like it if the capstone-rs of @m4b and @richo merged. I think that having multiple implementations hurts the community.

@m4b
Copy link
Collaborator

m4b commented Aug 8, 2017

I agree, and tried a PR but they never acknowledged or responded.

I think it's a favorite pasttime these days to write capstone bindings. There are at least 2 more (besides these two) I know about 😆

@m4b
Copy link
Collaborator

m4b commented Aug 8, 2017

I gave you access over there too, knock yourself out !

@m4b
Copy link
Collaborator

m4b commented Aug 8, 2017

Also feel free to try to merge again, or ask if they don't want to maintain maybe they can transfer ownership to you

@richo-stripe
Copy link

oh hey. github notifications get lost pretty easily, was the PR on my bindings?

I spoke to someone (you?) about having capstone-rs use capstone-sys under the hood, which I'm totally on board with but also don't have time to implement.

@tmfink
Copy link
Member Author

tmfink commented Aug 18, 2017

@richo-stripe we talked about modifying richo/capstone-rs to use m4b/capstone-sys.

However, I think it will be easier to modify m4b/capstone-rs to use the bindgen version of m4b/capstone-sys since it already consumes capstone-sys.

Once my changes merged into @m4b's capstone-sys and capstone-rs, I would like to merge the capstone-rs changes into richo/capstone-rs. Then, I want to consolidate the repositories to minimize the number of forks.

Possible solutions:

  1. Make a new capstone-rust GitHub group (and put both capstone-rs/capstone-sys there)
  2. Maintain capstone-rs under @richo-stripe and capstone-sys under @m4b (their original locations)

We should really reduce the fragmentation of capstone's rust bindings.

@m4b
Copy link
Collaborator

m4b commented Aug 18, 2017

/cc @endeav0r I know you have a fork too, so probably interested 😆

@m4b
Copy link
Collaborator

m4b commented Aug 18, 2017

So in case this wasn't clear:

  1. I don't really want to maintain either sys or high level bindings. I created them because old ones did not work sufficient to my needs.
  2. I am 100% ok with transferring this repo, adding an admin, transferring crates.io, whatever iff maintainer is responsible and timely

@tmfink
Copy link
Member Author

tmfink commented Aug 20, 2017

@m4b I would not mind maintaining the sys and high-level bindings =)

@m4b
Copy link
Collaborator

m4b commented Aug 20, 2017

@tmfink I'm ok with that.

What needs to be done next, besides getting it passing on CI?

I imagine high level bindings need to be updated as well, etc.?

I'll let you figure out how you want to integrate/re-merge/whatever with other capstone binding forks/projects :D

@tmfink
Copy link
Member Author

tmfink commented Aug 20, 2017

@m4b I already have https://github.com/m4b/capstone-rs/tree/bindgen-sys-wip working with this capstone-sys repo. I'm currently fixing the CI problem by maintaining a copy capstone without a git submodule.

The only other item I want to do is have a separate pre-generated capstone.rs for each architecture/platform. I imagine that different target CPU architectures may have slightly different bindgen output. Even if we only maintain pre-generated x86_64/Linux bindings, we want people using the use_bundled_capstone_bindings feature on other platforms to see an understandable error instead of running in weird run-time errors.

If it turns out that the bindgen-generated bindings are identical between platforms, then I'll just mark the existing work as done.

@tmfink
Copy link
Member Author

tmfink commented Aug 20, 2017

@m4b it looks like bindgen generates the same bindings for different platforms. I did not see any difference when targetting the following platforms (by passing the --target option to `cargo build):

arm-linux-androideabi
armv7-unknown-linux-musleabihf
i686-unknown-linux-gnu
powerpc-unknown-linux-gnu
x86_64-unknown-linux-gnu

@m4b
Copy link
Collaborator

m4b commented Aug 20, 2017

Ok you're saying it's ready to merge ?

@m4b
Copy link
Collaborator

m4b commented Aug 20, 2017

Actually I don't understand, why did the submodule need to disappear? Now capstone version/git commit isn't tracked, iiuc?

@m4b
Copy link
Collaborator

m4b commented Aug 20, 2017

E.g., I had a submodule before, and CI had no issues with it.

I'm not sure getting rid of the submodule is a good idea.

@tmfink
Copy link
Member Author

tmfink commented Aug 21, 2017

@m4b I would say this is ready to merge--except I want to squash some of the commits to minimize history pollution.

The capstone version is tracked with in the scripts/update_capstone.sh script.

Git submodules cause the following problems:

  • Travis CI ran into a hard-to-diagnose "error: Server does not allow request for unadvertised object"; I ran the same commands on an Ubuntu Trusty VM but they succeeded. I don't know what was causing the problem.
  • If the repository was not cloned recursively, then the capstone library would need to be fetched over git. This requires git and network access, even if you just downloaded a source tarball. We want users to be able to clone the repository (or download through some other means) and have all that's required to build.
  • They complicate use for rdevelopers because you can't just run git clone normally to get all of the code.

The following is a good read:
https://codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/

tmfink added 3 commits August 20, 2017 17:25
Using git submodules caused the following problem:

* Travis CI ran into a hard-to-diagnose "error: Server does not allow
  request for unadvertised object"
* If the repository was not cloned recursively, then the capstone
  library would need to be fetched over git. This requires git and
  network access.

`scripts/update_capstone.sh` can be used to update the bundled `capstone`.
Also reformatted code with rustfmt.
@m4b
Copy link
Collaborator

m4b commented Aug 21, 2017

Okie if you think it's best.

@m4b
Copy link
Collaborator

m4b commented Aug 21, 2017

@tmfink You can merge the branch now if you think its ready (once CI passes)

@tmfink tmfink dismissed m4b’s stale review August 21, 2017 00:42

implemented

@tmfink tmfink merged commit d3855d2 into capstone-rust:master Aug 21, 2017
@m4b
Copy link
Collaborator

m4b commented Aug 21, 2017

Do you want me to publish a new version on crates.io or you want to improve more stuff ?

@tmfink
Copy link
Member Author

tmfink commented Aug 21, 2017

@m4b I think publishing a new version would make sense. The heavy lifting will be done for capstone-rs.

@m4b
Copy link
Collaborator

m4b commented Aug 21, 2017

k, will publish right now; should i transfer this repo to the org you just made? (i assume this is possible)

@m4b
Copy link
Collaborator

m4b commented Aug 21, 2017

hum, why wouldn't this be a fast-forward?

remote: Counting objects: 733, done.
remote: Compressing objects: 100% (572/572), done.
remote: Total 733 (delta 123), reused 721 (delta 119), pack-reused 6
Receiving objects: 100% (733/733), 2.92 MiB | 259.00 KiB/s, done.
Resolving deltas: 100% (124/124), completed with 1 local object.
From github.com:m4b/capstone-sys
   fcc8eac..d3855d2  master      -> origin/master
 * [new branch]      bindgen-wip -> origin/bindgen-wip
Updating fcc8eac..d3855d2
error: The following untracked working tree files would be overwritten by merge:
	capstone/.appveyor.yml
	capstone/.gitignore
	capstone/.travis.yml
	capstone/CMakeLists.txt
	capstone/COMPILE.TXT
	capstone/COMPILE_CMAKE.TXT
	capstone/COMPILE_MSVC.TXT
	capstone/CREDITS.TXT
	capstone/ChangeLog
	capstone/HACK.TXT
	capstone/LEB128.h
	capstone/LICENSE.TXT
	capstone/LICENSE_LLVM.TXT
	capstone/MCDisassembler.h
	capstone/MCFixedLenDisassembler.h
	capstone/MCInst.c
	capstone/MCInst.h
	capstone/MCInstrDesc.c
	capstone/MCInstrDesc.h
	capstone/MCRegisterInfo.c
	capstone/MCRegisterInfo.h
	capstone/Makefile
	capstone/MathExtras.h
	capstone/README
	capstone/RELEASE_NOTES
	capstone/SStream.c
	capstone/SStream.h
	capstone/TODO
	capstone/arch/AArch64/AArch64AddressingModes.h
	capstone/arch/AArch64/AArch64BaseInfo.c
	capstone/arch/AArch64/AArch64BaseInfo.h
	capstone/arch/AArch64/AArch64Disassembler.c
	capstone/arch/AArch64/AArch64Disassembler.h
	capstone/arch/AArch64/AArch64GenAsmWriter.inc
	capstone/arch/AArch64/AArch64GenDisassemblerTables.inc
	capstone/arch/AArch64/AArch64GenInstrInfo.inc
	capstone/arch/AArch64/AArch64GenRegisterInfo.inc
	capstone/arch/AArch64/AArch64GenSubtargetInfo.inc
	capstone/arch/AArch64/AArch64InstPrinter.c
	capstone/arch/AArch64/AArch64InstPrinter.h
	capstone/arch/AArch64/AArch64Mapping.c
	capstone/arch/AArch64/AArch64Mapping.h
	capstone/arch/AArch64/AArch64Module.c
	capstone/arch/ARM/ARMAddressingModes.h
	capstone/arch/ARM/ARMBaseInfo.h
	capstone/arch/ARM/ARMDisassembler.c
	capstone/arch/ARM/ARMDisassembler.h
	capstone/arch/ARM/ARMGenAsmWriter.inc
	capstone/arch/ARM/ARMGenDisassemblerTables.inc
	capstone/arch/ARM/ARMGenInstrInfo.inc
	capstone/arch/ARM/ARMGenRegisterInfo.inc
	capstone/arch/ARM/ARMGenSubtargetInfo.inc
	capstone/arch/ARM/ARMInstPrinter.c
	capstone/arch/ARM/ARMInstPrinter.h
	capstone/arch/ARM/ARMMapping.c
	capstone/arch/ARM/ARMMapping.h
	capstone/arch/ARM/ARMModule.c
	capstone/arch/Mips/MipsDisassembler.c
	capstone/arch/Mips/MipsDisassembler.h
	capstone/arch/Mips/MipsGenAsmWriter.inc
	capstone/arch/Mips/MipsGenDisassemblerTables.inc
	capstone/arch/Mips/MipsGenInstrInfo.inc
	capstone/arch/Mips/MipsGenRegisterInfo.inc
	capstone/arch/Mips/MipsGenSubtargetInfo.inc
	capstone/arch/Mips/MipsInstPrinter.c
	capstone/arch/Mips/MipsInstPrinter.h
	capstone/arch/Mips/MipsMapping.c
	capstone/arch/Mips/MipsMapping.h
	capstone/arch/Mips/MipsModule.c
	capstone/arch/PowerPC/PPCDisassembler.c
	capstone/arch/PowerPC/PPCDisassembler.h
	capstone/arch/PowerPC/PPCGenAsmWriter.inc
	capstone/arch/PowerPC/PPCGenDisassemblerTables.inc
	capstone/arch/PowerPC/PPCGenInstrInfo.inc
	capstone/arch/PowerPC/PPCGenRegisterInfo.inc
	capstone/arch/PowerPC/PPCGenSubtargetInfo.inc
	capstone/arch/PowerPC/PPCInstPrinter.c
	capstone/arch/PowerPC/PPCInstPrinter.h
	capstone/arch/PowerPC/PPCMapping.c
	capstone/arch/PowerPC/PPCMapping.h
	capstone/arch/PowerPC/PPCModule.c
	capstone/arch/PowerPC/PPCPredicates.h
	capstone/arch/Sparc/Sparc.h
	capstone/arch/Sparc/SparcDisassembler.c
	capstone/arch/Sparc/SparcDisassembler.h
	capstone/arch/Sparc/SparcGenAsmWriter.inc
	capstone/arch/Sparc/SparcGenDisassemblerTables.inc
	capstone/arch/Sparc/SparcGenInstrInfo.inc
	capstone/arch/Sparc/SparcGenRegisterInfo.inc
	capstone/arch/Sparc/SparcGenSubtargetInfo.inc
	capstone/arch/Sparc/SparcInstPrinter.c
	capstone/arch/Sparc/SparcInstPrinter.h
	capstone/arch/Sparc/SparcMapping.c
	capstone/arch/Sparc/SparcMapping.h
	capstone/arch/Sparc/SparcModule.c
	capstone/arch/SystemZ/SystemZDisassembler.c
	capstone/arch/SystemZ/SystemZDisassembler.h
	capstone/arch/SystemZ/SystemZGenAsmWriter.inc
	capstone/arch/SystemZ/SystemZGenDisassemblerTables.inc
	capstone/arch/SystemZ/SystemZGenInstrInfo.inc
	capstone/arch/SystemZ/SystemZGenRegisterInfo.inc
	capstone/arch/SystemZ/SystemZGenSubtargetInfo.inc
	capstone/arch/SystemZ/SystemZInstPrinter.c
	capstone/arch/SystemZ/SystemZInstPrinter.h
	capstone/arch/SystemZ/SystemZMCTargetDesc.c
	capstone/arch/SystemZ/SystemZMCTargetDesc.h
	capstone/arch/SystemZ/SystemZMapping.c
	capstone/arch/SystemZ/SystemZMapping.h
	capstone/arch/SystemZ/SystemZModule.
Aborting

@m4b
Copy link
Collaborator

m4b commented Aug 21, 2017

it's probably too late but i also realized that bundling capstone forces users who want the system bindings to still download a 24mb repo; just a point in favor of git submodules

@m4b
Copy link
Collaborator

m4b commented Aug 21, 2017

alright published 0.3 on crates.io

@tmfink
Copy link
Member Author

tmfink commented Aug 21, 2017

@m4b I think the error you saw was because the individual files under capstone/ were not tracked by the repo, they were a submodule. Git saw them as untracked files.

I think the 24MB size is regrettable, but it's actually less data than normal. A git checkout of capstone is 57MB (because of the entire git history).

Please transfer this repo under https://github.com/capstone-rust =)
If possible, changing the pointers on crates.io would also be nice.

Thanks for the crates.io publish!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants