Skip to content

Commit 483ff58

Browse files
committed
C++: Replace the giant list of predicate parameters with a module signature.
1 parent b0af4cb commit 483ff58

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

cpp/ql/lib/semmle/code/cpp/models/implementations/Allocation.qll

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -220,41 +220,44 @@ private signature class CallAllocationExprTarget extends Function;
220220
* function using various heuristics.
221221
*/
222222
private module CallAllocationExprBase<CallAllocationExprTarget Target> {
223-
/**
224-
* A signature for a predicate that gets the index of the input pointer argument to
225-
* be reallocated, if this is a `realloc` function.
226-
*/
227-
signature int getReallocPtrArgSig(Target target);
223+
/** A module that contains the collection of member-predicates required on `Target`. */
224+
signature module Param {
225+
/**
226+
* Gets the index of the input pointer argument to be reallocated, if
227+
* this is a `realloc` function.
228+
*/
229+
int getReallocPtrArg(Target target);
228230

229-
/**
230-
* A signature for a predicate that gets the index of the argument for the allocation
231-
* size, if any. The actual allocation size is the value of this argument multiplied
232-
* by the result of `getSizeMult()`, in bytes.
233-
*/
234-
signature int getSizeArgSig(Target target);
231+
/**
232+
* Gets the index of the argument for the allocation size, if any. The actual
233+
* allocation size is the value of this argument multiplied by the result of
234+
* `getSizeMult()`, in bytes.
235+
*/
236+
int getSizeArg(Target target);
235237

236-
/**
237-
* A signature for a predicate that gets the index of an argument that multiplies the
238-
* allocation size given by `getSizeArg`, if any.
239-
*/
240-
signature int getSizeMultSig(Target target);
238+
/**
239+
* Gets the index of an argument that multiplies the allocation size given
240+
* by `getSizeArg`, if any.
241+
*/
242+
int getSizeMult(Target target);
241243

242-
/**
243-
* A signature for a predicate that determines whether or not this allocation requires a
244-
* corresponding deallocation of some sort (most do, but `alloca` for example does not).
245-
* If it is unclear, we default to no (for example a placement `new` allocation may or
246-
* may not require a corresponding `delete`).
247-
*/
248-
signature predicate requiresDeallocSig(Target target);
244+
/**
245+
* Holds if this allocation requires a
246+
* corresponding deallocation of some sort (most do, but `alloca` for example
247+
* does not). If it is unclear, we default to no (for example a placement `new`
248+
* allocation may or may not require a corresponding `delete`).
249+
*/
250+
predicate requiresDealloc(Target target);
251+
}
249252

250253
/**
251-
* A module that abstracts over the various predicates in a that should really be
252-
* member-predicates of `CallAllocationExprTarget` (which which we cannot yet write in
253-
* QL).
254+
* A module that abstracts over a collection of predicates in
255+
* the `Param` module). This should really be memeber-predicates
256+
* on `CallAllocationExprTarget`, but we cannot yet write this in QL.
254257
*/
255-
module With<
256-
getReallocPtrArgSig/1 getReallocPtrArg, getSizeArgSig/1 getSizeArg, getSizeMultSig/1 getSizeMult,
257-
requiresDeallocSig/1 requiresDealloc> {
258+
module With<Param P> {
259+
private import P
260+
258261
/**
259262
* An allocation expression that is a function call, such as call to `malloc`.
260263
*/
@@ -313,20 +316,22 @@ private module CallAllocationExprBase<CallAllocationExprTarget Target> {
313316
}
314317

315318
private module CallAllocationExpr {
316-
private int getReallocPtrArg(AllocationFunction f) { result = f.getReallocPtrArg() }
319+
private module Param implements CallAllocationExprBase<AllocationFunction>::Param {
320+
int getReallocPtrArg(AllocationFunction f) { result = f.getReallocPtrArg() }
317321

318-
private int getSizeArg(AllocationFunction f) { result = f.getSizeArg() }
322+
int getSizeArg(AllocationFunction f) { result = f.getSizeArg() }
319323

320-
private int getSizeMult(AllocationFunction f) { result = f.getSizeMult() }
324+
int getSizeMult(AllocationFunction f) { result = f.getSizeMult() }
321325

322-
private predicate requiresDealloc(AllocationFunction f) { f.requiresDealloc() }
326+
predicate requiresDealloc(AllocationFunction f) { f.requiresDealloc() }
327+
}
323328

324329
/**
325330
* A class that provides the implementation of `AllocationExpr` for an allocation
326331
* that calls an `AllocationFunction`.
327332
*/
328333
private class Base =
329-
CallAllocationExprBase<AllocationFunction>::With<getReallocPtrArg/1, getSizeArg/1, getSizeMult/1, requiresDealloc/1>::CallAllocationExprImpl;
334+
CallAllocationExprBase<AllocationFunction>::With<Param>::CallAllocationExprImpl;
330335

331336
class CallAllocationExpr extends AllocationExpr, Base {
332337
override Expr getSizeExpr() { result = super.getSizeExprImpl() }
@@ -444,20 +449,22 @@ private module HeuristicAllocation {
444449
override predicate requiresDealloc() { none() }
445450
}
446451

447-
private int getReallocPtrArg(HeuristicAllocationFunction f) { result = f.getReallocPtrArg() }
452+
private module Param implements CallAllocationExprBase<HeuristicAllocationFunction>::Param {
453+
int getReallocPtrArg(HeuristicAllocationFunction f) { result = f.getReallocPtrArg() }
448454

449-
private int getSizeArg(HeuristicAllocationFunction f) { result = f.getSizeArg() }
455+
int getSizeArg(HeuristicAllocationFunction f) { result = f.getSizeArg() }
450456

451-
private int getSizeMult(HeuristicAllocationFunction f) { result = f.getSizeMult() }
457+
int getSizeMult(HeuristicAllocationFunction f) { result = f.getSizeMult() }
452458

453-
private predicate requiresDealloc(HeuristicAllocationFunction f) { f.requiresDealloc() }
459+
predicate requiresDealloc(HeuristicAllocationFunction f) { f.requiresDealloc() }
460+
}
454461

455462
/**
456463
* A class that provides the implementation of `AllocationExpr` for an allocation
457464
* that calls an `HeuristicAllocationFunction`.
458465
*/
459466
private class Base =
460-
CallAllocationExprBase<HeuristicAllocationFunction>::With<getReallocPtrArg/1, getSizeArg/1, getSizeMult/1, requiresDealloc/1>::CallAllocationExprImpl;
467+
CallAllocationExprBase<HeuristicAllocationFunction>::With<Param>::CallAllocationExprImpl;
461468

462469
private class CallAllocationExpr extends HeuristicAllocationExpr, Base {
463470
override Expr getSizeExpr() { result = super.getSizeExprImpl() }

0 commit comments

Comments
 (0)