Skip to content

Commit

Permalink
Add a test that demonstrates a segfault when calling into rust with n…
Browse files Browse the repository at this point in the history
…on-c-like-enum.
  • Loading branch information
sw17ch authored and eddyb committed Feb 8, 2020
1 parent 8498c5f commit cd5ad99
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/test/run-make-fulldeps/return-non-c-like-enum/Makefile
@@ -0,0 +1,7 @@
-include ../tools.mk

all:
$(RUSTC) --crate-type=staticlib nonclike.rs
$(CC) test.c $(call STATICLIB,nonclike) $(call OUT_EXE,test) \
$(EXTRACFLAGS) $(EXTRACXXFLAGS)
$(call RUN,test)
13 changes: 13 additions & 0 deletions src/test/run-make-fulldeps/return-non-c-like-enum/nonclike.rs
@@ -0,0 +1,13 @@
#![crate_type = "lib"]
#![crate_name = "nonclike"]

#[repr(C,u8)]
pub enum T {
A(u64),
B,
}

#[no_mangle]
pub extern "C" fn t_new(a: u64) -> T {
T::A(a)
}
35 changes: 35 additions & 0 deletions src/test/run-make-fulldeps/return-non-c-like-enum/test.c
@@ -0,0 +1,35 @@
#include <stdint.h>
#include <assert.h>

/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
* in nonclike.rs . */
enum T_Tag {
A,
B,
};
typedef uint8_t T_Tag;

typedef struct {
uint64_t _0;
} A_Body;

typedef struct {
T_Tag tag;
union {
A_Body a;
};
} T;

/* This symbol is defined by the Rust staticlib built from
* nonclike.rs. */
extern T t_new(uint64_t v);

int main(int argc, char *argv[]) {
(void)argc; (void)argv;

T t = t_new(10);
assert(A == t.tag);
assert(10 == t.a._0);

return 0;
}

0 comments on commit cd5ad99

Please sign in to comment.