Skip to content

Clang and Rustc use incompatible Wasm signatures for struct arguments passed by value #130

Closed
@maxbrunsfeld

Description

@maxbrunsfeld

Apologies in advance if this is the wrong place to open this issue.

Summary

When compiling with the wasm32-unknown-unknown target triple, Clang 8.0 and Rustc 1.38.0 use two different Wasm function signatures for an extern "C" function that accepts a struct by value. This causes a linker error when trying to link C and Rust code together.

Details

There a minimal project which reproduces the problem here.

The C code contains this:

typedef struct {
  uint32_t line;
  uint32_t column;
} Point;

Point add_points_in_c(Point a, Point b) {
  // ...
}

The Rust code contains this:

#[repr(C)]
pub struct Point {
    pub line: u32,
    pub column: u32,
}

extern "C" {
    fn add_points_in_c(a: Point, b: Point) -> Point;
}

When trying to link the two, I get this error:

  = note: rust-lld: error: function signature mismatch: add_points_in_c
          >>> defined as (i32, i32, i32, i32, i32) -> void in wasm_lib_with_c.nxdi25wtl7eb7vo.rcgu.o
          >>> defined as (i32, i32, i32) -> void in libwasm-c-component.a(lib.o)

It looks like Clang always passes the structs via pointer, whereas Rust unpacks their fields into separate i32 parameters.

Both strategies seem reasonable, but it would be great if the two compilers would agree on a calling convention for this, so that C and Rust code could be linked together when compiling for Wasm, like they can when compiling for other targets.

Current Workaround

If I update the add_points_in_c function accept the parameters via a pointer, the project builds and runs as expected. I've done this here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions