Skip to content

Commit

Permalink
Adjust installer.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jun 18, 2024
1 parent 21044eb commit 9992b77
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ public class ByteBuddyAgent {
*/
public static final String LATENT_RESOLVE = "net.bytebuddy.agent.latent";

/**
* The name of the {@link Installer} class that is stored in an obfuscated format which will not be relocated.
*/
private static final String INSTALLER_NAME = new StringBuilder("rellatsnI.tnega.yddubetyb.ten").reverse().toString();

/**
* The manifest property specifying the agent class.
*/
Expand Down Expand Up @@ -735,8 +730,8 @@ private static File trySelfResolve() {
*/
@MaybeNull
private static Instrumentation doGetInstrumentation() {
if (!INSTALLER_NAME.equals(Installer.class.getName())) {
Instrumentation instrumentation = doGetInstrumentation(INSTALLER_NAME);
if (!Installer.NAME.equals(Installer.class.getName())) {
Instrumentation instrumentation = doGetInstrumentation(Installer.NAME);
if (instrumentation != null) {
return instrumentation;
}
Expand Down Expand Up @@ -1535,9 +1530,9 @@ private static File createJarFile() throws IOException {
*/
public File resolve() throws IOException {
try {
if (!Installer.class.getName().equals(INSTALLER_NAME)) {
if (!Installer.class.getName().equals(Installer.NAME)) {
try {
File resolved = trySelfResolve(Class.forName(INSTALLER_NAME,
File resolved = trySelfResolve(Class.forName(Installer.NAME,
false,
ClassLoader.getSystemClassLoader()));
if (resolved != null) {
Expand Down
26 changes: 24 additions & 2 deletions byte-buddy-agent/src/main/java/net/bytebuddy/agent/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
*/
public class Installer {

/**
* The name of the {@link Installer} class that is stored in an obfuscated format which will not be relocated.
*/
protected static final String NAME = new StringBuilder("rellatsnI.tnega.yddubetyb.ten").reverse().toString();

/**
* A field for carrying the {@link java.lang.instrument.Instrumentation} that was loaded by the Byte Buddy
* agent. Note that this field must never be accessed directly as the agent is injected into the VM's
Expand Down Expand Up @@ -90,8 +95,7 @@ public static Instrumentation getInstrumentation() {
* @param arguments The unused agent arguments.
* @param instrumentation The instrumentation instance.
*/
public static void premain(String arguments, Instrumentation instrumentation) {
Installer.instrumentation = instrumentation;
public static void premain(String arguments, Instrumentation instrumentation) {doMain(instrumentation);
}

/**
Expand All @@ -101,6 +105,24 @@ public static void premain(String arguments, Instrumentation instrumentation) {
* @param instrumentation The instrumentation instance.
*/
public static void agentmain(String arguments, Instrumentation instrumentation) {
doMain(instrumentation);
}

/**
* Installs the {@link Instrumentation} in the current class and possibly obfuscated class.
*
* @param instrumentation The instrumentation instance.
*/
private static void doMain(Instrumentation instrumentation) {
Installer.instrumentation = instrumentation;
try {
if (!Installer.class.getName().equals(NAME)) {
Class.forName(NAME, false, ClassLoader.getSystemClassLoader())
.getField("instrumentation")
.set(null, instrumentation);
}
} catch (Throwable ignored) {
/* do nothing */
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,4 @@ public void testConstructorThrowsException() throws Exception {
throw (Exception) exception.getTargetException();
}
}

@Test
public void testInstallerObfuscatedNameMatches() throws Exception {
Field field = ByteBuddyAgent.class.getDeclaredField("INSTALLER_NAME");
field.setAccessible(true);
Object value = field.get(null);
assertThat(value, CoreMatchers.is(Installer.class.getName()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.bytebuddy.agent;

import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -85,4 +86,9 @@ public void testConstructorThrowsException() throws Exception {
throw (Exception) exception.getTargetException();
}
}

@Test
public void testInstallerObfuscatedNameMatches() throws Exception {
assertThat(Installer.NAME, CoreMatchers.is(Installer.class.getName()));
}
}

0 comments on commit 9992b77

Please sign in to comment.