Skip to content

Commit 1b1fded

Browse files
author
Dave Bartolomeo
committed
C++/C#: Add new MemoryAccessKind to represent entire allocation
1 parent a9bcc1d commit 1b1fded

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
private newtype TMemoryAccessKind =
22
TIndirectMemoryAccess() or
33
TBufferMemoryAccess() or
4+
TEntireAllocationMemoryAccess() or
45
TEscapedMemoryAccess() or
56
TNonLocalMemoryAccess() or
67
TPhiMemoryAccess() or
@@ -43,6 +44,16 @@ class BufferMemoryAccess extends MemoryAccessKind, TBufferMemoryAccess {
4344
final override predicate usesAddressOperand() { any() }
4445
}
4546

47+
/**
48+
* The operand or results accesses all memory in the contiguous allocation that contains the address
49+
* specified by the `AddressOperand` on the same instruction.
50+
*/
51+
class EntireAllocationMemoryAccess extends MemoryAccessKind, TEntireAllocationMemoryAccess {
52+
override string toString() { result = "alloc" }
53+
54+
final override predicate usesAddressOperand() { any() }
55+
}
56+
4657
/**
4758
* The operand or result accesses all memory whose address has escaped.
4859
*/

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,31 @@ abstract class BufferReadOpcode extends BufferAccessOpcode {
232232
final override MemoryAccessKind getReadMemoryAccess() { result instanceof BufferMemoryAccess }
233233
}
234234

235+
/**
236+
* An opcode that access an entire memory allocation.
237+
*/
238+
abstract class EntireAllocationAccessOpcode extends Opcode {
239+
final override predicate hasAddressOperand() { any() }
240+
}
241+
242+
/**
243+
* An opcode that write to an entire memory allocation.
244+
*/
245+
abstract class EntireAllocationWriteOpcode extends EntireAllocationAccessOpcode {
246+
final override MemoryAccessKind getWriteMemoryAccess() {
247+
result instanceof EntireAllocationMemoryAccess
248+
}
249+
}
250+
251+
/**
252+
* An opcode that reads from an entire memory allocation.
253+
*/
254+
abstract class EntireAllocationReadOpcode extends EntireAllocationAccessOpcode {
255+
final override MemoryAccessKind getReadMemoryAccess() {
256+
result instanceof EntireAllocationMemoryAccess
257+
}
258+
}
259+
235260
/**
236261
* An opcode that accesses a memory buffer whose size is determined by a `BufferSizeOperand`.
237262
*/
@@ -325,7 +350,7 @@ module Opcode {
325350
final override string toString() { result = "InitializeParameter" }
326351
}
327352

328-
class InitializeIndirection extends IndirectWriteOpcode, TInitializeIndirection {
353+
class InitializeIndirection extends EntireAllocationWriteOpcode, TInitializeIndirection {
329354
final override string toString() { result = "InitializeIndirection" }
330355
}
331356

@@ -349,7 +374,7 @@ module Opcode {
349374
final override string toString() { result = "ReturnVoid" }
350375
}
351376

352-
class ReturnIndirection extends IndirectReadOpcode, TReturnIndirection {
377+
class ReturnIndirection extends EntireAllocationReadOpcode, TReturnIndirection {
353378
final override string toString() { result = "ReturnIndirection" }
354379

355380
final override predicate hasOperandInternal(OperandTag tag) {

csharp/ql/src/semmle/code/csharp/ir/implementation/MemoryAccessKind.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
private newtype TMemoryAccessKind =
22
TIndirectMemoryAccess() or
33
TBufferMemoryAccess() or
4+
TEntireAllocationMemoryAccess() or
45
TEscapedMemoryAccess() or
56
TNonLocalMemoryAccess() or
67
TPhiMemoryAccess() or
@@ -43,6 +44,16 @@ class BufferMemoryAccess extends MemoryAccessKind, TBufferMemoryAccess {
4344
final override predicate usesAddressOperand() { any() }
4445
}
4546

47+
/**
48+
* The operand or results accesses all memory in the contiguous allocation that contains the address
49+
* specified by the `AddressOperand` on the same instruction.
50+
*/
51+
class EntireAllocationMemoryAccess extends MemoryAccessKind, TEntireAllocationMemoryAccess {
52+
override string toString() { result = "alloc" }
53+
54+
final override predicate usesAddressOperand() { any() }
55+
}
56+
4657
/**
4758
* The operand or result accesses all memory whose address has escaped.
4859
*/

csharp/ql/src/semmle/code/csharp/ir/implementation/Opcode.qll

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,31 @@ abstract class BufferReadOpcode extends BufferAccessOpcode {
232232
final override MemoryAccessKind getReadMemoryAccess() { result instanceof BufferMemoryAccess }
233233
}
234234

235+
/**
236+
* An opcode that access an entire memory allocation.
237+
*/
238+
abstract class EntireAllocationAccessOpcode extends Opcode {
239+
final override predicate hasAddressOperand() { any() }
240+
}
241+
242+
/**
243+
* An opcode that write to an entire memory allocation.
244+
*/
245+
abstract class EntireAllocationWriteOpcode extends EntireAllocationAccessOpcode {
246+
final override MemoryAccessKind getWriteMemoryAccess() {
247+
result instanceof EntireAllocationMemoryAccess
248+
}
249+
}
250+
251+
/**
252+
* An opcode that reads from an entire memory allocation.
253+
*/
254+
abstract class EntireAllocationReadOpcode extends EntireAllocationAccessOpcode {
255+
final override MemoryAccessKind getReadMemoryAccess() {
256+
result instanceof EntireAllocationMemoryAccess
257+
}
258+
}
259+
235260
/**
236261
* An opcode that accesses a memory buffer whose size is determined by a `BufferSizeOperand`.
237262
*/
@@ -325,7 +350,7 @@ module Opcode {
325350
final override string toString() { result = "InitializeParameter" }
326351
}
327352

328-
class InitializeIndirection extends IndirectWriteOpcode, TInitializeIndirection {
353+
class InitializeIndirection extends EntireAllocationWriteOpcode, TInitializeIndirection {
329354
final override string toString() { result = "InitializeIndirection" }
330355
}
331356

@@ -349,7 +374,7 @@ module Opcode {
349374
final override string toString() { result = "ReturnVoid" }
350375
}
351376

352-
class ReturnIndirection extends IndirectReadOpcode, TReturnIndirection {
377+
class ReturnIndirection extends EntireAllocationReadOpcode, TReturnIndirection {
353378
final override string toString() { result = "ReturnIndirection" }
354379

355380
final override predicate hasOperandInternal(OperandTag tag) {

0 commit comments

Comments
 (0)