rust-lang / rust-lang/rust-bindgen

bindgen creates incorrect bindings for `__attribute__((pass_object_size(...)))`

Open
#1,964 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

I suspect that bindgen just doesn't look at the mentioned attribute at all, this is problematic because this attribute affects the generated function signature. It would be nice if bindgen did support the attribute, but at the very least I think it shouldn't create an incorrect binding, (perhaps just omitting the binding would be better)

Documentation about the attribute found here https://clang.llvm.org/docs/AttributeReference.html#pass-object-size-pass-dynamic-object-size

Input C/C++ Header
 void
 foo(const char *const p __attribute__((pass_object_size(0))));
Bindgen Invocation
$ bindgen input.h
Actual Results
extern "C" {
    pub fn foo(p: *const ::std::os::raw::c_char);
}
Expected Results

This function attribute causes clang to pass a hidden extra argument to the function which is the length of the string. If you look at the LLVM IR clang generates you can see it

; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @foo(i8* %0, i64 %1) #0 {
  %3 = alloca i8*, align 8
  %4 = alloca i64, align 8
  store i8* %0, i8** %3, align 8
  store i64 %1, i64* %4, align 8
  ret void
}

Given this function signature, the function should instead be something like this I think

extern "C" {
    pub fn foo(p: *const ::std::os::raw::c_char, length: isize);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided input.h declaration, the bindgen input.h invocation, and the Clang AttributeReference documentation for pass_object_size. Compare the generated Rust declaration with the LLVM IR showing the hidden length argument; done means bindgen either represents the extra argument correctly or omits the unsafe binding.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.