Skip to content

Commit 6bf22bd

Browse files
committed
C++: Replace allocation models with models from extensible predicates.
1 parent d308178 commit 6bf22bd

File tree

7 files changed

+73
-113
lines changed

7 files changed

+73
-113
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/cpp-all
4+
extensible: allocationFunctionModel
5+
data:
6+
- ["", "", False, "kmem_alloc", "0", "", "", True]
7+
- ["", "", False, "kmem_zalloc", "0", "", "", True]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/cpp-all
4+
extensible: allocationFunctionModel
5+
data:
6+
- ["", "", False, "g_malloc", "0", "", "", True]
7+
- ["", "", False, "g_try_malloc", "0", "", "", True]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/cpp-all
4+
extensible: allocationFunctionModel
5+
data:
6+
- ["", "", False, "CRYPTO_malloc", "0", "", "", True]
7+
- ["", "", False, "CRYPTO_zalloc", "0", "", "", True]
8+
- ["", "", False, "CRYPTO_secure_malloc", "0", "", "", True]
9+
- ["", "", False, "CRYPTO_secure_zalloc", "0", "", "", True]
10+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/cpp-all
4+
extensible: allocationFunctionModel
5+
data:
6+
- ["", "", False, "malloc", "0", "", "", True]
7+
- ["std", "", False, "malloc", "0", "", "", True]
8+
- ["bsl", "", False, "malloc", "0", "", "", True]
9+
- ["", "", False, "alloca", "0", "", "", False]
10+
- ["", "", False, "__builtin_alloca", "0", "", "", False]
11+
- ["", "", False, "_alloca", "0", "", "", False]
12+
- ["", "", False, "_malloca", "0", "", "", False]
13+
- ["", "", False, "calloc", "1", "0", "", True]
14+
- ["std", "", False, "calloc", "1", "0", "", True]
15+
- ["bsl", "", False, "calloc", "1", "0", "", True]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/cpp-all
4+
extensible: allocationFunctionModel
5+
data:
6+
- ["", "", False, "MmAllocateContiguousMemory", "0", "", "", True]
7+
- ["", "", False, "MmAllocateContiguousNodeMemory", "0", "", "", True]
8+
- ["", "", False, "MmAllocateContiguousMemorySpecifyCache", "0", "", "", True]
9+
- ["", "", False, "MmAllocateContiguousMemorySpecifyCacheNode", "0", "", "", True]
10+
- ["", "", False, "MmAllocateNonCachedMemory", "0", "", "", True]
11+
- ["", "", False, "MmAllocateMappingAddress", "0", "", "", True]
12+
- ["", "", False, "CoTaskMemAlloc", "0", "", "", True]
13+
- ["", "", False, "ExAllocatePool", "1", "", "", True]
14+
- ["", "", False, "ExAllocatePool2", "1", "", "", True]
15+
- ["", "", False, "ExAllocatePool3", "1", "", "", True]
16+
- ["", "", False, "ExAllocatePoolWithTag", "1", "", "", True]
17+
- ["", "", False, "ExAllocatePoolWithTagPriority", "1", "", "", True]
18+
- ["", "", False, "ExAllocatePoolWithQuota", "1", "", "", True]
19+
- ["", "", False, "ExAllocatePoolWithQuotaTag", "1", "", "", True]
20+
- ["", "", False, "ExAllocatePoolZero", "1", "", "", True]
21+
- ["", "", False, "IoAllocateMdl", "1", "", "", True]
22+
- ["", "", False, "IoAllocateErrorLogEntry", "1", "", "", True]
23+
- ["", "", False, "LocalAlloc", "1", "", "", True]
24+
- ["", "", False, "GlobalAlloc", "1", "", "", True]
25+
- ["", "", False, "VirtualAlloc", "1", "", "", True]
26+
- ["", "", False, "HeapAlloc", "2", "", "", True]
27+
- ["", "", False, "MmAllocatePagesForMdl", "3", "", "", True]
28+
- ["", "", False, "MmAllocatePagesForMdlEx", "3", "", "", True]
29+
- ["", "", False, "MmAllocateNodePagesForMdlEx", "3", "", "", True]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/cpp-all
4+
extensible: allocationFunctionModel
5+
data: []

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

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -7,119 +7,6 @@
77
import semmle.code.cpp.models.interfaces.Allocation
88
import semmle.code.cpp.models.interfaces.Taint
99

10-
/**
11-
* An allocation function (such as `malloc`) that has an argument for the size
12-
* in bytes.
13-
*/
14-
private class MallocAllocationFunction extends AllocationFunction {
15-
int sizeArg;
16-
17-
MallocAllocationFunction() {
18-
// --- C library allocation
19-
this.hasGlobalOrStdOrBslName("malloc") and // malloc(size)
20-
sizeArg = 0
21-
or
22-
this.hasGlobalName([
23-
// --- Windows Memory Management for Windows Drivers
24-
"MmAllocateContiguousMemory", // MmAllocateContiguousMemory(size, maxaddress)
25-
"MmAllocateContiguousNodeMemory", // MmAllocateContiguousNodeMemory(size, minaddress, maxaddress, bound, flag, prefer)
26-
"MmAllocateContiguousMemorySpecifyCache", // MmAllocateContiguousMemorySpecifyCache(size, minaddress, maxaddress, bound, type)
27-
"MmAllocateContiguousMemorySpecifyCacheNode", // MmAllocateContiguousMemorySpecifyCacheNode(size, minaddress, maxaddress, bound, type, prefer)
28-
"MmAllocateNonCachedMemory", // MmAllocateNonCachedMemory(size)
29-
"MmAllocateMappingAddress", // MmAllocateMappingAddress(size, tag)
30-
// --- Windows COM allocation
31-
"CoTaskMemAlloc", // CoTaskMemAlloc(size)
32-
// --- Solaris/BSD kernel memory allocator
33-
"kmem_alloc", // kmem_alloc(size, flags)
34-
"kmem_zalloc", // kmem_zalloc(size, flags)
35-
// --- OpenSSL memory allocation
36-
"CRYPTO_malloc", // CRYPTO_malloc(size_t num, const char *file, int line)
37-
"CRYPTO_zalloc", // CRYPTO_zalloc(size_t num, const char *file, int line)
38-
"CRYPTO_secure_malloc", // CRYPTO_secure_malloc(size_t num, const char *file, int line)
39-
"CRYPTO_secure_zalloc", // CRYPTO_secure_zalloc(size_t num, const char *file, int line)
40-
"g_malloc", // g_malloc (n_bytes);
41-
"g_try_malloc" // g_try_malloc(n_bytes);
42-
]) and
43-
sizeArg = 0
44-
or
45-
this.hasGlobalName([
46-
// --- Windows Memory Management for Windows Drivers
47-
"ExAllocatePool", // ExAllocatePool(type, size)
48-
"ExAllocatePool2", // ExAllocatePool2(flags, size, tag)
49-
"ExAllocatePool3", // ExAllocatePool3(flags, size, tag, extparams, extparamscount)
50-
"ExAllocatePoolWithTag", // ExAllocatePool(type, size, tag)
51-
"ExAllocatePoolWithTagPriority", // ExAllocatePoolWithTagPriority(type, size, tag, priority)
52-
"ExAllocatePoolWithQuota", // ExAllocatePoolWithQuota(type, size)
53-
"ExAllocatePoolWithQuotaTag", // ExAllocatePoolWithQuotaTag(type, size, tag)
54-
"ExAllocatePoolZero", // ExAllocatePoolZero(type, size, tag)
55-
"IoAllocateMdl", // IoAllocateMdl(address, size, flag, flag, irp)
56-
"IoAllocateErrorLogEntry", // IoAllocateErrorLogEntry(object, size)
57-
// --- Windows Global / Local legacy allocation
58-
"LocalAlloc", // LocalAlloc(flags, size)
59-
"GlobalAlloc", // GlobalAlloc(flags, size)
60-
// --- Windows System Services allocation
61-
"VirtualAlloc" // VirtualAlloc(address, size, type, flag)
62-
]) and
63-
sizeArg = 1
64-
or
65-
this.hasGlobalName("HeapAlloc") and // HeapAlloc(heap, flags, size)
66-
sizeArg = 2
67-
or
68-
this.hasGlobalName([
69-
// --- Windows Memory Management for Windows Drivers
70-
"MmAllocatePagesForMdl", // MmAllocatePagesForMdl(minaddress, maxaddress, skip, size)
71-
"MmAllocatePagesForMdlEx", // MmAllocatePagesForMdlEx(minaddress, maxaddress, skip, size, type, flags)
72-
"MmAllocateNodePagesForMdlEx" // MmAllocateNodePagesForMdlEx(minaddress, maxaddress, skip, size, type, prefer, flags)
73-
]) and
74-
sizeArg = 3
75-
}
76-
77-
override int getSizeArg() { result = sizeArg }
78-
}
79-
80-
/**
81-
* An allocation function (such as `alloca`) that does not require a
82-
* corresponding free (and has an argument for the size in bytes).
83-
*/
84-
private class AllocaAllocationFunction extends AllocationFunction {
85-
int sizeArg;
86-
87-
AllocaAllocationFunction() {
88-
this.hasGlobalName([
89-
// --- stack allocation
90-
"alloca", // // alloca(size)
91-
"__builtin_alloca", // __builtin_alloca(size)
92-
"_alloca", // _alloca(size)
93-
"_malloca" // _malloca(size)
94-
]) and
95-
sizeArg = 0
96-
}
97-
98-
override int getSizeArg() { result = sizeArg }
99-
100-
override predicate requiresDealloc() { none() }
101-
}
102-
103-
/**
104-
* An allocation function (such as `calloc`) that has an argument for the size
105-
* and another argument for the size of those units (in bytes).
106-
*/
107-
private class CallocAllocationFunction extends AllocationFunction {
108-
int sizeArg;
109-
int multArg;
110-
111-
CallocAllocationFunction() {
112-
// --- C library allocation
113-
this.hasGlobalOrStdOrBslName("calloc") and // calloc(num, size)
114-
sizeArg = 1 and
115-
multArg = 0
116-
}
117-
118-
override int getSizeArg() { result = sizeArg }
119-
120-
override int getSizeMult() { result = multArg }
121-
}
122-
12310
/**
12411
* An allocation function (such as `realloc`) that has an argument for the size
12512
* in bytes, and an argument for an existing pointer that is to be reallocated.

0 commit comments

Comments
 (0)