Skip to content

Commit 3a52cd1

Browse files
committed
Merge branch 'am0o0-python-codeExec' of https://github.com/amammad/codeql into am0o0-python-codeExec
2 parents 9b4ea88 + 0043d93 commit 3a52cd1

File tree

220 files changed

+11043
-9106
lines changed

Some content is hidden

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

220 files changed

+11043
-9106
lines changed

.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ build:linux --cxxopt=-std=c++20
1414
build:macos --cxxopt=-std=c++20 --cpu=darwin_x86_64
1515
build:windows --cxxopt=/std:c++20 --cxxopt=/Zc:preprocessor
1616

17+
# this requires developer mode, but is required to have pack installer functioning
18+
startup --windows_enable_symlinks
19+
common --enable_runfiles
20+
1721
common --registry=file:///%workspace%/misc/bazel/registry
1822
common --registry=https://bcr.bazel.build
1923

.github/workflows/buildifier.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ jobs:
2424
extra_args: >
2525
buildifier --all-files 2>&1 ||
2626
(
27-
echo -e "In order to format all bazel files, please run:\n bazel run //:buildifier"; exit 1
27+
echo -e "In order to format all bazel files, please run:\n bazel run //misc/bazel:buildifier"; exit 1
2828
)

.pre-commit-config.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ repos:
2626
name: Format bazel files
2727
files: \.(bazel|bzl)
2828
language: system
29-
entry: bazel run //:buildifier
29+
entry: bazel run //misc/bazel:buildifier
30+
pass_filenames: false
31+
32+
- id: go-gen
33+
name: Check checked in generated files in go
34+
files: ^go/.*
35+
language: system
36+
entry: bazel run //go:gen
3037
pass_filenames: false
3138

3239
- id: codeql-format

BUILD.bazel

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +0,0 @@
1-
load("@buildifier_prebuilt//:rules.bzl", "buildifier")
2-
3-
buildifier(
4-
name = "buildifier",
5-
exclude_patterns = [
6-
"./.git/*",
7-
],
8-
lint_mode = "fix",
9-
)

CONTRIBUTING.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ We welcome contributions to our CodeQL libraries and queries. Got an idea for a
44

55
There is lots of useful documentation to help you write queries, ranging from information about query file structure to tutorials for specific target languages. For more information on the documentation available, see [CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/codeql-queries) on [codeql.github.com](https://codeql.github.com).
66

7+
Note that the CodeQL for Visual Studio Code documentation has been migrated to https://docs.github.com/en/code-security/codeql-for-vs-code/, but you can still contribute to it via a different repository. For more information, see [Contributing to GitHub Docs documentation](https://docs.github.com/en/contributing)."
8+
79
## Change notes
810

911
Any nontrivial user-visible change to a query pack or library pack should have a change note. For details on how to add a change note for your change, see [this guide](docs/change-notes.md).
@@ -43,7 +45,7 @@ If you have an idea for a query that you would like to share with other CodeQL u
4345

4446
3. **Formatting**
4547

46-
- The queries and libraries must be autoformatted, for example using the "Format Document" command in [CodeQL for Visual Studio Code](https://codeql.github.com/docs/codeql-for-visual-studio-code/about-codeql-for-visual-studio-code).
48+
- The queries and libraries must be autoformatted, for example using the "Format Document" command in [CodeQL for Visual Studio Code](https://docs.github.com/en/code-security/codeql-for-vs-code/).
4749

4850
If you prefer, you can either:
4951
1. install the [pre-commit framework](https://pre-commit.com/) and install the configured hooks on this repo via `pre-commit install`, or

MODULE.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ local_path_override(
1313

1414
# see https://registry.bazel.build/ for a list of available packages
1515

16-
bazel_dep(name = "platforms", version = "0.0.8")
16+
bazel_dep(name = "platforms", version = "0.0.9")
17+
bazel_dep(name = "rules_go", version = "0.47.0")
1718
bazel_dep(name = "rules_pkg", version = "0.10.1")
1819
bazel_dep(name = "rules_nodejs", version = "6.0.3")
1920
bazel_dep(name = "rules_python", version = "0.31.0")
2021
bazel_dep(name = "bazel_skylib", version = "1.5.0")
2122
bazel_dep(name = "abseil-cpp", version = "20240116.0", repo_name = "absl")
2223
bazel_dep(name = "nlohmann_json", version = "3.11.3", repo_name = "json")
2324
bazel_dep(name = "fmt", version = "10.0.0")
25+
bazel_dep(name = "gazelle", version = "0.36.0")
2426

2527
bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)
2628

@@ -52,6 +54,9 @@ node.toolchain(
5254
)
5355
use_repo(node, "nodejs", "nodejs_toolchains")
5456

57+
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
58+
go_sdk.download(version = "1.22.2")
59+
5560
register_toolchains(
5661
"@nodejs_toolchains//:all",
5762
)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This open source repository contains the standard CodeQL libraries and queries t
44

55
## How do I learn CodeQL and run queries?
66

7-
There is [extensive documentation](https://codeql.github.com/docs/) on getting started with writing CodeQL using the [CodeQL extension for Visual Studio Code](https://codeql.github.com/docs/codeql-for-visual-studio-code/) and the [CodeQL CLI](https://codeql.github.com/docs/codeql-cli/).
7+
There is extensive documentation about the [CodeQL language](https://codeql.github.com/docs/), writing CodeQL using the [CodeQL extension for Visual Studio Code](https://docs.github.com/en/code-security/codeql-for-vs-code/) and using the [CodeQL CLI](https://docs.github.com/en/code-security/codeql-cli).
88

99
## Contributing
1010

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import semmle.code.cpp.models.interfaces.PartialFlow as PartialFlow
99
private import semmle.code.cpp.models.interfaces.FunctionInputsAndOutputs as FIO
1010
private import semmle.code.cpp.ir.internal.IRCppLanguage
1111
private import semmle.code.cpp.ir.dataflow.internal.ModelUtil
12+
private import semmle.code.cpp.ir.implementation.raw.internal.TranslatedInitialization
1213
private import DataFlowPrivate
1314
import SsaInternalsCommon
1415

@@ -104,8 +105,8 @@ predicate hasRawIndirectInstruction(Instruction instr, int indirectionIndex) {
104105
cached
105106
private newtype TDefImpl =
106107
TDefAddressImpl(BaseIRVariable v) or
107-
TDirectDefImpl(BaseSourceVariableInstruction base, Operand address, int indirectionIndex) {
108-
isDef(_, _, address, base, _, indirectionIndex)
108+
TDirectDefImpl(Operand address, int indirectionIndex) {
109+
isDef(_, _, address, _, _, indirectionIndex)
109110
} or
110111
TGlobalDefImpl(GlobalLikeVariable v, IRFunction f, int indirectionIndex) {
111112
// Represents the initial "definition" of a global variable when entering
@@ -115,8 +116,8 @@ private newtype TDefImpl =
115116

116117
cached
117118
private newtype TUseImpl =
118-
TDirectUseImpl(BaseSourceVariableInstruction base, Operand operand, int indirectionIndex) {
119-
isUse(_, operand, base, _, indirectionIndex) and
119+
TDirectUseImpl(Operand operand, int indirectionIndex) {
120+
isUse(_, operand, _, _, indirectionIndex) and
120121
not isDef(true, _, operand, _, _, _)
121122
} or
122123
TGlobalUse(GlobalLikeVariable v, IRFunction f, int indirectionIndex) {
@@ -210,19 +211,11 @@ abstract class DefImpl extends TDefImpl {
210211
*/
211212
abstract int getIndirection();
212213

213-
/**
214-
* Gets the instruction that computes the base of this definition or use.
215-
* This is always a `VariableAddressInstruction` or an `CallInstruction`.
216-
*/
217-
abstract BaseSourceVariableInstruction getBase();
218-
219214
/**
220215
* Gets the base source variable (i.e., the variable without
221216
* any indirection) of this definition or use.
222217
*/
223-
final BaseSourceVariable getBaseSourceVariable() {
224-
this.getBase().getBaseSourceVariable() = result
225-
}
218+
abstract BaseSourceVariable getBaseSourceVariable();
226219

227220
/** Gets the variable that is defined or used. */
228221
SourceVariable getSourceVariable() {
@@ -282,19 +275,11 @@ abstract class UseImpl extends TUseImpl {
282275
/** Gets the indirection index of this use. */
283276
final int getIndirectionIndex() { result = indirectionIndex }
284277

285-
/**
286-
* Gets the instruction that computes the base of this definition or use.
287-
* This is always a `VariableAddressInstruction` or an `CallInstruction`.
288-
*/
289-
abstract BaseSourceVariableInstruction getBase();
290-
291278
/**
292279
* Gets the base source variable (i.e., the variable without
293280
* any indirection) of this definition or use.
294281
*/
295-
final BaseSourceVariable getBaseSourceVariable() {
296-
this.getBase().getBaseSourceVariable() = result
297-
}
282+
abstract BaseSourceVariable getBaseSourceVariable();
298283

299284
/** Gets the variable that is defined or used. */
300285
SourceVariable getSourceVariable() {
@@ -329,6 +314,17 @@ private predicate sourceVariableHasBaseAndIndex(SourceVariable v, BaseSourceVari
329314
v.getIndirection() = ind
330315
}
331316

317+
/**
318+
* Gets the instruction that computes the address that's used to
319+
* initialize `v`.
320+
*/
321+
private Instruction getInitializationTargetAddress(IRVariable v) {
322+
exists(TranslatedVariableInitialization init |
323+
init.getIRVariable() = v and
324+
result = init.getTargetAddress()
325+
)
326+
}
327+
332328
/** An initial definition of an `IRVariable`'s address. */
333329
private class DefAddressImpl extends DefImpl, TDefAddressImpl {
334330
BaseIRVariable v;
@@ -347,8 +343,15 @@ private class DefAddressImpl extends DefImpl, TDefAddressImpl {
347343
final override Node0Impl getValue() { none() }
348344

349345
final override predicate hasIndexInBlock(IRBlock block, int index) {
350-
block = v.getIRVariable().getEnclosingIRFunction().getEntryBlock() and
351-
index = 0
346+
exists(IRVariable var | var = v.getIRVariable() |
347+
block.getInstruction(index) = getInitializationTargetAddress(var)
348+
or
349+
// If there is no translatated element that does initialization of the
350+
// variable we place the SSA definition at the entry block of the function.
351+
not exists(getInitializationTargetAddress(var)) and
352+
block = var.getEnclosingIRFunction().getEntryBlock() and
353+
index = 0
354+
)
352355
}
353356

354357
override Cpp::Location getLocation() { result = v.getIRVariable().getLocation() }
@@ -358,14 +361,13 @@ private class DefAddressImpl extends DefImpl, TDefAddressImpl {
358361
result.getIndirection() = 0
359362
}
360363

361-
final override BaseSourceVariableInstruction getBase() { none() }
364+
final override BaseSourceVariable getBaseSourceVariable() { result = v }
362365
}
363366

364367
private class DirectDef extends DefImpl, TDirectDefImpl {
365368
Operand address;
366-
BaseSourceVariableInstruction base;
367369

368-
DirectDef() { this = TDirectDefImpl(base, address, indirectionIndex) }
370+
DirectDef() { this = TDirectDefImpl(address, indirectionIndex) }
369371

370372
override Cpp::Location getLocation() { result = this.getAddressOperand().getUse().getLocation() }
371373

@@ -377,30 +379,36 @@ private class DirectDef extends DefImpl, TDirectDefImpl {
377379

378380
override Operand getAddressOperand() { result = address }
379381

380-
override BaseSourceVariableInstruction getBase() { result = base }
382+
private BaseSourceVariableInstruction getBase() {
383+
isDef(_, _, address, result, _, indirectionIndex)
384+
}
385+
386+
override BaseSourceVariable getBaseSourceVariable() {
387+
result = this.getBase().getBaseSourceVariable()
388+
}
381389

382-
override int getIndirection() { isDef(_, _, address, base, result, indirectionIndex) }
390+
override int getIndirection() { isDef(_, _, address, _, result, indirectionIndex) }
383391

384-
override Node0Impl getValue() { isDef(_, result, address, base, _, _) }
392+
override Node0Impl getValue() { isDef(_, result, address, _, _, _) }
385393

386-
override predicate isCertain() { isDef(true, _, address, base, _, indirectionIndex) }
394+
override predicate isCertain() { isDef(true, _, address, _, _, indirectionIndex) }
387395
}
388396

389397
private class DirectUseImpl extends UseImpl, TDirectUseImpl {
390398
Operand operand;
391-
BaseSourceVariableInstruction base;
392399

393-
DirectUseImpl() { this = TDirectUseImpl(base, operand, indirectionIndex) }
400+
DirectUseImpl() { this = TDirectUseImpl(operand, indirectionIndex) }
394401

395402
override string toString() { result = "Use of " + this.getSourceVariable() }
396403

397404
final override predicate hasIndexInBlock(IRBlock block, int index) {
398405
// See the comment in `ssa0`'s `OperandBasedUse` for an explanation of this
399406
// predicate's implementation.
400-
if base.getAst() = any(Cpp::PostfixCrementOperation c).getOperand()
407+
if this.getBase().getAst() = any(Cpp::PostfixCrementOperation c).getOperand()
401408
then
402-
exists(Operand op, int indirection |
409+
exists(Operand op, int indirection, Instruction base |
403410
indirection = this.getIndirection() and
411+
base = this.getBase() and
404412
op =
405413
min(Operand cand, int i |
406414
isUse(_, cand, base, indirection, indirectionIndex) and
@@ -413,15 +421,19 @@ private class DirectUseImpl extends UseImpl, TDirectUseImpl {
413421
else operand.getUse() = block.getInstruction(index)
414422
}
415423

416-
final override BaseSourceVariableInstruction getBase() { result = base }
424+
private BaseSourceVariableInstruction getBase() { isUse(_, operand, result, _, indirectionIndex) }
425+
426+
override BaseSourceVariable getBaseSourceVariable() {
427+
result = this.getBase().getBaseSourceVariable()
428+
}
417429

418430
final Operand getOperand() { result = operand }
419431

420432
final override Cpp::Location getLocation() { result = operand.getLocation() }
421433

422-
override int getIndirection() { isUse(_, operand, base, result, indirectionIndex) }
434+
override int getIndirection() { isUse(_, operand, _, result, indirectionIndex) }
423435

424-
override predicate isCertain() { isUse(true, operand, base, _, indirectionIndex) }
436+
override predicate isCertain() { isUse(true, operand, _, _, indirectionIndex) }
425437

426438
override Node getNode() { nodeHasOperand(result, operand, indirectionIndex) }
427439
}
@@ -480,13 +492,7 @@ class FinalParameterUse extends UseImpl, TFinalParameterUse {
480492
result instanceof UnknownDefaultLocation
481493
}
482494

483-
override BaseSourceVariableInstruction getBase() {
484-
exists(InitializeParameterInstruction init |
485-
init.getParameter() = p and
486-
// This is always a `VariableAddressInstruction`
487-
result = init.getAnOperand().getDef()
488-
)
489-
}
495+
override BaseIRVariable getBaseSourceVariable() { result.getIRVariable().getAst() = p }
490496
}
491497

492498
/**
@@ -572,8 +578,8 @@ class GlobalUse extends UseImpl, TGlobalUse {
572578
)
573579
}
574580

575-
override SourceVariable getSourceVariable() {
576-
sourceVariableIsGlobal(result, global, f, this.getIndirection())
581+
override BaseSourceVariable getBaseSourceVariable() {
582+
baseSourceVariableIsGlobal(result, global, f)
577583
}
578584

579585
final override Cpp::Location getLocation() { result = f.getLocation() }
@@ -590,8 +596,6 @@ class GlobalUse extends UseImpl, TGlobalUse {
590596
Type getUnderlyingType() { result = global.getUnderlyingType() }
591597

592598
override predicate isCertain() { any() }
593-
594-
override BaseSourceVariableInstruction getBase() { none() }
595599
}
596600

597601
/**
@@ -621,8 +625,8 @@ class GlobalDefImpl extends DefImpl, TGlobalDefImpl {
621625
}
622626

623627
/** Gets the global variable associated with this definition. */
624-
override SourceVariable getSourceVariable() {
625-
sourceVariableIsGlobal(result, global, f, this.getIndirection())
628+
override BaseSourceVariable getBaseSourceVariable() {
629+
baseSourceVariableIsGlobal(result, global, f)
626630
}
627631

628632
override int getIndirection() { result = indirectionIndex }
@@ -645,8 +649,6 @@ class GlobalDefImpl extends DefImpl, TGlobalDefImpl {
645649
override string toString() { result = "Def of " + this.getSourceVariable() }
646650

647651
override Location getLocation() { result = f.getLocation() }
648-
649-
override BaseSourceVariableInstruction getBase() { none() }
650652
}
651653

652654
/**
@@ -959,11 +961,10 @@ predicate fromPhiNode(SsaPhiNode nodeFrom, Node nodeTo) {
959961
)
960962
}
961963

962-
private predicate sourceVariableIsGlobal(
963-
SourceVariable sv, GlobalLikeVariable global, IRFunction func, int indirectionIndex
964+
private predicate baseSourceVariableIsGlobal(
965+
BaseIRVariable base, GlobalLikeVariable global, IRFunction func
964966
) {
965-
exists(IRVariable irVar, BaseIRVariable base |
966-
sourceVariableHasBaseAndIndex(sv, base, indirectionIndex) and
967+
exists(IRVariable irVar |
967968
irVar = base.getIRVariable() and
968969
irVar.getEnclosingIRFunction() = func and
969970
global = irVar.getAst() and

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,12 @@ newtype TTranslatedElement =
830830
not ignoreExpr(dc)
831831
)
832832
} or
833+
// The set of destructors to invoke after a handler for a `try` statement. These
834+
// need to be special cased because the destructors need to run following an
835+
// `ExceptionEdge`, but not following a `GotoEdge` edge.
836+
TTranslatedDestructorsAfterHandler(Handler handler) {
837+
exists(handler.getAnImplicitDestructorCall())
838+
} or
833839
// A precise side effect of an argument to a `Call`
834840
TTranslatedArgumentExprSideEffect(Call call, Expr expr, int n, SideEffectOpcode opcode) {
835841
not ignoreExpr(expr) and

0 commit comments

Comments
 (0)