Skip to content

Commit

Permalink
Include JS class names in the auto-generated transpiled Java class na…
Browse files Browse the repository at this point in the history
…mes to make deobfuscation of `class.getName()` output possible.

Most classes will have unique names, as for the most part Closure Compiler will assign them to variables or global-shared-object properties sharing a common namespace. In case there is ambiguity, a unique ID is still added as a suffix after the JS class name.

PiperOrigin-RevId: 702881060
  • Loading branch information
Googler authored and copybara-github committed Dec 4, 2024
1 parent 0745f99 commit df5242a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
22 changes: 17 additions & 5 deletions jre/java/javaemul/internal/nativebootstrap/Util.impl.java.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,21 @@ class Util {
*/
static $getGeneratedClassName_(ctor) {
const propName = '$$generatedClassName';
return ctor.prototype.hasOwnProperty(propName) ?
ctor.prototype[propName] :
ctor.prototype[propName] = 'Class$obf_' + ++Util.$nextUniqId_;
if (ctor.prototype.hasOwnProperty(propName)) {
return ctor.prototype[propName];
}

// Use the constructor "name", which should be the result of the variable
// name JSCompiler assigned the class/function to. This allows for manual
// deobfuscation of the class name.
const constructorName = ctor.name;
const nextUniqId = Util.$nextUniqIdByName_.get(constructorName) ?? 0;
Util.$nextUniqIdByName_.set(constructorName, nextUniqId + 1);
const generatedClassName =
'Class$obf_' + constructorName + '_' + nextUniqId;

ctor.prototype[propName] = generatedClassName;
return generatedClassName;
}

/**
Expand Down Expand Up @@ -289,9 +301,9 @@ class Util {


/**
* @private {number}
* @private @const {!Map<string, number>}
*/
Util.$nextUniqId_ = 1000;
Util.$nextUniqIdByName_ = new Map();

/**
* @type {number}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java.lang.RuntimeException: __the_message__!
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction$MyFunctionImpl.run(KotlinThrowsInJsFunction.kt:50)
at lambda(jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:196:31)
at lambda(jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:208:31)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction.executesFunction(KotlinThrowsInJsFunction.kt:55)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction$1.run(KotlinThrowsInJsFunction.kt:41)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction.executesFunction(KotlinThrowsInJsFunction.kt:55)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java.lang.RuntimeException: __the_message__!
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction$MyFunctionImpl.run(KotlinThrowsInJsFunction.kt:50)
at apply(<blaze-out>/jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:196:31)
at apply(<blaze-out>/jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:208:31)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction.executesFunction(KotlinThrowsInJsFunction.kt:55)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction$1.run(KotlinThrowsInJsFunction.kt:41)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction.executesFunction(KotlinThrowsInJsFunction.kt:55)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java.lang.RuntimeException: __the_message__!
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction$MyFunctionImpl.run(ThrowsInJsFunction.java:55)
at lambda(jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:196:31)
at lambda(jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:208:31)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction.executesFunction(ThrowsInJsFunction.java:60)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction$1.run(ThrowsInJsFunction.java:46)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction.executesFunction(ThrowsInJsFunction.java:60)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java.lang.RuntimeException: __the_message__!
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction$MyFunctionImpl.run(ThrowsInJsFunction.java:55)
at apply(<blaze-out>/jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:196:31)
at apply(<blaze-out>/jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:208:31)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction.executesFunction(ThrowsInJsFunction.java:60)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction$1.run(ThrowsInJsFunction.java:46)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction.executesFunction(ThrowsInJsFunction.java:60)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ private static void testNative() {
}

private static void assertClassName(String name, String canonicalName, String simpleName) {
assertTrue("Name should have the pattern XXX_Class$obf_1XXX", name.contains("Class$obf_10"));
assertTrue(
"Name should have the pattern Class$obf_XXX_# or Class$obf_####, but did not match: "
+ name,
name.matches("^Class\\$obf_(.*_\\d+|[1-9]\\d{3,})$"));
assertTrue(name.endsWith(simpleName));
assertEquals(name, canonicalName);
}
Expand Down

0 comments on commit df5242a

Please sign in to comment.