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

Add a builder-style utility class for injecting static method invocation instructions #132

Merged
merged 4 commits into from
Dec 17, 2024

Conversation

gnemyk
Copy link
Contributor

@gnemyk gnemyk commented Dec 3, 2024

Add an api for injecting a call to any static methods (with or without parameters) into the first block of any target methods.

@gnemyk gnemyk changed the title Add a builder-style utilty class for injecting static method invocation instructions Add a builder-style utility class for injecting static method invocation instructions Dec 3, 2024
@tvoc-gs
Copy link
Contributor

tvoc-gs commented Dec 4, 2024

./gradlew spotlessApply makes quite a few formatting changes on my end, can you run this on your PR and then update it first?

@gnemyk
Copy link
Contributor Author

gnemyk commented Dec 4, 2024

./gradlew spotlessApply makes quite a few formatting changes on my end, can you run this on your PR and then update it first?

Certainly. Done, and rebased.

private int initOffset = -1;

public int getConstructorCallOffset() {
assert initOffset != -1
Copy link
Contributor

Choose a reason for hiding this comment

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

why so many asserts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to prevent requesting for the results (i.e., offset) before this visitor has a chance to visit any methods

new InternalTypeEnumeration(method.getDescriptor(clazz));
Iterator<InjectedArgument> argumentsIterator = Arrays.stream(arguments).iterator();

while (parametersIterator.hasNext() || argumentsIterator.hasNext()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
while (parametersIterator.hasNext() || argumentsIterator.hasNext()) {
while (parametersIterator.hasNext() && argumentsIterator.hasNext()) {

Shouldnt this be an && to be safe? If somehow we erroneously have a difference in the size of parameters and arguments the next call would fail. Maybe we can properly handle that though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The || is rather intentional. Suppose we the parameterIterator and argumentsIterator has different length, we would fall through this loop so long the first part matche If we used &&. With ||, we can prevent this.

@Override
public void visitCodeAttribute(
Clazz clazz, Method method, CodeAttribute codeAttribute) {
editor.reset(codeAttribute.u4codeLength);
Copy link
Contributor

@piazzesiNiccolo-GS piazzesiNiccolo-GS Dec 10, 2024

Choose a reason for hiding this comment

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

this is more of a style preference, so consider this as a nit: I prefer this to be an explicit class even if it's only used here

Copy link
Contributor

@piazzesiNiccolo-GS piazzesiNiccolo-GS left a comment

Choose a reason for hiding this comment

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

I personally like the asserts but I am not sure if they are really idiomatic here, we also have @NotNull annotations here so maybe you can use those in some places?

Add an api for injecting a call to any static methods (with or without parameters) into the first block of any target methods.
@gnemyk gnemyk force-pushed the static-invoke-injector branch from 2e3f84a to 392a1f1 Compare December 15, 2024 18:53
@@ -0,0 +1,107 @@
package proguard.classfile.util;

import proguard.classfile.*;
Copy link
Contributor

Choose a reason for hiding this comment

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

Expand imports.

Copy link
Contributor

@tvoc-gs tvoc-gs left a comment

Choose a reason for hiding this comment

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

Ok aside from nit.

@piazzesiNiccolo-GS piazzesiNiccolo-GS merged commit a347e62 into Guardsquare:master Dec 17, 2024
2 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.

3 participants