From 94977471482032fa4a532b8e60721a45b0d7503c Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Mon, 10 Apr 2023 14:48:41 +0200 Subject: [PATCH] First message routed --- phasmplatform/common/router.py | 2 +- phasmplatform/worker/__main__.py | 2 +- phasmplatform/worker/runners/base.py | 24 ++++++++++++++++++++++++ phasmplatform/worker/runners/wasmtime.py | 21 +++++++++------------ 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/phasmplatform/common/router.py b/phasmplatform/common/router.py index 48e427f..5d2580d 100644 --- a/phasmplatform/common/router.py +++ b/phasmplatform/common/router.py @@ -5,4 +5,4 @@ class BaseRouter: class StdOutRouter(BaseRouter): def post_message(self, namespace: bytes, topic: bytes, kind: bytes, body: bytes) -> None: - print(f'{namespace.decode()}: {topic.decode()}: {kind.decode()}: {body.decode()}') + print(f'ns={namespace.decode()},t={topic.decode()},k={kind.decode()} {body.decode()}') diff --git a/phasmplatform/worker/__main__.py b/phasmplatform/worker/__main__.py index 9a9e7ff..0788256 100644 --- a/phasmplatform/worker/__main__.py +++ b/phasmplatform/worker/__main__.py @@ -17,7 +17,7 @@ def main() -> int: foo: BaseRunner - with open('/home/johan/projects/idea/phasm/examples/crc32.wasm', 'rb') as fil: + with open('/home/johan/projects/idea/phasm/examples/platform.wasm', 'rb') as fil: foo = WasmTimeRunner(stdout_router, fil.read()) namespace = b'test-namespace' diff --git a/phasmplatform/worker/runners/base.py b/phasmplatform/worker/runners/base.py index 2eaace4..02199b4 100644 --- a/phasmplatform/worker/runners/base.py +++ b/phasmplatform/worker/runners/base.py @@ -1,3 +1,5 @@ +from typing import TextIO + from phasmplatform.common.router import BaseRouter @@ -31,3 +33,25 @@ class BaseRunner: body = self.read_bytes(body_ptr) self.router.post_message(namespace, topic, kind, body) + + +def dump_memory(textio: TextIO, mem: bytes) -> None: + line_width = 16 + + prev_line = None + skip = False + for idx in range(0, len(mem), line_width): + line = '' + for idx2 in range(0, line_width): + line += f'{mem[idx + idx2]:02X}' + if idx2 % 2 == 1: + line += ' ' + + if prev_line == line: + if not skip: + textio.write('**\n') + skip = True + else: + textio.write(f'{idx:08x} {line}\n') + + prev_line = line diff --git a/phasmplatform/worker/runners/wasmtime.py b/phasmplatform/worker/runners/wasmtime.py index e90605f..6e937f8 100644 --- a/phasmplatform/worker/runners/wasmtime.py +++ b/phasmplatform/worker/runners/wasmtime.py @@ -30,18 +30,19 @@ class WasmTimeRunner(BaseRunner): self.exports = self.instance.exports(self.store) def alloc_bytes(self, data: bytes) -> int: - alloc_bytes = self.exports['stdlib.types.__alloc_bytes__'] - assert isinstance(alloc_bytes, wasmtime.Func) - ptr = alloc_bytes(self.store, len(data)) - assert isinstance(ptr, int) # type hint - memory = self.exports['memory'] assert isinstance(memory, wasmtime.Memory) # type hint data_ptr = memory.data_ptr(self.store) data_len = memory.data_len(self.store) - idx = ptr + 8 # This is the header from alloc plus the header from __alloc_bytes__ + alloc_bytes = self.exports['stdlib.types.__alloc_bytes__'] + assert isinstance(alloc_bytes, wasmtime.Func) + + ptr = alloc_bytes(self.store, len(data)) + assert isinstance(ptr, int) # type hint + + idx = ptr + 4 # Skip the header from header from __alloc_bytes__ for byt in data: assert idx < data_len data_ptr[idx] = ctypes.c_ubyte(byt) @@ -58,13 +59,9 @@ class WasmTimeRunner(BaseRunner): raw = ctypes.string_at(data_ptr, data_len) - ptr = ptr + 4 # This is the header from alloc + length, = struct.unpack(' None: namespace_ptr = self.alloc_bytes(namespace)