Skip to content

Commit 4c3a26f

Browse files
committed
Revert "Merge pull request github#4558 from rdmarsh2/rdmarsh2/cpp/remove-initialize-nonlocal"
This reverts commit 08efd7f, reversing changes made to cb8c5e8.
1 parent aa45920 commit 4c3a26f

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

+5007
-4276
lines changed

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

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

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-
8879
/**
8980
* The operand is a Phi operand, which accesses the same memory as its
9081
* definition.

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,8 +979,19 @@ 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+
982993
final override MemoryAccessKind getWriteMemoryAccess() {
983-
result instanceof EscapedInitializationMemoryAccess
994+
result instanceof NonLocalMemoryAccess
984995
}
985996
}
986997

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,34 @@ 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+
444472
query predicate invalidOverlap(
445473
MemoryOperand useOperand, string message, OptionalIRFunction irFunc, string irFuncText
446474
) {

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ 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-
10095
/**
10196
* Gets the zero-based index of this instruction within its block. This is
10297
* used by debugging and printing code only.
@@ -148,8 +143,7 @@ class Instruction extends Construction::TStageInstruction {
148143
*/
149144
final string getResultString() {
150145
shouldGenerateDumpStrings() and
151-
result =
152-
getConflationPrefix() + getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
146+
result = getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
153147
}
154148

155149
/**
@@ -601,6 +595,16 @@ class InitializeParameterInstruction extends VariableInstruction {
601595
}
602596
}
603597

598+
/**
599+
* An instruction that initializes all memory that existed before this function was called.
600+
*
601+
* This instruction provides a definition for memory that, because it was actually allocated and
602+
* initialized elsewhere, would not otherwise have a definition in this function.
603+
*/
604+
class InitializeNonLocalInstruction extends Instruction {
605+
InitializeNonLocalInstruction() { getOpcode() instanceof Opcode::InitializeNonLocal }
606+
}
607+
604608
/**
605609
* An instruction that initializes the memory pointed to by a parameter of the enclosing function
606610
* 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: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,8 @@ private newtype TMemoryLocation =
8181
TAllNonLocalMemory(IRFunction irFunc, boolean isMayAccess) {
8282
isMayAccess = false or isMayAccess = true
8383
} or
84-
TAllAliasedMemory(IRFunction irFunc, boolean isMayAccess, boolean canDefineReadOnly) {
85-
isMayAccess = false and
86-
canDefineReadOnly = [true, false]
87-
or
88-
isMayAccess = true and canDefineReadOnly = false
84+
TAllAliasedMemory(IRFunction irFunc, boolean isMayAccess) {
85+
isMayAccess = false or isMayAccess = true
8986
}
9087

9188
/**
@@ -157,7 +154,7 @@ abstract class AllocationMemoryLocation extends MemoryLocation {
157154

158155
final override VirtualVariable getVirtualVariable() {
159156
if allocationEscapes(var)
160-
then result = TAllAliasedMemory(var.getEnclosingIRFunction(), false, true)
157+
then result = TAllAliasedMemory(var.getEnclosingIRFunction(), false)
161158
else result.(AllocationMemoryLocation).getAllocation() = var
162159
}
163160

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

288285
final override string toStringInternal() { result = "{Unknown}" }
289286

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

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

331326
final override predicate isMayAccess() { isMayAccess = true }
332327

333-
override predicate canDefineReadOnly() { none() }
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+
}
334335
}
335336

336337
/**
@@ -339,9 +340,8 @@ class AllNonLocalMemory extends TAllNonLocalMemory, MemoryLocation {
339340
class AllAliasedMemory extends TAllAliasedMemory, MemoryLocation {
340341
IRFunction irFunc;
341342
boolean isMayAccess;
342-
boolean canDefineReadOnly;
343343

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

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

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

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

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

362360
final override predicate isMayAccess() { isMayAccess = true }
363-
364-
final override predicate canDefineReadOnly() { canDefineReadOnly = true }
365361
}
366362

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

372368
/**
@@ -593,10 +589,7 @@ MemoryLocation getResultMemoryLocation(Instruction instr) {
593589
unbindBool(isMayAccess))
594590
or
595591
kind instanceof EscapedMemoryAccess and
596-
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), isMayAccess, false)
597-
or
598-
kind instanceof EscapedInitializationMemoryAccess and
599-
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), false, true)
592+
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), isMayAccess)
600593
or
601594
kind instanceof NonLocalMemoryAccess and
602595
result = TAllNonLocalMemory(instr.getEnclosingIRFunction(), isMayAccess)
@@ -627,10 +620,7 @@ MemoryLocation getOperandMemoryLocation(MemoryOperand operand) {
627620
isMayAccess)
628621
or
629622
kind instanceof EscapedMemoryAccess and
630-
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), isMayAccess, false)
631-
or
632-
kind instanceof EscapedInitializationMemoryAccess and
633-
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), false, true)
623+
result = TAllAliasedMemory(operand.getEnclosingIRFunction(), isMayAccess)
634624
or
635625
kind instanceof NonLocalMemoryAccess and
636626
result = TAllNonLocalMemory(operand.getEnclosingIRFunction(), isMayAccess)

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

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

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,34 @@ 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+
444472
query predicate invalidOverlap(
445473
MemoryOperand useOperand, string message, OptionalIRFunction irFunc, string irFuncText
446474
) {

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ 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-
10095
/**
10196
* Gets the zero-based index of this instruction within its block. This is
10297
* used by debugging and printing code only.
@@ -148,8 +143,7 @@ class Instruction extends Construction::TStageInstruction {
148143
*/
149144
final string getResultString() {
150145
shouldGenerateDumpStrings() and
151-
result =
152-
getConflationPrefix() + getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
146+
result = getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
153147
}
154148

155149
/**
@@ -601,6 +595,16 @@ class InitializeParameterInstruction extends VariableInstruction {
601595
}
602596
}
603597

598+
/**
599+
* An instruction that initializes all memory that existed before this function was called.
600+
*
601+
* This instruction provides a definition for memory that, because it was actually allocated and
602+
* initialized elsewhere, would not otherwise have a definition in this function.
603+
*/
604+
class InitializeNonLocalInstruction extends Instruction {
605+
InitializeNonLocalInstruction() { getOpcode() instanceof Opcode::InitializeNonLocal }
606+
}
607+
604608
/**
605609
* An instruction that initializes the memory pointed to by a parameter of the enclosing function
606610
* 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ predicate hasModeledMemoryResult(Instruction instruction) { none() }
166166

167167
predicate hasConflatedMemoryResult(Instruction instruction) {
168168
instruction instanceof AliasedDefinitionInstruction
169+
or
170+
instruction.getOpcode() instanceof Opcode::InitializeNonLocal
169171
}
170172

171173
Instruction getRegisterOperandDefinition(Instruction instruction, RegisterOperandTag tag) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ newtype TInstructionTag =
2828
ReturnTag() or
2929
ExitFunctionTag() or
3030
AliasedDefinitionTag() or
31+
InitializeNonLocalTag() or
3132
AliasedUseTag() or
3233
SwitchBranchTag() or
3334
CallTargetTag() or
@@ -127,6 +128,8 @@ string getInstructionTagId(TInstructionTag tag) {
127128
or
128129
tag = AliasedDefinitionTag() and result = "AliasedDef"
129130
or
131+
tag = InitializeNonLocalTag() and result = "InitNonLocal"
132+
or
130133
tag = AliasedUseTag() and result = "AliasedUse"
131134
or
132135
tag = SwitchBranchTag() and result = "SwitchBranch"

0 commit comments

Comments
 (0)