back to index

2+2=5 in Java


Whisper Transcript | Transcript Only Page

00:00:00.000 | This video is about how we can hack Java by using reflection of its own source code to
00:00:05.600 | make this 2+2 statement, output 5.
00:00:12.680 | Just as George Orwell, one of my favorite writers, warned us about in 1984, about propaganda
00:00:19.020 | machines that sublimate the nature of truth.
00:00:23.020 | This video is not about politics, philosophy, nor is it about the apparent, as I have just
00:00:29.480 | learned, woke Twitter madness around 2+2=5.
00:00:35.000 | Though perhaps if we're living in the simulation and it's written in Java, this might be a
00:00:40.280 | way to make the simulation just a bit more dystopian.
00:00:44.440 | So here's what the full source code looks like, and it uses Java's ability to do reflection,
00:00:49.880 | which is the ability of a programming language to inspect itself.
00:00:53.560 | So if we look at the code, it actually dives into the implementation of the integer class,
00:00:59.200 | pulls out the integer cache class from that implementation, makes it accessible and writable,
00:01:06.280 | pulls it into an array of integer object of size 256, and modifies that array.
00:01:13.240 | Now what does this array contain?
00:01:15.060 | So interestingly, if we look at the integer cache class inside the integer object implementation
00:01:20.320 | in Java, it defines a hard-coded low of -128 and a high that's passed in as a parameter,
00:01:27.320 | that's 127 as a default.
00:01:29.960 | And what that does is create a cache of integer objects from -128 to 127, and then reuses
00:01:37.820 | this cache every time an integer object with a value in this range is used.
00:01:43.480 | This is exactly the cache with reflection that we pull out and modify.
00:01:49.200 | It so happens that the 132nd element in the cache is where the 4 resides, and so by way
00:01:57.120 | of obfuscation, it takes the 133rd element, which has the number 5 in it, and it sizes
00:02:03.360 | it to 132nd, but you can just assign value 5 here.
00:02:08.680 | And then the result, and you are else in the code, if you use integer objects and the number
00:02:14.200 | 4 comes up, it will instead output the number 5.
00:02:18.240 | There you go, 2+2=5.
00:02:21.600 | Check out the link in the description that points to the Stack Exchange Code Golf, has
00:02:25.520 | a bunch of interesting discussions around this, including the possibility of taking
00:02:30.480 | the entire 256 element array and shuffling it, thereby not only making 2+2=5, but messing
00:02:38.600 | with the entirety of low value arithmetic in Java.
00:02:43.320 | So there you go, that's how you hack the simulation.
00:02:45.960 | Let me quickly thank the sponsors that somehow amazingly support the podcast and the videos
00:02:50.320 | I make.
00:02:51.760 | This time it's Asleep Mattress, click the link to get a discount in the description.
00:02:56.320 | And by the way, I have a conversation with James Gosling, the creator of Java, coming
00:02:59.900 | up on the podcast, so check that out.
00:03:02.620 | And remember, try to learn something new every day.
00:03:05.740 | [end]
00:03:07.740 | [end]
00:03:09.740 | [end]
00:03:11.740 | [end]
00:03:13.740 | [end]
00:03:15.740 | [end]
00:03:17.740 | [end]
00:03:19.740 | [end]
00:03:21.740 | [BLANK_AUDIO]