Skip to content

Conversation

@jbj
Copy link
Contributor

@jbj jbj commented Jan 24, 2020

This PR changes the structure of the IR translation in order to improve the mapping between Expr and Instruction. Previously, the result instruction of e in e++ was the address corresponding to the lvalue of e. With this PR, the result instruction of e is instead the instruction that loads from e. We use the result instruction to map results from IR-based analyses back to their AST elements. I expect this PR to have the following effects on such libraries:

  1. Data flow. In x = source; x++;, there will now be flow to the x in x++. This fixes the problem observed by @MathiasVP on ArithmeticWithExtremeValues.ql. That query has at least one more problem, which I'll address in a separate PR. The fix can be observed on flow0.expected.
  2. Range analysis. If we had a wrapper to translate the range analysis back to Expr, then in x = 0; x++;, the x in x++ should now be considered equal to 0. Previously it would instead be considered equal to the stack address of the x variable. The same effect can be observed in this PR on SignAnalysis.expected.
  3. Value numbering. We have an open PR (C++: Make AST GVN a wrapper for IR-based GVN #1542) to map the value numbering back to AST. When that's merged, in f(x); f(x++);, the two VariableAccesses to x should now get the same value number. Previously, the value number of the second load from x was not the result of any Expr, so the x in x++ would instead share its value number with any occurrences of &x.

I think it can be argued that it's technically correct to associate the e in e++ with the lvalue (address) of e rather than the loaded value. That's how it'll still be for overloaded e++, but a crucial difference is that an overloaded e++ has an associated read-side-effect instruction on which the value should appear. There was and is no such thing for the non-overloaded e++.

Everything I wrote above about e++ also applies to --e, e <<= ..., etc.

I recommend reading each commit individually.

jbj and others added 4 commits January 23, 2020 17:42
Previously, the `Load` would be associated with the `CrementOperation`
rather than its operand, which gave surprising results when mapping
taint sinks back to `Expr`.

The changes in `raw_ir.expected` are to add `Copy` operations on the
`x++` in code like `y = x++`. This is now needed because the result that
`x++` would otherwise have (the Load) no longer belongs to the `++`
expression. Copies are inserted to ensure that all expressions are
associated with an `Instruction` result.

The changes in `*aliased_ssa_ir.expected` appear to be just wobble.
This commit undoes the code sharing between `TranslatedAssignExpr` (`=`)
and `TranslatedAssignOperation` (`+=`, `<<=`, ...). In the next commit,
when we change how the `Load` works on the LHS of
`TranslatedAssignOperation`, these classes will become so different that
sharing is no longer helpful.
This is analogous to what was done for `CrementOperation`.
@jbj jbj added the C++ label Jan 24, 2020
@jbj jbj requested a review from a team as a code owner January 24, 2020 08:41
@jbj jbj requested a review from dbartol January 24, 2020 08:54
jbj added a commit to jbj/ql that referenced this pull request Jan 27, 2020
The issue for that test is being tested and fixed on PR github#2686. Adding a
test here will cause a semantic merge conflict.
jbj and others added 2 commits January 29, 2020 15:56
Update test output to fix semantic merge conflict.
Conflicts:
	cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll
dbartol
dbartol previously approved these changes Feb 5, 2020
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
I think this was a really clean way of handling these constructs.

Conflicts:
	cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected
	cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected
Conflicts:
	cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected
	cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected
	cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected
	cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir_unsound.expected
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 19e1d82 into github:master Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants