News for hackers. Stuff that matters.
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.