From d9fb2bc159b28903546cead6ec77e29f3d39970e Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 11 Nov 2022 20:59:09 +0100 Subject: [PATCH] Fix bugs in Wasi support. Fix GC issue in Wasm --- core/src/main/java/org/teavm/backend/wasm/WasmHeap.java | 1 - .../java/org/teavm/backend/wasm/runtime/WasiSupport.java | 4 ++-- .../main/java/org/teavm/model/lowlevel/Characteristics.java | 3 ++- core/src/main/java/org/teavm/runtime/ExceptionHandling.java | 6 +++--- .../main/resources/org/teavm/backend/wasm/wasm-runtime.c | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/WasmHeap.java b/core/src/main/java/org/teavm/backend/wasm/WasmHeap.java index 696c05d13..dba362529 100644 --- a/core/src/main/java/org/teavm/backend/wasm/WasmHeap.java +++ b/core/src/main/java/org/teavm/backend/wasm/WasmHeap.java @@ -69,7 +69,6 @@ public final class WasmHeap { public static void initHeap(Address start, int minHeap, int maxHeap, int stackSize, int bufferSize) { initHeapTrace(maxHeap); - buffer = start; buffer = WasmRuntime.align(start, 16); WasmHeap.bufferSize = bufferSize; stack = WasmRuntime.align(buffer.add(bufferSize), 16); diff --git a/core/src/main/java/org/teavm/backend/wasm/runtime/WasiSupport.java b/core/src/main/java/org/teavm/backend/wasm/runtime/WasiSupport.java index 7bead97ab..21cb6970a 100644 --- a/core/src/main/java/org/teavm/backend/wasm/runtime/WasiSupport.java +++ b/core/src/main/java/org/teavm/backend/wasm/runtime/WasiSupport.java @@ -112,8 +112,8 @@ public class WasiSupport { if (errno != ERRNO_SUCCESS) { throw new RuntimeException("Could not get command line arguments"); } - int argvSize = (int) sizesReceiver.value; - int argvBufSize = (int) bufferSizeReceiver.value; + int argvSize = sizesReceiver.value; + int argvBufSize = bufferSizeReceiver.value; int[] argvOffsets = new int[argvSize]; byte[] argvBuffer = new byte[argvBufSize]; diff --git a/core/src/main/java/org/teavm/model/lowlevel/Characteristics.java b/core/src/main/java/org/teavm/model/lowlevel/Characteristics.java index c1c8d034c..976742f7e 100644 --- a/core/src/main/java/org/teavm/model/lowlevel/Characteristics.java +++ b/core/src/main/java/org/teavm/model/lowlevel/Characteristics.java @@ -60,10 +60,11 @@ public class Characteristics { public boolean isResource(String className) { byte result = isResource.getOrDefault(className, (byte) -1); if (result < 0) { - if (className.equals("org/teavm/platform/metadata/Resource")) { + if (className.equals("org.teavm.platform.metadata.Resource")) { result = 1; } else { ClassReader cls = classSource.get(className); + result = 0; if (cls != null) { if (cls.getParent() != null) { result = isResource(cls.getParent()) ? (byte) 1 : 0; diff --git a/core/src/main/java/org/teavm/runtime/ExceptionHandling.java b/core/src/main/java/org/teavm/runtime/ExceptionHandling.java index cff031859..e85584a9c 100644 --- a/core/src/main/java/org/teavm/runtime/ExceptionHandling.java +++ b/core/src/main/java/org/teavm/runtime/ExceptionHandling.java @@ -141,19 +141,19 @@ public final class ExceptionHandling { @Unmanaged public static void throwClassCastException() { - throw new ClassCastException(); + throwException(new ClassCastException()); } @Unmanaged @Export(name = "teavm_throwNullPointerException") public static void throwNullPointerException() { - throw new NullPointerException(); + throwException(new NullPointerException()); } @Unmanaged @Export(name = "teavm_throwArrayIndexOutOfBoundsException") public static void throwArrayIndexOutOfBoundsException() { - throw new ArrayIndexOutOfBoundsException(); + throwException(new ArrayIndexOutOfBoundsException()); } @Unmanaged diff --git a/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.c b/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.c index ef3fdbdb0..acc980de3 100644 --- a/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.c +++ b/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.c @@ -92,7 +92,7 @@ int32_t wasi_snapshot_preview1_args_sizes_get(int32_t argv_size, int32_t argv_bu int32_t bufferSize = 0; for (int i = 0; i < wasm_args; ++i) { - bufferSize += (int32_t) strlen(wasm_argv[i]); + bufferSize += (int32_t) strlen(wasm_argv[i]) + 1; } *argvBufferSizePtr = bufferSize; return 0; @@ -103,8 +103,8 @@ int32_t wasi_snapshot_preview1_args_get(int32_t sizes_ptr, int32_t args_ptr) { char* argsPtr = (char*) (wasm_heap + args_ptr); int offset = 0; for (int i = 0; i < wasm_args; ++i) { - sizesPtr[i] = (int32_t) offset; - int len = strlen(wasm_argv[i]); + sizesPtr[i] = (int32_t) offset + args_ptr; + int len = strlen(wasm_argv[i]) + 1; memcpy(argsPtr + offset, wasm_argv[i], len); offset += len; }