Skip to content

Fix panics when class is allowlisted but its base class is not#3394

Open
Darksonn wants to merge 1 commit into
rust-lang:mainfrom
Darksonn:bitfield-allowlist-fix
Open

Fix panics when class is allowlisted but its base class is not#3394
Darksonn wants to merge 1 commit into
rust-lang:mainfrom
Darksonn:bitfield-allowlist-fix

Conversation

@Darksonn

Copy link
Copy Markdown
Member

In Android we encountered the following crash in bindgen 0.72.1:

panicked at bindgen/ir/context.rs:1492:21:
Not an item: ItemId(...)

I reduced the crash to the following reproduction test case:

// bindgen-flags: --allowlist-type StructWithBitfieldAndMethod -- -std=c++20

struct Base {
    template<typename T> void t_func(T t);
};

struct StructWithBitfieldAndMethod : Base {
    int field : 1;
    template<typename T> void t_method(T t);
    void regular_method();
};

The crash occurs because has_method() is invoked for bitfields:

.map(|bitfield_name| {
let bitfield_name = bitfield_name.to_string();
let getter = {
let mut getter =
ctx.rust_mangle(&bitfield_name).to_string();
if has_method(methods, ctx, &getter) {
getter.push_str("_bindgen_bitfield");
}
getter
};

fn has_method(
methods: &[Method],
ctx: &BindgenContext,
name: &str,
) -> bool {
methods.iter().any(|method| {
let method_name = ctx.resolve_func(method.signature()).name();
method_name == name || ctx.rust_mangle(method_name) == name
})
}

The has_method() method iterates the list of methods and looks them up, crashing if they are missing. However, due to the use of --allowlist-type, the base class is missing and this leads to a crash inside resolve_func(). Note that this only crashes on some LLVM versions because LLVM changed whether it tells bindgen about these uninstantiated methods.

Please see #3393 for verification that this test case crashes without the fix.

Closes: #3393

@rustbot rustbot added the A-C++ label Jul 24, 2026
@Darksonn

Copy link
Copy Markdown
Member Author

Hrm ... we're seeing the failure on LLVM 20+, and I didn't notice that the CI failure in #3393 was from an old LLVM instead. I guess if 20 is not included in CI then this bug isn't covered by CI right now.

@Darksonn
Darksonn force-pushed the bitfield-allowlist-fix branch from 342f7d1 to 8d941ab Compare July 24, 2026 11:53

@emilio emilio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but seems like clippy etc is unhappy at least (#3395).

I can try to get coverage for newer LLVM versions on CI. But the test looks good as well, tysm!

View changes since this review

@emilio

emilio commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

It seems the test might be failing in LLVM 9 because of std=c++20. Try gnu++20 or so? But if not at this point we should probably officially retire support for that LLVM version.

@emilio

emilio commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

#3396 should help here.

@Darksonn
Darksonn force-pushed the bitfield-allowlist-fix branch from 8d941ab to 47457a3 Compare July 24, 2026 13:28
@rustbot

rustbot commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Darksonn
Darksonn force-pushed the bitfield-allowlist-fix branch 3 times, most recently from c77eb11 to d7dbf03 Compare July 24, 2026 13:33
@Darksonn Darksonn changed the title Fix panic on allowlisted class with bitfield and base class Fix panics when class is allowlisted but its base class is not Jul 24, 2026
@Darksonn
Darksonn force-pushed the bitfield-allowlist-fix branch from d7dbf03 to c6d783a Compare July 24, 2026 13:42
@Darksonn

Copy link
Copy Markdown
Member Author

Ok I think it's working now. I made a few more changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants