From b98890d36170cb56e0d8e9dc2f339e1b0a2392ff Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Thu, 24 Oct 2024 21:53:34 +0200 Subject: [PATCH 01/10] setup test case --- sootup.jimple.frontend/pom.xml | 5 ++ .../JB_CP.jimple | 51 +++++++++++++++++++ .../JimpleAnalysisInputLocationTest.java | 28 ++++++++++ 3 files changed, 84 insertions(+) create mode 100644 sootup.jimple.frontend/src/test/java/resources/jimple/testbodyinterceptorsinjimpleinputlocation/JB_CP.jimple diff --git a/sootup.jimple.frontend/pom.xml b/sootup.jimple.frontend/pom.xml index 88f68c5dd0b..536c26dd2bd 100644 --- a/sootup.jimple.frontend/pom.xml +++ b/sootup.jimple.frontend/pom.xml @@ -51,6 +51,11 @@ sootup.java.core + + org.soot-oss + sootup.interceptors + + org.antlr diff --git a/sootup.jimple.frontend/src/test/java/resources/jimple/testbodyinterceptorsinjimpleinputlocation/JB_CP.jimple b/sootup.jimple.frontend/src/test/java/resources/jimple/testbodyinterceptorsinjimpleinputlocation/JB_CP.jimple new file mode 100644 index 00000000000..398c2d92981 --- /dev/null +++ b/sootup.jimple.frontend/src/test/java/resources/jimple/testbodyinterceptorsinjimpleinputlocation/JB_CP.jimple @@ -0,0 +1,51 @@ +public class JB_CP extends java.lang.Object +{ + public void () + { + JB_CP r0; + + + r0 := @this: JB_CP; + specialinvoke r0.()>(); + + return; + } + + int tc1() + { + byte b0, b1; + java.io.PrintStream r0; + JB_CP r1; + + + r1 := @this: JB_CP; + b0 = 5; + b1 = b0; + r0 = ; + virtualinvoke r0.(b1); + + return b1; + } + + public static void main(java.lang.String[]) + { + int i0; + java.io.PrintStream r0; + java.lang.String r1; + java.lang.String[] r2; + JB_CP r3, r4; + + + r2 := @parameter0: java.lang.String[]; + r3 = new JB_CP; + specialinvoke r3.()>(); + r4 = r3; + r0 = ; + i0 = virtualinvoke r4.(); + r1 = dynamicinvoke "makeConcatWithConstants" (i0) ("tc1: \u0001"); + virtualinvoke r0.(r1); + + return; + } +} + diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java index 006e051b0a5..1f37a6f673f 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java @@ -4,12 +4,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import sootup.core.model.SootClass; +import sootup.core.model.SourceType; import sootup.core.signatures.PackageName; import sootup.core.types.ClassType; +import sootup.interceptors.CopyPropagator; @Tag("Java8") public class JimpleAnalysisInputLocationTest { @@ -97,4 +102,27 @@ public PackageName getPackageName() { final Optional classSource4 = jv2.getClass(classType); assertTrue(classSource4.isPresent()); } + + @Test + public void testIfBodyInterceptorsApplied() { + final String resourceDir = "src/test/java/resources/"; + final JimpleAnalysisInputLocation inputLocation = + new JimpleAnalysisInputLocation( + Paths.get(resourceDir + "/jimple/testbodyinterceptorsinjimpleinputlocation"), + SourceType.Application, + Arrays.asList(new CopyPropagator())); + JimpleView jv1 = new JimpleView(inputLocation); + List applicationClasses = jv1.getClasses().collect(Collectors.toList()); + applicationClasses.forEach( + cls -> { + cls.getMethods() + .forEach( + m -> { + if (m.getSignature().getName().contains("tc1")) { + String s = m.getBody().toString(); + System.out.println(s); + } + }); + }); + } } From cff81e9bcdb1274f5a93a4314b922c7f1364ddf0 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Fri, 1 Nov 2024 17:29:38 +0100 Subject: [PATCH 02/10] applied BI to JimpleAnalysisInputLocation --- .../frontend/JimpleAnalysisInputLocation.java | 4 +- .../jimple/frontend/JimpleClassProvider.java | 7 ++- .../jimple/frontend/JimpleConverter.java | 49 ++++++++++++++----- .../JimpleStringAnalysisInputLocation.java | 2 +- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleAnalysisInputLocation.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleAnalysisInputLocation.java index 4cbef4bcb89..6d558b20f6e 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleAnalysisInputLocation.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleAnalysisInputLocation.java @@ -120,13 +120,13 @@ List walkDirectory( @Nonnull public Collection getClassSources(@Nonnull View view) { return walkDirectory( - path, view.getIdentifierFactory(), new JimpleClassProvider(bodyInterceptors)); + path, view.getIdentifierFactory(), new JimpleClassProvider(bodyInterceptors, view)); } @Override @Nonnull public Optional getClassSource(@Nonnull ClassType type, @Nonnull View view) { - final JimpleClassProvider classProvider = new JimpleClassProvider(bodyInterceptors); + final JimpleClassProvider classProvider = new JimpleClassProvider(bodyInterceptors, view); final String ext = classProvider.getHandledFileType().toString().toLowerCase(); diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleClassProvider.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleClassProvider.java index cc3d8245720..4c3f0702dd0 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleClassProvider.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleClassProvider.java @@ -37,16 +37,19 @@ import sootup.core.inputlocation.FileType; import sootup.core.transform.BodyInterceptor; import sootup.core.types.ClassType; +import sootup.core.views.View; /** @author Markus Schmidt */ public class JimpleClassProvider implements ClassProvider { @Nonnull private final List bodyInterceptors; + @Nonnull private final View view; private static final @Nonnull Logger logger = LoggerFactory.getLogger(JimpleClassProvider.class); - public JimpleClassProvider(List bodyInterceptors) { + public JimpleClassProvider(List bodyInterceptors, @Nonnull View view) { this.bodyInterceptors = bodyInterceptors; + this.view = view; } @Override @@ -57,7 +60,7 @@ public Optional createClassSource( final JimpleConverter jimpleConverter = new JimpleConverter(); return Optional.of( jimpleConverter.run( - CharStreams.fromPath(sourcePath), inputlocation, sourcePath, bodyInterceptors)); + CharStreams.fromPath(sourcePath), inputlocation, sourcePath, bodyInterceptors, view)); } catch (IOException | ResolveException e) { logger.warn( "The jimple file of " diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java index 29d53d070d5..49c1d39085f 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java @@ -49,6 +49,7 @@ import sootup.core.signatures.SootClassMemberSubSignature; import sootup.core.transform.BodyInterceptor; import sootup.core.types.*; +import sootup.core.views.View; import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; import sootup.jimple.JimpleBaseVisitor; @@ -60,40 +61,40 @@ public OverridingClassSource run( @Nonnull CharStream charStream, @Nonnull AnalysisInputLocation inputlocation, @Nonnull Path sourcePath) { - return run(charStream, inputlocation, sourcePath, Collections.emptyList()); + return run(charStream, inputlocation, sourcePath, Collections.emptyList(), null); } public OverridingClassSource run( @Nonnull CharStream charStream, @Nonnull AnalysisInputLocation inputlocation, @Nonnull Path sourcePath, - @Nonnull List bodyInterceptors) { + @Nonnull List bodyInterceptors, + @Nonnull View view) { final JimpleParser jimpleParser = JimpleConverterUtil.createJimpleParser(charStream, sourcePath); jimpleParser.setErrorHandler(new BailErrorStrategy()); - return run(jimpleParser, inputlocation, sourcePath, bodyInterceptors); + return run(jimpleParser, inputlocation, sourcePath, bodyInterceptors, view); } public OverridingClassSource run( @Nonnull JimpleParser parser, @Nonnull AnalysisInputLocation inputlocation, @Nonnull Path sourcePath) { - return run(parser, inputlocation, sourcePath, Collections.emptyList()); + return run(parser, inputlocation, sourcePath, Collections.emptyList(), null); } public OverridingClassSource run( @Nonnull JimpleParser parser, @Nonnull AnalysisInputLocation inputlocation, @Nonnull Path sourcePath, - @Nonnull List bodyInterceptors) { - - // FIXME: [ms] apply bodyInterceptors or better: move that logic into View itself! + @Nonnull List bodyInterceptors, + @Nonnull View view) { ClassVisitor classVisitor; try { - classVisitor = new ClassVisitor(sourcePath); + classVisitor = new ClassVisitor(sourcePath, bodyInterceptors, view); classVisitor.visit(parser.file()); } catch (ParseCancellationException ex) { throw new ResolveException("Syntax Error", sourcePath, ex); @@ -119,10 +120,15 @@ private static class ClassVisitor extends JimpleBaseVisitor { @Nonnull private final JimpleConverterUtil util; @Nonnull private final Path path; + @Nonnull private final List bodyInterceptors; + @Nonnull private final View view; - public ClassVisitor(@Nonnull Path path) { + public ClassVisitor( + @Nonnull Path path, @Nonnull List bodyInterceptors, @Nonnull View view) { this.path = path; util = new JimpleConverterUtil(path); + this.bodyInterceptors = bodyInterceptors; + this.view = view; } private ClassType clazz = null; @@ -188,17 +194,36 @@ public Boolean visitFile(@Nonnull JimpleParser.FileContext ctx) { for (int i = 0; i < ctx.member().size(); i++) { if (ctx.member(i).method() != null) { final SootMethod m = new MethodVisitor().visitMember(ctx.member(i)); + Body.BodyBuilder bodyBuilder = Body.builder(m.getBody(), m.getModifiers()); + for (BodyInterceptor bodyInterceptor : bodyInterceptors) { + try { + bodyInterceptor.interceptBody(bodyBuilder, view); + bodyBuilder + .getStmtGraph() + .validateStmtConnectionsInGraph(); // TODO: remove in the future ;-) + } catch (Exception e) { + throw new IllegalStateException( + "Failed to apply " + bodyInterceptor + " to " + m.getSignature(), e); + } + } + Body modifiedBody = bodyBuilder.build(); + SootMethod sm = + new SootMethod( + new OverridingBodySource(m.getBodySource()).withBody(modifiedBody), + m.getSignature(), + m.getModifiers(), + m.getExceptionSignatures(), + m.getPosition()); if (methods.stream() .anyMatch( meth -> { - final MethodSignature signature = m.getSignature(); + final MethodSignature signature = sm.getSignature(); return meth.getSignature().equals(signature); })) { throw new ResolveException( "Method with the same Signature does already exist.", path, m.getPosition()); } - methods.add(m); - + methods.add(sm); } else { final JimpleParser.FieldContext fieldCtx = ctx.member(i).field(); EnumSet modifier = getFieldModifiers(fieldCtx.field_modifier()); diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java index 392628b41e1..cb82d54cd32 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java @@ -66,7 +66,7 @@ public JimpleStringAnalysisInputLocation( JimpleConverter jimpleConverter = new JimpleConverter(); classSource = jimpleConverter.run( - CharStreams.fromString(jimpleFileContents), this, path, bodyInterceptors); + CharStreams.fromString(jimpleFileContents), this, path, bodyInterceptors, null); } catch (Exception e) { throw new IllegalArgumentException("No valid Jimple given.", e); } From 2379275ff3f4900b5159066f901ee09164923b29 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Fri, 1 Nov 2024 18:15:42 +0100 Subject: [PATCH 03/10] not apply BI if method is not concrete --- .../jimple/frontend/JimpleConverter.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java index 49c1d39085f..1284ff3e2e2 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java @@ -194,36 +194,40 @@ public Boolean visitFile(@Nonnull JimpleParser.FileContext ctx) { for (int i = 0; i < ctx.member().size(); i++) { if (ctx.member(i).method() != null) { final SootMethod m = new MethodVisitor().visitMember(ctx.member(i)); - Body.BodyBuilder bodyBuilder = Body.builder(m.getBody(), m.getModifiers()); - for (BodyInterceptor bodyInterceptor : bodyInterceptors) { - try { - bodyInterceptor.interceptBody(bodyBuilder, view); - bodyBuilder - .getStmtGraph() - .validateStmtConnectionsInGraph(); // TODO: remove in the future ;-) - } catch (Exception e) { - throw new IllegalStateException( - "Failed to apply " + bodyInterceptor + " to " + m.getSignature(), e); - } - } - Body modifiedBody = bodyBuilder.build(); - SootMethod sm = - new SootMethod( - new OverridingBodySource(m.getBodySource()).withBody(modifiedBody), - m.getSignature(), - m.getModifiers(), - m.getExceptionSignatures(), - m.getPosition()); if (methods.stream() .anyMatch( meth -> { - final MethodSignature signature = sm.getSignature(); + final MethodSignature signature = m.getSignature(); return meth.getSignature().equals(signature); })) { throw new ResolveException( "Method with the same Signature does already exist.", path, m.getPosition()); } - methods.add(sm); + if (m.isConcrete()) { + Body.BodyBuilder bodyBuilder = Body.builder(m.getBody(), m.getModifiers()); + for (BodyInterceptor bodyInterceptor : bodyInterceptors) { + try { + bodyInterceptor.interceptBody(bodyBuilder, view); + bodyBuilder + .getStmtGraph() + .validateStmtConnectionsInGraph(); // TODO: remove in the future ;-) + } catch (Exception e) { + throw new IllegalStateException( + "Failed to apply " + bodyInterceptor + " to " + m.getSignature(), e); + } + } + Body modifiedBody = bodyBuilder.build(); + SootMethod sm = + new SootMethod( + new OverridingBodySource(m.getBodySource()).withBody(modifiedBody), + m.getSignature(), + m.getModifiers(), + m.getExceptionSignatures(), + m.getPosition()); + methods.add(sm); + } else { + methods.add(m); + } } else { final JimpleParser.FieldContext fieldCtx = ctx.member(i).field(); EnumSet modifier = getFieldModifiers(fieldCtx.field_modifier()); From cb8e58036e0502481fd37be0ccd15c8196112e41 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Mon, 4 Nov 2024 14:05:33 +0100 Subject: [PATCH 04/10] making view as mandatory parameter in JimpleConverter run method --- sootup.jimple.frontend/pom.xml | 12 ++++++------ .../sootup/jimple/frontend/JimpleConverter.java | 14 -------------- .../JimpleStringAnalysisInputLocation.java | 2 +- .../jimple/frontend/JimpleConverterTest.java | 3 ++- 4 files changed, 9 insertions(+), 22 deletions(-) diff --git a/sootup.jimple.frontend/pom.xml b/sootup.jimple.frontend/pom.xml index 536c26dd2bd..0720efd2a9d 100644 --- a/sootup.jimple.frontend/pom.xml +++ b/sootup.jimple.frontend/pom.xml @@ -50,18 +50,18 @@ org.soot-oss sootup.java.core - - - org.soot-oss - sootup.interceptors - - org.antlr antlr4-runtime + + org.soot-oss + sootup.interceptors + test + + org.junit.jupiter junit-jupiter-api diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java index 1284ff3e2e2..dc423ead704 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverter.java @@ -57,13 +57,6 @@ public class JimpleConverter { - public OverridingClassSource run( - @Nonnull CharStream charStream, - @Nonnull AnalysisInputLocation inputlocation, - @Nonnull Path sourcePath) { - return run(charStream, inputlocation, sourcePath, Collections.emptyList(), null); - } - public OverridingClassSource run( @Nonnull CharStream charStream, @Nonnull AnalysisInputLocation inputlocation, @@ -78,13 +71,6 @@ public OverridingClassSource run( return run(jimpleParser, inputlocation, sourcePath, bodyInterceptors, view); } - public OverridingClassSource run( - @Nonnull JimpleParser parser, - @Nonnull AnalysisInputLocation inputlocation, - @Nonnull Path sourcePath) { - return run(parser, inputlocation, sourcePath, Collections.emptyList(), null); - } - public OverridingClassSource run( @Nonnull JimpleParser parser, @Nonnull AnalysisInputLocation inputlocation, diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java index cb82d54cd32..128f34b2280 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java @@ -66,7 +66,7 @@ public JimpleStringAnalysisInputLocation( JimpleConverter jimpleConverter = new JimpleConverter(); classSource = jimpleConverter.run( - CharStreams.fromString(jimpleFileContents), this, path, bodyInterceptors, null); + CharStreams.fromString(jimpleFileContents), this, path, bodyInterceptors, new JimpleView(this)); } catch (Exception e) { throw new IllegalArgumentException("No valid Jimple given.", e); } diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java index 37fa00efb42..9917d130425 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java @@ -30,8 +30,9 @@ public class JimpleConverterTest { private SootClass parseJimpleClass(CharStream cs) throws ResolveException { JimpleConverter jimpleVisitor = new JimpleConverter(); + EagerInputLocation eagerInputLocation = new EagerInputLocation(); final OverridingClassSource scs = - jimpleVisitor.run(cs, new EagerInputLocation(), Paths.get("")); + jimpleVisitor.run(cs, eagerInputLocation, Paths.get(""), Collections.emptyList(), new JimpleView(eagerInputLocation)); return new SootClass(scs, SourceType.Application); } From 7649be51b9c1e7e157575b4e5e7cc5afd473d9cc Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Mon, 4 Nov 2024 14:06:41 +0100 Subject: [PATCH 05/10] format style --- .../jimple/frontend/JimpleStringAnalysisInputLocation.java | 6 +++++- .../java/sootup/jimple/frontend/JimpleConverterTest.java | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java index 128f34b2280..64b40872b1f 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java @@ -66,7 +66,11 @@ public JimpleStringAnalysisInputLocation( JimpleConverter jimpleConverter = new JimpleConverter(); classSource = jimpleConverter.run( - CharStreams.fromString(jimpleFileContents), this, path, bodyInterceptors, new JimpleView(this)); + CharStreams.fromString(jimpleFileContents), + this, + path, + bodyInterceptors, + new JimpleView(this)); } catch (Exception e) { throw new IllegalArgumentException("No valid Jimple given.", e); } diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java index 9917d130425..2c10cad184e 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java @@ -32,7 +32,12 @@ private SootClass parseJimpleClass(CharStream cs) throws ResolveException { JimpleConverter jimpleVisitor = new JimpleConverter(); EagerInputLocation eagerInputLocation = new EagerInputLocation(); final OverridingClassSource scs = - jimpleVisitor.run(cs, eagerInputLocation, Paths.get(""), Collections.emptyList(), new JimpleView(eagerInputLocation)); + jimpleVisitor.run( + cs, + eagerInputLocation, + Paths.get(""), + Collections.emptyList(), + new JimpleView(eagerInputLocation)); return new SootClass(scs, SourceType.Application); } From 58fa5b774ed6e348bfa6e7f654fff213d1f9047d Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Wed, 6 Nov 2024 16:03:08 +0100 Subject: [PATCH 06/10] modify JimpleStringAnalysisInputLocation instantiation --- .../JimpleStringAnalysisInputLocation.java | 24 +++++++++++-------- ...JimpleStringAnalysisInputLocationTest.java | 4 ++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java index 64b40872b1f..f40e0110c9d 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java @@ -48,8 +48,8 @@ public class JimpleStringAnalysisInputLocation implements AnalysisInputLocation @Nonnull final Path path = Paths.get("only-in-memory.jimple"); @Nonnull final List bodyInterceptors; - @Nonnull private final OverridingClassSource classSource; @Nonnull final SourceType sourceType; + private String jimpleFileContents; public JimpleStringAnalysisInputLocation(@Nonnull String jimpleFileContents) { this(jimpleFileContents, SourceType.Application, Collections.emptyList()); @@ -59,34 +59,37 @@ public JimpleStringAnalysisInputLocation( @Nonnull String jimpleFileContents, @Nonnull SourceType sourceType, @Nonnull List bodyInterceptors) { + this.jimpleFileContents = jimpleFileContents; this.bodyInterceptors = bodyInterceptors; this.sourceType = sourceType; + } + private OverridingClassSource getOverridingClassSource( + String jimpleFileContents, List bodyInterceptors, View view) { + final @Nonnull OverridingClassSource classSource; try { JimpleConverter jimpleConverter = new JimpleConverter(); classSource = jimpleConverter.run( - CharStreams.fromString(jimpleFileContents), - this, - path, - bodyInterceptors, - new JimpleView(this)); + CharStreams.fromString(jimpleFileContents), this, path, bodyInterceptors, view); } catch (Exception e) { throw new IllegalArgumentException("No valid Jimple given.", e); } + return classSource; } @Nonnull @Override public Optional getClassSource( @Nonnull ClassType type, @Nonnull View view) { - return Optional.of(classSource); + return Optional.of(getOverridingClassSource(jimpleFileContents, bodyInterceptors, view)); } @Nonnull @Override public Collection getClassSources(@Nonnull View view) { - return Collections.singletonList(classSource); + return Collections.singletonList( + getOverridingClassSource(jimpleFileContents, bodyInterceptors, view)); } @Nonnull @@ -101,8 +104,9 @@ public List getBodyInterceptors() { return bodyInterceptors; } + /** This is expensive, don't use in production code. Use it only for test case for convenience. */ @Nonnull - public ClassType getClassType() { - return classSource.getClassType(); + public ClassType getClassType(@Nonnull View view) { + return getOverridingClassSource(jimpleFileContents, bodyInterceptors, view).getClassType(); } } diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java index 5e37cfffbd2..e93dfd7831f 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java @@ -57,12 +57,12 @@ public void test() { new JimpleStringAnalysisInputLocation(methodStr); View view = new JimpleView(Collections.singletonList(analysisInputLocation)); - assertTrue(view.getClass(analysisInputLocation.getClassType()).isPresent()); + assertTrue(view.getClass(analysisInputLocation.getClassType(view)).isPresent()); MethodSignature methodSig = view.getIdentifierFactory() .getMethodSignature( - analysisInputLocation.getClassType(), + analysisInputLocation.getClassType(view), "banana", VoidType.getInstance(), Collections.emptyList()); From 7db44c650c9db3133df49dfddfd81f6611c3b3ea Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Wed, 6 Nov 2024 16:53:42 +0100 Subject: [PATCH 07/10] updated test --- .../frontend/JimpleStringAnalysisInputLocationTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java index e93dfd7831f..68c07bc0a9e 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java @@ -39,7 +39,13 @@ public class JimpleStringAnalysisInputLocationTest { public void testInvalidInput() { String methodStr = "This is not Jimple its just a Sentence."; assertThrows( - IllegalArgumentException.class, () -> new JimpleStringAnalysisInputLocation(methodStr)); + IllegalArgumentException.class, + () -> { + JimpleStringAnalysisInputLocation analysisInputLocation = + new JimpleStringAnalysisInputLocation(methodStr); + JimpleView view = new JimpleView(analysisInputLocation); + analysisInputLocation.getClassSources(view); + }); } @Test From 51d68bad3e701823bc05e33e70d08c5b986d519d Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Fri, 15 Nov 2024 12:53:34 +0100 Subject: [PATCH 08/10] moved getClassTypes() to view --- sootup.jimple.frontend/pom.xml | 1 - .../frontend/JimpleStringAnalysisInputLocation.java | 12 +++++------- .../main/java/sootup/jimple/frontend/JimpleView.java | 8 ++++++++ .../JimpleStringAnalysisInputLocationTest.java | 11 ++++++----- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/sootup.jimple.frontend/pom.xml b/sootup.jimple.frontend/pom.xml index 0720efd2a9d..95f91c55ba8 100644 --- a/sootup.jimple.frontend/pom.xml +++ b/sootup.jimple.frontend/pom.xml @@ -59,7 +59,6 @@ org.soot-oss sootup.interceptors - test diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java index f40e0110c9d..ff3fac31a46 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocation.java @@ -37,6 +37,7 @@ import sootup.core.transform.BodyInterceptor; import sootup.core.types.ClassType; import sootup.core.views.View; +import sootup.interceptors.BytecodeBodyInterceptors; /** * This AnalysisInputLocation encapsulates and represents a single Jimple "file" - the contents of @@ -52,7 +53,10 @@ public class JimpleStringAnalysisInputLocation implements AnalysisInputLocation private String jimpleFileContents; public JimpleStringAnalysisInputLocation(@Nonnull String jimpleFileContents) { - this(jimpleFileContents, SourceType.Application, Collections.emptyList()); + this( + jimpleFileContents, + SourceType.Application, + BytecodeBodyInterceptors.Default.getBodyInterceptors()); } public JimpleStringAnalysisInputLocation( @@ -103,10 +107,4 @@ public SourceType getSourceType() { public List getBodyInterceptors() { return bodyInterceptors; } - - /** This is expensive, don't use in production code. Use it only for test case for convenience. */ - @Nonnull - public ClassType getClassType(@Nonnull View view) { - return getOverridingClassSource(jimpleFileContents, bodyInterceptors, view).getClassType(); - } } diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleView.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleView.java index bb169ec5f58..45fba1c89f1 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleView.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleView.java @@ -157,4 +157,12 @@ private synchronized void resolveAll() { .forEach(this::buildClassFrom); isFullyResolved = true; } + + /** This is expensive, don't use in production code. Use it only for test case for convenience. */ + @Nonnull + public Optional getClassTypes(@Nonnull AnalysisInputLocation analysisInputLocation) { + Collection classSources = + analysisInputLocation.getClassSources(this); + return classSources.stream().map(AbstractClassSource::getClassType).findAny(); + } } diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java index 68c07bc0a9e..ae862a10266 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java @@ -28,9 +28,9 @@ import java.util.Collections; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import sootup.core.model.SourceType; import sootup.core.signatures.MethodSignature; import sootup.core.types.VoidType; -import sootup.core.views.View; @Tag("Java8") public class JimpleStringAnalysisInputLocationTest { @@ -60,15 +60,16 @@ public void test() { + "}"; JimpleStringAnalysisInputLocation analysisInputLocation = - new JimpleStringAnalysisInputLocation(methodStr); + new JimpleStringAnalysisInputLocation( + methodStr, SourceType.Application, Collections.emptyList()); - View view = new JimpleView(Collections.singletonList(analysisInputLocation)); - assertTrue(view.getClass(analysisInputLocation.getClassType(view)).isPresent()); + JimpleView view = new JimpleView(Collections.singletonList(analysisInputLocation)); + assertTrue(view.getClassTypes(analysisInputLocation).isPresent()); MethodSignature methodSig = view.getIdentifierFactory() .getMethodSignature( - analysisInputLocation.getClassType(view), + view.getClassTypes(analysisInputLocation).get(), "banana", VoidType.getInstance(), Collections.emptyList()); From 66eadd35fd8334d6050d845bbfc696643bbbd7f7 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Fri, 15 Nov 2024 13:51:16 +0100 Subject: [PATCH 09/10] removed getClassTypes() --- .../main/java/sootup/jimple/frontend/JimpleView.java | 8 -------- .../JimpleStringAnalysisInputLocationTest.java | 12 +++++++----- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleView.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleView.java index 45fba1c89f1..bb169ec5f58 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleView.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleView.java @@ -157,12 +157,4 @@ private synchronized void resolveAll() { .forEach(this::buildClassFrom); isFullyResolved = true; } - - /** This is expensive, don't use in production code. Use it only for test case for convenience. */ - @Nonnull - public Optional getClassTypes(@Nonnull AnalysisInputLocation analysisInputLocation) { - Collection classSources = - analysisInputLocation.getClassSources(this); - return classSources.stream().map(AbstractClassSource::getClassType).findAny(); - } } diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java index ae862a10266..6293ea617de 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java @@ -22,8 +22,7 @@ * #L% */ -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.util.Collections; import org.junit.jupiter.api.Tag; @@ -31,6 +30,7 @@ import sootup.core.model.SourceType; import sootup.core.signatures.MethodSignature; import sootup.core.types.VoidType; +import sootup.interceptors.DeadAssignmentEliminator; @Tag("Java8") public class JimpleStringAnalysisInputLocationTest { @@ -61,15 +61,17 @@ public void test() { JimpleStringAnalysisInputLocation analysisInputLocation = new JimpleStringAnalysisInputLocation( - methodStr, SourceType.Application, Collections.emptyList()); + methodStr, + SourceType.Application, + Collections.singletonList(new DeadAssignmentEliminator())); JimpleView view = new JimpleView(Collections.singletonList(analysisInputLocation)); - assertTrue(view.getClassTypes(analysisInputLocation).isPresent()); + assertNotNull(view.getIdentifierFactory().getClassType("DummyClass")); MethodSignature methodSig = view.getIdentifierFactory() .getMethodSignature( - view.getClassTypes(analysisInputLocation).get(), + view.getIdentifierFactory().getClassType("DummyClass"), "banana", VoidType.getInstance(), Collections.emptyList()); From d1c5e82d15262ed3533e634aaa04e8976b78ed5b Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Fri, 15 Nov 2024 13:55:18 +0100 Subject: [PATCH 10/10] missed changes --- .../jimple/frontend/JimpleStringAnalysisInputLocationTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java index 6293ea617de..b5ca885c431 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleStringAnalysisInputLocationTest.java @@ -30,6 +30,7 @@ import sootup.core.model.SourceType; import sootup.core.signatures.MethodSignature; import sootup.core.types.VoidType; +import sootup.core.views.View; import sootup.interceptors.DeadAssignmentEliminator; @Tag("Java8") @@ -65,7 +66,7 @@ public void test() { SourceType.Application, Collections.singletonList(new DeadAssignmentEliminator())); - JimpleView view = new JimpleView(Collections.singletonList(analysisInputLocation)); + View view = new JimpleView(Collections.singletonList(analysisInputLocation)); assertNotNull(view.getIdentifierFactory().getClassType("DummyClass")); MethodSignature methodSig =