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

[php2cpg] Php type recovery #3723

Merged
merged 35 commits into from
Nov 15, 2023
Merged

Conversation

wunused
Copy link
Contributor

@wunused wunused commented Oct 6, 2023

An implementation of a PhpTypeRecovery module that extends the XTypeRecovery abstract class to add some inferred types to PHP AST nodes.

Any feedback is welcome as I continue work on this to ensure that the class is correct - I hope that this will be helpful for anyone else analyzing PHP programs.

@wunused wunused changed the title Php type recovery [php2spg] Php type recovery Oct 6, 2023
@DavidBakerEffendi
Copy link
Collaborator

@wunused Thanks for leading this effort, so far it's looking good. I am currently redesigning some parts of the type recovery abstract class to make it a bit more rigid and guaranteed, but will help you if it is merged before this PR goes through.

@DavidBakerEffendi DavidBakerEffendi self-assigned this Oct 10, 2023
@DavidBakerEffendi
Copy link
Collaborator

@wunused, run sbt scalafmt and sbt test:scalafmt to fix the format failure

@wunused
Copy link
Contributor Author

wunused commented Oct 11, 2023

@wunused, run sbt scalafmt and sbt test:scalafmt to fix the format failure

Fixed, thanks!

@wunused wunused changed the title [php2spg] Php type recovery [php2cpg] Php type recovery Oct 12, 2023
Pass will set the types for builtin functions with known function
signatures. This pass will run before the PhpTypeRecovery pass.

Currently, the pass does not handle variadic parameters.
Copy link
Collaborator

@DavidBakerEffendi DavidBakerEffendi left a comment

Choose a reason for hiding this comment

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

Great to see tests are passing! Added some suggestions. Additionally, could you add something like Php2Cpg.postProcessingPasses(cpg: Cpg)? See jssrc2cpg. This should help prevent duplication in PhpCode2CpgFixture as well as PhpCpgGenerator.

m: MethodRef,
rec: Option[String] = None
): Set[String] = {
logger.debug(s"visiting identifier ${i.name} assigned to MethodRef ${m.code}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the sole purpose for overriding these functions is to set debugging info, I'd rather you set the logging in the base class

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'll remove these from my pass and consider whether what the appropriate amount of logging is in the base class. These were helpful for me getting started with learning how the type recovery system worked, but they do clutter up the logging messages (unless that's acceptable for the debug log level?).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, likely more a thing for TRACE. But either way it looks like this is more-or-less the last thing left before this is ready I'd say.

This defines the list of default PHP postProcessingPasses in one
location, so that they can all be applied without code duplication
between the different places where passes are applied, like
PhpCpgGenerator and PhpCode2CpgFixture.

Additionally, the XTypeRecoveryConfig options are now exposed to the
frontend command line arguments for PHP analysis as a result of this
refactor.
@DavidBakerEffendi
Copy link
Collaborator

@wunused I think let's get rid of the debugging, then it'll be ready to merge I'd say

@wunused
Copy link
Contributor Author

wunused commented Nov 15, 2023

@DavidBakerEffendi sounds good - debug logging is removed and all tests are passing, so I'm marking this ready for review!

@wunused wunused marked this pull request as ready for review November 15, 2023 14:18
Copy link
Collaborator

@DavidBakerEffendi DavidBakerEffendi left a comment

Choose a reason for hiding this comment

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

Just minor comments then we are ready

!name.isBlank && name.charAt(0).isUpper

override def assignments: Iterator[Assignment] = {
logger.debug(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some debugging here still

- Changed string equality comparisons from equals() to ==
- Changed "<module>" to "<global>" as top-level method namespace
- Removed errant debug statement
@wunused
Copy link
Contributor Author

wunused commented Nov 15, 2023

@DavidBakerEffendi thanks, and comments are addressed!

@DavidBakerEffendi DavidBakerEffendi merged commit 66089b8 into joernio:master Nov 15, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants