Skip to content

Commit 08efd7f

Browse files
author
Dave Bartolomeo
authored
Merge pull request github#4558 from rdmarsh2/rdmarsh2/cpp/remove-initialize-nonlocal
Remove InitializeNonlocalInstruction
2 parents cb8c5e8 + 5753a2d commit 08efd7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4276
-5007
lines changed

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 & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -441,34 +441,6 @@ module InstructionConsistency {
441441
isOnAliasedDefinitionChain(instr.(PhiInstruction).getAnInputOperand().getAnyDef())
442442
}
443443

444-
private predicate shouldBeConflated(Instruction instr) {
445-
isOnAliasedDefinitionChain(instr)
446-
or
447-
instr.getOpcode() instanceof Opcode::InitializeNonLocal
448-
}
449-
450-
query predicate notMarkedAsConflated(
451-
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
452-
) {
453-
shouldBeConflated(instr) and
454-
not instr.isResultConflated() and
455-
message =
456-
"Instruction '" + instr.toString() +
457-
"' should be marked as having a conflated result in function '$@'." and
458-
irFunc = getInstructionIRFunction(instr, irFuncText)
459-
}
460-
461-
query predicate wronglyMarkedAsConflated(
462-
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
463-
) {
464-
instr.isResultConflated() and
465-
not shouldBeConflated(instr) and
466-
message =
467-
"Instruction '" + instr.toString() +
468-
"' should not be marked as having a conflated result in function '$@'." and
469-
irFunc = getInstructionIRFunction(instr, irFuncText)
470-
}
471-
472444
query predicate invalidOverlap(
473445
MemoryOperand useOperand, string message, OptionalIRFunction irFunc, string irFuncText
474446
) {

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class Instruction extends Construction::TStageInstruction {
9292
else result = "r"
9393
}
9494

95+
private string getConflationPrefix() {
96+
shouldGenerateDumpStrings() and
97+
if isResultConflated() then result = "%" else result = ""
98+
}
99+
95100
/**
96101
* Gets the zero-based index of this instruction within its block. This is
97102
* used by debugging and printing code only.
@@ -143,7 +148,8 @@ class Instruction extends Construction::TStageInstruction {
143148
*/
144149
final string getResultString() {
145150
shouldGenerateDumpStrings() and
146-
result = getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
151+
result =
152+
getConflationPrefix() + getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
147153
}
148154

149155
/**
@@ -584,16 +590,6 @@ class InitializeParameterInstruction extends VariableInstruction {
584590
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
585591
}
586592

587-
/**
588-
* An instruction that initializes all memory that existed before this function was called.
589-
*
590-
* This instruction provides a definition for memory that, because it was actually allocated and
591-
* initialized elsewhere, would not otherwise have a definition in this function.
592-
*/
593-
class InitializeNonLocalInstruction extends Instruction {
594-
InitializeNonLocalInstruction() { getOpcode() instanceof Opcode::InitializeNonLocal }
595-
}
596-
597593
/**
598594
* An instruction that initializes the memory pointed to by a parameter of the enclosing function
599595
* 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: 26 additions & 16 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, 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,9 @@ 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() {
291+
result = TAllAliasedMemory(irFunc, false, true)
292+
}
288293

289294
final override Language::LanguageType getType() {
290295
result = any(IRUnknownType type).getCanonicalLanguageType()
@@ -325,13 +330,7 @@ class AllNonLocalMemory extends TAllNonLocalMemory, MemoryLocation {
325330

326331
final override predicate isMayAccess() { isMayAccess = true }
327332

328-
override predicate canDefineReadOnly() {
329-
// A "must" access that defines all non-local memory appears only on the `InitializeNonLocal`
330-
// instruction, which provides the initial definition for all memory outside of the current
331-
// function's stack frame. This memory includes string literals and other read-only globals, so
332-
// we allow such an access to be the definition for a use of a read-only ___location.
333-
not isMayAccess()
334-
}
333+
override predicate canDefineReadOnly() { none() }
335334
}
336335

337336
/**
@@ -340,8 +339,9 @@ class AllNonLocalMemory extends TAllNonLocalMemory, MemoryLocation {
340339
class AllAliasedMemory extends TAllAliasedMemory, MemoryLocation {
341340
IRFunction irFunc;
342341
boolean isMayAccess;
342+
boolean canDefineReadOnly;
343343

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

346346
final override string toStringInternal() { result = "{AllAliased}" }
347347

@@ -355,14 +355,18 @@ class AllAliasedMemory extends TAllAliasedMemory, MemoryLocation {
355355

356356
final override string getUniqueId() { result = " " + toString() }
357357

358-
final override VirtualVariable getVirtualVariable() { result = TAllAliasedMemory(irFunc, false) }
358+
final override VirtualVariable getVirtualVariable() {
359+
result = TAllAliasedMemory(irFunc, false, true)
360+
}
359361

360362
final override predicate isMayAccess() { isMayAccess = true }
363+
364+
final override predicate canDefineReadOnly() { canDefineReadOnly = true }
361365
}
362366

363367
/** A virtual variable that groups all escaped memory within a function. */
364368
class AliasedVirtualVariable extends AllAliasedMemory, VirtualVariable {
365-
AliasedVirtualVariable() { not isMayAccess() }
369+
AliasedVirtualVariable() { not isMayAccess() and canDefineReadOnly() }
366370
}
367371

368372
/**
@@ -585,7 +589,10 @@ MemoryLocation getResultMemoryLocation(Instruction instr) {
585589
isMayAccess)
586590
or
587591
kind instanceof EscapedMemoryAccess and
588-
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), isMayAccess)
592+
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), isMayAccess, false)
593+
or
594+
kind instanceof EscapedInitializationMemoryAccess and
595+
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), false, true)
589596
or
590597
kind instanceof NonLocalMemoryAccess and
591598
result = TAllNonLocalMemory(instr.getEnclosingIRFunction(), isMayAccess)
@@ -616,7 +623,10 @@ MemoryLocation getOperandMemoryLocation(MemoryOperand operand) {
616623
isMayAccess)
617624
or
618625
kind instanceof EscapedMemoryAccess and
619-
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), isMayAccess)
626+
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), isMayAccess, false)
627+
or
628+
kind instanceof EscapedInitializationMemoryAccess and
629+
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), false, true)
620630
or
621631
kind instanceof NonLocalMemoryAccess and
622632
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 & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -441,34 +441,6 @@ module InstructionConsistency {
441441
isOnAliasedDefinitionChain(instr.(PhiInstruction).getAnInputOperand().getAnyDef())
442442
}
443443

444-
private predicate shouldBeConflated(Instruction instr) {
445-
isOnAliasedDefinitionChain(instr)
446-
or
447-
instr.getOpcode() instanceof Opcode::InitializeNonLocal
448-
}
449-
450-
query predicate notMarkedAsConflated(
451-
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
452-
) {
453-
shouldBeConflated(instr) and
454-
not instr.isResultConflated() and
455-
message =
456-
"Instruction '" + instr.toString() +
457-
"' should be marked as having a conflated result in function '$@'." and
458-
irFunc = getInstructionIRFunction(instr, irFuncText)
459-
}
460-
461-
query predicate wronglyMarkedAsConflated(
462-
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
463-
) {
464-
instr.isResultConflated() and
465-
not shouldBeConflated(instr) and
466-
message =
467-
"Instruction '" + instr.toString() +
468-
"' should not be marked as having a conflated result in function '$@'." and
469-
irFunc = getInstructionIRFunction(instr, irFuncText)
470-
}
471-
472444
query predicate invalidOverlap(
473445
MemoryOperand useOperand, string message, OptionalIRFunction irFunc, string irFuncText
474446
) {

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class Instruction extends Construction::TStageInstruction {
9292
else result = "r"
9393
}
9494

95+
private string getConflationPrefix() {
96+
shouldGenerateDumpStrings() and
97+
if isResultConflated() then result = "%" else result = ""
98+
}
99+
95100
/**
96101
* Gets the zero-based index of this instruction within its block. This is
97102
* used by debugging and printing code only.
@@ -143,7 +148,8 @@ class Instruction extends Construction::TStageInstruction {
143148
*/
144149
final string getResultString() {
145150
shouldGenerateDumpStrings() and
146-
result = getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
151+
result =
152+
getConflationPrefix() + getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
147153
}
148154

149155
/**
@@ -584,16 +590,6 @@ class InitializeParameterInstruction extends VariableInstruction {
584590
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
585591
}
586592

587-
/**
588-
* An instruction that initializes all memory that existed before this function was called.
589-
*
590-
* This instruction provides a definition for memory that, because it was actually allocated and
591-
* initialized elsewhere, would not otherwise have a definition in this function.
592-
*/
593-
class InitializeNonLocalInstruction extends Instruction {
594-
InitializeNonLocalInstruction() { getOpcode() instanceof Opcode::InitializeNonLocal }
595-
}
596-
597593
/**
598594
* An instruction that initializes the memory pointed to by a parameter of the enclosing function
599595
* 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)