Skip to content
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

Rust: Basic control flow graph setup #17415

Merged
merged 4 commits into from
Sep 11, 2024

Conversation

paldepind
Copy link
Contributor

This is an attempt to add the basic setup/boilerplate needed to construct a CFG.

The PR is based on looking at how the CFG library is used for Swift, Kaleidoscope, Ruby, and C#. I've mixed and matched a bit form these, but mostly taken stuff from Swift (because it also uses codegen) and Kaleidoscope (because it's the simplest setup). I've erred on the side of simplicity to avoid adding stuff that we don't need (right away at least).

@paldepind paldepind marked this pull request as draft September 9, 2024 15:47
@paldepind paldepind added the no-change-note-required This PR does not need a change note label Sep 10, 2024
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.

Looks great! A few (mostly minor) comments.

* A basic block, that is, a maximal straight-line sequence of control flow nodes
* without branches or joins.
*/
class BasicBlock extends TBasicBlockStart {
Copy link
Contributor

Choose a reason for hiding this comment

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

final

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BasicBlock is extended further down in the file so I did

final class BasicBlock = BasicBlockImpl;

private class BasicBlockImpl extends TBasicBlockStart { ... }

similar to what you suggested for SuccessorType.

rust/ql/lib/codeql/rust/controlflow/internal/Scope.qll Outdated Show resolved Hide resolved
rust/ql/lib/codeql/rust/controlflow/ControlFlowGraph.qll Outdated Show resolved Hide resolved
private import internal.Completion
private import internal.SuccessorType
private import codeql.rust.controlflow.BasicBlocks
import internal.Scope
Copy link
Contributor

Choose a reason for hiding this comment

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

This exposes the abstract class CfgScope, which users could then in principle extend. So I think it would be better to do

private import internal.Scope as Scope

final class CfgScope = Scope::CfgScope;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've done as suggested. But I wonder if it would make sense to do it inside Scope.qll instead?

rust/ql/test/library-tests/controlflow/CFG.ql Outdated Show resolved Hide resolved
@paldepind paldepind force-pushed the rust-control-flow-graph branch from e1f83de to 6d972be Compare September 11, 2024 07:09
@github-actions github-actions bot added the Rust Pull requests that update Rust code label Sep 11, 2024
@paldepind paldepind force-pushed the rust-control-flow-graph branch from 1126dc4 to 857edb7 Compare September 11, 2024 09:19
@paldepind
Copy link
Contributor Author

I've addressed the comments 👍

I think we have the basic foundation for the CFG now. Looking at the result of the test we are at the very least creating some nodes and edges :) My guess it that they will still require some tweaking to be entirely right though.

@paldepind paldepind marked this pull request as ready for review September 11, 2024 10:00
@paldepind paldepind merged commit fea6017 into github:rust-experiment Sep 11, 2024
4 checks passed
@paldepind paldepind deleted the rust-control-flow-graph branch September 11, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants