Visitar URL original
C++: generate IR for global variables with initializers by rdmarsh2 · Pull Request #8515 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20260227005729/https://github.com/github/codeql/pull/8515
Skip to content

C++: generate IR for global variables with initializers#8515

Merged
MathiasVP merged 23 commits intogithub:mainfrom
rdmarsh2:rdmarsh2/ir-global-vars
Apr 26, 2022
Merged

C++: generate IR for global variables with initializers#8515
MathiasVP merged 23 commits intogithub:mainfrom
rdmarsh2:rdmarsh2/ir-global-vars

Conversation

@rdmarsh2
Copy link
Contributor

No description provided.

@rdmarsh2 rdmarsh2 requested review from a team as code owners March 21, 2022 20:04
@rdmarsh2 rdmarsh2 changed the title Rdmarsh2/ir global vars C++: generate IR for global variables with initializers Mar 21, 2022
Comment on lines 12845 to 12848
# 1674| getVariable().getInitializer(): [Initializer] initializer for (unnamed local variable)
# 1674| getExpr(): [VariableAccess] xs
# 1674| getExpr(): (no string representation)
# 1674| Type = [ArrayType] int[2]
# 1674| ValueCategory = prvalue(load)
# 1674| ValueCategory = prvalue
# 1674| getDeclarationEntry(1): [VariableDeclarationEntry] definition of x0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes imply you used an outdated extractor.

Copy link
Contributor

@MathiasVP MathiasVP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great so far! I took a brief pass over it, and I only have a single comment right now.

private class PrintConfig extends PrintIRConfiguration {
override predicate shouldPrintFunction(Function func) { shouldDumpFunction(func) }
override predicate shouldPrintFunction(Declaration decl) {
shouldDumpFunction(decl) or decl instanceof GlobalOrNamespaceVariable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a shouldDumpDeclaration predicate as well? I guess that predicate should exclude global variables with a location that satisfies locationIsInStandardHeaders (like we do for shouldDumpFunction)

@rdmarsh2 rdmarsh2 added the no-change-note-required This PR does not need a change note label Mar 22, 2022
Copy link

@dbartol dbartol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. Several comments, including a couple blocking ones.

# 30| v30_6(void) = ExitFunction :

clang.cpp:
# 3| int globalInt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that this is a global variable with no initializer? Do we also dump functions that don't have IR as well, like virtual functions with no body? It just seems odd, although not necessarily wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is. I think you're right that these can be excluded.

predicate functionHasIR(Function func) { exists(getTranslatedFunction(func)) }

cached
predicate varHasIRFunc(GlobalOrNamespaceVariable var) { any() } // TODO: restrict?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO?

Also, does this cover static data members?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't. I think it could be expanded to StaticStorageDurationVariable, but that might make the getEnclosingFunction issues worse...

init = var.getInitializer() and
not var.hasDynamicInitialization() and
expr = init.getExpr().getFullyConverted()
expr = init.getExpr().getFullyConverted() and
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important for this PR, but shouldn't we be creating a separate IR function for the initializer of a static local that really is statically initialized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. It might be worth addressing now, actually.


final override Function getFunction() { result = expr.getEnclosingFunction() }
final override Declaration getFunction() {
result = expr.getEnclosingFunction() or
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we wind up with this exact same disjunction in many places. Maybe factor out a predicate like:

Declaration getEnclosingDeclaration(Expr e) {
  result = e.getEnclosingFunction() or result = e.getEnclosingVariable().(GlobalOrNamespaceVariable)
}

?


int global_2 = 1;

const int global_3 = 2;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't create IR for anything as trivial as static const int x = 5;. Maybe only create it if the variable is every used as a glvalue? Or only if the variable is either non-const or has a non-constant initializer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what the TODO in varHasIRFunc was about. I was originally planning to include constants in the IR because that means they'll be available as dataflow sources once I add flow through global variables in a followup PR.


int global_2 = 1;

const int global_3 = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal hobby horse I guess: a test case for a structured binding would also be welcome (yes these can occur globally outside functions).

dbartol
dbartol previously approved these changes Apr 14, 2022
Copy link

@dbartol dbartol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now, modulo merge conflicts

@rdmarsh2
Copy link
Contributor Author

The new tests revealed some consistency issues. Are those known from other consistency queries or are they new?

@jketema
Copy link
Contributor

jketema commented Apr 19, 2022

The new tests revealed some consistency issues. Are those known from other consistency queries or are they new?

AFAICT running with the latest version of the extractor should fix those.

@rdmarsh2 rdmarsh2 force-pushed the rdmarsh2/ir-global-vars branch from e83f030 to f94fcf1 Compare April 19, 2022 17:32
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 1 vulnerability.

Comment on lines 7 to +9
private newtype TIRFunction =
MkIRFunction(Language::Function func) { IRConstruction::Raw::functionHasIR(func) }
TFunctionIRFunction(Language::Function func) { IRConstruction::Raw::functionHasIR(func) } or
TVarInitIRFunction(Language::GlobalVariable var) { IRConstruction::Raw::varHasIRFunc(var) }

Check warning

Code scanning / CodeQL

Acronyms should be PascalCase/camelCase.

Acronyms should be PascalCase/camelCase
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 10 vulnerabilities.

Comment on lines 7 to +9
private newtype TIRFunction =
MkIRFunction(Language::Function func) { IRConstruction::Raw::functionHasIR(func) }
TFunctionIRFunction(Language::Function func) { IRConstruction::Raw::functionHasIR(func) } or
TVarInitIRFunction(Language::GlobalVariable var) { IRConstruction::Raw::varHasIRFunc(var) }

Check warning

Code scanning / CodeQL

Acronyms should be PascalCase/camelCase.

Acronyms should be PascalCase/camelCase
}

override Instruction getTargetAddress() {
result = getInstruction(InitializerVariableAddressTag())

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
kind instanceof GotoEdge and
(
tag = EnterFunctionTag() and
result = getInstruction(AliasedDefinitionTag())

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
result = getInstruction(AliasedDefinitionTag())
or
tag = AliasedDefinitionTag() and
result = getInstruction(InitializerVariableAddressTag())

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
result = getInstruction(InitializerVariableAddressTag())
or
tag = InitializerVariableAddressTag() and
result = getChild(1).getFirstInstruction()

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
result = getChild(1).getFirstInstruction()
or
tag = ReturnTag() and
result = getInstruction(AliasedUseTag())

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
result = getInstruction(AliasedUseTag())
or
tag = AliasedUseTag() and
result = getInstruction(ExitFunctionTag())

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
}

override Instruction getChildSuccessor(TranslatedElement child) {
child = getChild(1) and

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.

override Instruction getChildSuccessor(TranslatedElement child) {
child = getChild(1) and
result = getInstruction(ReturnTag())

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
Comment on lines +949 to +951
/**
* Represents the IR translation of a root element, either a function or a global variable.
*/

Check warning

Code scanning / CodeQL

Class QLDoc style.

The QLDoc for a class should start with 'A', 'An', or 'The'.
Copy link
Contributor

@MathiasVP MathiasVP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@MathiasVP MathiasVP merged commit 800e4ea into github:main Apr 26, 2022
MathiasVP added a commit to MathiasVP/ql that referenced this pull request Apr 27, 2022
…al-vars"

This reverts commit 800e4ea, reversing
changes made to 7ce040f.
jketema added a commit to jketema/codeql that referenced this pull request Apr 27, 2022
…al-vars"

This reverts commit 800e4ea, reversing
changes made to 7ce040f.
MathiasVP added a commit that referenced this pull request Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C# C++ no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants