rust-lang / rust-lang/rust-bindgen

rust-bindgen 0.73.2 segfaults with nested types defined with `using`

Open
#3,478 2 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

In the following minimized example project:

$ ls -lR
total 16
-rw-r--r--@ 1 miriam  staff  808 Sep 10 17:22 build.rs
-rw-r--r--@ 1 miriam  staff  106 Sep 10 17:23 Cargo.toml
drwxr-xr-x  5 miriam  staff  160 Sep 10 17:22 src

./src:
total 16
-rw-r--r--  1 miriam  staff  212 Sep 10 17:22 bindgen_entry.h
-rw-r--r--@ 1 miriam  staff  210 Sep 10 16:56 lib.rs

build.rs:

use std::path::Path;
use std::env;

fn main() {
    let bindings = bindgen::Builder::default()
        .header("src/bindgen_entry.h")
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .clang_args(["-x", "c++", "-std=c++23"])
        // prevent generating bindings for stdlib and the like
        .allowlist_file("src/bindgen_entry.h")
        .generate()
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let cargo_out = env::var("OUT_DIR").unwrap();
    let out_path = Path::new(&cargo_out);
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

Cargo.toml:

[package]
name = "bindgen-segv"
version = "0.1.0"
edition = "2024"

[build-dependencies]
bindgen = "0.73"

src/bindgen_entry.h:

#ifndef BINDGEN_ENTRY_H__
#define BINDGEN_ENTRY_H__

template <typename T>
using Wrapper = T*;

typedef struct {
  Wrapper<Wrapper<const char>> char_arr;
  int arr_size;
} CharArr;


#endif // BINDGEN_ENTRY_H__

src/lib.rs (just the default generated by cargo init):

pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

cargo build fails as such:

$ cargo build
    Updating crates.io index
     Locking 24 packages to latest Rust 1.97.1 compatible versions
  Downloaded prettyplease v0.3.0
  Downloaded syn v3.0.5
  Downloaded 2 crates (376.2KiB) in 0.17s
   Compiling proc-macro2 v1.0.107
   Compiling unicode-ident v1.0.24
   Compiling quote v1.0.47
   Compiling glob v0.3.4
   Compiling libc v0.2.189
   Compiling prettyplease v0.3.0
   Compiling cfg-if v1.0.4
   Compiling regex-syntax v0.8.11
   Compiling memchr v2.8.3
   Compiling minimal-lexical v0.2.1
   Compiling bindgen v0.73.2
   Compiling log v0.4.34
   Compiling bitflags v2.13.2
   Compiling rustc-hash v2.1.3
   Compiling shlex v2.0.1
   Compiling libloading v0.8.9
   Compiling clang-sys v1.9.1
   Compiling nom v7.1.3
   Compiling regex-automata v0.4.18
   Compiling cexpr v0.6.0
   Compiling syn v3.0.5
   Compiling regex v1.13.1
   Compiling bindgen-segv v0.1.0 (/Users/miriam/bindgen-segv)
error: failed to run custom build command for `bindgen-segv v0.1.0 (/Users/miriam/bindgen-segv)`

Caused by:
  process didn't exit successfully: `/Users/miriam/bindgen-segv/target/debug/build/bindgen-segv-b96f88365a6bc922/build-script-build` (signal: 11, SIGSEGV: invalid memory reference)
  --- stdout
  cargo:rerun-if-env-changed=TARGET
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_aarch64-apple-darwin
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_aarch64_apple_darwin
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS
  cargo:rerun-if-changed=src/bindgen_entry.h

version info:

$ rustc --version --verbose
rustc 1.97.1 (8bab26f4f 2026-07-14)
binary: rustc
commit-hash: 8bab26f4f68e0e26f0bb7960be334d5b520ea452
commit-date: 2026-07-14
host: aarch64-apple-darwin
release: 1.97.1
LLVM version: 22.1.6

I am running on an M3 mac with macOS 26.6.2.

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 by reproducing the crash with the minimized project’s build.rs and src/bindgen_entry.h using cargo build, then trace bindgen’s C++ parsing and binding-generation entry points for the nested using aliases. Done means the example no longer segfaults and cargo build completes with bindings generated for CharArr.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.