Comments (7)
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?
Any memory that you want to be visible to the WebAssembly instance needs to be allocated inside the instance's memory region. Performing as deep copy in and out at API boundary is one way to achieve this.
Another alternative is have the host directly use memory allocated inside the module's memory. For example, you could export a malloc
function from your module and then call w2c_unsafe_malloc
to get a memory pointer into the instance's wasm_rt_memory_t
. Then you can read and write to that location (remember that the return value from malloc is an offset relative to the start of the memory's buffer), which is shared with the instance. without needing to copy.
What you can't do is allocate memory on the outside and somehow "transfer" to the instance without a copy.
from wabt.
(BTW, this is exactly how WebAssembly works on the web.. you either need to make a copy of the data, or have it initially allocated from within the instance memory)
from wabt.
Do you know of any examples where the WebAssembly module was originally written in C (C -> WASM -> C)? The examples in this repo use .wat
, and I'm not entirely sure how to 'import' functions (like below) from the host into the WebAssembly module.
wabt/wasm2c/examples/rot13/rot13.wat
Lines 1 to 3 in dddc03d
from wabt.
Here's an example of how you could write the rot13.wat
module in C and compile it with a wasm32-targeting clang. The generated code isn't quite as concise, but it works as a drop-in replacement to the hand-written one (importing functions and its memory from the host):
$ cat rot13_in_c.c
#include <stdint.h>
uint32_t fill_buf(char* ptr, uint32_t size) __attribute((import_module("host"), import_name("fill_buf")));
void buf_done(char* ptr, uint32_t size) __attribute((import_module("host"), import_name("buf_done")));
char rot13c(char c)
{
char ch = c & 0xdf;
if (ch < 'A') return c;
if (ch <= 'M') return c + 13;
if (ch <= 'Z') return c - 13;
return c;
}
void rot13(void) __attribute((export_name("rot13")))
{
uint32_t size = fill_buf(0, 1024);
for (char* ptr = 0; ptr < (char*)size; ptr++) {
char rot13_char = rot13c(*ptr);
*ptr = rot13_char;
}
buf_done(0, size);
}
$ clang -nostdlib -Wl,--no-entry -Xlinker --import-memory=host,mem -Os rot13_in_c.c -o rot13_in_c.wasm
$ wasm2wat -f --generate-names rot13_in_c.wasm | tee ./wabt/wasm2c/examples/rot13/rot13.wat
(module $rot13_in_c.wasm
(type $t0 (func (param i32 i32) (result i32)))
(type $t1 (func (param i32 i32)))
(type $t2 (func))
(import "host" "mem" (memory $host.mem 2))
(import "host" "fill_buf" (func $fill_buf (type $t0)))
(import "host" "buf_done" (func $buf_done (type $t1)))
(func $rot13 (type $t2)
(local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32)
(local.set $l0
(i32.const 0))
(block $B0
(br_if $B0
(i32.eqz
(local.tee $l1
(call $fill_buf
(i32.const 0)
(i32.const 1024)))))
(local.set $l2
(local.get $l1))
(loop $L1
(block $B2
(br_if $B2
(i32.lt_s
(i32.extend8_s
(local.tee $l4
(i32.and
(local.tee $l3
(i32.load8_u
(local.get $l0)))
(i32.const -33))))
(i32.const 65)))
(block $B3
(br_if $B3
(i32.gt_u
(local.tee $l4
(i32.and
(local.get $l4)
(i32.const 255)))
(i32.const 77)))
(local.set $l3
(i32.add
(local.get $l3)
(i32.const 13)))
(br $B2))
(local.set $l3
(select
(i32.add
(local.get $l3)
(i32.const -13))
(local.get $l3)
(i32.lt_u
(local.get $l4)
(i32.const 91)))))
(i32.store8
(local.get $l0)
(local.get $l3))
(local.set $l0
(i32.add
(local.get $l0)
(i32.const 1)))
(br_if $L1
(local.tee $l2
(i32.add
(local.get $l2)
(i32.const -1))))))
(call $buf_done
(i32.const 0)
(local.get $l1)))
(table $T0 1 1 funcref)
(global $__stack_pointer (mut i32) (i32.const 66560))
(export "rot13" (func $rot13)))
$ cd wabt/wasm2c/examples/rot13/
$ make
../../../bin/wat2wasm rot13.wat -o rot13.wasm
../../../bin/wasm2c rot13.wasm -o rot13.c --disable-simd
cc -I../.. -c -o rot13.o rot13.c
cc -I../.. -c -o main.o main.c
cc rot13.o main.o ../../wasm-rt-impl.o /usr/lib/x86_64-linux-gnu/libm.so -o rot13
$ ./rot13 This is a test.
This -> Guvf
is -> vf
a -> n
test. -> grfg.
from wabt.
Related Issues (20)
- WastParser handling of `(module binary)` and `(module quote)`
- wasm2c: we need to pick standard C data structure implementations to finish thread proposal HOT 10
- Inconsistent archive name for macos HOT 1
- wasm-objdump has incorrect local numbering HOT 2
- error: couldn't allocate input reg for constraint 'r' on MIPS with clang when compiling wasm2c-generated source HOT 10
- Where I can find binary .exe files? HOT 1
- desugar: overflow issue for typeidx of call_indirect instruction HOT 5
- huge memory alloc causes SIGABORT signal at reserve function in binary-reader-interp.cc:761 HOT 6
- Shall the objdump API be included in libwabtjs target
- wasm2wat : Unexpected opcode 0xfe 0x25 HOT 2
- `wasm-validate` gives false positive errors that occur after a true positive. HOT 1
- `w2c_env` and `w2c_wasi`: what are they? HOT 6
- Hash-pin workflow GitHub Actions HOT 1
- Proposal: stats HOT 3
- Tech debt: keep man pages up to date
- Tech debt: keep mandoc html up to date
- Can't run on Ubuntu focal HOT 7
- wasm-decompile: Assertion `is_name()' failed. HOT 1
- wasm-interp errors on seemingly semantically-correct wasm module HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wabt.