Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supplying a memory instance to a function before execution #38

Closed
void4 opened this issue Dec 23, 2020 · 7 comments
Closed

Supplying a memory instance to a function before execution #38

void4 opened this issue Dec 23, 2020 · 7 comments

Comments

@void4
Copy link

void4 commented Dec 23, 2020

In Python, I have a list of integers in the range 0-255, so in the uint8 range.
I want to supply them to the WebAssembly runtime before it starts execution, so the called function will be able to access it when it runs. Can I do this with pywasm?

It might look like this:

import pywasm

numbers = [1,2,3,4,5]#,...
memory = pywasm.Memory(numbers, dtype=pywasm.uint8)
runtime = pywasm.load('./examples/addlist.wasm', memories=[memory])
r = runtime.exec('addlist', [])
print(r) # 1+2+3+4+5+... = x
@mohanson
Copy link
Owner

It is very difficult for us to maintain the memory structure myself. The more I suggest is by importing external functions such as:

Source C code

char data[1024] = {};

extern int load_data(int addr_from, int addr_to);

int main() { 
  load_data(&data[0], &data[1023]);
  return data[0];
}

Compile C to wasm program

(module
 (type $FUNCSIG$iii (func (param i32 i32) (result i32)))
 (import "env" "load_data" (func $load_data (param i32 i32) (result i32)))
 (table 0 anyfunc)
 (memory $0 1)
 (export "memory" (memory $0))
 (export "main" (func $main))
 (func $main (; 1 ;) (result i32)
  (drop
   (call $load_data
    (i32.const 16)
    (i32.const 1039)
   )
  )
  (i32.load8_s offset=16
   (i32.const 0)
  )
 )
)

And used in pywasm as

import pywasm


your_data = [4 for i in range(1024)]

def load_data(ctx: pywasm.Ctx, addr_from: int, addr_to: int):
    ctx.memory_list[0].data[addr_from:addr_to] = your_data
    return 0

runtime = pywasm.load('/tmp/program.wasm', {'env': {'load_data': load_data}})
r = runtime.exec('main', [])
print(r)  # 4

@void4
Copy link
Author

void4 commented Dec 23, 2020

Nice! I think it might also work this way:

data = [1,2,3,4,5]

gamestate = pywasm.Memory(pywasm.binary.MemoryType())
gamestate.grow(len(data))

for figindex, fig in enumerate(data):
gamestate.data[figindex] = fig

runtime = pywasm.load(path, {
	'env': {
		'abort': env_abort,
		"gamestate": gamestate
	}
}, opts=option)

@mohanson
Copy link
Owner

You did not use it correctly. The memory is managed by your program. For example, if your program uses alloc to apply for a block of memory (always starting from address 0), then this will overwrite your initial data.

@mohanson
Copy link
Owner

Where you should write data to the memory, this information should always come from your program, let it decide.

@void4
Copy link
Author

void4 commented Dec 23, 2020

In this case, I may have to inject the function call manually somehow if I did it this way.

The AssemblyScript compiler seems to have an --importMemory flag (also here), which imports env.memory. But I didn't get it to work yet, it fails with

		runtime = pywasm.load(path, {
			'env': {
				'abort': env_abort,
				"memory": gamestate
			}
		}, opts=option)
Traceback (most recent call last):
  File "main.py", line 131, in <module>
    value = runWasm(players[board.turn], board)
  File "main.py", line 46, in runWasm
    runtime = pywasm.load(path, {
  File "/home/one/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pywasm/__init__.py", line 99, in load
    return Runtime(module, imps, opts)
  File "/home/one/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pywasm/__init__.py", line 57, in __init__
    self.machine.instantiate(module, extern_value_list)
  File "/home/one/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pywasm/execution.py", line 1984, in instantiate
    assert match_memory(a, b)

You are probably right, but I'll try it this way one more time

@void4
Copy link
Author

void4 commented Dec 23, 2020

Okay, I give up my alternative attempt, your solution works, and probably saves memory too. Thank you :)

@void4 void4 closed this as completed Dec 23, 2020
@prescod
Copy link

prescod commented Dec 24, 2020

So is it impossible to supply memories as inputs? I ran into the same exception void4 did when I tried it.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants