Disgruntled Bomb: Java Edition

A little while back, we had a Bring Your own Code called The Disgruntled Bomb that sought to answer, “what is the worst thing a disgruntled employee could leave behind in the source code?”

The comments were great and featured all sorts of solutions. Most were in C and C++, but there were few unique ones like a cronjob and even an incredible one-liner for .NET.

While C and C++ give programmers enough rope to shoot themselves (or build a crazy bomb), managed platforms like .NET and Java limit your options. That is, unless you know where to look. Alexander Keul took advantage of Java’s cached boxing conversions to come up with this concept:

package dont.try_this.at_home;
import java.lang.*;

class ValueMunger extends Thread {
public void run() {
while(true) {
munge();
try { sleep(1000); } catch (Throwable t) { }
}
}

public void munge() {
try {
Field field = Integer.class.getDeclaredField( “value” );
field.setAccessible( true );
for(int i = -127; i<=128; i++)
field.setInt(
Integer.valueOf(i),
// either the same (90%), +1 (10%), or 42 (1%)
Math.random() < 0.9 ? i : Math.random() < 0.1 ? 42 : i+1 );
} catch (Throwable t) { ; }
}

}

A simple call to (new ValueMunger()).start(); will spin off a new thread that will randomly redefines the values of integers between -127 and 128, ensuring that maths will never be the same again:

Integer a = 2;
Integer b = 3;

System.out.println( a + b ); // could be 5, 6, 7, 44, 45, or 84

To extend the fun, this works for Boolean and several other primative types like BigInteger, Long, Short, etc.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s