Skip to content

Commit 7c061c2

Browse files
committed
Add a test that demonstrates a segfault when calling into rust with non-c-like-enum.
1 parent 5e8897b commit 7c061c2

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) --crate-type=staticlib nonclike.rs
5+
$(CC) test.c $(call STATICLIB,nonclike) $(call OUT_EXE,test) \
6+
$(EXTRACFLAGS) $(EXTRACXXFLAGS)
7+
$(call RUN,test)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![crate_type = "lib"]
2+
#![crate_name = "nonclike"]
3+
4+
#[repr(C,u8)]
5+
pub enum T {
6+
A(u64),
7+
B,
8+
}
9+
10+
#[no_mangle]
11+
pub extern "C" fn t_new(a: u64) -> T {
12+
T::A(a)
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdint.h>
2+
#include <assert.h>
3+
4+
/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
5+
* in nonclike.rs . */
6+
enum T_Tag {
7+
A,
8+
B,
9+
};
10+
typedef uint8_t T_Tag;
11+
12+
typedef struct {
13+
uint64_t _0;
14+
} A_Body;
15+
16+
typedef struct {
17+
T_Tag tag;
18+
union {
19+
A_Body a;
20+
};
21+
} T;
22+
23+
/* This symbol is defined by the Rust staticlib built from
24+
* nonclike.rs. */
25+
extern T t_new(uint64_t v);
26+
27+
int main(int argc, char *argv[]) {
28+
(void)argc; (void)argv;
29+
30+
T t = t_new(10);
31+
assert(A == t.tag);
32+
assert(10 == t.a._0);
33+
34+
return 0;
35+
}

0 commit comments

Comments
 (0)