From f264865cc1677e085d991da7a1f687830cbc0806 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 9 Jul 2019 17:57:28 +0300 Subject: [PATCH] C: fix bugs in exception handling lowerer --- ...ceptionHandlingShadowStackContributor.java | 40 +++++++++---------- .../org/teavm/model/text/ListingLexer.java | 7 ++++ .../org/teavm/model/ListingParseUtils.java | 5 ++- 3 files changed, 30 insertions(+), 22 deletions(-) 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 6db4c50d9..68ef8ca65 100644 --- a/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java +++ b/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java @@ -141,7 +141,6 @@ public class ExceptionHandlingShadowStackContributor { IntObjectMap jointReceiverMaps = new IntObjectHashMap<>(); Arrays.fill(currentJointSources, -1); - IntSet outgoingVariablesToRemove = new IntHashSet(); IntSet variablesDefinedHere = new IntHashSet(); for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { @@ -175,6 +174,17 @@ public class ExceptionHandlingShadowStackContributor { jointReceiverMaps.put(tryCatch.getHandler().getIndex(), jointReceiverMap); } + for (Phi phi : block.getPhis()) { + Variable definedVar = phi.getReceiver(); + for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { + int jointReceiver = jointReceiverMaps.get(tryCatch.getHandler().getIndex())[definedVar.getIndex()]; + if (jointReceiver >= 0) { + currentJointSources[jointReceiver] = definedVar.getIndex(); + } + } + variablesDefinedHere.add(definedVar.getIndex()); + } + DefinitionExtractor defExtractor = new DefinitionExtractor(); List blocksToClearHandlers = new ArrayList<>(); blocksToClearHandlers.add(block); @@ -228,7 +238,7 @@ public class ExceptionHandlingShadowStackContributor { callSites.add(callSite); List pre = setLocation(getInstructionsBeforeCallSite(callSite), insn.getLocation()); List post = getInstructionsAfterCallSite(initialBlock, block, next, callSite, - currentJointSources, outgoingVariablesToRemove, variablesDefinedHere); + currentJointSources, variablesDefinedHere); post = setLocation(post, insn.getLocation()); block.getLastInstruction().insertPreviousAll(pre); block.addAll(post); @@ -238,7 +248,6 @@ public class ExceptionHandlingShadowStackContributor { break; } block = next; - outgoingVariablesToRemove.clear(); variablesDefinedHere.clear(); } @@ -247,12 +256,6 @@ public class ExceptionHandlingShadowStackContributor { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { int jointReceiver = jointReceiverMaps.get(tryCatch.getHandler().getIndex())[definedVar.getIndex()]; if (jointReceiver >= 0) { - int formerVar = currentJointSources[jointReceiver]; - if (formerVar >= 0) { - if (variableDefinitionPlaces[formerVar] == initialBlock) { - outgoingVariablesToRemove.add(formerVar); - } - } currentJointSources[jointReceiver] = definedVar.getIndex(); } } @@ -260,7 +263,7 @@ public class ExceptionHandlingShadowStackContributor { } } - fixOutgoingPhis(initialBlock, block, currentJointSources, outgoingVariablesToRemove, variablesDefinedHere); + fixOutgoingPhis(initialBlock, block, currentJointSources, variablesDefinedHere); for (BasicBlock blockToClear : blocksToClearHandlers) { blockToClear.getTryCatchBlocks().clear(); } @@ -321,8 +324,7 @@ public class ExceptionHandlingShadowStackContributor { } private List getInstructionsAfterCallSite(BasicBlock initialBlock, BasicBlock block, BasicBlock next, - CallSiteDescriptor callSite, int[] currentJointSources, IntSet outgoingVariablesToRemove, - IntSet variablesDefinedHere) { + CallSiteDescriptor callSite, int[] currentJointSources, IntSet variablesDefinedHere) { Program program = block.getProgram(); List instructions = new ArrayList<>(); @@ -360,7 +362,7 @@ public class ExceptionHandlingShadowStackContributor { switchInsn.getEntries().add(catchEntry); } } - fixOutgoingPhis(initialBlock, block, currentJointSources, outgoingVariablesToRemove, variablesDefinedHere); + fixOutgoingPhis(initialBlock, block, currentJointSources, variablesDefinedHere); if (!defaultExists) { switchInsn.setDefaultTarget(getDefaultExceptionHandler()); @@ -393,7 +395,7 @@ public class ExceptionHandlingShadowStackContributor { } private void fixOutgoingPhis(BasicBlock block, BasicBlock newBlock, int[] currentJointSources, - IntSet outgoingVariablesToRemove, IntSet variablesDefinedHere) { + IntSet variablesDefinedHere) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (Phi phi : tryCatch.getHandler().getPhis()) { int value = currentJointSources[phi.getReceiver().getIndex()]; @@ -403,13 +405,10 @@ public class ExceptionHandlingShadowStackContributor { List additionalIncomings = new ArrayList<>(); for (int i = 0; i < phi.getIncomings().size(); i++) { Incoming incoming = phi.getIncomings().get(i); - if (incoming.getSource() != block) { + if (incoming.getSource() != block || incoming.getSource() == newBlock) { continue; } - if (outgoingVariablesToRemove.contains(incoming.getValue().getIndex())) { - phi.getIncomings().remove(i--); - break; - } else if (incoming.getValue().getIndex() == value && incoming.getSource() != newBlock) { + if (incoming.getValue().getIndex() == value) { if (variablesDefinedHere.contains(value)) { incoming.setSource(newBlock); } else { @@ -418,7 +417,8 @@ public class ExceptionHandlingShadowStackContributor { incomingCopy.setValue(incoming.getValue()); additionalIncomings.add(incomingCopy); } - break; + } else { + phi.getIncomings().remove(i--); } } diff --git a/core/src/main/java/org/teavm/model/text/ListingLexer.java b/core/src/main/java/org/teavm/model/text/ListingLexer.java index b13ea6e11..9aa1e76d0 100644 --- a/core/src/main/java/org/teavm/model/text/ListingLexer.java +++ b/core/src/main/java/org/teavm/model/text/ListingLexer.java @@ -62,6 +62,13 @@ class ListingLexer { case -1: token = ListingToken.EOF; break; + case '\r': + token = ListingToken.EOL; + nextChar(); + if (c == '\n') { + nextChar(); + } + break; case '\n': token = ListingToken.EOL; nextChar(); diff --git a/core/src/test/java/org/teavm/model/ListingParseUtils.java b/core/src/test/java/org/teavm/model/ListingParseUtils.java index e3725b92c..9e9981b3d 100644 --- a/core/src/test/java/org/teavm/model/ListingParseUtils.java +++ b/core/src/test/java/org/teavm/model/ListingParseUtils.java @@ -18,6 +18,7 @@ package org.teavm.model; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import org.junit.Assert; import org.teavm.model.text.ListingParseException; import org.teavm.model.text.ListingParser; @@ -29,7 +30,7 @@ public final class ListingParseUtils { public static Program parseFromResource(String resourceName) { ClassLoader classLoader = ListingParseUtils.class.getClassLoader(); try (InputStream input = classLoader.getResourceAsStream(resourceName); - InputStreamReader reader = new InputStreamReader(input, "UTF-8")) { + InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) { return new ListingParser().parse(reader); } catch (IOException e) { throw new RuntimeException(e); @@ -49,7 +50,7 @@ public final class ListingParseUtils { private static Location offsetToLocation(int offset, InputStream input) throws IOException { int row = 0; int column = 0; - try (InputStreamReader reader = new InputStreamReader(input, "UTF-8")) { + try (InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) { for (int i = 0; i < offset; ++i) { int c = reader.read(); if (c == '\n') {