Skip to content

rustc: wasm_import_module confuses module names of identically-named functions #50021

Closed

Description

@lostman

The following code illustrates the issue:

#![allow(dead_code, unused_imports)]
#![feature(lang_items, core_intrinsics)]
#![feature(custom_attribute)] // <- required for wasm_import_module
#![feature(wasm_import_module)]
#![no_std]
use core::intrinsics;

#[lang = "panic_fmt"]
#[no_mangle]
pub extern fn rust_begin_panic(
    _msg: core::fmt::Arguments,
    _file: &'static str,
    _line: u32,
    _column: u32) -> ! {
        unsafe { intrinsics::abort() }
}

mod m1 {
    #[wasm_import_module = "m1"]
    extern "C" {
        pub fn f();
    }
    #[wasm_import_module = "m1"]
    extern "C" {
        pub fn g();
    }
}

mod m2 {
    #[wasm_import_module = "m2"]
    extern "C" {
        pub fn f(_: i32);
    }
}

#[no_mangle]
pub unsafe fn run() {
    m1::g();

    // In generated code, expected:
    // (import "m2" "f" (func $f (param i32)))
    // but got:
    // (import "m1" "f" (func $f (param i32)))
    m2::f(0);
}

To compile:

❯ rustc \
--crate-type=cdylib \
--target=wasm32-unknown-unknown \
-o result.wasm \
src/lib.rs

Then, using binaryen's wasm-dis:

❯ wasm-dis foo.wasm | grep import
(import "m1" "g" (func $g))
(import "m1" "f" (func $f (param i32)))

The type of f is correct since m1::f() takes no arguments, but the module name is not. Looks like rustc has the right function but with wrong module name.

Activity

  1. added
    A-linkageArea: linking into static, shared libraries and binaries
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    O-wasmTarget: WASM (WebAssembly), http://webassembly.org/
    C-bugCategory: This is a bug.
    on Aug 27, 2018
  2. lostman commented on Oct 4, 2018

    @lostman
    Author

    Just checked with the latest nightly:

    $ rustc --version
    rustc 1.31.0-nightly (5597ee8a6 2018-10-03)
    

    This code still generates an incorrect Wasm:

    #![allow(dead_code, unused_imports)]
    #![feature(lang_items, core_intrinsics)]
    #![feature(custom_attribute)] // <- required for wasm_import_module
    #![no_std]
    use core::intrinsics;
    use core::panic::PanicInfo;
    
    #[panic_handler]
    fn panic_handler(_: &PanicInfo) -> ! {
        unsafe {
            ::core::intrinsics::abort()
        }
    }
    
    mod m1 {
        #[link(wasm_import_module = "m1")]
        extern "C" {
            pub fn f();
        }
        #[link(wasm_import_module = "m1")]
        extern "C" {
            pub fn g();
        }
    }
    
    mod m2 {
        #[link(wasm_import_module = "m2")]
        extern "C" {
            pub fn f(_: i32);
        }
    }
    
    #[no_mangle]
    pub unsafe fn run() {
        m1::f();
        m1::g();
        m2::f(0);
    }
    

    The current issue is different though:

    (module
     (type $0 (func))
     (type $1 (func (param i32)))
     (import "m2" "f" (func $f))
     (import "m1" "g" (func $g))
     (global $global$0 (mut i32) (i32.const 1048576))
     (global $global$1 i32 (i32.const 1048576))
     (global $global$2 i32 (i32.const 1048576))
     (table 1 1 anyfunc)
     (memory $0 16)
     (export "memory" (memory $0))
     (export "__indirect_function_table" (table $0))
     (export "__heap_base" (global $global$1))
     (export "__data_end" (global $global$2))
     (export "run" (func $run))
     (func $__wasm_call_ctors (; 2 ;) (type $0)
     )
     (func $run (; 3 ;) (type $0)
      (call $f)
      (call $g)
      (call $.Lf_bitcast
       (i32.const 0)
      )
     )
     (func $.Lf_bitcast (; 4 ;) (type $1) (param $var$0 i32)
      (call $f)
     )
    )
    

    We are calling the same function f twice. Once with and once without the integer argument!

  3. added a commit that references this issue on Dec 16, 2019
    aa0ef5a
  4. added a commit that references this issue on Dec 19, 2019
    74b1902
  5. added 2 commits that reference this issue on Dec 20, 2019
    8e9f09c
    43d1532
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.O-wasmTarget: WASM (WebAssembly), http://webassembly.org/T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions