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

cbindgen panics during generation #2 #131

Closed
pjenvey opened this issue Feb 16, 2018 · 2 comments
Closed

cbindgen panics during generation #2 #131

pjenvey opened this issue Feb 16, 2018 · 2 comments

Comments

@pjenvey
Copy link
Contributor

pjenvey commented Feb 16, 2018

A little similar to #65 but different, I'm seeing an assertion error when running cbindgen --lang C over the https://github.com/mozilla-services/autopush/tree/master/autopush_rs codebase. It doesn't occur under C++:

thread 'main' panicked at 'assertion failed: self.generic_params.len() > 0 &&
    self.generic_params.len() == generic_values.len()', /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/ir/typedef.rs:134:8
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:68
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:57
             at /checkout/src/libstd/panicking.rs:381
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:397
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:577
   5: std::panicking::begin_panic
             at /checkout/src/libstd/panicking.rs:538
   6: <cbindgen::bindgen::ir::typedef::Typedef as cbindgen::bindgen::ir::item::Item>::instantiate_monomorph
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/ir/typedef.rs:134
   7: cbindgen::bindgen::ir::ty::Type::add_monomorphs
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/ir/ty.rs:428
   8: cbindgen::bindgen::ir::typedef::Typedef::add_monomorphs
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/ir/typedef.rs:89
   9: cbindgen::bindgen::library::Library::instantiate_monomorphs::{{closure}}
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/library.rs:290
  10: <cbindgen::bindgen::ir::item::ItemMap<T>>::for_all_items
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/ir/item.rs:175
  11: cbindgen::bindgen::library::Library::instantiate_monomorphs
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/library.rs:289
  12: cbindgen::bindgen::library::Library::generate
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/library.rs:62
  13: cbindgen::bindgen::builder::Builder::generate
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/bindgen/builder.rs:279
  14: cbindgen::generate_with_config
             at /home/pjenvey/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.4.3/src/lib.rs:33
  15: build_script_build::main
             at ./build.rs:16
  16: __rust_maybe_catch_panic
             at /checkout/src/libpanic_unwind/lib.rs:101
  17: std::rt::lang_start
             at /checkout/src/libstd/panicking.rs:459
             at /checkout/src/libstd/panic.rs:365
             at /checkout/src/libstd/rt.rs:58
  18: main
  19: __libc_start_main
  20: _start

I've narrowed it down somewhat to an addition to one of the integration tests, it's blowing up on the Sender type alias:

diff --git a/tests/rust/typedef.rs b/tests/rust/typedef.rs
index bf7ceb8..91eee76 100644
--- a/tests/rust/typedef.rs
+++ b/tests/rust/typedef.rs
@@ -9,3 +9,24 @@ type IntFoo<T> = Foo<i32, T>;
 #[no_mangle]
 pub extern "C" fn root(a: IntFoo<i32>)
 { }
+
+use std::sync::mpsc;
+use std::sync::Mutex;
+#[repr(C)]
+pub struct AutopushQueue {
+    rx: Mutex<Option<Sender>>,
+}
+trait FnBox: Send {
+    fn call(self: Box<Self>, input: &str);
+}
+pub struct PythonCall {
+    input: String,
+    output: Box<FnBox>,
+}
+pub type Sender = mpsc::Sender<Option<PythonCall>>;
+#[no_mangle]
+pub extern "C" fn autopush_queue_free(queue: *mut AutopushQueue) {
+    unsafe {
+        Box::from_raw(queue);
+    }
+}
['cargo', 'run', '--', '--lang', 'c', 'tests/rust/typedef.rs', '-o', 'tests/expectations/typedef.c']
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/cbindgen --lang c tests/rust/typedef.rs -o tests/expectations/typedef.c`
thread 'main' panicked at 'assertion failed: self.generic_params.len() > 0 &&
    self.generic_params.len() == generic_values.len()', src/bindgen/ir/typedef.rs:137:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
@eqrion
Copy link
Collaborator

eqrion commented Feb 26, 2018

Okay, the problem here is that cbindgen doesn't understand rust paths well. So the mpsc in pub type Sender = mpsc::Sender<..> is ignored causing cbindgen to try and instantiate a generic type for the Sender typedef but it doesn't have any parameters.

The issue to fix the root cause is #7. A workaround would be to rename the type alias to something other than Sender. Running locally I can confirm that resolves this.

pjenvey added a commit to mozilla-services/autopush that referenced this issue May 11, 2018
requires a couple workarounds:

- rename queue::Sender -> AutopushSender to avoid cbindgen bug w/ type aliases:
  mozilla/cbindgen#131
- make structs not representable in C opaque (remove #[repr(C)])
- have to pin to an older serde_derive for now because cbindgen does

Closes #1045
pjenvey added a commit to mozilla-services/autopush that referenced this issue May 11, 2018
requires a couple workarounds:

- rename queue::Sender -> AutopushSender to avoid cbindgen bug w/ type aliases:
  mozilla/cbindgen#131
- make structs not representable in C opaque (remove #[repr(C)])
- have to pin to an older serde_derive for now because cbindgen does

Closes #1045
pjenvey added a commit to mozilla-services/autopush that referenced this issue May 11, 2018
requires a couple workarounds:

- rename queue::Sender -> AutopushSender to avoid cbindgen bug w/ type aliases:
  mozilla/cbindgen#131
- make structs not representable in C opaque (remove #[repr(C)])
- have to pin to an older serde_derive for now because cbindgen does

Closes #1045
pjenvey added a commit to mozilla-services/autopush that referenced this issue May 11, 2018
requires a couple workarounds:

- rename queue::Sender -> AutopushSender to avoid cbindgen bug w/ type aliases:
  mozilla/cbindgen#131
- make structs not representable in C opaque (remove #[repr(C)])
- have to pin to an older serde_derive for now because cbindgen does

Closes #1045
pjenvey added a commit to mozilla-services/autopush that referenced this issue May 11, 2018
requires a couple workarounds:

- rename queue::Sender -> AutopushSender to avoid cbindgen bug w/ type aliases:
  mozilla/cbindgen#131
- make structs not representable in C opaque (remove #[repr(C)])
- have to pin to an older serde_derive for now because cbindgen does

Closes #1045
@eqrion
Copy link
Collaborator

eqrion commented Aug 18, 2018

I'm going to close this as a duplicate of #7.

@eqrion eqrion closed this as completed Aug 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants