Fix how structs are passed by value in absence of a thunk impl.

This CL fixes 2 problems:

* Before this CL bindings for `extern "C"`, `#[export_name = ... ]`
  functions would incorrectly declare the Rust-side original functions
  with a modified signature - e.g. injecting `__ret_ptr` parameter.
  Undefined behavior would result when calling a function declared this
  way and trying to pass `__ret_ptr` argument even when the original
  function doesn't actually accept such parameter.
* Before this CL bindings for `extern "C"`, `#[no_mangle]` functions
  would incorrectly pass structs by value, even though in the current
  implementation `#[repr(C)]` structs may also get blobs of bytes
  injected as artificial fields (e.g. as padding) which may change the
  ABI classification of such structs.  In other words, b/270454629
  wasn't really fixed for such functions.

The fix is to set `needs_thunk` to true whenever
`!is_c_abi_compatible_by_value`.  This changes the generated bindings
for some `extern "C"` functions (i.e. thunks are now generated for
some such functions).

PiperOrigin-RevId: 521591455
3 files changed
tree: e6d8280e24cec5ad54d4461c87371dddccb63334
  1. .bazelci/
  2. bazel/
  3. cc_bindings_from_rs/
  4. common/
  5. docs/
  6. lifetime_analysis/
  7. lifetime_annotations/
  8. migrator/
  9. nullability_verification/
  10. rs_bindings_from_cc/
  11. support/
  12. .bazelrc
  13. .gitignore
  14. BUILD
  15. Cargo.Bazel.lock
  16. CODE_OF_CONDUCT
  17. CONTRIBUTING
  18. LICENSE
  19. README.md
  20. WORKSPACE
README.md

Crubit: C++/Rust Bidirectional Interop Tool

Build status

Crubit is an experimental bidirectional bindings generator for C++ and Rust.

Please don‘t use, this is an experiment and we don’t yet know where will it take us. There will be breaking changes without warning. Unfortunately, we can't take contributions at this point.

Crubit allows for C++ code and Rust code to call each other without manually wrapping the APIs in an FFI-friendly interop layer. For example, a C++ function like this:

bool IsAbsPath(std::string_view path);

... becomes callable from Rust as if it were defined as:

pub fn IsAbsPath(path: std::string_view) -> bool {...}

Crubit automatically generates ABI-compatible bindings for structs (which can be passed both by value and by reference), functions, and methods, for a large variety of types. (Trivial types, nontrivial types, templated types, etc.)

Building Crubit

$ apt install clang lld bazel
$ git clone git@github.com:google/crubit.git
$ cd crubit
$ bazel build --linkopt=-fuse-ld=/usr/bin/ld.lld //rs_bindings_from_cc:rs_bindings_from_cc_impl

Using a prebuilt LLVM tree

$ git clone https://github.com/llvm/llvm-project
$ cd llvm-project
$ CC=clang CXX=clang++ cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS='clang' -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install
$ cmake --build build -j
$ # wait...
$ cmake --install build
$ cd ../crubit
$ LLVM_INSTALL_PATH=../llvm-project/install bazel build //rs_bindings_from_cc:rs_bindings_from_cc_impl