From 055f39c1c89ab4c541d2401de667e7316d6ad0e1 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 28 Jan 2017 23:25:13 +0300 Subject: [PATCH] WASM: fix exception handling --- .../org/teavm/backend/wasm/WasmTarget.java | 4 ++-- ...xceptionHandlingShadowStackContributor.java | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java b/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java index ed57305b4..a5f5fb45a 100644 --- a/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java +++ b/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java @@ -200,11 +200,11 @@ public class WasmTarget implements TeaVMTarget { @Override public void contributeDependencies(DependencyChecker dependencyChecker) { - for (Class type : Arrays.asList(int.class, long.class, float.class, double.class)) { + for (Class type : Arrays.asList(int.class, long.class, float.class, double.class)) { MethodReference method = new MethodReference(WasmRuntime.class, "compare", type, type, int.class); dependencyChecker.linkMethod(method, null).use(); } - for (Class type : Arrays.asList(float.class, double.class)) { + for (Class type : Arrays.asList(float.class, double.class)) { MethodReference method = new MethodReference(WasmRuntime.class, "remainder", type, type, type); dependencyChecker.linkMethod(method, null).use(); } diff --git a/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java b/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java index 8ca3a67f5..c6878c8ce 100644 --- a/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java +++ b/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java @@ -17,7 +17,9 @@ package org.teavm.model.lowlevel; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import org.teavm.common.DominatorTree; import org.teavm.common.Graph; @@ -90,8 +92,12 @@ public class ExceptionHandlingShadowStackContributor { allPhis.addAll(program.basicBlockAt(i).getPhis()); } + Set exceptionHandlers = new HashSet<>(); for (int i = 0; i < blockCount; ++i) { BasicBlock block = program.basicBlockAt(i); + for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { + exceptionHandlers.add(tryCatch.getHandler()); + } if (block.getExceptionVariable() != null) { InvokeInstruction catchCall = new InvokeInstruction(); @@ -111,9 +117,11 @@ public class ExceptionHandlingShadowStackContributor { } for (Phi phi : allPhis) { - for (Incoming incoming : phi.getIncomings()) { - int mappedSource = blockMapping[incoming.getSource().getIndex()]; - incoming.setSource(program.basicBlockAt(mappedSource)); + if (!exceptionHandlers.contains(phi.getBasicBlock())) { + for (Incoming incoming : phi.getIncomings()) { + int mappedSource = blockMapping[incoming.getSource().getIndex()]; + incoming.setSource(program.basicBlockAt(mappedSource)); + } } } @@ -187,8 +195,8 @@ public class ExceptionHandlingShadowStackContributor { List pre = setLocation(getInstructionsBeforeCallSite(callSite), insn.getLocation()); List post = getInstructionsAfterCallSite(block, next, callSite, currentJointSources); post = setLocation(post, insn.getLocation()); - insn.insertPreviousAll(pre); - insn.insertNextAll(post); + block.getLastInstruction().insertPreviousAll(pre); + block.addAll(post); hasExceptionHandlers = true; if (next == null || last) {