@@ -163,6 +163,8 @@ class DataflowAnalysis {
163163 // / The id of the annotation allocator to be used
164164 MCPlusBuilder::AllocatorIdTy AllocatorId = 0 ;
165165
166+ bool IsPrivateAllocatorId;
167+
166168 // / Tracks the state at basic block start (end) if direction of the dataflow
167169 // / is forward (backward).
168170 std::unordered_map<const BinaryBasicBlock *, StateTy> StateAtBBEntry;
@@ -243,9 +245,6 @@ class DataflowAnalysis {
243245 }
244246
245247public:
246- // / Return the allocator id
247- unsigned getAllocatorId () { return AllocatorId; }
248-
249248 // / If the direction of the dataflow is forward, operates on the last
250249 // / instruction of all predecessors when performing an iteration of the
251250 // / dataflow equation for the start of this BB. If backwards, operates on
@@ -259,11 +258,40 @@ class DataflowAnalysis {
259258
260259 // / We need the current binary context and the function that will be processed
261260 // / in this dataflow analysis.
262- DataflowAnalysis (BinaryFunction &BF,
263- MCPlusBuilder::AllocatorIdTy AllocatorId = 0 )
264- : BC(BF.getBinaryContext()), Func(BF), AllocatorId(AllocatorId) {}
265-
266- virtual ~DataflowAnalysis () { cleanAnnotations (); }
261+ // /
262+ // / There are 2 constructors:
263+ // / * One version takes an AllocId, where it is assumed that:
264+ // / * If multiple threads are used, each different thread will get a different
265+ // / AllocId.
266+ // / * AllocIds may be reused by other functions/objects/DataflowAnalyses that
267+ // / take an AllocId of where to allocate MCAnnotations.
268+ // / As a result, the MCAnnotations this dataflow analysis allocates will
269+ // / not get freed before the end of the program run.
270+ // / * The second version takes a bool argument UsePrivateAllocators, which
271+ // / must be set to true.
272+ // / * When constructed this way, the dataflowanalysis will construct its own
273+ // / allocator, only used for MCAnnotations allocated by this DataFlowAnalysis.
274+ // / * When the DataflowAnalysis is destructed, that allocator gets freed,
275+ // / so that the memory used by this DataflowAnalysis gets completely freed.
276+ // / * Note however, that there cannot be a huge number of allocators all live
277+ // / at the same time. Therefore, only use this function when you're going
278+ // / to destruct this DataFlowAnalysis after analyzing one BinaryFunction,
279+ // / before creating a new DataFlowAnalysis to analyze another BinaryFunction.
280+ DataflowAnalysis (BinaryFunction &BF, const bool UsePrivateAllocators)
281+ : BC(BF.getBinaryContext()), Func(BF),
282+ AllocatorId (BC.MIB->getPrivateAllocatorId ()),
283+ IsPrivateAllocatorId(true ) {
284+ assert (UsePrivateAllocators == true );
285+ }
286+ DataflowAnalysis (BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy AllocId)
287+ : BC(BF.getBinaryContext()), Func(BF), AllocatorId(AllocId),
288+ IsPrivateAllocatorId(false ) {}
289+
290+ virtual ~DataflowAnalysis () {
291+ cleanAnnotations ();
292+ if (IsPrivateAllocatorId)
293+ BC.MIB ->freePrivateAllocatorId (AllocatorId);
294+ }
267295
268296 // / Track the state at basic block start (end) if direction of the dataflow
269297 // / is forward (backward).
@@ -529,8 +557,11 @@ class InstrsDataflowAnalysis
529557 return count (*Expressions[PointIdx], Expr);
530558 }
531559
560+ InstrsDataflowAnalysis (BinaryFunction &BF, const bool UsePrivateAllocators)
561+ : DataflowAnalysis<Derived, BitVector, Backward, StatePrinterTy>(
562+ BF, UsePrivateAllocators) {}
532563 InstrsDataflowAnalysis (BinaryFunction &BF,
533- MCPlusBuilder::AllocatorIdTy AllocId = 0 )
564+ MCPlusBuilder::AllocatorIdTy AllocId)
534565 : DataflowAnalysis<Derived, BitVector, Backward, StatePrinterTy>(
535566 BF, AllocId) {}
536567 virtual ~InstrsDataflowAnalysis () {}
0 commit comments