Skip to content

Commit

Permalink
applied BI to JimpleAnalysisInputLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilagichani14 committed Nov 1, 2024
1 parent 0faa131 commit cff81e9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ List<SootClassSource> walkDirectory(
@Nonnull
public Collection<SootClassSource> getClassSources(@Nonnull View view) {
return walkDirectory(
path, view.getIdentifierFactory(), new JimpleClassProvider(bodyInterceptors));
path, view.getIdentifierFactory(), new JimpleClassProvider(bodyInterceptors, view));
}

@Override
@Nonnull
public Optional<SootClassSource> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BodyInterceptor> bodyInterceptors;
@Nonnull private final View view;

private static final @Nonnull Logger logger = LoggerFactory.getLogger(JimpleClassProvider.class);

public JimpleClassProvider(List<BodyInterceptor> bodyInterceptors) {
public JimpleClassProvider(List<BodyInterceptor> bodyInterceptors, @Nonnull View view) {
this.bodyInterceptors = bodyInterceptors;
this.view = view;
}

@Override
Expand All @@ -57,7 +60,7 @@ public Optional<SootClassSource> 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 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<BodyInterceptor> bodyInterceptors) {
@Nonnull List<BodyInterceptor> 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<BodyInterceptor> bodyInterceptors) {

// FIXME: [ms] apply bodyInterceptors or better: move that logic into View itself!
@Nonnull List<BodyInterceptor> 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);
Expand All @@ -119,10 +120,15 @@ private static class ClassVisitor extends JimpleBaseVisitor<Boolean> {

@Nonnull private final JimpleConverterUtil util;
@Nonnull private final Path path;
@Nonnull private final List<BodyInterceptor> bodyInterceptors;
@Nonnull private final View view;

public ClassVisitor(@Nonnull Path path) {
public ClassVisitor(
@Nonnull Path path, @Nonnull List<BodyInterceptor> bodyInterceptors, @Nonnull View view) {
this.path = path;
util = new JimpleConverterUtil(path);
this.bodyInterceptors = bodyInterceptors;
this.view = view;
}

private ClassType clazz = null;
Expand Down Expand Up @@ -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<FieldModifier> modifier = getFieldModifiers(fieldCtx.field_modifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit cff81e9

Please sign in to comment.