Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a365d2

Browse files
author
Robert Marsh
committed
C++: remove InitializeNonLocalInstruction from IR
Instead, have AliasedDefinition initialize read-only nonlocal memory
1 parent 9964885 commit 1a365d2

25 files changed

Lines changed: 1756 additions & 2093 deletions

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ private newtype TMemoryAccessKind =
1010
TEntireAllocationMemoryAccess() or
1111
TEscapedMemoryAccess() or
1212
TNonLocalMemoryAccess() or
13+
TEscapedInitializationMemoryAccess() or
1314
TPhiMemoryAccess() or
1415
TUnmodeledMemoryAccess() or
1516
TChiTotalMemoryAccess() or
@@ -76,6 +77,14 @@ class NonLocalMemoryAccess extends MemoryAccessKind, TNonLocalMemoryAccess {
7677
override string toString() { result = "nonlocal" }
7778
}
7879

80+
/**
81+
* The operand or result accesses all memory whose address has escaped and can define read-only
82+
* memory (such as string constants).
83+
*/
84+
class EscapedInitializationMemoryAccess extends MemoryAccessKind, TEscapedInitializationMemoryAccess {
85+
override string toString() { result = "escaped(init)" }
86+
}
87+
7988
/**
8089
* The operand is a Phi operand, which accesses the same memory as its
8190
* definition.

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -979,19 +979,8 @@ module Opcode {
979979
class AliasedDefinition extends Opcode, TAliasedDefinition {
980980
final override string toString() { result = "AliasedDefinition" }
981981

982-
final override MemoryAccessKind getWriteMemoryAccess() { result instanceof EscapedMemoryAccess }
983-
}
984-
985-
/**
986-
* The `Opcode` for an `InitializeNonLocalInstruction`.
987-
*
988-
* See the `InitializeNonLocalInstruction` documentation for more details.
989-
*/
990-
class InitializeNonLocal extends Opcode, TInitializeNonLocal {
991-
final override string toString() { result = "InitializeNonLocal" }
992-
993982
final override MemoryAccessKind getWriteMemoryAccess() {
994-
result instanceof NonLocalMemoryAccess
983+
result instanceof EscapedInitializationMemoryAccess
995984
}
996985
}
997986

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRConsistency.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,6 @@ module InstructionConsistency {
443443

444444
private predicate shouldBeConflated(Instruction instr) {
445445
isOnAliasedDefinitionChain(instr)
446-
or
447-
instr.getOpcode() instanceof Opcode::InitializeNonLocal
448446
}
449447

450448
query predicate notMarkedAsConflated(

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,16 +574,6 @@ class InitializeParameterInstruction extends VariableInstruction {
574574
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
575575
}
576576

577-
/**
578-
* An instruction that initializes all memory that existed before this function was called.
579-
*
580-
* This instruction provides a definition for memory that, because it was actually allocated and
581-
* initialized elsewhere, would not otherwise have a definition in this function.
582-
*/
583-
class InitializeNonLocalInstruction extends Instruction {
584-
InitializeNonLocalInstruction() { getOpcode() instanceof Opcode::InitializeNonLocal }
585-
}
586-
587577
/**
588578
* An instruction that initializes the memory pointed to by a parameter of the enclosing function
589579
* with the value of that memory on entry to the function.

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/AliasedSSA.qll

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ private newtype TMemoryLocation =
8181
TAllNonLocalMemory(IRFunction irFunc, boolean isMayAccess) {
8282
isMayAccess = false or isMayAccess = true
8383
} or
84-
TAllAliasedMemory(IRFunction irFunc, boolean isMayAccess) {
85-
isMayAccess = false or isMayAccess = true
84+
TAllAliasedMemory(IRFunction irFunc, boolean isMayAccess, boolean canDefineReadOnly) {
85+
isMayAccess = false and
86+
(canDefineReadOnly = true or canDefineReadOnly = false)
87+
or
88+
isMayAccess = true and canDefineReadOnly = false
8689
}
8790

8891
/**
@@ -154,7 +157,7 @@ abstract class AllocationMemoryLocation extends MemoryLocation {
154157

155158
final override VirtualVariable getVirtualVariable() {
156159
if allocationEscapes(var)
157-
then result = TAllAliasedMemory(var.getEnclosingIRFunction(), false)
160+
then result = TAllAliasedMemory(var.getEnclosingIRFunction(), false, true)
158161
else result.(AllocationMemoryLocation).getAllocation() = var
159162
}
160163

@@ -284,7 +287,7 @@ class UnknownMemoryLocation extends TUnknownMemoryLocation, MemoryLocation {
284287

285288
final override string toStringInternal() { result = "{Unknown}" }
286289

287-
final override VirtualVariable getVirtualVariable() { result = TAllAliasedMemory(irFunc, false) }
290+
final override VirtualVariable getVirtualVariable() { result = TAllAliasedMemory(irFunc, false, true) }
288291

289292
final override Language::LanguageType getType() {
290293
result = any(IRUnknownType type).getCanonicalLanguageType()
@@ -340,8 +343,9 @@ class AllNonLocalMemory extends TAllNonLocalMemory, MemoryLocation {
340343
class AllAliasedMemory extends TAllAliasedMemory, MemoryLocation {
341344
IRFunction irFunc;
342345
boolean isMayAccess;
346+
boolean canDefineReadOnly;
343347

344-
AllAliasedMemory() { this = TAllAliasedMemory(irFunc, isMayAccess) }
348+
AllAliasedMemory() { this = TAllAliasedMemory(irFunc, isMayAccess, canDefineReadOnly) }
345349

346350
final override string toStringInternal() { result = "{AllAliased}" }
347351

@@ -355,14 +359,17 @@ class AllAliasedMemory extends TAllAliasedMemory, MemoryLocation {
355359

356360
final override string getUniqueId() { result = " " + toString() }
357361

358-
final override VirtualVariable getVirtualVariable() { result = TAllAliasedMemory(irFunc, false) }
362+
final override VirtualVariable getVirtualVariable() { result = TAllAliasedMemory(irFunc, false, true) }
359363

360364
final override predicate isMayAccess() { isMayAccess = true }
365+
366+
final override predicate canDefineReadOnly() { canDefineReadOnly = true }
367+
361368
}
362369

363370
/** A virtual variable that groups all escaped memory within a function. */
364371
class AliasedVirtualVariable extends AllAliasedMemory, VirtualVariable {
365-
AliasedVirtualVariable() { not isMayAccess() }
372+
AliasedVirtualVariable() { not isMayAccess() and canDefineReadOnly() }
366373
}
367374

368375
/**
@@ -585,7 +592,10 @@ MemoryLocation getResultMemoryLocation(Instruction instr) {
585592
isMayAccess)
586593
or
587594
kind instanceof EscapedMemoryAccess and
588-
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), isMayAccess)
595+
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), isMayAccess, isMayAccess)
596+
or
597+
kind instanceof EscapedInitializationMemoryAccess and
598+
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), false, true)
589599
or
590600
kind instanceof NonLocalMemoryAccess and
591601
result = TAllNonLocalMemory(instr.getEnclosingIRFunction(), isMayAccess)
@@ -616,7 +626,10 @@ MemoryLocation getOperandMemoryLocation(MemoryOperand operand) {
616626
isMayAccess)
617627
or
618628
kind instanceof EscapedMemoryAccess and
619-
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), isMayAccess)
629+
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), isMayAccess, false)
630+
or
631+
kind instanceof EscapedInitializationMemoryAccess and
632+
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), false, true)
620633
or
621634
kind instanceof NonLocalMemoryAccess and
622635
result = TAllNonLocalMemory(operand.getEnclosingIRFunction(), isMayAccess)

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/SSAConstruction.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ private module Cached {
6868
predicate hasConflatedMemoryResult(Instruction instruction) {
6969
instruction instanceof AliasedDefinitionInstruction
7070
or
71-
instruction.getOpcode() instanceof Opcode::InitializeNonLocal
72-
or
7371
// Chi instructions track virtual variables, and therefore a chi instruction is
7472
// conflated if it's associated with the aliased virtual variable.
7573
exists(OldInstruction oldInstruction | instruction = getChi(oldInstruction) |

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRConsistency.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,6 @@ module InstructionConsistency {
443443

444444
private predicate shouldBeConflated(Instruction instr) {
445445
isOnAliasedDefinitionChain(instr)
446-
or
447-
instr.getOpcode() instanceof Opcode::InitializeNonLocal
448446
}
449447

450448
query predicate notMarkedAsConflated(

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,16 +574,6 @@ class InitializeParameterInstruction extends VariableInstruction {
574574
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
575575
}
576576

577-
/**
578-
* An instruction that initializes all memory that existed before this function was called.
579-
*
580-
* This instruction provides a definition for memory that, because it was actually allocated and
581-
* initialized elsewhere, would not otherwise have a definition in this function.
582-
*/
583-
class InitializeNonLocalInstruction extends Instruction {
584-
InitializeNonLocalInstruction() { getOpcode() instanceof Opcode::InitializeNonLocal }
585-
}
586-
587577
/**
588578
* An instruction that initializes the memory pointed to by a parameter of the enclosing function
589579
* with the value of that memory on entry to the function.

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ predicate hasModeledMemoryResult(Instruction instruction) { none() }
166166

167167
predicate hasConflatedMemoryResult(Instruction instruction) {
168168
instruction instanceof AliasedDefinitionInstruction
169-
or
170-
instruction.getOpcode() instanceof Opcode::InitializeNonLocal
171169
}
172170

173171
Instruction getRegisterOperandDefinition(Instruction instruction, RegisterOperandTag tag) {

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/InstructionTag.qll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ newtype TInstructionTag =
2828
ReturnTag() or
2929
ExitFunctionTag() or
3030
AliasedDefinitionTag() or
31-
InitializeNonLocalTag() or
3231
AliasedUseTag() or
3332
SwitchBranchTag() or
3433
CallTargetTag() or
@@ -128,8 +127,6 @@ string getInstructionTagId(TInstructionTag tag) {
128127
or
129128
tag = AliasedDefinitionTag() and result = "AliasedDef"
130129
or
131-
tag = InitializeNonLocalTag() and result = "InitNonLocal"
132-
or
133130
tag = AliasedUseTag() and result = "AliasedUse"
134131
or
135132
tag = SwitchBranchTag() and result = "SwitchBranch"

0 commit comments

Comments
 (0)