Skip to content

C#: SSA refactorings #4923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 14, 2021
Merged

C#: SSA refactorings #4923

merged 4 commits into from
Jan 14, 2021

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Jan 7, 2021

SSA refactorings in preparation for a shared SSA library implementation.

The first commit removes the class ImplicitUntrackedDefinition, which was used to generate SSA definitions for variables not amenable to SSA analysis. The class was never actually used.

The second commit unifies how SSA definitions are constructed, by collapsing the existing TSsaExplicitDef, TSsaImplicitEntryDef, TSsaImplicitCallDef, and TSsaImplicitQualifierDef IPA injectors into a single TWriteDef injector. This has the benefit that only variableWrite() is used to construct SSA definitions (a predicate similar to that will be part of the API for a shared implementation), and a common liveness analysis is applied in all cases (though in some corner cases this is less precise).

Liveness of writes to captured variables, where reads happen inside capturing callables, is ensured by adding a new (pseudo) read of kind CapturedVarCallRead at calls that may reach a read. This means that the common liveness analysis can be applied here as well. For example, in

int M()
{
   var captured = 0;
   int Inner() => captured + 1;
   return Inner();
}

a pseudo read of captured is added at the call to Inner, which makes the write captured = 0 live.

https://jenkins.internal.semmle.com/job/Changes/job/CSharp-Differences/751/

@github-actions github-actions bot added the C# label Jan 7, 2021
@hvitved hvitved force-pushed the csharp/ssa/refactor branch from 0c641dc to 63f76b1 Compare January 7, 2021 14:17
@hvitved hvitved requested a review from a team as a code owner January 12, 2021 12:42
@hvitved hvitved marked this pull request as ready for review January 12, 2021 12:42
Copy link
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

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

Found some minor typos, and added a couple of questions.

Comment on lines +190 to +192
implicitEntryDefinition(bb, v) and
i = -1 and
certain = true
Copy link
Contributor

Choose a reason for hiding this comment

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

implicitEntryDefinition covers captured variables, but the writes of captured variables might not be actually executed. So wouldn't it be more accurate to set certain to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't matter, actually, as there cannot be any prior definitions.

Comment on lines +293 to +294
lsv.isRef() and
strictcount(v.getAnAccess()) > 1
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this case is for ref locals. Why do we need strictcount(v.getAnAccess()) > 1?

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 for ref locals. The check is there to avoid SSA definitions for cases like this: https://github.com/github/codeql/blob/main/csharp/ql/test/library-tests/dataflow/ssa/Consistency.cs#L55. Before the refactoring, this was taken care of by https://github.com/github/codeql/pull/4923/files#diff-7a5016d3998d3aa9a71ee689d871333e9bffa227aae862f5d5183a131fdc4f40L1800-L1810, but since we are now using a common liveness analysis, I applied this (less precise) check instead.

Copy link
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

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

LGTM

@hvitved hvitved merged commit d7ca065 into github:main Jan 14, 2021
@hvitved hvitved deleted the csharp/ssa/refactor branch January 14, 2021 16:28
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.

2 participants