Skip to content

Delete dead code #8431

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 5 commits into from
Mar 15, 2022
Merged

Delete dead code #8431

merged 5 commits into from
Mar 15, 2022

Conversation

erik-krogh
Copy link
Contributor

@erik-krogh erik-krogh commented Mar 14, 2022

Deletes all dead code.
As found by a QL-for-QL query that attempts to overapproximate what code is alive.

Code is alive if:

  • It can affect a query result (the from-where-select or a query predicate imported by a .ql file).
  • It is part of a public API that can be imported from another .qll file.
  • Can affect or be referenced by any of the above (recursively).
  • (Other edge cases, see DeadCodeQuery.qll).

Any code not marked as alive is dead, and is deleted by this PR.
No test or external code should be affected by this PR.

This PR is a followup to #8347, because I wanted to check if I had introduced dead code by deleting deprecations.
(I had not, the code was dead already).


There were some benign cases.
E.g. in the shared DataFlow library a lot of code is copy-pasted between stages, and not all the predicates are used in all the stages.


The dead code wasn't marked as unused by the CodeQL compiler for various reasons.

  • Recursive predicates that depend on themself. (Or mutually recursive predicates).
  • Public classes/predicates in private modules.
  • Public classes in .ql files that aren't used.

The QL-for-QL code also contains predicate for detecting "unqueryable" code.
That is: code that cannot affect any query result (but it might be part of the public API).
I already found a bunch of interesting results using that code, but a QL-for-QL query needs more work to weed out benign results.

smowton
smowton previously approved these changes Mar 14, 2022
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

👍 Java

@erik-krogh erik-krogh marked this pull request as ready for review March 14, 2022 13:50
@erik-krogh erik-krogh requested review from a team as code owners March 14, 2022 13:50
@erik-krogh erik-krogh added the no-change-note-required This PR does not need a change note label Mar 14, 2022
esbena
esbena previously approved these changes Mar 14, 2022
Copy link
Contributor

@esbena esbena left a comment

Choose a reason for hiding this comment

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

JS 👍

@intrigus-lgtm
Copy link
Contributor

Regarding this part of the PR:
https://github.com/github/codeql/pull/8431/files#diff-ae052bd5f05c791a2d3f7fbb99ab3a5a7fadac51704cceefef602db27a00686c

Why is ConcreteSensitiveInsecureUrl dead code?

// Materialize flow labels
private class ConcreteSensitiveInsecureUrl extends Label::SensitiveInsecureUrl {
ConcreteSensitiveInsecureUrl() { this = this }
}

A search for it only shows two results:
https://cs.github.com/github/codeql?q=%22ConcreteSensitiveInsecureUrl%22

But for ConcretePosixPath there are also only two results in the code search!

// Materialize flow labels
private class ConcretePosixPath extends TaintedPath::Label::PosixPath {
ConcretePosixPath() { this = this }
}

https://cs.github.com/github/codeql?q=%22ConcretePosixPath%22

Why is the first considered dead code, while the last is not? What am I missing?

@erik-krogh erik-krogh dismissed stale reviews from esbena and smowton via c7509c4 March 15, 2022 08:19
@erik-krogh
Copy link
Contributor Author

erik-krogh commented Mar 15, 2022

Why is the first considered dead code, while the last is not? What am I missing?

The first extends a non-abstract class, the second extends an abstract class.
The documentation has more information about abstract classes.
Relevant snippet: an abstract class is defined as the union of its subclasses.

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

C#/SsaImplCommon.qll 👍

@@ -189,12 +189,6 @@ module IO {
}
}

// "Direct" `IO` instances, i.e. cases where there is no more specific
// subtype such as `File`
private class IOInstanceStrict extends IOInstance {
Copy link
Contributor

Choose a reason for hiding this comment

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

@alexrford you added this class originally. Is its deadness unintentional?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I should have removed this as part of the refactoring in #8138

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, cool. Just checking we're not deleting code that we want and is only dead because of a bug.

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.

C++ and QL 👍.

Copy link
Contributor

@alexrford alexrford left a comment

Choose a reason for hiding this comment

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

👍 Ruby

* Holds if the SSA definition of `v` at `def` reaches uncertain SSA definition
* `redef` in the same basic block, without crossing another SSA definition of `v`.
*/
predicate ssaDefReachesUncertainDefWithinBlock(
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this change preserve identical files?

Copy link
Contributor

Choose a reason for hiding this comment

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

It does because all the identical files were modified :)

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, the code is dead in all copies.

Copy link
Contributor

@geoffw0 geoffw0 Mar 15, 2022

Choose a reason for hiding this comment

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

... ah, and it looks like there's a PR check and it passed. :)

@erik-krogh erik-krogh merged commit b45f56a into github:main Mar 15, 2022
@RasmusWL
Copy link
Member

Python 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants