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

rustc-link-arg does not propagate transitively #9554

Closed
tronical opened this issue Jun 7, 2021 · 13 comments
Closed

rustc-link-arg does not propagate transitively #9554

tronical opened this issue Jun 7, 2021 · 13 comments
Assignees
Labels
A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug disposition-close finished-final-comment-period FCP complete T-cargo Team: Cargo

Comments

@tronical
Copy link

tronical commented Jun 7, 2021

Problem
With #9523 the following use-case does not compile anymore with nightly. It works with current stable.

  1. A -sys crate links against an external library using cargo:rustc-link-lib/cargo:rustc-link-lib-framework in its build.rs. The crate is to be consumed by a cdylib crate. To make consumption easy, build.rs of the -sys crate also prints out cargo:rustc-cdylib-link-arg=-Wl,-rpath,/some/path.
  2. The cdylib crate depends on the -sys crate and the cdylib is linked in a way that the external library becomes a dependency for the dynamic linker and the rpath is set due to the propagation of rustc-cdylib-link-arg from the -sys crate to the final cdylib crate.

With stable rust this works as described, with nightly this now produces a build error when compiling the -sys crate due to the use of rustc-cdylib-link-arg in the non-cdylib -sys crate.

Concrete example:

Building the qttypes crate from https://github.com/woboq/qmetaobject-rs/tree/master/qttypes produces the error that #9523 introduced:

$ cargo +nightly build
   Compiling qttypes v0.2.1 (/Users/simon/src/qmetaobject-rs/qttypes)
error: invalid instruction `cargo:rustc-cdylib-link-arg` from build script of `qttypes v0.2.1 (/Users/simon/src/qmetaobject-rs/qttypes)`
The package qttypes v0.2.1 (/Users/simon/src/qmetaobject-rs/qttypes) does not have a cdylib target.

Possible Solution(s)

Allow rustc-cdylib-link-arg again in non-cdylib crates to avoid breaking their build with the next stable release.

Notes

Output of cargo version:

cargo 1.54.0-nightly (0cecbd6 2021-06-01)
cargo 1.52.0 (6976741 2021-04-21)

@tronical tronical added the C-bug Category: bug label Jun 7, 2021
@joshtriplett
Copy link
Member

@tronical We discussed this in the @rust-lang/cargo meeting today.

The behavior on stable wasn't intended to pass the flags to any package other than the current one. Up till 1.50, it was silently ignored. From 1.50 to 1.52, it was incorrectly propagating to the final linking package. On nightly, an error was added to indicate that the package doesn't have a cdylib. The behavior on stable wasn't known at the time.

A crate shouldn't be able to silently affect the link arguments of the crate depending on it; the crate depending on it should have to opt into that by calling a dependency-provided function from its own build script or similar. (Such a function could reference metadata provided by the dependency, just as it could currently reference such metadata to find things like C include paths.)

In general, rpath is something that a dependency can't safely set for a crate that depends on it; that might work for a specific cooperating set of crates, but not for an arbitrary dependency. Different crates may want to set rpath in different ways. (And, for that matter, different builders/distributors of executables may want to set it differently; for instance, Linux distributions typically do not want rpath set.)

So, rustc-cdylib-link-arg shouldn't affect the linker invocations of depending crates. To avoid breaking builds, we will make this a warning rather than an error for now, though we may make it an error in the future. We suggest altering your build scripts to only pass rustc-cdylib-link-arg within the package that contains the cdylib.

@ehuss ehuss self-assigned this Jun 9, 2021
@ehuss ehuss added A-linkage Area: linker issues, dylib, cdylib, shared libraries, so A-build-scripts Area: build.rs scripts labels Jun 9, 2021
@ogoffart
Copy link

ogoffart commented Jun 9, 2021

A crate shouldn't be able to silently affect the link arguments of the crate depending on it;

Why not?
For example, in C++ with cmake, this is quite typical to do target_link_options with INTERFACE and the link flags are propagated to all the dependees.
I would say that this is a valid use case to add some flags from the -sys crate, so that everything is setup for any crate using it.
I wouldn't call this behavior "silent".

the crate depending on it should have to opt into that by calling a dependency-provided function from its own build script or similar.

That's quite an overkill workaround. I've seen crates doing that (e.g, neon) but i guess it's only because it did not work with previous versions.
Asking every crate at the bottom of the tree to use a build script and depending on a build crate because one crate deep in the hierarchy might need a specific flag is quite not convenient when one could just tell cargo to do the right thing with something like rustc-cdylib-link-arg

In general, rpath is something that a dependency can't safely set for a crate that depends on it [...]. Different crates may want to set rpath in different ways.

This is off topic, but i was under the impression rpath are additive, so that different crates just append all the rpath they need.

(And, for that matter, different builders/distributors of executables may want to set it differently; for instance, Linux distributions typically do not want rpath set.)

That's true, but on the other hand, we added the rpath on our project because people filling github issues about our crate "not working" as it couldn't load shared libraries. We thought rpath was a good way to fix this problem to get a better out-of-the-box experience. Even of packagers, rpath doesn't hurt, does it?

@joshtriplett
Copy link
Member

