I am trying to understand how memory is shared between the host and a WebAssembly module (generated using `wasm2c` from an object file generated with `emcc`).

Consider the following very simple example:

```c
int foo(char *buff) {
  return buff[0];
}
```

This is compiled using `emcc` and then `wasm2c` and `clang` to get an object file. However, the function signature eventually becomes:

```c
u32 w2c_unsafe_foo(w2c_unsafe*, u32);
```

I assume, based on the available WebAssembly types, there is no available (semantic) equivalence between the original C code and a WebAssembly representation?

In this case, naturally, passing a pointer to this function casts it to a `u32`, and then the `buff[0]` access causes a trap (I suppose because the pointer is to a region of memory outside of the WebAssembly module's allocated memory?).

What is the correct way to share memory in and out of a WebAssembly module? I have had a look at the [rot13 example](https://github.com/WebAssembly/wabt/blob/main/wasm2c/examples/rot13/main.c), but this uses a `.wat` file, where it is possible to import an external library (e.g., `host`). As was alluded to me elsewhere (https://github.com/WebAssembly/wabt/issues/2289#issuecomment-1701224778), this seems to be what is required to access 'things' (functions/memory) from the host.

Is it possible to do this in C code, which is then compiled into a WebAssebly module?

---

Based on the `rot13` example, I think I need to do something like the following:

```c
int foo() {
  char buff[1024];
  host_get_buff(buff, 1024);
  return buff[0];
}
```

`host_get_buff` would be implemented in the host code and copy the relevant data into `buff`? The questions I have are as follows.

1. Copying into `buff` requires access to this region of memory in `host_get_buff`. In the ro13 example, this appears to be done by allocating a page into `w2c_host`, which is passed as the first argument to `host_get_buff` when it is called. I think the WebAssebly module is then set to use this page for all of its memory uses?

https://github.com/WebAssembly/wabt/blob/dddc03d3bc4df2bca26a880e8f078f84ea3d2454/wasm2c/examples/rot13/rot13.wat#L1

2. Given that the module appears to have its memory set 'globally' to that of  `w2c_host`, does this mean that it is not possible to have multiple host libraries, each sharing data into the module? If it is, how could it be achieved?

3. I think that in my example, what I actually need is `buff` to be a pointer to a valid memory region from that allocated in the host by `wasm_rt_allocate_memory`? Then both the host and module can access this shared memory? But how do I guarantee this? Doesn't the C to WASM compiler decide what memory is used to back `buff`?

---

Suppose I wanted to pass a deep data structure from the host into a WebAssebly module. This structure has numerous pointers to sub-structures, pointers to buffers etc.

Am I correct in thinking that I need a function which is capable of deep-copying the data structure from one region of memory to another? Then, once copied to memory accessible by the WebAssembly module, any changes made by the module need to be copied back to the host?

If this is the case, then it is not too dissimilar to implementing IPC between two processes?
Sharing memory between the host and a WebAssembly module
This is a GitHub Issue
repo:wabt
owner:WebAssembly
Title : Sharing memory between the host and a WebAssembly module
Issue date:

--- start body ---
I am trying to understand how memory is shared between the host and a WebAssembly module (generated using `wasm2c` from an object file generated with `emcc`).

Consider the following very simple example:

```c
int foo(char *buff) {
  return buff[0];
}
```

This is compiled using `emcc` and then `wasm2c` and `clang` to get an object file. However, the function signature eventually becomes:

```c
u32 w2c_unsafe_foo(w2c_unsafe*, u32);
```

I assume, based on the available WebAssembly types, there is no available (semantic) equivalence between the original C code and a WebAssembly representation?

In this case, naturally, passing a pointer to this function casts it to a `u32`, and then the `buff[0]` access causes a trap (I suppose because the pointer is to a region of memory outside of the WebAssembly module's allocated memory?).

What is the correct way to share memory in and out of a WebAssembly module? I have had a look at the [rot13 example](https://github.com/WebAssembly/wabt/blob/main/wasm2c/examples/rot13/main.c), but this uses a `.wat` file, where it is possible to import an external library (e.g., `host`). As was alluded to me elsewhere (https://github.com/WebAssembly/wabt/issues/2289#issuecomment-1701224778), this seems to be what is required to access 'things' (functions/memory) from the host.

Is it possible to do this in C code, which is then compiled into a WebAssebly module?

---

Based on the `rot13` example, I think I need to do something like the following:

```c
int foo() {
  char buff[1024];
  host_get_buff(buff, 1024);
  return buff[0];
}
```

`host_get_buff` would be implemented in the host code and copy the relevant data into `buff`? The questions I have are as follows.

1. Copying into `buff` requires access to this region of memory in `host_get_buff`. In the ro13 example, this appears to be done by allocating a page into `w2c_host`, which is passed as the first argument to `host_get_buff` when it is called. I think the WebAssebly module is then set to use this page for all of its memory uses?

https://github.com/WebAssembly/wabt/blob/dddc03d3bc4df2bca26a880e8f078f84ea3d2454/wasm2c/examples/rot13/rot13.wat#L1

2. Given that the module appears to have its memory set 'globally' to that of  `w2c_host`, does this mean that it is not possible to have multiple host libraries, each sharing data into the module? If it is, how could it be achieved?

3. I think that in my example, what I actually need is `buff` to be a pointer to a valid memory region from that allocated in the host by `wasm_rt_allocate_memory`? Then both the host and module can access this shared memory? But how do I guarantee this? Doesn't the C to WASM compiler decide what memory is used to back `buff`?

---

Suppose I wanted to pass a deep data structure from the host into a WebAssebly module. This structure has numerous pointers to sub-structures, pointers to buffers etc.

Am I correct in thinking that I need a function which is capable of deep-copying the data structure from one region of memory to another? Then, once copied to memory accessible by the WebAssembly module, any changes made by the module need to be copied back to the host?

If this is the case, then it is not too dissimilar to implementing IPC between two processes?
--- end body ---

None