From 2e7370dcfd0326ec42bc19765dd029c1dfad49dc Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 10 Mar 2019 18:32:26 +0100 Subject: [PATCH] ignore monitorenter and monitorexit from synchronized blocks. see #5 --- .../jwebassembly/binary/BinaryModuleWriter.java | 4 ++++ .../module/JavaMethodWasmCodeBuilder.java | 8 ++++++-- .../jwebassembly/text/TextModuleWriter.java | 4 ++++ .../jwebassembly/wasm/WasmBlockOperator.java | 2 ++ .../jwebassembly/runtime/Exceptions.java | 12 ++++++++++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 38845f2d..ab8f1042 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -891,6 +891,10 @@ protected void writeBlockCode( @Nonnull WasmBlockOperator op, @Nullable Object d codeStream.writeVaruint32( (Integer)data ); // break depth codeStream.writeVaruint32( 0 ); // event/exception ever 0 because currently there is only one with signature anyref break; + case MONITOR_ENTER: + case MONITOR_EXIT: + codeStream.writeOpCode( DROP ); + break; default: throw new Error( "Unknown block: " + op ); } diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index 22af886a..157f8b78 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -609,8 +609,12 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, boo break; //TODO case 192: // checkcast //TODO case 193: // instanceof - //TODO case 194: // monitorenter - //TODO case 195: // monitorexit + case 194: // monitorenter + addBlockInstruction( WasmBlockOperator.MONITOR_ENTER, null, codePos ); + break; + case 195: // monitorexit + addBlockInstruction( WasmBlockOperator.MONITOR_EXIT, null, codePos ); + break; //TODO case 196: // wide //TODO case 197: // multianewarray case 198: // ifnull diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 5a0d0393..b5a1af34 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -432,6 +432,10 @@ protected void writeBlockCode( @Nonnull WasmBlockOperator op, @Nullable Object d case BR_ON_EXN: name = "br_on_exn " + data + " 0"; // br_on_exn, break depth, event; // currently there is only one event/exception with anyref break; + case MONITOR_ENTER: + case MONITOR_EXIT: + name = "drop"; + break; default: throw new Error( "Unknown block: " + op ); } diff --git a/src/de/inetsoftware/jwebassembly/wasm/WasmBlockOperator.java b/src/de/inetsoftware/jwebassembly/wasm/WasmBlockOperator.java index c0de6661..499f9751 100644 --- a/src/de/inetsoftware/jwebassembly/wasm/WasmBlockOperator.java +++ b/src/de/inetsoftware/jwebassembly/wasm/WasmBlockOperator.java @@ -39,4 +39,6 @@ public enum WasmBlockOperator { THROW, RETHROW, BR_ON_EXN, + MONITOR_ENTER, + MONITOR_EXIT, } diff --git a/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java b/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java index 096b3d90..95bb3a78 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java +++ b/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java @@ -118,6 +118,18 @@ static int complex() { private static int divNull() { return 5 / 0; } + + // a synchronized also add an try/finally internally + @Export + static int sync() { + int v = 1; + Object monitor = new Object(); + synchronized( monitor ) { + v++; + } + return v; + } + // @Export // static int npe() { // Object obj = new NullPointerException();