3

I am trying to replace Java bootstrap class method System.currentTimeMillis with ByteBuddy. However after spent more than one day I still don't get the expected result. Following is my code:-

The inteceptor class in bootstrap.jar used for appendToBootstrapClassLoaderSearch:

public class SystemTimeInterceptor {

public static long currentTimeMillis() {
    return 1L;
}

The agent class:

    instrumention.appendToBootstrapClassLoaderSearch(new JarFile("path/to/bootstrap.jar"));
    final ByteBuddy byteBuddy = new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE);

    new AgentBuilder.Default()
        .enableNativeMethodPrefix("foo")
        .with(byteBuddy)
        .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
        .with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
        .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
        .ignore(none())
        .type(ElementMatchers.named("java.lang.System"))
        .transform(new AgentBuilder.Transformer() {
            @Override
            public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader,
                JavaModule module) {
                return builder.method(ElementMatchers.named("currentTimeMillis")).intercept(
                    MethodDelegation.to(SystemTimeInterceptor.class));
            }

        }).installOn(inst);

And after I start my test main method with specifying the javaagent argument, I still see the current time in milliseconds in the output. With above way, I tried to redefine System.setSecurityManager and it works. I also tried to remove the strategy settings in order to use the default. But it doesn't work.

James Z
  • 12,209
  • 10
  • 24
  • 44
Tatera
  • 448
  • 3
  • 11

1 Answers1

1

After trying with different combinations I finally get it works now. Actually the above code is correct. I think the possible reason not works at first was I commented out parts of the settings. Anyway, the most important setting is .ignore(none()). Without that, bootstrap classes will be ignored by default.

Tatera
  • 448
  • 3
  • 11