Skip to content

Commit 72679c8

Browse files
committed
C++: Add a new opcode and instruction.
1 parent 4953e7e commit 72679c8

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/MemoryAccessKind.qll

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ private newtype TMemoryAccessKind =
1313
TPhiMemoryAccess() or
1414
TUnmodeledMemoryAccess() or
1515
TChiTotalMemoryAccess() or
16-
TChiPartialMemoryAccess()
16+
TChiPartialMemoryAccess() or
17+
TGroupedMemoryAccess()
1718

1819
/**
1920
* Describes the set of memory locations memory accessed by a memory operand or
@@ -99,3 +100,11 @@ class ChiTotalMemoryAccess extends MemoryAccessKind, TChiTotalMemoryAccess {
99100
class ChiPartialMemoryAccess extends MemoryAccessKind, TChiPartialMemoryAccess {
100101
override string toString() { result = "chi(partial)" }
101102
}
103+
104+
/**
105+
* The result of an `InitializeGroup` instruction, which initializes a set of
106+
* allocations that are each assigned the same virtual variable.
107+
*/
108+
class GroupedMemoryAccess extends MemoryAccessKind, TGroupedMemoryAccess {
109+
override string toString() { result = "group" }
110+
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/Opcode.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private newtype TOpcode =
8989
TSizedBufferMayWriteSideEffect() or
9090
TInitializeDynamicAllocation() or
9191
TChi() or
92+
TInitializeGroup() or
9293
TInlineAsm() or
9394
TUnreached() or
9495
TNewObj()
@@ -1237,6 +1238,17 @@ module Opcode {
12371238
}
12381239
}
12391240

1241+
/**
1242+
* The `Opcode` for a `InitializeGroup`.
1243+
*
1244+
* See the `InitializeGroupInstruction` documentation for more details.
1245+
*/
1246+
class InitializeGroup extends Opcode, TInitializeGroup {
1247+
final override string toString() { result = "InitializeGroup" }
1248+
1249+
override GroupedMemoryAccess getWriteMemoryAccess() { any() }
1250+
}
1251+
12401252
/**
12411253
* The `Opcode` for an `InlineAsmInstruction`.
12421254
*

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,33 @@ class ChiInstruction extends Instruction {
21422142
final predicate isPartialUpdate() { Construction::chiOnlyPartiallyUpdatesLocation(this) }
21432143
}
21442144

2145+
/**
2146+
* An instruction that initializes a set of allocations that are each assigned
2147+
* the same "virtual variable".
2148+
*
2149+
* As an example, consider the following snippet:
2150+
* ```
2151+
* int a;
2152+
* int b;
2153+
* int* p;
2154+
* if(b) {
2155+
* p = &a;
2156+
* } else {
2157+
* p = &b;
2158+
* }
2159+
* *p = 5;
2160+
* int x = a;
2161+
* ```
2162+
*
2163+
* Since both the address of `a` and `b` reach `p` at `*p = 5` the IR alias
2164+
* analysis will create a region that contains both `a` and `b`. The region
2165+
* containing both `a` and `b` are initialized by an `InitializeGroup`
2166+
* instruction in the entry block of the enclosing function.
2167+
*/
2168+
class InitializeGroupInstruction extends Instruction {
2169+
InitializeGroupInstruction() { this.getOpcode() instanceof Opcode::InitializeGroup }
2170+
}
2171+
21452172
/**
21462173
* An instruction representing unreachable code.
21472174
*

0 commit comments

Comments
 (0)