I would say that this is a valid use case to add some flags from the -sys crate, so that everything is setup for any crate using it.

Linker options can conflict or create various issues, and may break if the depending crate is doing something non-trivial with linking, or if they have different needs than one of their dependencies might expect.

And there's no way for the depending crate to control or filter out those options, if they're propagated from a dependency. In many cases, the depending crate may not even see those options; nothing will even display them unless you verbosely display your link line or the output of dependency build scripts.

Also, where to load shared libraries from can be a very project-specific or distribution-specific problem of release engineering. Binaries may be built on one system, packaged up, and deployed to other systems, with shared libraries in a different place than they were on the build system. In general, the -sys crate can't know the right solution for many use cases.

If we were going to support this, I could imagine doing so by allowing crates to declaratively (without using a build.rs script) state that they want to use link flags from a specific dependency's metadata (DEP_THEDEPNAME_LINK_FLAGS, for instance).

But beyond that, rpath is one of many things that we should really have a standard solution to. People should be able to make a single top-level decision like "I want rpath set in this common way" or "I want rpath set in this uncommon way" or "I don't want rpath set at all", and then crates supplying shared libraries should be able to give cargo enough information that it could set that consistently with the user's preferences and those of the top-level crate.

This is off topic, but i was under the impression rpath are additive, so that different crates just append all the rpath they need.
Even of packagers, rpath doesn't hurt, does it?

It can hurt, for various reasons. https://wiki.debian.org/RpathIssue gives one explanation. An rpath may also cause libraries to be loaded from an unexpected path; that may have security implications, or may break if there are other libraries in that path. It's important for people building crates to be able to control where they load libraries from.

@tronical
Copy link
Author

Perhaps the -sys crate can't know the right solution for many use-cases, but I believe that it should be in a position to make some decisions and it can take "user input" into account. If the consumers of the crate have to express that they want to use link flags from a dependency's meta-data and it also needs to be specified by the consumers of the consuming crate, then it becomes hard to use. We want the crate that we are developing to be easy to use. It's okay if the build fails, but it's bad if everything compiles but the program can't be started because the dynamic linker can't find a library that the regular linker clearly found at build time.

The feature that build systems like CMake offer is the encapsulation, that a CMake target can be used without having to know what other things it depends on. If I create a library with CMake and say that it needs this particular linker flag, then I can be sure that in a dynamic build the library will be linked with the flag and in a static build that flag will be forwarded until a shared library (or regular binary) is created. The only equivalent that I can think of from before that were lib tool archives.

I agree that built-in support for rpath in Cargo would be awesome, combined with a single top-level decision as you pointed out. It would be great to have a solution in the meantime, which is what rustc-cdylib-link-arg seems to offer - at the risk of being imperfect.

In any case, thanks for considering this issue and our input :-). It's much appreciated that you plan on making this a warning again, instead of an error.

@ehuss ehuss changed the title rustc-link-arg validation breaks link-arg propagation from -sys crates rustc-link-arg does not propagate transitively Jul 2, 2021
@ivmarkov
Copy link

ivmarkov commented Jul 4, 2021

Given that this issue was recently closed as a duplicate of this one, let me state that the scope of the problem is not only about passing an rpath from a -SYS crate down to a 'cdylib' crate.

We would like to pass - transitively of course - ALL sorts of linker args from a -SYS crate all the way down to a binary (as opposed to cdylib) crate.

The above is very, very useful in the embedded world when the SYS crate is modelled to represent the unsafe bindings to the Vendor-provided SDK for the MCU, as well as the vendor-specific linker args (including custom linker scripts and whatnot) that have to be passed through to the binary crate.

@kaspar030
Copy link

Just to chime in: This also broke our build, in embedded context. In RIOT-rs, one crate compiles and links a static library (from C code). that crate also needs to link in newlib (-lc_nano). Previously, with "-Zextra-link-args", that worked fine. Not anymore.

@joshtriplett joshtriplett added the T-cargo Team: Cargo label Jul 26, 2021
@joshtriplett
Copy link
Member

Opening a poll to check consensus among the rest of the team.

Should we confirm that the current behavior is correct, and dependencies shouldn't be able to affect the linker command-line of the depending crate without some kind of explicit opt-in from the depending crate?

@rfcbot close

@rfcbot
Copy link
Collaborator

rfcbot commented Jul 26, 2021

Team member @joshtriplett has proposed to close this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-close final-comment-period FCP — a period for last comments before action is taken and removed proposed-final-comment-period An FCP proposal has started, but not yet signed off. labels Jul 26, 2021
@rfcbot
Copy link
Collaborator

rfcbot commented Jul 26, 2021

🔔 This is now entering its final comment period, as per the review above. 🔔

@Lokathor
Copy link

My final comment is that this is poor for the embedded space where the API crate for a particular device, which includes the necessary linker script for said device, can't send the linker script along to binaries built for the device that depend on the API crate.

@ivmarkov
Copy link

ivmarkov commented Jul 28, 2021

I do agree with @Lokathor that not having any form of transitive arguments passing from the API/SYS crate to the binary one is very suboptimal. At the same time, I also agree that such a scheme would ideally require an explicit opt-in from the binary crate.

