· · ─ ·✶· ─ · ·

A couple of days ago I was browsing HN and I came across this post about someone talking about a bug in OpenJDK, the open source implementation of Java. The post linked to the PR which talks about the bug. I spent a solid 10 mins trying to make sense of what was going on and it was all just going over my head. I felt defeated and honestly sad at my lack of understanding.

Now, typically I would’ve just browsed away, opened YT shorts and doomscrolled my ignorance away. But this time something snapped in me. How could I possibly be ok not understanding something in my domain, in a piece of technology I use daily at work? How did the original reporter figure this bug out? Seems complicated. What are the other folks on the issue talking about? AVX-512? SIGSEGV?

Nah. I was determined to setup some time during the weekend to do my best to understand as much as I could. I wanted to challenge myself. So here we are. It’s Saturday. I was procrastinating on starting this but I pulled myself together and got started on it.

Now, I did do some initial reading and worked out a plan with Claude to guide me through this. So I’m hoping I would have more structure to get through this.

I like to ask myself questions as I work through a problem. The first question that popped up is

Whats Adoptium and whats Temurin and whats OpenJDK?

OpenJDK is the open-source implementation of Java primarily led by Oracle. It does not have a polished installer. And this where Adoptium comes in. Its a non-profit working group (Adoptium comes under The Eclipse Foundation) who take the raw OpenJDK source code, compile, test and release a polished product. The product takes the form of an installer for various OS’s. And the product’s name is “Temurin” (Fun fact: TEMURIN is an anagram of RUNTIME)

I also noticed people calling it “Java4Shell”. And this reminded me of “PHP2Shell” which made the news a few weeks ago. The “4Shell” or “2Shell” is a naming convention used by security researchers which lets people immediately know that this bug is not playing around. It allows an attacker to gain access to the system’s command line (aka shell). It’s classified as an RCE (Remote Control Execution) bug.

Onto the actual bug.

Reading the bug report, I can kinda sorta figured out that this bug occurs on CPU’s which have an AVX-512 VBMI. More specifically, it occurs if its a linux machine with x86_64 processors supporting AVX-512 VBMI.

What is x86_64 processor and whats AVX-512 VMBI?

x86_64 is the normal 64 bit CPU architecture. AVX-512 (Advanced Vector Extensions 512) is an advanced instruction set that speeds up heavy data manipulation on these processors. Instead of processing one piece of data at a time, AVX-512 lets the CPU process huge 512-bit chunks of data simultaneously.

VBMI (Vector Byte Manipulation Instructions) is a specific upgrade to AVX-512 designed to rapidly shuffle, rearrange, and decode individual bytes inside those massive 512-bit chunks.

How does this relate to Java’s Base64 decoder? Base64 requires lots of byte-shuffling and modern Java uses AVX-512 VBMI to decode Base64 at blazing speeds.

This bug primarily deals with Base64. Read this excellent post to learn the fundamentals.

The bug report mentions environment as grep -o 'avx512vbmi' /proc/cpuinfo | head -1. This is the command I can run on my linux machine to check if I indeed have a CPU with this feature or not? Since I dont have a linux system (Yes, I know, I know), I opened up WSL (Windows Subsystem for Linux) (Yes, I know… Sigh) and typed in that command and got the below ouput:

WSL output

Bummer. I can’t really install the broken version of the JDK, see it fail, patch it and see it working.

What now? I’m gonna rent a virtual CPU from Google. More on that later.

The issue mentions:

The crash is in the Parallel GC (PSPromotionManager::copy_unmarked_to_survivor_space) during a young-generation scavenge.

What is young-generation? Modern Java JVM’s have 2 major heap regions: Young-Generation and Old-Generation. It’s designed to optimize garbage collection based on the generational hypothesis (the observation that most objects die shortly after creation). This is called a Generation GC strategy.

  • The Young Generation is where all newly created objects are initially allocated. Because most objects become unreachable quickly, this area is smaller and cleaned frequently
  • The Old Generation holds long-lived objects that have survived multiple garbage collection cycles in the Young Generation

What is Parallel GC?

OpenJDK has differnet types of GCs. Two of which are:

  1. Serial GC
  • Simplest GC, used in environments with limited CPU and memory resources
  • Uses a single thread to do all GC work, hence its a Stop-The-World collector, which means that when GC work is ongoing, all other threads are paused
  1. Parallel GC
  • Aka “Throughput collector”
  • Similar to Serial GC but uses parallelism
  • Scales with CPU cores because its able to take advantage of the parallelism

What does Scavenge mean in the context of GCs? The young-generation part of the heap is divided into “regions”. As per the generational hypothesis, there are more dead objects in these (young-generation) regions than live objects. So in these regions, a GC algorithm which operates on live objects is much more efficient than one operating on all objects, because operating on all objects, we’re unnecessarily focusing on a lot of dead objects. We can simply free up dead objects. Scavenging is a process by which the GC scans these regions and copies the live objects from a region (a region having less live objects overall) to another region. The goal is to have regions with more live objects and free up regions with dead objects.

Read this to get a more clearer picture of the scavenging process.

So look at PSPromotionManager::copy_unmarked_to_survivor_space(). PS is Parallel Scavenge. And this method is just copying the unmarked (or uncopied) objects from Eden and S0 suvivor space to S1 survivor space as part of the scavenging process.

And this is where the bug lies.

A note on Eden, S0 and S1

Eden, S0, and S1 are the three contiguous memory regions that make up the Young Generation, designed to separate brand-new objects from objects that have already survived a garbage collection cycle.

Almost every new object created with new is allocated directly into Eden. S0 and S1 act as a staging area (a “waiting room”) for objects that survived Eden, giving short-lived objects one or more extra cycles to die before they are permanently promoted to the Old Generation.

Why 2 parts - S0 and S1? At any given time, one Survivor space is actively holding objects (From), and the other is 100% empty (To).

· · ─ ·✶· ─ · ·