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

Returning false if file does not end with .class #580

Merged

Conversation

joachimhs
Copy link
Collaborator

When using scriptOutputDir as a param to -javaagent, the folder might contain non Java class files. Ensuring that the files loaded into BTrace is a .class file.

When using scriptOutputDir as a param to -javaagent, the folder might contain non Java class files. Ensuring that the files loaded into BTrace is a .class file.
@@ -750,6 +750,10 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc)
}

private static boolean loadBTraceScript(String filePath, boolean traceToStdOut) {
if (!filePath.endsWith(".class")) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you modify this to look at the magic bytes instead?
For Java class files it would be 0xCAFEBABE and for the BTrace probe-pack it is 0xBACECACA.
The problem with only extension check is that the BTrace probe-pack can have any extension name - it is not really standardized. For simplicity it defaults to .class but it is possible to specify some other extension so you eg. don't confuse the classloader if the BTrace probe pack files are put together with regular class files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How would I check for the magic bytes? Is it as simple as opening the file, read the first few bytes and compare them to the mentioned 2 magic bytes?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, I am sorry, I completely missed the notification about the activity here :(
Yes, it is exactly the way you are describing. It is 4 first bytes of the file which is containing the magic number.

Copy link
Collaborator

@jbachorik jbachorik left a comment

Choose a reason for hiding this comment

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

Hi, thanks for contributing!
Please, see my comments 'inline'

@jbachorik
Copy link
Collaborator

Hi @joachimhs , same as for #579 - will you be able to finish this PR, please?

@jbachorik
Copy link
Collaborator

Ok, I am going to merge as it is and add the magic number checks as a followup step.

@jbachorik jbachorik merged commit 06e6efa into btraceio:develop Nov 10, 2022
@jbachorik jbachorik modified the milestones: 2.2.3, 2.3.0 Nov 10, 2022
@jbachorik jbachorik modified the milestones: 2.3.0, 2.2.3 Dec 23, 2022
This pull request was closed.
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