Skip to content

Conversation

@dbartol
Copy link

@dbartol dbartol commented Sep 3, 2021

This PR fixes the order in which side effect instructions are emitted for calls such that all read-only side effects always appear before any write side effect. Previously, for historical implementation reasons, we always emitted the CallSideEffect instruction immediately after the Call instruction, even if there were specific argument read side effects that might read from memory that overlaps with the memory written by CallSideEffect.

The approach is to reuse the implementation we already were already using for argument indirection side effects, where a single placeholder TranslatedSideEffects object has a sequence of TranslatedSideEffect objects as children. All four kinds of side effect (argument indirection, constructor call qualifier, conservative CallSideEffect instruction, and dynamic memory allocation) now just insert themselves into that sequence of TranslatedSideEffects, sorted in an order that ensures that all read side effects happen before any write side effects.

I suggest reviewing this PR commit-by-commit. The first couple of commits do a bit of refactoring with zero IR diffs to prepare for the main changes. The next couple commits move the existing side effect implementations into the new common implementation. Only one of the commits actually changes the order of the side effects in the IR.

Fixes https://github.com/github/codeql-c-team/issues/596

…Call`.

This is the first step to fixing the order of side effects on call instructions. The goal is to move all side effects (argument side effects, allocation side effects, and conservative call side effects) to be treated as elements in a single sequence of side effects, which will then be handled in a single place similar to how we already handle argument side effects.
@dbartol dbartol added C++ no-change-note-required This PR does not need a change note labels Sep 3, 2021
Dave Bartolomeo added 3 commits September 3, 2021 11:31
This is step two of fixing the ordering of call side effects. This commit refactors the existing `TranslatedSideEffect` class into an abstract `TranslatedSideEffect` class, which contains functionality common to all kinds of side effect, and a concrete `TranslatedArgumentSideEffect` class, which is the implementation of argument side effects. A future commit will add additional concrete classes for conservative call side effects and allocation side effects.

This change has zero diffs to the generated IR.
…rguments

This commit is yet another step to fixing the order of IR side effect instructions. Instead of having a special `StructorCallSideEffects` class for the call itself, I've introduced a `TranslatedStructorCallQualifierSideEffect` class that shares a bunch of common code with `TranslatedArgumentExprSideEffect`, but handles the case where there's no `Expr` for the qualifier of the constructor call. Because this class uses the same ordering as regular argument side effects, these side effects now appear in the correct order, reads before writes.

The test expectations have changed to reflect the new, correct order.
…other side effects during IR generation

This commit moves the IR generation for the `CallSideEffect` and `InitializeDynamicAllocation` side effect instruction into their own subclasses of `TranslatedSideEffect`. Previously, they were embeddded in `TranslatedCall` and `TranslatedAllocationSideEffects`. There are no diffs in the generated IR. This just makes the implementation of all side effect generation be consistent.
@dbartol dbartol marked this pull request as ready for review September 7, 2021 20:00
@dbartol dbartol requested a review from a team as a code owner September 7, 2021 20:00
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.

Only took a quick look at the changes so far. I'll continue tomorrow!

@MathiasVP
Copy link
Contributor

The changes LGTM! I'll wait for:

  1. @rdmarsh2 to take a look at the changes
  2. DCA to stop spinning on your Windows machine so that you can test it

Does this PR also fix https://github.com/github/codeql-c-team/issues/596?

@dbartol
Copy link
Author

dbartol commented Sep 8, 2021

Does this PR also fix github/codeql-c-team#596?

It does, but I couldn't remember if we were OK with linking to private issues from public repos. I guess the cat's out of the bag now, though, so I'll make it official.

rdmarsh2
rdmarsh2 previously approved these changes Sep 8, 2021
Copy link
Contributor

@rdmarsh2 rdmarsh2 left a comment

Choose a reason for hiding this comment

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

One minor point for readability, but I'm happy with merging as-is

* call in a `new` or `new[]` expression.
*/
class TranslatedCallSideEffects extends TranslatedSideEffects, TTranslatedCallSideEffects {
Call expr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be CallOrAllocation expr;?

@jbj
Copy link
Contributor

jbj commented Oct 4, 2021

@dbartol I see two requests for you here:

  1. Mathias asking for a DCA run.
  2. Robert asking if a type could be tighter.

@MathiasVP
Copy link
Contributor

We should make sure this PR doesn't fall between the cracks.

@MathiasVP MathiasVP assigned dbartol and unassigned dbartol Jan 10, 2022
@dbartol dbartol self-assigned this Jan 10, 2022
@dbartol
Copy link
Author

dbartol commented Jan 25, 2022

@MathiasVP @rdmarsh2 DCA experiment is done: https://github.com/github/codeql-dca-main/tree/data/dbartol/pr-6601-9183a4d__Differences__code-scanning/reports

A couple of non-trivial analysis time increases, in ChakraCore and putty, but no analysis diffs.

@dbartol
Copy link
Author

dbartol commented Jan 26, 2022

@MathiasVP @rdmarsh2 Are the two slowdowns significant enough to worry about?

@MathiasVP
Copy link
Contributor

@MathiasVP @rdmarsh2 Are the two slowdowns significant enough to worry about?

Probably not. If you don't see anything obvious in the tuple logs I think we should just merge it as-is.

FWIW, the (3, 3) notation on https://github.com/github/codeql-dca-main/blob/data/dbartol/pr-6601-9183a4d__Differences__code-scanning/reports/scan-timings/log:execute-queries-time.txt for microsoft__ChakraCore (resp. dsp-testing__putty) tells us that it ran the analysis 3 times (because there was a timing difference of more than 5%), and came to an average analysis time increase of ~39 (resp. 18) seconds across those three runs.

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! Feel free to merge if you like the tuple counts you're seeing.

@rdmarsh2
Copy link
Contributor

Those runs have bigger relative slowdowns in database build time than analysis time, which makes me suspect it's a machine issue, although it's weird that it's that consistent.

@dbartol dbartol merged commit d069d91 into github:main Jan 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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