diff --git a/lib/class-factory.js b/lib/class-factory.js index 98f1cac1..bbf2958d 100644 --- a/lib/class-factory.js +++ b/lib/class-factory.js @@ -669,9 +669,14 @@ function ClassFactory (vm) { try { const fieldName = invokeObjectMethodNoArgs(env.handle, field, fieldGetName); try { - const fieldjsName = env.stringFromJni(fieldName); + let fieldjsName = env.stringFromJni(fieldName); + while (jsMethods.hasOwnProperty(fieldjsName)) { + fieldjsName = "_" + fieldjsName; + } + const fieldHandle = env.newGlobalRef(field); fieldHandles.push(fieldHandle); + jsFields[fieldjsName] = fieldHandle; } finally { env.deleteLocalRef(fieldName); diff --git a/test/re/frida/MethodTest.java b/test/re/frida/MethodTest.java index 98794ae9..7c050717 100644 --- a/test/re/frida/MethodTest.java +++ b/test/re/frida/MethodTest.java @@ -65,6 +65,61 @@ public void genericsCanBeUsed() { assertEquals("Badger", script.getNextMessage()); } + @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');" + @@ -118,3 +173,16 @@ public int returnZero() { return 0; } } + +class Collider { + static int particle = 1; + int particle2 = 2; + + int particle() { + return 3; + } + + static int particle2() { + return 4; + } +}