wasi-sdk icon indicating copy to clipboard operation
wasi-sdk copied to clipboard

Clang exports memory by default

Open loloRvz opened this issue 3 years ago • 3 comments

I'm a rookie with this stuff so bear with me please.

When compiling .c program to .wasm using clang, it automatically exports memory, which then for some reasons causes problems when I try to run in using the wasm-c-api. When converting .wat files to .wasm using the WABT, there's no mention of memory and it runs perfectly.

Here's the C module I'm trying to run:

int sum(int a, int b){
	return a+b;
}


__attribute__((import_name("callback"))) void callback(void);

void callcallback(){
	callback();
}

converting that module to .wasm, then to .wat, we can the memory is being exported:

(module
  (type (;0;) (func))
  (type (;1;) (func (param i32 i32) (result i32)))
  (import "env" "callback" (func (;0;) (type 0)))
  (func (;1;) (type 1) (param i32 i32) (result i32)
    local.get 1
    local.get 0
    i32.add)
  (func (;2;) (type 0)
    call 0)
  (table (;0;) 1 1 funcref)
  (memory (;0;) 1)
  (global (;0;) (mut i32) (i32.const 9216))
  (global (;1;) i32 (i32.const 9216))
  (export "memory" (memory 0))
  (export "sum" (func 1))
  (export "callcallback" (func 2))

Now if I write a .wat module like this:

(module
	(func $call (import "" "call"))
	(func $sum (param $x i32) (param $y i32) (result i32)
		local.get $x
		local.get $y
		i32.add)
	(export "sum" (func $sum))
	(func (export "callfunc") (call $call))
)

it runs smoothly. What am I doing wrong? Should the memory be imported, or exported? What does that even mean?

Thanks for helping out a lost intern.

loloRvz avatar Jun 02 '21 20:06 loloRvz

Exporting linear memory is the currently how C-like languages are compiled to wasm. It lets you pass pointers to wasm imports, and the other side can properly dereference them within your your memory. This approach has some downsides, enough that better approaches are already in development, however I'm not aware of any incompatibilities with wabt today. Can you post more information about the errors you're seeing?

sunfishcode avatar Jun 02 '21 22:06 sunfishcode

Hi, thanks for the answer. I set up a repo with the program I'm trying run: https://github.com/loloRvz/embedding-wamr

Here's the terminal log trying to first run the .wasm module compiled from C (mod.wasm), then the one obtained from .wat using wabt (sum.wasm):

lolo@lolo-linux:~/Documents/embedding-wamr$ ./wafle ./src/mod.wasm 
Initialising...
[01:17:28:718 - 25BE6B80]: running under mode 0
Loading binary...
Compiling module...
Creating callback...
Instantiating module...
Extracting export...
Segmentation fault (core dumped)
lolo@lolo-linux:~/Documents/embedding-wamr$ ./wafle ./src/sum.wasm 
Initialising...
[01:17:31:922 - 44744B80]: running under mode 0
Loading binary...
Compiling module...
Creating callback...
Instantiating module...
Extracting export...
Calling sum()...
Printing result of sum()...
> 15
Calling callback()...
Running callback function...
Shutting down...

Their outputs should be the same, but the first one seems to fail at wasm_instance_exports(instance, &exports);. I tried using gdb to debug it but I ran into hurdles trying to enter the shared library libiswam.so.

I feel like I'm doing everything wrong, thanks for helping out!

loloRvz avatar Jun 03 '21 12:06 loloRvz

Are you sure it's failing at the wasm_instance_exports call, or could it be failing at the wasm_extern_as_func call, which may be expecting the export to be a function?

sunfishcode avatar Jun 03 '21 14:06 sunfishcode

Closing; @loloRvz, feel free to re-open this if you want to re-visit the issue.

abrown avatar Mar 13 '23 16:03 abrown