|
1 | 1 | package soot.jimple.infoflow.data.pathBuilders;
|
2 | 2 |
|
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.Set; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | + |
3 | 8 | import soot.jimple.infoflow.InfoflowManager;
|
4 | 9 | import soot.jimple.infoflow.data.AbstractionAtSink;
|
5 | 10 | import soot.jimple.infoflow.memory.ISolverTerminationReason;
|
6 | 11 | import soot.jimple.infoflow.results.InfoflowResults;
|
7 | 12 | import soot.jimple.infoflow.solver.executors.InterruptableExecutor;
|
8 | 13 |
|
9 |
| -import java.util.HashSet; |
10 |
| -import java.util.Iterator; |
11 |
| -import java.util.Set; |
12 |
| -import java.util.concurrent.TimeUnit; |
13 |
| - |
14 | 14 | /**
|
15 | 15 | * Path builder that forwards all its requests to another path builder in
|
16 | 16 | * batches. This builder waits for each batch to complete before submitting
|
|
21 | 21 | */
|
22 | 22 | public class BatchPathBuilder extends AbstractAbstractionPathBuilder {
|
23 | 23 |
|
24 |
| - protected final IAbstractionPathBuilder innerBuilder; |
25 |
| - protected int batchSize = 5; |
26 |
| - protected ISolverTerminationReason terminationReason = null; |
27 |
| - |
28 |
| - public BatchPathBuilder(InfoflowManager manager, IAbstractionPathBuilder innerBuilder) { |
29 |
| - super(manager); |
30 |
| - this.innerBuilder = innerBuilder; |
31 |
| - } |
32 |
| - |
33 |
| - @Override |
34 |
| - public void computeTaintPaths(Set<AbstractionAtSink> res) { |
35 |
| - Set<AbstractionAtSink> batch = new HashSet<>(); |
36 |
| - Iterator<AbstractionAtSink> resIt = res.iterator(); |
37 |
| - int batchId = 1; |
38 |
| - long startTime = System.nanoTime(); |
39 |
| - long totalTime = manager.getConfig().getPathConfiguration().getPathReconstructionTotalTime(); |
40 |
| - |
41 |
| - while (resIt.hasNext()) { |
42 |
| - // checking if the execution time exceeds the configured totalTime and logging |
43 |
| - long executionNanoTime = System.nanoTime() - startTime; |
44 |
| - if (totalTime > 0 && executionNanoTime / 1E9 >= totalTime) { |
45 |
| - logger.info("Path Reconstruction has terminated as it exceeds the configured pathReconstructionTotalTime."); |
46 |
| - logger.info("The pathReconstructionTotalTime is set to " + totalTime + "seconds."); |
47 |
| - logger.info("Now the current batchId:" + batchId); |
48 |
| - logger.info("The number of remaining res:"+(res.size() - batchId * batch.size())); |
49 |
| - break; |
50 |
| - } else { |
51 |
| - logger.info("Path Reconstruction has used " + executionNanoTime / 1E9 + " seconds"); |
52 |
| - } |
53 |
| - |
54 |
| - // Build the next batch |
55 |
| - while (batch.size() < this.batchSize && resIt.hasNext()) |
56 |
| - batch.add(resIt.next()); |
57 |
| - logger.info("Running path reconstruction batch {} with {} elements", batchId++, batch.size()); |
58 |
| - |
59 |
| - // Run the next batch |
60 |
| - innerBuilder.reset(); |
61 |
| - innerBuilder.computeTaintPaths(batch); |
62 |
| - |
63 |
| - // Save the termination reason |
64 |
| - if (this.terminationReason == null) |
65 |
| - this.terminationReason = innerBuilder.getTerminationReason(); |
66 |
| - else |
67 |
| - this.terminationReason = this.terminationReason.combine(innerBuilder.getTerminationReason()); |
68 |
| - |
69 |
| - // Wait for the batch to complete |
70 |
| - if (innerBuilder instanceof ConcurrentAbstractionPathBuilder) { |
71 |
| - ConcurrentAbstractionPathBuilder concurrentBuilder = (ConcurrentAbstractionPathBuilder) innerBuilder; |
72 |
| - final InterruptableExecutor resultExecutor = concurrentBuilder.getExecutor(); |
73 |
| - try { |
74 |
| - // The path reconstruction should stop on time anyway. In case it doesn't, we |
75 |
| - // make sure that we don't get stuck. |
76 |
| - long pathTimeout = manager.getConfig().getPathConfiguration().getPathReconstructionTimeout(); |
77 |
| - if (pathTimeout > 0) |
78 |
| - resultExecutor.awaitCompletion(pathTimeout + 20, TimeUnit.SECONDS); |
79 |
| - else |
80 |
| - resultExecutor.awaitCompletion(); |
81 |
| - } catch (InterruptedException e) { |
82 |
| - logger.error("Could not wait for executor termination", e); |
83 |
| - } |
84 |
| - resultExecutor.reset(); |
85 |
| - } |
86 |
| - |
87 |
| - // Prepare for the next batch |
88 |
| - batch.clear(); |
89 |
| - } |
90 |
| - } |
91 |
| - |
92 |
| - @Override |
93 |
| - public InfoflowResults getResults() { |
94 |
| - return innerBuilder.getResults(); |
95 |
| - } |
96 |
| - |
97 |
| - @Override |
98 |
| - public void runIncrementalPathComputation() { |
99 |
| - innerBuilder.runIncrementalPathComputation(); |
100 |
| - } |
101 |
| - |
102 |
| - @Override |
103 |
| - public void forceTerminate(ISolverTerminationReason reason) { |
104 |
| - innerBuilder.forceTerminate(reason); |
105 |
| - } |
106 |
| - |
107 |
| - @Override |
108 |
| - public boolean isTerminated() { |
109 |
| - return innerBuilder.isTerminated(); |
110 |
| - } |
111 |
| - |
112 |
| - @Override |
113 |
| - public boolean isKilled() { |
114 |
| - return innerBuilder.isKilled(); |
115 |
| - } |
116 |
| - |
117 |
| - @Override |
118 |
| - public ISolverTerminationReason getTerminationReason() { |
119 |
| - return terminationReason; |
120 |
| - } |
121 |
| - |
122 |
| - @Override |
123 |
| - public void reset() { |
124 |
| - innerBuilder.reset(); |
125 |
| - } |
126 |
| - |
127 |
| - @Override |
128 |
| - public void addStatusListener(IMemoryBoundedSolverStatusNotification listener) { |
129 |
| - innerBuilder.addStatusListener(listener); |
130 |
| - } |
131 |
| - |
132 |
| - /** |
133 |
| - * Sets the number of paths that shall be part of one batch, i.e., that shall be |
134 |
| - * forwarded to the inner path builder at the same time |
135 |
| - * |
136 |
| - * @param batchSize The number of paths in one batch |
137 |
| - */ |
138 |
| - public void setBatchSize(int batchSize) { |
139 |
| - this.batchSize = batchSize; |
140 |
| - } |
| 24 | + protected final IAbstractionPathBuilder innerBuilder; |
| 25 | + protected int batchSize = 5; |
| 26 | + protected ISolverTerminationReason terminationReason = null; |
| 27 | + |
| 28 | + public BatchPathBuilder(InfoflowManager manager, IAbstractionPathBuilder innerBuilder) { |
| 29 | + super(manager); |
| 30 | + this.innerBuilder = innerBuilder; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void computeTaintPaths(Set<AbstractionAtSink> res) { |
| 35 | + Set<AbstractionAtSink> batch = new HashSet<>(); |
| 36 | + Iterator<AbstractionAtSink> resIt = res.iterator(); |
| 37 | + int batchId = 1; |
| 38 | + long startTime = System.nanoTime(); |
| 39 | + long totalTime = manager.getConfig().getPathConfiguration().getPathReconstructionTotalTime(); |
| 40 | + |
| 41 | + while (resIt.hasNext()) { |
| 42 | + // checking if the execution time exceeds the configured totalTime and logging |
| 43 | + long executionNanoTime = System.nanoTime() - startTime; |
| 44 | + if (totalTime > 0 && executionNanoTime / 1E9 >= totalTime) { |
| 45 | + logger.info( |
| 46 | + "Path Reconstruction has terminated as it exceeds the configured pathReconstructionTotalTime."); |
| 47 | + logger.info("The pathReconstructionTotalTime is set to " + totalTime + "seconds."); |
| 48 | + logger.info("Now the current batchId:" + batchId); |
| 49 | + logger.info("The number of remaining res:" + (res.size() - batchId * batch.size())); |
| 50 | + break; |
| 51 | + } else { |
| 52 | + logger.info("Path Reconstruction has used " + executionNanoTime / 1E9 + " seconds"); |
| 53 | + } |
| 54 | + |
| 55 | + // Build the next batch |
| 56 | + while (batch.size() < this.batchSize && resIt.hasNext()) |
| 57 | + batch.add(resIt.next()); |
| 58 | + logger.info("Running path reconstruction batch {} with {} elements", batchId++, batch.size()); |
| 59 | + |
| 60 | + // Run the next batch |
| 61 | + long beforeBatch = System.nanoTime(); |
| 62 | + innerBuilder.reset(); |
| 63 | + innerBuilder.computeTaintPaths(batch); |
| 64 | + |
| 65 | + // Save the termination reason |
| 66 | + if (this.terminationReason == null) |
| 67 | + this.terminationReason = innerBuilder.getTerminationReason(); |
| 68 | + else |
| 69 | + this.terminationReason = this.terminationReason.combine(innerBuilder.getTerminationReason()); |
| 70 | + |
| 71 | + // Wait for the batch to complete |
| 72 | + if (innerBuilder instanceof ConcurrentAbstractionPathBuilder) { |
| 73 | + ConcurrentAbstractionPathBuilder concurrentBuilder = (ConcurrentAbstractionPathBuilder) innerBuilder; |
| 74 | + final InterruptableExecutor resultExecutor = concurrentBuilder.getExecutor(); |
| 75 | + try { |
| 76 | + // The path reconstruction should stop on time anyway. In case it doesn't, we |
| 77 | + // make sure that we don't get stuck. |
| 78 | + long pathTimeout = manager.getConfig().getPathConfiguration().getPathReconstructionTimeout(); |
| 79 | + if (pathTimeout > 0) |
| 80 | + resultExecutor.awaitCompletion(pathTimeout + 20, TimeUnit.SECONDS); |
| 81 | + else |
| 82 | + resultExecutor.awaitCompletion(); |
| 83 | + } catch (InterruptedException e) { |
| 84 | + logger.error("Could not wait for executor termination", e); |
| 85 | + } |
| 86 | + resultExecutor.reset(); |
| 87 | + } |
| 88 | + logger.info("Single batch has used " + (System.nanoTime() - beforeBatch) / 1E9 + " seconds"); |
| 89 | + |
| 90 | + // Prepare for the next batch |
| 91 | + batch.clear(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public InfoflowResults getResults() { |
| 97 | + return innerBuilder.getResults(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void runIncrementalPathComputation() { |
| 102 | + innerBuilder.runIncrementalPathComputation(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void forceTerminate(ISolverTerminationReason reason) { |
| 107 | + innerBuilder.forceTerminate(reason); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean isTerminated() { |
| 112 | + return innerBuilder.isTerminated(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean isKilled() { |
| 117 | + return innerBuilder.isKilled(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public ISolverTerminationReason getTerminationReason() { |
| 122 | + return terminationReason; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void reset() { |
| 127 | + innerBuilder.reset(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void addStatusListener(IMemoryBoundedSolverStatusNotification listener) { |
| 132 | + innerBuilder.addStatusListener(listener); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the number of paths that shall be part of one batch, i.e., that shall be |
| 137 | + * forwarded to the inner path builder at the same time |
| 138 | + * |
| 139 | + * @param batchSize The number of paths in one batch |
| 140 | + */ |
| 141 | + public void setBatchSize(int batchSize) { |
| 142 | + this.batchSize = batchSize; |
| 143 | + } |
141 | 144 |
|
142 | 145 | }
|
0 commit comments