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

handle field / method name collision #21

Merged
merged 4 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/class-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,12 @@ function ClassFactory (vm) {
try {
const fieldName = invokeObjectMethodNoArgs(env.handle, field, fieldGetName);
try {
const fieldjsName = env.stringFromJni(fieldName);
let fieldjsName = env.stringFromJni(fieldName);
const fieldHandle = env.newGlobalRef(field);
fieldHandles.push(fieldHandle);
while (jsMethods.hasOwnProperty(fieldjsName)){
Copy link
Member

Choose a reason for hiding this comment

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

  • Let's move these 3 lines to right after the let, as they belong together. Then put a blank line between that and the fieldHandle stuff (those two lines), and then one blank line before assigning to jsFields. Just to make the code a bit more readable.
  • Missing a space before {.

fieldjsName = "_" + fieldjsName;
}
jsFields[fieldjsName] = fieldHandle;
} finally {
env.deleteLocalRef(fieldName);
Expand Down
70 changes: 70 additions & 0 deletions test/re/frida/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,62 @@ public void genericsCanBeUsed() {
assertEquals("Badger", script.getNextMessage());
}


Copy link
Member

Choose a reason for hiding this comment

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

There's an extra blank line here.

@Test
public void fieldsThatCollideWithMethodsGetSuffixed() {
loadScript("var Collider = Java.use('re.frida.Collider');" +
"var collider = Collider.$new();" +
"send(collider._particle);");
assertEquals("1", script.getNextMessage());
}

@Test
public void methodsThatCollideWithFieldsKeepName() {
loadScript("var Collider = Java.use('re.frida.Collider');" +
"var collider = Collider.$new();" +
"send(collider.particle());");
assertEquals("3", script.getNextMessage());
}

@Test
public void fieldsThatCollideWithMethodsGetSuffixed2() {
loadScript("var Collider = Java.use('re.frida.Collider');" +
"var collider = Collider.$new();" +
"send(collider._particle2);");
assertEquals("2", script.getNextMessage());
}

@Test
public void methodsThatCollideWithFieldsKeepName2() {
loadScript("var Collider = Java.use('re.frida.Collider');" +
"var collider = Collider.$new();" +
"send(collider.particle2());");
assertEquals("4", script.getNextMessage());
}

@Test
public void collidedMethodsFieldsCanStillBeInstrumented() {
loadScript("var Collider = Java.use('re.frida.Collider');" +
"Collider._particle.implementation = function () {" +
"return 11;" +
"};" +
"Collider._particle2.implementation = function () {" +
"return 22;" +
"};" +
"Collider.particle.implementation = function () {" +
"return 33;" +
"};" +
"Collider.particle2.implementation = function () {" +
"return 44;" +
"};");

Collider collider = new Collider();
assertEquals(11, Collider.particle);
assertEquals(22, collider.particle2);
assertEquals(33, collider.particle());
assertEquals(44, Collider.particle2());
}

// @Test
public void interfaceCanBeImplemented() {
loadScript("var X509TrustManager = Java.use('javax.net.ssl.X509TrustManager');" +
Expand Down Expand Up @@ -118,3 +174,17 @@ public int returnZero() {
return 0;
}
}

class Collider {
static int particle = 1;

Copy link
Member

Choose a reason for hiding this comment

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

Trailing whitespace.

int particle2 = 2;

int particle() {
return 3;
}

static int particle2(){
Copy link
Member

Choose a reason for hiding this comment

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

Missing space before {.

return 4;
}
Copy link
Member

Choose a reason for hiding this comment

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

Field- and method-names should follow the coding style, i.e. camelCase, not CamelCase.

}