Coder Social home page Coder Social logo

Comments (7)

sbc100 avatar sbc100 commented on June 22, 2024

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.

sbc100 avatar sbc100 commented on June 22, 2024

(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.

kiancross avatar kiancross commented on June 22, 2024

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.

(import "host" "mem" (memory $mem 1))
(import "host" "fill_buf" (func $fill_buf (param i32 i32) (result i32)))
(import "host" "buf_done" (func $buf_done (param i32 i32)))

from wabt.

keithw avatar keithw commented on June 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.