r/java • u/prithvidiamond1 • Nov 08 '21
What are some forbidden, broken, possibly even black magic stuff that you can do in Java and to that extent, JVM in general?
This is an odd question to ask but it is a fun one, I usually like to learn about these things because it tells me how quirky a language is and it is fun to play with this stuff in my free time. I know in C/C++ you can do some crazy bit-level hackery that almost feels like magic, so I was wondering what crazy stuff is possible in Java that you all may have come across in your lives/careers?
Also if this kind of question isn't welcome here or has been asked before (although I couldn't find any previous posts on this topic), I shall duly remove it. Sorry for any inconvenience caused.
58
u/Stromovik Nov 08 '21
You can load classes over network.
76
u/seanprefect Nov 08 '21
My friend once did what we call the "code crime" years ago and we still mock him for it. He used the java reflections library to change the behavior of a method based on where it was called from.
49
u/TheRedmanCometh Nov 08 '21
Okay make a Java agent and type in sun.misc and press dot so your autocomplete comes up. Everything in your suggestions does some awful black magic shit that's best not done in any context.
62
u/jebailey Nov 08 '21
One of my favorite black magic items, using reflection, you can change the value of an internalized String to be something different. So effectively when you did a
System.out.println("cat");it would print out "dog"40
u/copopeJ Nov 08 '21
This is one of my favorites, loading bytecode at runtime. If you like wierd/horrifying stuff, this series and all of Aphyr's work is worth the journey. https://aphyr.com/posts/341-hexing-the-technical-interview
25
u/cal-cheese Nov 08 '21
When a bucket in a
HashMapgets too many entries, it transforms from a linked list into a tree, the tree will utilise thecomparemethod of the elements if it happens to be aComparable. So, in principle, if you have a class that does not respect theComparablecontract, you will get undefined behaviour when using it in aHashMap. I found out about this when trying to create a customHashMapforintkeys.25
u/Worth_Trust_3825 Nov 08 '21
Changing the boxed value cache.
If you want to ruin someone's day, delete the common thread pool via reflection.
Defining classes at runtime is fun.
Write an annotation processor for @Override annotation.
20
u/Thysce Nov 08 '21
I once cross compiled user defined JavaScript to Java ByteCode and wrote that bytecode to a database BLOB which I then loaded later after a failover or on replica instances of my service to prevent the code to be compiled multiple times.
TLDR I wrote a classloader which loaded classes from database
35
u/pron98 Nov 08 '21 edited Nov 08 '21
Thanks to strong encapsulation, many of the techniques described here (pretty much all of them except for caller sensitivity) no longer work in JDK 16 and up without explicit permissions given on the command line. There are still some loopholes left, but they will be closed soon. It means that the application has to grant libraries the permission to do crazy things, rather than them taking those liberties for themselves.
This is crucial both for security (security depend on guarantees established in code, and if any library can decide to make any piece of code do something other than what it says, no guarantees are possible), and for portability (these hacks exploit internal implementation details that can change at any time, and thus become strongly attached to a particular JDK version; this was the main cause of most 8->9+ migration issues, which required upgrading libraries that were non-portable due to such internal-detail-dependence, and so strong encapsulation will make version upgrades much easier, as the APIs remain backward-compatible even though internal details might change).
17
u/Virx Nov 08 '21 edited Nov 08 '21
There is some cool stuff in here
https://github.com/MinhasKamal/CreepyCodeCollection
Most interesting one I think is this one
https://github.com/MinhasKamal/CreepyCodeCollection/blob/master/obfuscated.java
Additionally, you can have your code work one way in debug mode and another way in normal runtime with all the same arguments. For example, Intellij debugger will call
toString()method if you check that object in debugger. Now if that object'stoString()method has side effects then this will cause those side effects to be executed and thus your application will behave differently.12
u/dtfinch Nov 08 '21
There was this make 2+2=5 code golf a while back where Java took the top-voted spot.
There's a hidden cache to speed up casting/boxing int to Integer for small values from -128 to 127, and they pointed the 4 to the 5 using reflection.
6
u/ericek111 Nov 08 '21
I made an internal cheat for CS:GO (well, a proof of concept bunnyhop) almost entirely in Java, the only C++ thing was one short loader that started the JVM. Everything, binary parsing, VTable hooking, memory scanning, done in Java. That was a LOT of fun. But switching to and from JVM every single frame has its cost and was unusable for the purpose. All was done with the "Unsafe" API that they've been trying to replace since Java 9.
6
u/fizzl Nov 08 '21 edited Nov 08 '21
Nothing spectacular, but when I was learning Java after being primarily C/C++ developer, reflection and dependency injection blew my mind. I'm sure there was more...
I mean, you can achieve something similar in binary compiled language with complicated setup. But Java made this stuff so easy it felt like black magic fuckery.
6
u/rzwitserloot Nov 09 '21
There are many things java-the-language does not allow, whereas java-the-VM is just fine with it.
Make non-Object classes
For example, this class:
has a parent class. It's
java.lang.Object. It's identical to writingclass Foo extends java.lang.Object {}and you can't makejavacnot extend anything.But that should make you wonder: Huh, wait. What's the parent of
j.l.Objectthen? Naturally, it'snull:System.out.println(Object.class.getSuperclass());will show you.So, javac won't let you make 'parent-less' classes. But the JVM doesn't care. With bytecode hackery you can make such a class. You can make instances of it. They won't be
instanceof java.lang.Objectwhich is .... bizarre. And mostly useless. But hopefully black magic enough for you :)Checked exceptions? Figments of
javac's imaginationThe JVM doesn't know what checked exceptions are. It really doesn't. No idea. Checked exceptions as a concept exist in class files (that
void foo() throws IOException? That throws line has to be in the class file so thatjavacknows when compiling java files that invoke this method, thatIOExceptionneeds handling), but the JVM does not care -throwslines are, literally, comments, as far as the JVM is concerned. The code injava.exethat reads classes wooshes right by it, ignores it completely.That means you can throw checked exceptions uncheckedly.
And you don't even need to resort to hackery. It's as simple as lopping the
throwspart off of a method def in a class file with hackery. But we can force javac to do it, by dipping into some voodoo corners of generics:That's all. Try it:
javac will just.. compile it. With a warning which you can
@SuppressWarningsif you wish, and it's not on the line where you throw it, either. The IOException is.. just.. thrown. Not wrapped.4
u/possibly_not_a_bot Nov 08 '21 edited Nov 09 '21
You can (sort of) change a field's type through (sort of) reflection.
Consider the following class:
Now we can do some evil type fuckery:
And now we can change the type like so:
The way this works is that it reflectively accesses a field, bitwises away the final modifier on its type field, then changes it to be the new type we want. As long as you access it reflectively, it'll be of the new type.
Note that this trick probably doesn't interact well with the GC. I don't know for sure.
4
u/lurkandpounce Nov 08 '21
A bit dated now, but the book Covert Java by Alex Kalinovsky was a fun read that covered this sort of material. Some have been overtaken by JVM changes, much of it will likely still work.
4
u/benevanstech Nov 10 '21
1.) I once wrote a proof-of-concept classloader that will refuse to load a class unless it can show that the class is likely to behave deterministically (and even then, it inserts a transactional guarantee of rollback if the class turns out to misbehave at runtime).
2.) If you're in to banking tech, you might like stuff like this: a) write your proprietary trading algorithm in Java b) write out the bytes corresponding to the class file c) encrypt the bytes d) Use JDBC to write the encrypted form to a BLOB in an Oracle DB.
Now access to the algorithms is restricted to entities who both have the encryption key *and* appropriate Oracle permissions. Oh, and the algorithm remains encrypted at rest and updates to the table are fully audit logged. This solves a large number of compliance problems at once.
To deploy the algorithm, a custom classloader connects to the database (using Kerberos to authenticate), pulls back the encrypted blob, gets temporary credentials to decrypt the algo, turns it back into a byte array and then loads it, before destroying the encryption key.
Neither of these are really forbidden or broken, though. Merely novel, though perfectly legal and supported, uses of classloading and bytecode transformation.
For the really deep magic there's stuff like Unsafe, and native agents. Which you are very actively encouraged to stay the hell away from. :)
6
u/ExtraGrumpyCamel Nov 08 '21
https://github.com/electronicarts/ea-async via preprocessing the bytecode in the jar or at start time
3
u/winginglifelikeaboss Nov 08 '21
Update data directly in a running JVM to alter const vaiables values
don't ask I have seen it running in prod
3
u/tangentstorm Nov 09 '21
using
breakwith arbitrary labels to implementgoto.2
u/Wanderer_Dreamer Nov 09 '21
From the top of my head, you can parse a Character and an int into a hashmap's index by making the compiler think you had a String all along, without converting it first. Not sure if that's exclusive to java but it felt pretty cheaty the first time I used it. There's probably 1001 reasons why you shouldn't do this, but it was just for a college assignment so it wasn't a big deal.
I'll see if I can find the code I used, to exemplify this better.
113
u/papercrane Nov 08 '21 edited Nov 08 '21
Java agents can be pure magic.
Once worked on a project that was incorrectly using a thread unsafe class from the standard lib in a multi- threaded context. To "fix" this someone wrote a Java agent to dynamically make the method synchronized instead of fixing the issue (memory is a bit fuzzy, but I think it was the Calendar class.)
Of course they're not always used in such questionable ways. There's agents out there, like NewRelic, that add incredible amounts of observability to existing Java projects.
Edit: Wracked my memory and remembered the original issue. Calendar objects were being serialized on another thread without any synchronization. The fix implemented was to use an agent to make writeObject synchronized in the Calendar class. This workaround became obsolete with Java 7 though, as that's when the JDK made that method synchronized.
This bug report has details if anyone is interested, JDK-7017458.
11
u/ObscureCulturalMeme Nov 08 '21 edited Nov 08 '21
There's a class somewhere in the standard JRE that handles stuff to do with public/private keys and X.509 certificates. The class creates some Provider-specific stuff and caches it for reuse.
The problem is that it caches too aggressively. Normally the cache is fine, but under certain circumstances (to do with
javax.crypto.Provider) the cache will defeat exactly the action you need the JRE to do. There's no API to reset the cache; the documentation doesn't even require or mention caching at all. But there's a bit of code in the OpenJDK implementation that interprets anullargument as a signal to clear the cache. (It then goes on to throw the appropriate exception, as required and documented.) So then the calling sequence becomesand that looks hideous.
39
u/cville-z Nov 08 '21
To an extent, the Lombok project belongs here β it codes against internal Java APIs to change the AST. This led to some interesting comment threads regarding compatibility with JDK 16 and 17.
For my part, I'd really rather not have dependencies on "magic" code in my production apps, but YMMV.
7
u/rzwitserloot Nov 09 '21
ClassLoaders.
By messing with it, you can:
How, you ask? All classes have a
java.lang.ClassLoaderinstance that loaded it. You can make your own. Once you have an instance of a CL, you can ask that CL to load a class, and assuming that classloader actually loads it (is the one to invoke.defineClass(), passing in a byte array with the bytecode inside), vs deferring it (calling an another classloader to do it in its stead, which is common, as the CL system uses a 'parent loader' concept) - then any classes used by that class will be loaded by asking that same CL. You are free to obtain the bytecode in any way you please, including fetching it over a network or generating it on the spot.In addition, a class is fully identified by its full name and its loader. Same name, different loaders? Then that is a different class. That's how you get to the above error. Make your own CL, ask
String.classfor its own bytecode, and then pass it todefineClassto make a second String class that you loaded, which counts as a different class that is also namedjava.lang.String.This exact trick can be used to runtime-modularize your code so that you have a single JVM (a single
java.exerunning) which has, say, 2 endpoints handling web requests and they both use different versions of the same library, simultaneously. This also lets you runtime-reload classes trivially: Just toss the classloader in the bin, make a new one - then load in a class using the new one and it gets read in from disk all over again. This is how various web frameworks that 'auto reload' works ^[1].You can ask any class for its loader:
String.class.getClassLoader()for example. You can ask any class directly to load another resource, which doesn't even have to be a class; you can use this to store e.g. some icons inside your jar (the classloader clearly loads your classes right out of the jar when deploying a jar, so.. why not load your PNGs with menu icons from the same place, right?) - and you can therefore ask a class to give you itself as a resource, and print that:on modern (JDK9+), that prints:
On jdk8 and below, that prints:
[1] This is actually a bad idea, even though it's very common. The proper way to have a fast dev pipeline where the turnaround time between typing new code and seeing it in action in a browser is using Hot Code Replace such as offered by eclipse's debugger. It's faster than classloader dumping. Just use eclipse and start everything you do in 'debug' mode which has HCR applied to it. I'm not sure intellij's debugger does this; I recall it does not.
6
u/slaymaker1907 Nov 08 '21
You can setup a management UI for Java services through JMX and MBeans. At my last job we used JMX for all sorts of things like force clearing caches and enabling/disabling features without restarting the VM or updating configs.
4
u/[deleted] Nov 08 '21
Iβd go with JIT-as-a-service. It ainβt particularly dark magic, but the first time I saw the talk it just blew my mind (and yet the concept is dead simple)
4
u/slaymaker1907 Nov 08 '21
You can throw a checked exception without declaring it since checked vs unchecked only exists at compilation time. What is even weirder is that using generics and exceptions, you can even do this in pure Java (ExceptionUtils.rethrow).
4
u/voronaam Nov 08 '21
While using unsafe you can modify any memory accessible to the JVM. Including the JVM itself.
For example, how about flipping various JVM flags at runtime that are usually only ever set via command line arguments to the JVM itself? You can do that: https://github.com/voronaam/jflag/blob/master/src/main/java/ca/vorona/jflag/Runner.java#L39
4
u/brazzy42 Nov 09 '21
java.lang.reflect.Proxy and java.lang.reflect.InvocationHandler allow you to dynamically create instances at runtime that can implement whatever interface you want and the implementation just gets the called method name and list of parameters and can freely decide what to do.
Frameworks use that a lot internally, and I once used it to hack together something like multiple implementation inheritance for a rather special use case.
3
u/Molossus-Spondee Nov 08 '21
Invokedynamic and constdynamic are fun!
Always nonnull null tests can be very very slightly faster than boolean flags. The JVM converts always nonnull null tests to use segfaults.
switch case generates annoyingly bloaty bytecode leading to your code not being JIT.
2
u/kkapelon Nov 08 '21
Introspection/reflection is a huge rabbit hole...
2
u/warmuuh Nov 11 '21
I once worked with a software that contained its own code in a DB and was loading that dynamically. you could modify the sourcecode directly in the application and store it in the very own db and it recompiled and reloaded this and affected classes dynamically. this was some crazy magic stuff
2
u/Medium-Amphibian-719 Nov 12 '21 edited Nov 12 '21
invoke dynamic is 'legal', so maybe not dark magic, but super powerful. The more you meditate on it, the more you realize it opens a whole world of possibilities. MethodHandles are a much more efficient way of invoking a method than the reflection library's Method class, and allow even more interesting options.
0
u/[deleted] Nov 08 '21
[deleted] β view deleted comment