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

Adding support for external C functions that have integer (or empty) args and/or returns #1

Closed
wants to merge 191 commits into from

Conversation

emarteca
Copy link

@emarteca emarteca commented Jul 8, 2022

Starts addressing @rust-lang#11

Implementation

Adding support for calling external C functions that have any number of integer arguments (types of integers: i8, i16, i32, i64, u8, u16, u32, u64) and an integer return type (or void).
As suggested in @rust-lang#11, the libffi crate is used to dispatch the calls to external C functions.

Modifications

Main modifications are to:

  • helper.rs : adding a function call_and_add_external_c_fct_to_context to read the code pointer to the external C function, dispatch the call, and save the return in MIRI internal memory. Handles all conversions between MIRI and C values.
  • foreign_items.rs : handles the calling of call_and_add_external_c_fct_to_context in helper.rs when a foreign item is encountered. Also adds some structs to model C representations of arguments, and the signature of the external C call.

Testing

Adds tests for the following external functions which are now supported:

  • int tests:
    • adds 2 to a provided int (no type of int specified, so autocasts)
    • takes the sum of its 12 arguments (tests stack spill)
    • adds 3 to a 16 bit int
    • adds an i16 to an i64
    • returns -10 as an unsigned int
  • void tests
    • void function that prints from C

RalfJung and others added 15 commits July 6, 2022 14:06
rustup; ptr atomics

Adds support for the operations added in rust-lang/rust#96935.
I made the pointer-binops always return the provenance of the *left* argument; `@thomcc` I hope that is what you intended. I have honestly no idea if it has anything to do with what LLVM does...

I also simplified our pointer comparison code while I was at it -- now that *all* comparison operators support wide pointers, we can unify those branches.
adjust for removed unsized_locals

The Miri side of rust-lang/rust#98831
remove a dead optimization

This became dead code when I removed "Untagged".
@oli-obk
Copy link

oli-obk commented Jul 8, 2022

🤯

did you mean to open this here? This is not the main miri repository

oli-obk and others added 22 commits July 21, 2022 07:35
Print one . character per test instead of one line

`./miri bless -- --quiet` now prints a dot per test, along with the regular Rust unit tests that listen to this flag
fix auto-toolchain pwd

`rustup-toolchain` needs to be called in the right directory
remove rand (large dependency) and page-size (testing the dependency, not cargo-miri).
keep only byteorder as a "demo" dependency, it is a leaf and builds quickly.
and move crate tests to their own folders
make test-cargo-miri only about cargo

Move the things that actually test dependency behavior into the regular test suite, now that we can do that. :)
Some cleanups and docs around the auto ops
@maurer
Copy link
Owner

maurer commented Jul 25, 2022

Closing in favor of the real PR

@maurer maurer closed this Jul 25, 2022
bors added a commit to rust-lang/miri that referenced this pull request Aug 26, 2022
Adding support for external C functions that have integer (or empty) args and/or returns

Starts addressing `@https://github.com/rust-lang/miri/issues/11`

### Implementation
Adding support for calling external C functions that have any number of integer arguments (types of integers: `i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64`) and an integer return type (or `void`).
As suggested in `@https://github.com/rust-lang/miri/issues/11,` the [`libffi` crate](https://docs.rs/libffi/latest/libffi/index.html) is used to dispatch the calls to external C functions.

#### Modifications
Main modifications are to:
* [helper.rs](https://github.com/emarteca/miri/blob/int-function-args-returns/src/helpers.rs) : adding a function `call_and_add_external_c_fct_to_context` to read the code pointer to the external C function, dispatch the call, and save the return in MIRI internal memory. Handles all conversions between MIRI and C values (using some macros, also defined in this file).
* [foreign_items.rs](https://github.com/emarteca/miri/blob/int-function-args-returns/src/shims/foreign_items.rs) : handles the calling of `call_and_add_external_c_fct_to_context` in [helper.rs](https://github.com/emarteca/miri/blob/int-function-args-returns/src/helpers.rs) when a foreign item is encountered. Also adds some structs to model C representations of arguments, and the signature of the external C call.

### Testing
Adds tests for the following external functions which are now supported:
* [int tests](https://github.com/emarteca/miri/blob/int-function-args-returns/tests/pass/external_C/int_c_tests.rs):
     - adds 2 to a provided int (no type of int specified, so autocasts)
     - takes the sum of its 12 arguments (tests stack spill)
     - adds 3 to a 16 bit int
     - adds an `i16` to an `i64`
     - returns -10 as an unsigned int
* [void tests](https://github.com/emarteca/miri/blob/int-function-args-returns/tests/pass/external_C/print_from_c.rs)
     - void function that prints from C

### Code review
The code in this PR was reviewed by `@maurer` on [another fork](maurer#1) -- thanks!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants