Skip to content

Commit dd7d44a

Browse files
authored
Merge pull request #342 from jpstotz/TimeoutWatcher
fixed usage of currentTimeMillis in TimeoutWatcher + minor improvements
2 parents 0adfd3a + 25a8a9c commit dd7d44a

2 files changed

Lines changed: 38 additions & 29 deletions

File tree

soot-infoflow/src/soot/jimple/infoflow/data/AccessPathFactory.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public AccessPath createAccessPath(Value val, SootField[] appendingFields, Type
132132
boolean canHaveImmutableAliases) {
133133
// Make sure that the base object is valid
134134
if (val != null && !AccessPath.canContainValue(val)) {
135-
logger.error(String.format("Access paths cannot be rooted in values of type %s", val.getClass().getName()));
135+
logger.error("Access paths cannot be rooted in values of type {}", val.getClass().getName());
136136
return null;
137137
}
138138

@@ -424,8 +424,8 @@ public AccessPath createAccessPath(Value val, SootField[] appendingFields, Type
424424
assert value == null || !(!(baseType instanceof ArrayType) && !TypeUtils.isObjectLikeType(baseType)
425425
&& value.getType() instanceof ArrayType);
426426
assert value == null || !(baseType instanceof ArrayType && !(value.getType() instanceof ArrayType)
427-
&& !TypeUtils.isObjectLikeType(value.getType())) : "Type mismatch. Type was " + baseType
428-
+ ", value was: " + (value == null ? null : value.getType());
427+
&& !TypeUtils.isObjectLikeType(value.getType()))
428+
: "Type mismatch. Type was " + baseType + ", value was: " + (value == null ? null : value.getType());
429429

430430
if ((fields == null && fieldTypes != null) || (fields != null && fieldTypes == null))
431431
throw new RuntimeException("When there are fields, there must be field types and vice versa");
@@ -435,16 +435,20 @@ public AccessPath createAccessPath(Value val, SootField[] appendingFields, Type
435435
// Sanity check
436436
if (baseType instanceof PrimType) {
437437
if (fields != null) {
438-
logger.warn("Primitive types cannot have fields");
438+
logger.warn("Primitive types cannot have fields: baseType={} fields={}", baseType,
439+
Arrays.toString(fields));
439440
return null;
440441
}
441442
}
442443
if (fields != null) {
443-
for (int i = 0; i < fields.length - 2; i++)
444-
if (fields[i].getType() instanceof PrimType) {
445-
logger.warn("Primitive types cannot have fields");
444+
for (int i = 0; i < fields.length - 2; i++) {
445+
SootField f = fields[i];
446+
Type fieldType = f.getType();
447+
if (fieldType instanceof PrimType) {
448+
logger.warn("Primitive types cannot have fields: field={} type={}", f, fieldType);
446449
return null;
447450
}
451+
}
448452
}
449453

450454
return new AccessPath(value, fields, baseType, fieldTypes, taintSubFields, cutOffApproximation, arrayTaintType,

soot-infoflow/src/soot/jimple/infoflow/memory/FlowDroidTimeoutWatcher.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44
import java.util.concurrent.ConcurrentHashMap;
5+
import java.util.concurrent.TimeUnit;
56

67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
@@ -25,26 +26,26 @@ public class FlowDroidTimeoutWatcher implements IMemoryBoundedSolverStatusNotifi
2526
*
2627
*/
2728
private enum SolverState {
28-
/**
29-
* The solver has not been started yet
30-
*/
31-
IDLE,
32-
/**
33-
* The solver is running
34-
*/
35-
RUNNING,
36-
/**
37-
* The solver has completed its work
38-
*/
39-
DONE
29+
/**
30+
* The solver has not been started yet
31+
*/
32+
IDLE,
33+
/**
34+
* The solver is running
35+
*/
36+
RUNNING,
37+
/**
38+
* The solver has completed its work
39+
*/
40+
DONE
4041
}
4142

4243
private final Logger logger = LoggerFactory.getLogger(getClass());
4344

4445
private final long timeout;
4546
private final InfoflowResults results;
4647
private final Map<IMemoryBoundedSolver, SolverState> solvers = new ConcurrentHashMap<>();
47-
private boolean stopped = false;
48+
private volatile boolean stopped = false;
4849
private ISolversTerminatedCallback terminationCallback = null;
4950

5051
/**
@@ -93,7 +94,7 @@ public void addSolver(IMemoryBoundedSolver solver) {
9394
* Starts the timeout watcher
9495
*/
9596
public void start() {
96-
final long startTime = System.currentTimeMillis();
97+
final long startTime = System.nanoTime();
9798
logger.info("FlowDroid timeout watcher started");
9899
this.stopped = false;
99100

@@ -103,34 +104,38 @@ public void start() {
103104
public void run() {
104105
// Sleep until we have reached the timeout
105106
boolean allTerminated = isTerminated();
106-
long timeElapsed = 0;
107107

108-
while (!stopped && ((timeElapsed = System.currentTimeMillis() - startTime) < 1000 * timeout)) {
108+
long timeoutNano = TimeUnit.SECONDS.toNanos(timeout);
109+
while (!stopped && ((System.nanoTime() - startTime) < timeoutNano)) {
109110
allTerminated = isTerminated();
110-
if (allTerminated)
111+
if (allTerminated) {
111112
break;
113+
}
112114

113115
try {
114116
Thread.sleep(1000);
115117
} catch (InterruptedException e) {
116118
// There's little we can do here
117-
118119
}
119120
}
121+
long timeElapsed = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime);
120122

121123
// If things have not stopped on their own account, we force
122124
// them to
123125
if (!stopped & !allTerminated) {
124126
logger.warn("Timeout reached, stopping the solvers...");
125-
if (results != null)
127+
if (results != null) {
126128
results.addException("Timeout reached");
129+
}
127130

128-
TimeoutReason reason = new TimeoutReason(timeElapsed / 1000, timeout);
129-
for (IMemoryBoundedSolver solver : solvers.keySet())
131+
TimeoutReason reason = new TimeoutReason(timeElapsed, timeout);
132+
for (IMemoryBoundedSolver solver : solvers.keySet()) {
130133
solver.forceTerminate(reason);
134+
}
131135

132-
if (terminationCallback != null)
136+
if (terminationCallback != null) {
133137
terminationCallback.onSolversTerminated();
138+
}
134139
}
135140

136141
logger.info("FlowDroid timeout watcher terminated");

0 commit comments

Comments
 (0)