Skip to content

Commit 4f6b6b4

Browse files
committed
Merge branch 'main' into no-dtt-in-unbounded-write
2 parents 7048190 + 340b20f commit 4f6b6b4

File tree

1,406 files changed

+53091
-29698
lines changed

Some content is hidden

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

1,406 files changed

+53091
-29698
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added models for the `sprintf` variants from the `StrSafe.h` header.

cpp/ql/lib/semmle/code/cpp/models/implementations/Printf.qll

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,32 @@ private class SnprintfImpl extends Snprintf {
147147

148148
/**
149149
* The Microsoft `StringCchPrintf` function and variants.
150+
* See: https://learn.microsoft.com/en-us/windows/win32/api/strsafe/
151+
* and
152+
* https://learn.microsoft.com/en-us/previous-versions/windows/embedded/ms860435(v=msdn.10)
150153
*/
151154
private class StringCchPrintf extends FormattingFunction {
152155
StringCchPrintf() {
153156
this instanceof TopLevelFunction and
154-
this.hasGlobalName([
155-
"StringCchPrintf", "StringCchPrintfEx", "StringCchPrintf_l", "StringCchPrintf_lEx",
156-
"StringCbPrintf", "StringCbPrintfEx", "StringCbPrintf_l", "StringCbPrintf_lEx"
157-
]) and
157+
exists(string baseName |
158+
baseName in [
159+
"StringCchPrintf", //StringCchPrintf(pszDest, cchDest, pszFormat, ...)
160+
"StringCchPrintfEx", //StringCchPrintfEx(pszDest,cchDest, ppszDestEnd, pcchRemaining, dwFlags, pszFormat, ...)
161+
"StringCchPrintf_l", //StringCchPrintf_l(pszDest, cbDest, pszFormat, locale, ...)
162+
"StringCchPrintf_lEx", //StringCchPrintf_lEx(pszDest, cchDest, ppszDestEnd, pcchRemaining, dwFlags, pszFormat, locale, ...)
163+
"StringCbPrintf", //StringCbPrintf(pszDest, cbDest, pszFormat, ...)
164+
"StringCbPrintfEx", //StringCbPrintfEx(pszDest, cbDest, ppszDestEnd, pcbRemaining, dwFlags, pszFormat, ...)
165+
"StringCbPrintf_l", //StringCbPrintf_l(pszDest, cbDest, pszFormat, locale, ...)
166+
"StringCbPrintf_lEx" //StringCbPrintf_lEx(pszDest, cbDest, ppszDestEnd, pcbRemaining, dwFlags, pszFormat, locale, ...)
167+
]
168+
|
169+
this.hasGlobalName(baseName + ["", "A", "W"])
170+
) and
158171
not exists(this.getDefinition().getFile().getRelativePath())
159172
}
160173

161174
override int getFormatParameterIndex() {
162-
if this.getName().matches("%Ex") then result = 5 else result = 2
175+
if this.getName().matches("%Ex" + ["", "A", "W"]) then result = 5 else result = 2
163176
}
164177

165178
override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = false }

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/SemanticExprSpecific.qll

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ module SemanticExprConfig {
130130

131131
newtype TSsaVariable =
132132
TSsaInstruction(IR::Instruction instr) { instr.hasMemoryResult() } or
133-
TSsaOperand(IR::Operand op) { op.isDefinitionInexact() }
133+
TSsaOperand(IR::PhiInputOperand op) { op.isDefinitionInexact() }
134134

135135
class SsaVariable extends TSsaVariable {
136136
string toString() { none() }
@@ -139,7 +139,7 @@ module SemanticExprConfig {
139139

140140
IR::Instruction asInstruction() { none() }
141141

142-
IR::Operand asOperand() { none() }
142+
IR::PhiInputOperand asOperand() { none() }
143143
}
144144

145145
class SsaInstructionVariable extends SsaVariable, TSsaInstruction {
@@ -155,15 +155,15 @@ module SemanticExprConfig {
155155
}
156156

157157
class SsaOperand extends SsaVariable, TSsaOperand {
158-
IR::Operand op;
158+
IR::PhiInputOperand op;
159159

160160
SsaOperand() { this = TSsaOperand(op) }
161161

162162
final override string toString() { result = op.toString() }
163163

164164
final override Location getLocation() { result = op.getLocation() }
165165

166-
final override IR::Operand asOperand() { result = op }
166+
final override IR::PhiInputOperand asOperand() { result = op }
167167
}
168168

169169
predicate explicitUpdate(SsaVariable v, Expr sourceExpr) {
@@ -190,83 +190,25 @@ module SemanticExprConfig {
190190

191191
SemType getSsaVariableType(SsaVariable v) {
192192
result = getSemanticType(v.asInstruction().getResultIRType())
193+
or
194+
result = getSemanticType(v.asOperand().getUse().getResultIRType())
193195
}
194196

195197
BasicBlock getSsaVariableBasicBlock(SsaVariable v) {
196198
result = v.asInstruction().getBlock()
197199
or
198-
result = v.asOperand().getUse().getBlock()
199-
}
200-
201-
private newtype TReadPosition =
202-
TReadPositionBlock(IR::IRBlock block) or
203-
TReadPositionPhiInputEdge(IR::IRBlock pred, IR::IRBlock succ) {
204-
exists(IR::PhiInputOperand input |
205-
pred = input.getPredecessorBlock() and
206-
succ = input.getUse().getBlock()
207-
)
208-
}
209-
210-
class SsaReadPosition extends TReadPosition {
211-
string toString() { none() }
212-
213-
Location getLocation() { none() }
214-
215-
predicate hasRead(SsaVariable v) { none() }
216-
}
217-
218-
private class SsaReadPositionBlock extends SsaReadPosition, TReadPositionBlock {
219-
IR::IRBlock block;
220-
221-
SsaReadPositionBlock() { this = TReadPositionBlock(block) }
222-
223-
final override string toString() { result = block.toString() }
224-
225-
final override Location getLocation() { result = block.getLocation() }
226-
227-
final override predicate hasRead(SsaVariable v) {
228-
exists(IR::Operand operand | operand.getDef() = v.asInstruction() |
229-
not operand instanceof IR::PhiInputOperand and
230-
operand.getUse().getBlock() = block
231-
)
232-
}
233-
}
234-
235-
private class SsaReadPositionPhiInputEdge extends SsaReadPosition, TReadPositionPhiInputEdge {
236-
IR::IRBlock pred;
237-
IR::IRBlock succ;
238-
239-
SsaReadPositionPhiInputEdge() { this = TReadPositionPhiInputEdge(pred, succ) }
240-
241-
final override string toString() { result = pred.toString() + "->" + succ.toString() }
242-
243-
final override Location getLocation() { result = succ.getLocation() }
244-
245-
final override predicate hasRead(SsaVariable v) {
246-
exists(IR::PhiInputOperand operand | operand.getDef() = v.asInstruction() |
247-
operand.getPredecessorBlock() = pred and
248-
operand.getUse().getBlock() = succ
249-
)
250-
}
251-
}
252-
253-
predicate hasReadOfSsaVariable(SsaReadPosition pos, SsaVariable v) { pos.hasRead(v) }
254-
255-
predicate readBlock(SsaReadPosition pos, BasicBlock block) { pos = TReadPositionBlock(block) }
256-
257-
predicate phiInputEdge(SsaReadPosition pos, BasicBlock origBlock, BasicBlock phiBlock) {
258-
pos = TReadPositionPhiInputEdge(origBlock, phiBlock)
200+
result = v.asOperand().getAnyDef().getBlock()
259201
}
260202

261-
predicate phiInput(SsaReadPosition pos, SsaVariable phi, SsaVariable input) {
203+
/** Holds if `inp` is an input to the phi node along the edge originating in `bb`. */
204+
predicate phiInputFromBlock(SsaVariable phi, SsaVariable inp, BasicBlock bb) {
262205
exists(IR::PhiInputOperand operand |
263-
pos = TReadPositionPhiInputEdge(operand.getPredecessorBlock(), operand.getUse().getBlock())
264-
|
206+
bb = operand.getPredecessorBlock() and
265207
phi.asInstruction() = operand.getUse() and
266208
(
267-
input.asInstruction() = operand.getDef()
209+
inp.asInstruction() = operand.getDef()
268210
or
269-
input.asOperand() = operand
211+
inp.asOperand() = operand
270212
)
271213
)
272214
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/SemanticSSA.qll

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,8 @@ class SemSsaPhiNode extends SemSsaVariable {
3131
SemSsaPhiNode() { Specific::phi(this) }
3232

3333
final SemSsaVariable getAPhiInput() { result = Specific::getAPhiInput(this) }
34-
}
35-
36-
class SemSsaReadPosition instanceof Specific::SsaReadPosition {
37-
final string toString() { result = super.toString() }
38-
39-
final Specific::Location getLocation() { result = super.getLocation() }
40-
41-
final predicate hasReadOfVar(SemSsaVariable var) { Specific::hasReadOfSsaVariable(this, var) }
42-
}
43-
44-
class SemSsaReadPositionPhiInputEdge extends SemSsaReadPosition {
45-
SemBasicBlock origBlock;
46-
SemBasicBlock phiBlock;
47-
48-
SemSsaReadPositionPhiInputEdge() { Specific::phiInputEdge(this, origBlock, phiBlock) }
49-
50-
predicate phiInput(SemSsaPhiNode phi, SemSsaVariable inp) { Specific::phiInput(this, phi, inp) }
51-
52-
SemBasicBlock getOrigBlock() { result = origBlock }
53-
54-
SemBasicBlock getPhiBlock() { result = phiBlock }
55-
}
56-
57-
class SemSsaReadPositionBlock extends SemSsaReadPosition {
58-
SemBasicBlock block;
59-
60-
SemSsaReadPositionBlock() { Specific::readBlock(this, block) }
61-
62-
SemBasicBlock getBlock() { result = block }
6334

64-
SemExpr getAnExpr() { result = this.getBlock().getAnExpr() }
35+
final predicate hasInputFromBlock(SemSsaVariable inp, SemBasicBlock bb) {
36+
Specific::phiInputFromBlock(this, inp, bb)
37+
}
6538
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeAnalysisImpl.qll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ module Sem implements Semantic {
7474

7575
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
7676

77+
int getBlockId1(BasicBlock bb) { result = bb.getUniqueId() }
78+
7779
class Guard = SemGuard;
7880

7981
predicate implies_v2 = semImplies_v2/4;
@@ -92,12 +94,6 @@ module Sem implements Semantic {
9294

9395
class SsaExplicitUpdate = SemSsaExplicitUpdate;
9496

95-
class SsaReadPosition = SemSsaReadPosition;
96-
97-
class SsaReadPositionPhiInputEdge = SemSsaReadPositionPhiInputEdge;
98-
99-
class SsaReadPositionBlock = SemSsaReadPositionBlock;
100-
10197
predicate conversionCannotOverflow(Type fromType, Type toType) {
10298
SemanticType::conversionCannotOverflow(fromType, toType)
10399
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeUtils.qll

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -133,33 +133,4 @@ module RangeUtil<DeltaSig D, LangSig<Sem, D> Lang> implements UtilSig<Sem, D> {
133133
or
134134
not exists(Lang::getAlternateTypeForSsaVariable(var)) and result = var.getType()
135135
}
136-
137-
import Ranking
138-
}
139-
140-
import Ranking
141-
142-
module Ranking {
143-
/**
144-
* Holds if `rix` is the number of input edges to `phi`.
145-
*/
146-
predicate maxPhiInputRank(SemSsaPhiNode phi, int rix) {
147-
rix = max(int r | rankedPhiInput(phi, _, _, r))
148-
}
149-
150-
/**
151-
* Holds if `inp` is an input to `phi` along `edge` and this input has index `r`
152-
* in an arbitrary 1-based numbering of the input edges to `phi`.
153-
*/
154-
predicate rankedPhiInput(
155-
SemSsaPhiNode phi, SemSsaVariable inp, SemSsaReadPositionPhiInputEdge edge, int r
156-
) {
157-
edge.phiInput(phi, inp) and
158-
edge =
159-
rank[r](SemSsaReadPositionPhiInputEdge e |
160-
e.phiInput(phi, _)
161-
|
162-
e order by e.getOrigBlock().getUniqueId()
163-
)
164-
}
165136
}

0 commit comments

Comments
 (0)