Re:Overriding C++ virtual functions at run time
by The_rationalist on Sunday August 09, 2020 @05:36AM
(#24098410)
I was wondering whether such a thing is possible for JVM based languages and it turns out it is:
https://stackoverflow.com/questions/8273685/is-it-possible-t...
[ Parent
| on HN ]
Re:Overriding C++ virtual functions at run time
by danmg on Sunday August 09, 2020 @02:50PM
(#24101878)
In Java, you can write an agent and intercept when the bytecode for a class is loaded by the JVM by the Class Loader.
ASM is just a bytecode reader/writer/visitor library that can then modify it. You could also just do it statically by having the bytecode you want injected ready to go in an array or resource.
if (className.equals("a/b/c/d$e"))
return fixedClassBytecode;
else
return bytecode;
It's possible to create custom loaders (e.g., loading a class from an encrypted zip-file, or one that creates custom bytecode on the fly) and things which are trying to obfuscate what they're doing will have custom loaders, but this is a choke point that every class that's read in and instantiated must pass through.
Re:Overriding C++ virtual functions at run time
by MaxBarraclough on Sunday August 09, 2020 @07:17AM
(#24098846)
So, is it possible? I get the impression you can create a new class using a bytecode-manipulation library, but that's not the same as modifying an existing class at runtime.
I believe classes, once loaded by the JVM, cannot be changed. https://stackoverflow.com/a/43653466/
Re:Overriding C++ virtual functions at run time
by CHY872 on Sunday August 09, 2020 @08:06AM
(#24099044)
It's generally possible to rewrite loaded classes. Some changes are possible (e.g. replacing a method's body), others are not (adding new methods). The state of the art library for doing this at the application layer is ByteBuddy https://github.com/raphw/byte-buddy#changing-existing-classe... but this functionality is used by plenty of tooling - profilers such as YourKit rewrite methods to add telemetry - I've seen some security libraries attempt to add additional security related hooks.
Re:Overriding C++ virtual functions at run time
by The_rationalist on Sunday August 09, 2020 @08:17AM
(#24099103)
When would you use ByteBuddy versus https://asm.ow2.io ?
Re:Overriding C++ virtual functions at run time
by CHY872 on Sunday August 09, 2020 @10:49AM
(#24099965)
ASM is far lower level than ByteBuddy. To write some ASM code you probably want to have some proficiency with the bytecode format itself (e.g. you're typically outputting individual JVM bytecode instructions ;(invokespecial,athrow,etc). Personally I'd probably always use ByteBuddy unless I had some very specific reason why not. For comparison, these two examples https://github.com/raphw/byte-buddy#changing-existing-classe... http://web.cs.ucla.edu/~msb/cs239-tutorial/ explain how to do a System.out.println wrapper around a method.
In one case, the developer writes `System.out.println`. In the other, the developer must individually get the static field System.err and push it to the stack, reference PrintStream's method println, including the arguments (Ljava.lang.String;)V. That means it takes an array of strings and returns void.