Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:soot-oss/soot into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenArzt committed May 24, 2024
2 parents 7bb46b9 + 06750f0 commit 6dc6ad1
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 22 deletions.
29 changes: 17 additions & 12 deletions src/main/java/soot/jimple/spark/solver/TopoSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
* #L%
*/

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;

import soot.jimple.spark.pag.Node;
import soot.jimple.spark.pag.PAG;
Expand Down Expand Up @@ -61,26 +61,31 @@ public TopoSorter(PAG pag, boolean ignoreTypes) {
protected HashSet<VarNode> visited;

protected void dfsVisit(VarNode n) {
if (visited.contains(n)) {
if (!visited.add(n)) {
return;
}
List<VarNode> stack = new ArrayList<>();
List<VarNode> all = new ArrayList<>();

Stack<VarNode> stack = new Stack<>();
Set<VarNode> visitedSuccessors = new HashSet<>();
stack.add(n);

while (!stack.isEmpty()) {
VarNode s = stack.remove(stack.size() - 1);
if (visited.add(s)) {
all.add(s);
VarNode s = stack.peek();

if (visitedSuccessors.add(s)) {
Node[] succs = pag.simpleLookup(s);
for (Node element : succs) {
if (ignoreTypes || pag.getTypeManager().castNeverFails(n.getType(), element.getType())) {
stack.add((VarNode) element);
if (visited.add((VarNode) element)) {
stack.push((VarNode) element);
}
}
}

} else {
stack.pop();
s.setFinishingNumber(nextFinishNumber++);
}
}
for (int i = all.size() - 1; i >= 0; i--) {
all.get(i).setFinishingNumber(nextFinishNumber++);
}
}
}
20 changes: 10 additions & 10 deletions src/main/java/soot/toDex/DexPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
Expand Down Expand Up @@ -313,7 +313,7 @@ private void printZip() throws IOException {
}

if (Options.v().output_jar()) {
// if we create JAR file, MANIFEST.MF is preferred
// if we create JAR file, MANIFEST.MF is preferred.
addManifest(outputZip, files);
}

Expand Down Expand Up @@ -404,8 +404,8 @@ private void addManifest(ZipOutputStream destination, Collection<File> dexFiles)
try (BufferedOutputStream bufOut = new BufferedOutputStream(destination)) {
manifest.write(bufOut);
bufOut.flush();
destination.closeEntry();
}
destination.closeEntry();
}

/**
Expand Down Expand Up @@ -1116,7 +1116,7 @@ protected Collection<Method> toMethods(SootClass clazz) {

/**
* Checks whether the given method shall be ignored, i.e., not written out to dex
*
*
* @param sm
* The method to check
* @return True to ignore the method while writing the dex file, false to write it out as normal
Expand All @@ -1127,7 +1127,7 @@ protected boolean isIgnored(SootMethod sm) {

/**
* Checks whether the given field shall be ignored, i.e., not written out to dex
*
*
* @param sf
* The field to check
* @return True to ignore the field while writing the dex file, false to write it out as normal
Expand Down Expand Up @@ -1293,9 +1293,9 @@ protected MethodImplementation toMethodImplementation(SootMethod m) {

/**
* Creates a statement visitor to build code for each statement.
*
*
* Allows subclasses to use own implementations
*
*
* @param belongingMethod
* the method
* @param arrayInitDetector
Expand All @@ -1308,7 +1308,7 @@ protected StmtVisitor buildStmtVisitor(SootMethod belongingMethod, DexArrayInitD

/**
* Writes out the information stored in the tags associated with the given statement
*
*
* @param builder
* The builder used to generate the Dalvik method implementation
* @param stmt
Expand Down Expand Up @@ -1397,7 +1397,7 @@ private void fixLongJumps(List<BuilderInstruction> instructions, LabelAssigner l
/*
* Assuming every instruction between this instruction and the jump target is a CONST_STRING instruction, how
* much could the distance increase?
*
*
* Because we only spend the effort to count the number of CONST_STRING instructions if there is a real
* chance that it changes the distance to overflow the allowed maximum.
*/
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/soot/PackManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,31 @@
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/

import com.google.common.io.Files;
import org.junit.Test;
import soot.options.Options;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/**
* Some tests to disable jb transformers.
*
* @author Linghui Luo
*/
public class PackManagerTest {
Expand Down Expand Up @@ -122,6 +131,30 @@ public void testDisableUnusedLocalEliminatorInJBPhase() {
}
}

@Test
public void testWritingToJar() throws Exception {
setup();
File tempDir = Files.createTempDir();
Options.v().set_output_jar(true);
Options.v().set_output_dir(tempDir.getAbsolutePath());
Options.v().set_output_format(Options.output_format_dex);
Scene.v().loadNecessaryClasses();
PackManager.v().runBodyPacks();
PackManager.v().writeOutput();
assertEquals(1, tempDir.listFiles().length);
File targetJar = tempDir.listFiles()[0];
ZipFile jarRead = new ZipFile(targetJar.getAbsolutePath());
Enumeration<? extends ZipEntry> entries = jarRead.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().toLowerCase().endsWith("manifest.mf")) {
assertTrue(entry.getSize() > 0);
return;
}
}
fail("No Manifest entry found in " + targetJar.getAbsolutePath());
}

public static List<String> expectedBody(String... jimpleLines) {
return Stream.of(jimpleLines).collect(Collectors.toList());
}
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/soot/jimple/spark/solver/SCCCollapserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package soot.jimple.spark.solver;

/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 2018 Raja Vallée-Rai and others
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/

import java.util.Collections;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

import soot.Scene;
import soot.Type;
import soot.jimple.spark.pag.PAG;
import soot.jimple.spark.pag.VarNode;
import soot.options.SparkOptions;

public class SCCCollapserTest {

@Test
public void testSeparateComponents() {
Scene.v().loadBasicClasses();
Type type = Scene.v().getObjectType();

SparkOptions sparkOptions = new SparkOptions(Collections.emptyMap());
PAG pag = new PAG(sparkOptions);

VarNode a = pag.makeGlobalVarNode("a", type);
VarNode b = pag.makeGlobalVarNode("b", type);
VarNode c = pag.makeGlobalVarNode("c", type);
pag.addEdge(a, b);
pag.addEdge(a, c);
pag.addEdge(b, c);

SCCCollapser sccCollapser = new SCCCollapser(pag, false);
sccCollapser.collapse();
pag.cleanUpMerges();

assertEquals(a, a.getReplacement());
assertEquals(b, b.getReplacement());
assertEquals(c, c.getReplacement());
}
}

0 comments on commit 6dc6ad1

Please sign in to comment.