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

Audit ABI implementations in rustc_target (vs Clang). #65111

Open
eddyb opened this issue Oct 4, 2019 · 10 comments
Open

Audit ABI implementations in rustc_target (vs Clang). #65111

eddyb opened this issue Oct 4, 2019 · 10 comments
Labels
A-abi Area: Concerning the application binary interface (ABI). A-codegen Area: Code generation A-ffi Area: Foreign Function Interface (FFI) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-llvm Working group: LLVM backend code generation

Comments

@eddyb
Copy link
Member

eddyb commented Oct 4, 2019

In #63649 (comment) I wrote:

The wasm-bindgen mismatch could've been avoided, had we validated the call ABI by comparing it with what Clang does, when it was initially implemented (to be clear, I think I dropped the ball here).

Irrespective of what happens to this PR, I think we should do an audit of all our call ABIs, and institute a policy to prevent this from happening again in the future.
cc @rust-lang/compiler @rust-lang/wg-codegen

The context there was that our wasm32 ABI has been wrong since it was added, and wasm-bindgen now relies on the wrong ABI.

I opened this issue so we don't lose track of this, because I'm sure I will.

@Mark-Simulacrum Mark-Simulacrum added A-ffi Area: Foreign Function Interface (FFI) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 4, 2019
@jonas-schievink jonas-schievink added A-codegen Area: Code generation WG-llvm Working group: LLVM backend code generation labels Oct 4, 2019
@nagisa
Copy link
Member

nagisa commented Oct 5, 2019

If my memory serves me right, I asked about a formal ABI specification back when this target was being added and there wasn’t one at the time so we ended up with something implementation defined. Implementation of the ABI in Clang is not a formal spec, either, so I don’t see much purpose in changing one implementation-defined ABI to another one.

Maybe makes sense for other formally specified ABIs, though.

@joshtriplett
Copy link
Member

Implementation of the ABI in Clang is not a formal spec, either, so I don’t see much purpose in changing one implementation-defined ABI to another one.

Interoperability between the two?

@eddyb
Copy link
Member Author

eddyb commented Oct 31, 2019

More data points:

  • mips64 has an impossible-for-C-FFI rule about arrays, see Rust arrays on C FFI are super confusing #58905 (comment)
  • the entirety of PowerPC C ZST ABI fixes #64259, where the GNU C extension for ZSTs ended up being discussed a bunch
  • MSP430 doesn't match GCC, haven't looked at clang yet
    • according to the ABI spec (thanks @gnzlbg), GCC is wrong?
    • however, it doesn't use the "cast" functionality so it's possible that trying to pass a (u8, u8) on MSP430 would use two registers simply by letting LLVM see aggregate fields
      • this may be a problem for many other ABIs, we need to investigate more thoroughly, and maybe make the "cast" functionality of rustc_target::abi::call mandatory for multi-scalar aggregates passed directly, to avoid that footgun

@eddyb
Copy link
Member Author

eddyb commented Nov 8, 2019

Here's one more:

@eddyb
Copy link
Member Author

eddyb commented Jan 16, 2020

@Gankra
Copy link
Contributor

Gankra commented Mar 9, 2022

While looking into #54341 I wrote a tool for actually generating+compiling+running+checking rust<->C ffi interfaces:

https://github.com/Gankra/abi-checker

It needs some polish and UX work for it to be totally useful in the context of automated tooling/reporting, but I think it's a pretty useful baseline for just objectively verifying whether C and Rust agree on how different concepts get passed across the FFI boundary. I tried to document/comment it a bunch in case anyone finds it useful to extend and iterate on.

@eddyb
Copy link
Member Author

eddyb commented Jun 27, 2022

We should probably run @Gankra's tool instead of our own (less comprehensive) tests. cc @rust-lang/compiler

Anyway came here to add one more to the pile:

@Gankra
Copy link
Contributor

Gankra commented Jun 28, 2022

I filed a few new issues on abi-checker and made a metabug for what needs to be done to get this up to snuff for y'all. Please feel free to drive-by it with whatever you want, and to file more issues on the repo. This is not my job atm, but if there's enough interest I can maybe make an argument to mozilla to allocate some resources to it.

Metabug: things rust-lang needs/wants to use this in CI

@Gankra
Copy link
Contributor

Gankra commented Aug 17, 2022

Just a little update on abi-checker:

All the major issues I could think of are now at least minimally resolved, and integrating this tool into rustc's CI should be relatively low-effort (see the above link to how rustc_codegen_cranelift did it).

@Gankra
Copy link
Contributor

Gankra commented Aug 17, 2022

Possibly one annoyance remaining is that we currently use rustc by literally invoking Command::new(rustc), which works well for a rustup user but may be annoying for rustc itself. I can potentially change this to accept some sort of configuration if desired. Or I could use the RUSTC that built picks up. It's already mildly important that abi-checker itself and the tests it builds are themselves abi-compatible, because the former dlopen's the latter. However the tests are compiled as c staticlibs that get linked into cdylibs, so the ABI is as stable as it gets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-abi Area: Concerning the application binary interface (ABI). A-codegen Area: Code generation A-ffi Area: Foreign Function Interface (FFI) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-llvm Working group: LLVM backend code generation
Projects
None yet
Development

No branches or pull requests

7 participants