My only concern is that if the current restricted scheme is stabilized in its current form, future transitive + explicit opt-in extensions might take a lot of time to stabilize or not receive enough attention for that to happen at all in the foreseeable future.

@joshtriplett
Copy link
Member

joshtriplett commented Jul 28, 2021

To be clear, I'm quite amenable to proposals for opt-in linker arguments; I just wouldn't want them to happen implicitly without the explicit opt-in from the top-level crate.

We also already have the DEP_ mechanism, which is enough to support manually propagating these arguments via build.rs. Any further mechanism would be to make that more automatic and declarative without requiring a build.rs in the top-level crate.

@rfcbot
Copy link
Collaborator

rfcbot commented Aug 5, 2021

The final comment period, with a disposition to close, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@rfcbot rfcbot added finished-final-comment-period FCP complete and removed final-comment-period FCP — a period for last comments before action is taken labels Aug 5, 2021
branchseer added a commit to branchseer/rust-nodejs that referenced this issue Oct 29, 2021
@ehuss ehuss closed this as completed Jan 15, 2022
Ectras added a commit to Ectras/rust-hptt that referenced this issue Jan 25, 2024
linker-args do not propagate transitively
(rust-lang/cargo#9554). Hence, -fopenmp would
need to be set in every crate that uses this crate.
The openmp-sys crate instead searches for the libraries and adds them
as link-lib, which is picked up by crates using this crate.
jacob-hughes added a commit to jacob-hughes/alloy that referenced this issue Mar 4, 2024
This is useful for debugging, where profilers intercept and override
dynamically linked symbols (e.g. `malloc` and `free`).

As of this commit, there is no way to build a custom BDWGC fork and have
that dynamically link to a program compiled with Alloy automatically.
This is because `cargo` does not support bubbling up linker args to the
final linked binary, so there is no way to programmatically set the
rpath for BDWGC to the OUT directory in `library/bdwgc` [1].

Alloy programs must therefore set the `LD_PRELOAD` environment variable
to prevent it from linking against the system libgc.

[1]: rust-lang/cargo#9554
jacob-hughes added a commit to jacob-hughes/alloy that referenced this issue Mar 4, 2024
This is useful for debugging, where profilers intercept and override
dynamically linked symbols (e.g. `malloc` and `free`).

As of this commit, there is no way to build a custom BDWGC fork and have
that dynamically link to a program compiled with Alloy automatically.
This is because `cargo` does not support bubbling up linker args to the
final linked binary, so there is no way to programmatically set the
rpath for BDWGC to the OUT directory in `library/bdwgc` [1].

Alloy programs must therefore set the `LD_PRELOAD` environment variable
to prevent it from linking against the system libgc.

[1]: rust-lang/cargo#9554
jacob-hughes added a commit to jacob-hughes/alloy that referenced this issue Apr 23, 2024
This is useful for debugging, where profilers intercept and override
dynamically linked symbols (e.g. `malloc` and `free`).

As of this commit, there is no way to build a custom BDWGC fork and have
that dynamically link to a program compiled with Alloy automatically.
This is because `cargo` does not support bubbling up linker args to the
final linked binary, so there is no way to programmatically set the
rpath for BDWGC to the OUT directory in `library/bdwgc` [1].

Alloy programs must therefore set the `LD_PRELOAD` environment variable
to prevent it from linking against the system libgc.

[1]: rust-lang/cargo#9554
emesare added a commit to Vector35/binaryninja-api that referenced this issue Dec 31, 2024
When linking you must depend on the -sys crate.

This is because linker arguments (what says where to find binaryninjacore) are NOT transitive. The top level application crate MUST provide it.

For more information see:
- rust-lang/cargo#9554 (comment)
- oxidecomputer/omicron#225
emesare added a commit to Vector35/binaryninja-api that referenced this issue Jan 25, 2025
Moves a bunch of stuff out of src/types.rs that did not belong:
- Confidence
- Variable
- Function specific stuff

- Refactored InstructionInfo, see the msp430 and riscv examples.
- Renamed Function::from_raw to Function::ref_from_raw and fixed places where the ref was incremented twice
- Fixed FunctionRecognizer leaking functions (see above)
- Fixed some apis incorrectly returning Result where Option is clearer
- Started to move destructured types to the From trait for converting to an from ffi types, see Location for an example
- Started to remove bad module level imports (importing std modules like mem all over the place)
- Moved some wrapper handle types to named handle field (this improves readability), see CoreArchitecture for an example
- Removed some unintuitive getters, this is bad practice for Rust code, just access the field directly, see DataVariable for an example
- General code cleanup, purposely did not run rustfmt, that will be a single seperate commit

More rust cleanup

- Fixed invalid views being able to invoke UB when dealing with databases
- Cleaned up some helper code in dwarf_import
- Fixed inverted is_null checks causing crashes! Oops!

More rust cleanup

Still a WIP, I think branch info is still invalid, need to figure out the issue there.

- Fixed some invalid Ref lifetimes when constructing indirectly, see Array<DataVariable> for example
- Added some more comments
- Removed some "magic" functions like MLIL Function::ssa_variables

There are still a bunch of invalid lifetimes that aren't crashing us due to the usage of those API's not living long enough. But they ARE an issue.

More rust cleanup

Trying to comment more TODO's as I go along.

- Renamed llil::Function to llil::LowLevelILFunction for clarity and consistency
- Take base structures by ref in StructureBuilder::set_base_structures to prevent premature drops
- Added more raw to wrapper conversions
- Removed UB prone apis
- Getting rid of more core module references, use std!
- Removed improper Guard usage in array impls for wrapper types with no context
- Untangling the UB of the Label api, still only half done :/

More rust cleanup

- Misc formatting
- Made Logger ref counted
- Fixed leaking name of logger every time something was logged
- Fixed the last (hopefully) of the unresolved labels
- Simplified some code

Fix leak in DebugInfo::AddType

componentArray was never freed

Add more HLIL related functions to rust

More rust cleanup

improve the CustomBinaryView init process

Canonicalize path in `create_delete_empty` test

Link core in rust

When linking you must depend on the -sys crate.

This is because linker arguments (what says where to find binaryninjacore) are NOT transitive. The top level application crate MUST provide it.

For more information see:
- rust-lang/cargo#9554 (comment)
- oxidecomputer/omicron#225

Remove vendored pdb crate

Use cargo to manage the git repo ref instead

Fix misc rustdoc warnings

Move actual plugins out of `rust/examples` and into `plugins`

This is where all shipped public plugins that are not arch/view/platform/lang will be at from now on

Originally they were in the rust workspace, meaning they all shared a Cargo.lock which is ill-advised.

More rust cleanup

- More clarification on plugin/executable requirements
- Made examples actually rust examples
- Add Display impl for InstructionTextToken
- Renamed feature "noexports" to "no_exports"

Move under_construction.png to assets directory

This really did bother me

Remove unneeded `extern crate bindgen`

Replace nop'd log::info with println

We don't register a compatible log sink so they will just get sent into the void

Move inline tests into tests directory

This is being done in the hopes of curbing the multi-thousand line files that will occur once we flesh out the tests

Format rust code

Update rust ci

Still need to add support for running tests in ci

More rust cleanup

- Architecture id's are now typed accordingly
- Fix some clippy lints
- Make instruction index public in LLIL
- Removed useless helper functions
- LLIL expressions and instruction indexes are now typed accordingly

Generate binaryninjacore-sys documentation

This should show binaryninjacore-sys alongside binaryninja crate

More rust cleanup

- Remove lazy_static dependency
- Remove hacky impl Debug for Type and just use the display impl
- Add more debug impls
- Reorder some top level namespace items
- Add first type test

Remove unneeded script helper in rust api

More rust cleanup

- Added main thread handler api
- Register a headless main thread handler by default in initialization
- Refactor QualifiedName to be properly owned
- Loosened some type constraints on some apis involving QualifiedName
- Fixed some apis that were crashing due to incorrect param types
- Removed extern crate cruft for log crate
- Simplified headless initialization using more wrapper apis
- Fixed segments leaking because of no ref wrapper, see BinaryViewExt::segment_at
- Added rstest to manage headless init in unit tests
- Added some more unit tests
- Refactored demangler api to be more ergonomic
- Fixed minidump plugin not building

More rust cleanup

- Fixup usage of QualifiedName in plugins
- Make QualifiedName more usable

Implement rust TypeParser

fix Platform TypeParser related functions

separate User and System implementations of TypeParserResult

Implement rust TypeContainer

More rust cleanup

- Hopefully fixed the rust.yml CI
- Added TypePrinter API (this is still WIP and will crash)
- Added TypeParser API
- Added TypeContainer API
- More work around QualifiedName apis

Oh your suppose to do this

Add workflow_dispatch trigger to rust.yml

More rust fixes

- Swapped some usage of raw 255 to MAX_CONFIDENCE, no one likes magic numbers
- New InstructionTextToken API, complete with owned data, this still needs a lot of testing.
- InstructionTextTokenKind describes a destructured InstructionTextToken, this should make token usage much clearer, some docs pending
- Added some misc Default and Debug impls
- Updated TypePrinter and architectures to use new InstructionTextToken API

Misc formatting changes

More rust cleanup

- Fixed MANY memory leaks (most due to QualifiedName)
- Made StructureBuilder more builder and less structure
- Fixed CMakeLists.txt that were globbing entire api, resulting in 100 second slowdown on cmake generation
- Added more Debug impl's
- Added some more tests
- Fixed TypeParserResult UB
- Moved the From impls to blank impl for clarity, we have multiple different variants of core to rust for some structures, it should be explicit which one you are choosing.
- PossibleValueSet should now be able to allocate so we can go from rust to core with those variants that require allocation
- Misc doc code formatting

Misc clippy lints and clippy CI

Co-authored-by: Michael Krasnitski <michael.krasnitski@gmail.com>

Fix typo in rust CI

Misc rust formatting

Fix misc typos and add typos to rust CI

Add cargo workspace

This will help tooling and external contributors get a map of the rust crates within binaryninja-api

More rust cleanup

- Format all rust plugins
- Fix some tests that were out of date
- Simplify WARP tests to only binaries, building object files from source is a pain
- Link to core in all rust plugins
- Fix some memory leaks
- Update warp insta snapshots
- Fix some misc clippy lints

Run rust tests in CI

This commit also coincides with the creation of the "testing" environment which exposes a BN_SERIAL secret for pulling a headless Binary Ninja

Install missing wayland dependency in github CI

Apparently its needed for linux file picker for the WARP integration

Set the BINARYNINJADIR so rust can find binaryninjacore in CI

The chances of this working are low

Misc remove unused dependency

Rust misc formatting fixes

Improve initialization in rust headless scripts

Provide sensible errors and validation to rust headless scripts, solves #5796

Add BN_LICENSE environment variable to rust CI

We pass the serial to download binary ninja, but we never provided the license for core initialization

Fix typo

More rust cleanup

- Improved binary view initialization (see init_with_opts)
- Allow floating license to free itself before initialization
- Add initialization unit test
- Add better Debug impls for some common types
- Use Path api for opening binary views, this is not breaking as it uses the AsRef impl
- Bump rayon dependency and constrain dependencies to x.x

Update readme and include that directly in the rustdocs

More rust documentation changes

Add format comment to InitializationOptions::with_license

Misc formatting and clippy lint allow

More rust cleanup

- Remove under_construction.png from the build.rs it has been removed
- Use InstructionIndex in more places
- Add missing PartialOrd and Ord impls for id types

More rust cleanup

- Make workflow cloning explicit
- Add workflow tests
- Add missing property string list getting for settings
- Remove IntoActivityName (see #6257)

More rust cleanup

This commit is half done

Misc rust formatting

More rust cleanup

- Renamed common name conflictions (I will put my justification in the PR)
- Fixed invalid instruction retrieval for LLIL
- Added common aliases for llil function, instruction and expression types (see my comment in the PR)
- Refactored the instruction retrieval for LLIL, MLIL and HLIL
- Added instruction index types to MLIL and HLIL
- Moved llil module to lowlevelil module (mlil and hlil will be moved as well)
- Added preliminary LLIL unit testing

Fix typos

Misc clippy fixes

More rust cleanup

- Normalized modules
- Split some code out into own files
- Fixed some UB in type archive and projects
- Improved API around type archives and projects substantially
- Added ProgressExecutor abstraction for functions which have a progress callback
- Improved background task documentation and added unit tests
- Added worker thread api and unit tests
- Moved some owned types to ref types, this is still not complete, but this is the path forward.
- Add external location/library accessors to the binary view
- Added some misc documentation
- Replaced mod.rs with the module name source file

Still need to normalize some paths and also update some documentation surrounding that change.

Update some tests and examples

Fix background task tests colliding

We were creating multiple background tasks with the same progress text on multiple threads

More rust cleanup

- Fixed progress executor freeing itself after one iteration
- Updated the last of the doc imports
- Moved mainthread to main_thread
- Made project creation and opening failable

We could probably AsRef<ProgressExecutor> to get around the allocation and free inside the function bodies, not high priority as those functions are long running anyways.

Move binary view initialization into function body for LLIL test

Normalize test file names

More rust cleanup

- Updated README to clarify offline documentation
- Refactored settings api
- Added settings example to show dumping settings value and specific properties
- Use the workspace to depend on binaryninja and binaryninjacore-sys
- Remove binaryninjacore-sys as a workspace member (its not really required)

Update workflow test to new settings api

More rust cleanup

- Rename Label to LowLevelILLabel
- Update the functions label map automatically

This fixed a major api blunder where the label map is returned as a reference and originally resulted in UB prone lifetime semantics. It was temporarily "fixed" with explicit label updates in the architecture implementation code. But that was less than ideal and was easy to mess up. Now the label map will be updated automatically as the location of labels is now tracked.

Misc clippy lints

More rust cleanup

- Get rid of RawFunctionViewType
- Add better Debug impl for Function

More rust cleanup

- Fixed the documentation icon using local files (thank you @mkrasnitski)
- Fixed labels being updated and overwriting the label location used to update the label map

More rust cleanup

- Added unit tests for MLIL and HLIL
- "Fixed" MLIL, LLIL, and HLIL having issues regarding Instruction vs Expression indexes
- Renamed CallingConvention to CoreCallingConvention and removed architecture generic
- Renamed CallingConventionBase to CallingConvention
- Simplified calling convention code and fixed some bugs with implicit registers
- Added impl Debug to MLIL and HLIL instructions

Still need to at some point add an Expression to MLIL and HLIL. We also might want to look into having the Instruction kind just return the expression kind.

Misc clippy lint

More rust cleanup

- Allow calling conventions to be registered for multiple architectures
- Swapped a unreachable statement to an unimplemented statement

More rust cleanup

- Fixed the issue with PDB types, this has caused me an insane amount of grief
- Fixed LLIL visit_tree missing LLIL_LOAD expressions
- Added LLIL visitor test
- Made all WARP file pickers use the rfd crate

Use the dev branch of Binary Ninja in rust CI

Misc rust fmt

More rust cleanup

- Refactored BinaryReader and BinaryWriter
- Added some warnings to high_level_il and medium_level_il modules that they are unstable
- Add BinaryReader and BinaryWriter tests
- Changed BinaryView len to return u64 (that is what the core returns)
- Misc formatting changes
- Remove extern uses in lib.rs

Add impl Debug for BinaryReader and BinaryWriter

Turn off broken tests

Add more info to the rust README.md

More rust cleanup

- Make EdgeStyle type not wrap raw
- Regression tests for WARP will run on all bins in the out dir now

impl rust Collaboration and Remote API

Fix typo

Update collaboration API

Makes collaboration more in line with the refactor. Still a lot of work to do.

Namely still need:
- Proper errors
- _with_opts functions
- More ergonomic api
- Better connection procedure
- Updated documentation
- A LOT of unit tests
- An example
- Typed id's for everything (i dont want BnString as the id!!!)
- NEED to refactor the progress callbacks into the new progress api, but we should pull in some of the stuff the collab progress has
- Elimination of apis that are dumb helpers

Separate out the rust testing and use pull_request_target

pull_request_target allows PR's to access the headless license, for this to be safe we need to prevent people from running the job.

To prevent the job from being ran we add an environment requirement on testing that a reviewer must review the code and then manually approve it to run.

More rust cleanup

- Use GroupId instead of u64
- Use ProgressCallback in place of ProgressExecutor
- Misc cleanup of FileMetadata
- Add `save_to_path` and `save_to_accessor` to save modified binaries
- Added binary_view unit tests
- Added collaboration unit tests
- Fixed a few issues with the collaboration apis
- Renamed Command registration functions so that there is no import ambiguity
- Split out RemoteUndoEntry
- Collaboration apis now have a explicit `_with_progress` set of apis
- Misc clippy lint fixes

Fix some typos

More rust cleanup

- Add extra info to README.md
- Refactor components api
- Add components unit test
emesare added a commit to Vector35/binaryninja-api that referenced this issue Jan 25, 2025
Moves a bunch of stuff out of src/types.rs that did not belong:
- Confidence
- Variable
- Function specific stuff

- Refactored InstructionInfo, see the msp430 and riscv examples.
- Renamed Function::from_raw to Function::ref_from_raw and fixed places where the ref was incremented twice
- Fixed FunctionRecognizer leaking functions (see above)
- Fixed some apis incorrectly returning Result where Option is clearer
- Started to move destructured types to the From trait for converting to an from ffi types, see Location for an example
- Started to remove bad module level imports (importing std modules like mem all over the place)
- Moved some wrapper handle types to named handle field (this improves readability), see CoreArchitecture for an example
- Removed some unintuitive getters, this is bad practice for Rust code, just access the field directly, see DataVariable for an example
- General code cleanup, purposely did not run rustfmt, that will be a single seperate commit

More rust cleanup

- Fixed invalid views being able to invoke UB when dealing with databases
- Cleaned up some helper code in dwarf_import
- Fixed inverted is_null checks causing crashes! Oops!

More rust cleanup

Still a WIP, I think branch info is still invalid, need to figure out the issue there.

- Fixed some invalid Ref lifetimes when constructing indirectly, see Array<DataVariable> for example
- Added some more comments
- Removed some "magic" functions like MLIL Function::ssa_variables

There are still a bunch of invalid lifetimes that aren't crashing us due to the usage of those API's not living long enough. But they ARE an issue.

More rust cleanup

Trying to comment more TODO's as I go along.

- Renamed llil::Function to llil::LowLevelILFunction for clarity and consistency
- Take base structures by ref in StructureBuilder::set_base_structures to prevent premature drops
- Added more raw to wrapper conversions
- Removed UB prone apis
- Getting rid of more core module references, use std!
- Removed improper Guard usage in array impls for wrapper types with no context
- Untangling the UB of the Label api, still only half done :/

More rust cleanup

- Misc formatting
- Made Logger ref counted
- Fixed leaking name of logger every time something was logged
- Fixed the last (hopefully) of the unresolved labels
- Simplified some code

Fix leak in DebugInfo::AddType

componentArray was never freed

Add more HLIL related functions to rust

More rust cleanup

improve the CustomBinaryView init process

Canonicalize path in `create_delete_empty` test

Link core in rust

When linking you must depend on the -sys crate.

This is because linker arguments (what says where to find binaryninjacore) are NOT transitive. The top level application crate MUST provide it.

For more information see:
- rust-lang/cargo#9554 (comment)
- oxidecomputer/omicron#225

Remove vendored pdb crate

Use cargo to manage the git repo ref instead

Fix misc rustdoc warnings

Move actual plugins out of `rust/examples` and into `plugins`

This is where all shipped public plugins that are not arch/view/platform/lang will be at from now on

Originally they were in the rust workspace, meaning they all shared a Cargo.lock which is ill-advised.

More rust cleanup

- More clarification on plugin/executable requirements
- Made examples actually rust examples
- Add Display impl for InstructionTextToken
- Renamed feature "noexports" to "no_exports"

Move under_construction.png to assets directory

This really did bother me

Remove unneeded `extern crate bindgen`

Replace nop'd log::info with println

We don't register a compatible log sink so they will just get sent into the void

Move inline tests into tests directory

This is being done in the hopes of curbing the multi-thousand line files that will occur once we flesh out the tests

Format rust code

Update rust ci

Still need to add support for running tests in ci

More rust cleanup

- Architecture id's are now typed accordingly
- Fix some clippy lints
- Make instruction index public in LLIL
- Removed useless helper functions
- LLIL expressions and instruction indexes are now typed accordingly

Generate binaryninjacore-sys documentation

This should show binaryninjacore-sys alongside binaryninja crate

More rust cleanup

- Remove lazy_static dependency
- Remove hacky impl Debug for Type and just use the display impl
- Add more debug impls
- Reorder some top level namespace items
- Add first type test

Remove unneeded script helper in rust api

More rust cleanup

- Added main thread handler api
- Register a headless main thread handler by default in initialization
- Refactor QualifiedName to be properly owned
- Loosened some type constraints on some apis involving QualifiedName
- Fixed some apis that were crashing due to incorrect param types
- Removed extern crate cruft for log crate
- Simplified headless initialization using more wrapper apis
- Fixed segments leaking because of no ref wrapper, see BinaryViewExt::segment_at
- Added rstest to manage headless init in unit tests
- Added some more unit tests
- Refactored demangler api to be more ergonomic
- Fixed minidump plugin not building

More rust cleanup

- Fixup usage of QualifiedName in plugins
- Make QualifiedName more usable

Implement rust TypeParser

fix Platform TypeParser related functions

separate User and System implementations of TypeParserResult

Implement rust TypeContainer

More rust cleanup

- Hopefully fixed the rust.yml CI
- Added TypePrinter API (this is still WIP and will crash)
- Added TypeParser API
- Added TypeContainer API
- More work around QualifiedName apis

Oh your suppose to do this

Add workflow_dispatch trigger to rust.yml

More rust fixes

- Swapped some usage of raw 255 to MAX_CONFIDENCE, no one likes magic numbers
- New InstructionTextToken API, complete with owned data, this still needs a lot of testing.
- InstructionTextTokenKind describes a destructured InstructionTextToken, this should make token usage much clearer, some docs pending
- Added some misc Default and Debug impls
- Updated TypePrinter and architectures to use new InstructionTextToken API

Misc formatting changes

More rust cleanup

- Fixed MANY memory leaks (most due to QualifiedName)
- Made StructureBuilder more builder and less structure
- Fixed CMakeLists.txt that were globbing entire api, resulting in 100 second slowdown on cmake generation
- Added more Debug impl's
- Added some more tests
- Fixed TypeParserResult UB
- Moved the From impls to blank impl for clarity, we have multiple different variants of core to rust for some structures, it should be explicit which one you are choosing.
- PossibleValueSet should now be able to allocate so we can go from rust to core with those variants that require allocation
- Misc doc code formatting

Misc clippy lints and clippy CI

Co-authored-by: Michael Krasnitski <michael.krasnitski@gmail.com>

Fix typo in rust CI

Misc rust formatting

Fix misc typos and add typos to rust CI

Add cargo workspace

This will help tooling and external contributors get a map of the rust crates within binaryninja-api

More rust cleanup

- Format all rust plugins
- Fix some tests that were out of date
- Simplify WARP tests to only binaries, building object files from source is a pain
- Link to core in all rust plugins
- Fix some memory leaks
- Update warp insta snapshots
- Fix some misc clippy lints

Run rust tests in CI

This commit also coincides with the creation of the "testing" environment which exposes a BN_SERIAL secret for pulling a headless Binary Ninja

Install missing wayland dependency in github CI

Apparently its needed for linux file picker for the WARP integration

Set the BINARYNINJADIR so rust can find binaryninjacore in CI

The chances of this working are low

Misc remove unused dependency

Rust misc formatting fixes

Improve initialization in rust headless scripts

Provide sensible errors and validation to rust headless scripts, solves #5796

Add BN_LICENSE environment variable to rust CI

We pass the serial to download binary ninja, but we never provided the license for core initialization

Fix typo

More rust cleanup

- Improved binary view initialization (see init_with_opts)
- Allow floating license to free itself before initialization
- Add initialization unit test
- Add better Debug impls for some common types
- Use Path api for opening binary views, this is not breaking as it uses the AsRef impl
- Bump rayon dependency and constrain dependencies to x.x

Update readme and include that directly in the rustdocs

More rust documentation changes

Add format comment to InitializationOptions::with_license

Misc formatting and clippy lint allow

More rust cleanup

- Remove under_construction.png from the build.rs it has been removed
- Use InstructionIndex in more places
- Add missing PartialOrd and Ord impls for id types

More rust cleanup

- Make workflow cloning explicit
- Add workflow tests
- Add missing property string list getting for settings
- Remove IntoActivityName (see #6257)

More rust cleanup

This commit is half done

Misc rust formatting

More rust cleanup

- Renamed common name conflictions (I will put my justification in the PR)
- Fixed invalid instruction retrieval for LLIL
- Added common aliases for llil function, instruction and expression types (see my comment in the PR)
- Refactored the instruction retrieval for LLIL, MLIL and HLIL
- Added instruction index types to MLIL and HLIL
- Moved llil module to lowlevelil module (mlil and hlil will be moved as well)
- Added preliminary LLIL unit testing

Fix typos

Misc clippy fixes

More rust cleanup

- Normalized modules
- Split some code out into own files
- Fixed some UB in type archive and projects
- Improved API around type archives and projects substantially
- Added ProgressExecutor abstraction for functions which have a progress callback
- Improved background task documentation and added unit tests
- Added worker thread api and unit tests
- Moved some owned types to ref types, this is still not complete, but this is the path forward.
- Add external location/library accessors to the binary view
- Added some misc documentation
- Replaced mod.rs with the module name source file

Still need to normalize some paths and also update some documentation surrounding that change.

Update some tests and examples

Fix background task tests colliding

We were creating multiple background tasks with the same progress text on multiple threads

More rust cleanup

- Fixed progress executor freeing itself after one iteration
- Updated the last of the doc imports
- Moved mainthread to main_thread
- Made project creation and opening failable

We could probably AsRef<ProgressExecutor> to get around the allocation and free inside the function bodies, not high priority as those functions are long running anyways.

Move binary view initialization into function body for LLIL test

Normalize test file names

More rust cleanup

- Updated README to clarify offline documentation
- Refactored settings api
- Added settings example to show dumping settings value and specific properties
- Use the workspace to depend on binaryninja and binaryninjacore-sys
- Remove binaryninjacore-sys as a workspace member (its not really required)

Update workflow test to new settings api

More rust cleanup

- Rename Label to LowLevelILLabel
- Update the functions label map automatically

This fixed a major api blunder where the label map is returned as a reference and originally resulted in UB prone lifetime semantics. It was temporarily "fixed" with explicit label updates in the architecture implementation code. But that was less than ideal and was easy to mess up. Now the label map will be updated automatically as the location of labels is now tracked.

Misc clippy lints

More rust cleanup

- Get rid of RawFunctionViewType
- Add better Debug impl for Function

More rust cleanup

- Fixed the documentation icon using local files (thank you @mkrasnitski)
- Fixed labels being updated and overwriting the label location used to update the label map

More rust cleanup

- Added unit tests for MLIL and HLIL
- "Fixed" MLIL, LLIL, and HLIL having issues regarding Instruction vs Expression indexes
- Renamed CallingConvention to CoreCallingConvention and removed architecture generic
- Renamed CallingConventionBase to CallingConvention
- Simplified calling convention code and fixed some bugs with implicit registers
- Added impl Debug to MLIL and HLIL instructions

Still need to at some point add an Expression to MLIL and HLIL. We also might want to look into having the Instruction kind just return the expression kind.

Misc clippy lint

More rust cleanup

- Allow calling conventions to be registered for multiple architectures
- Swapped a unreachable statement to an unimplemented statement

More rust cleanup

- Fixed the issue with PDB types, this has caused me an insane amount of grief
- Fixed LLIL visit_tree missing LLIL_LOAD expressions
- Added LLIL visitor test
- Made all WARP file pickers use the rfd crate

Use the dev branch of Binary Ninja in rust CI

Misc rust fmt

More rust cleanup

- Refactored BinaryReader and BinaryWriter
- Added some warnings to high_level_il and medium_level_il modules that they are unstable
- Add BinaryReader and BinaryWriter tests
- Changed BinaryView len to return u64 (that is what the core returns)
- Misc formatting changes
- Remove extern uses in lib.rs

Add impl Debug for BinaryReader and BinaryWriter

Turn off broken tests

Add more info to the rust README.md

More rust cleanup

- Make EdgeStyle type not wrap raw
- Regression tests for WARP will run on all bins in the out dir now

impl rust Collaboration and Remote API

Fix typo

Update collaboration API

Makes collaboration more in line with the refactor. Still a lot of work to do.

Namely still need:
- Proper errors
- _with_opts functions
- More ergonomic api
- Better connection procedure
- Updated documentation
- A LOT of unit tests
- An example
- Typed id's for everything (i dont want BnString as the id!!!)
- NEED to refactor the progress callbacks into the new progress api, but we should pull in some of the stuff the collab progress has
- Elimination of apis that are dumb helpers

Separate out the rust testing and use pull_request_target

pull_request_target allows PR's to access the headless license, for this to be safe we need to prevent people from running the job.

To prevent the job from being ran we add an environment requirement on testing that a reviewer must review the code and then manually approve it to run.

More rust cleanup

- Use GroupId instead of u64
- Use ProgressCallback in place of ProgressExecutor
- Misc cleanup of FileMetadata
- Add `save_to_path` and `save_to_accessor` to save modified binaries
- Added binary_view unit tests
- Added collaboration unit tests
- Fixed a few issues with the collaboration apis
- Renamed Command registration functions so that there is no import ambiguity
- Split out RemoteUndoEntry
- Collaboration apis now have a explicit `_with_progress` set of apis
- Misc clippy lint fixes

Fix some typos

More rust cleanup

- Add extra info to README.md
- Refactor components api
- Add components unit test

Add testing and documentation to contributing section in README.md

Fix misc doc comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug disposition-close finished-final-comment-period FCP complete T-cargo Team: Cargo
Projects
None yet
Development

No branches or pull requests

8 participants