MoreRSS

site iconDaniel LemireModify

Computer science professor at the University of Quebec (TELUQ), open-source hacker, and long-time blogger.
Please copy the RSS to your reader, or quickly subscribe to:

Inoreader Feedly Follow Feedbin Local Reader

Rss preview of Blog of Daniel Lemire

Java4Shell: when base64 decoding leads to remote code execution?

2026-11-19 03:43:03

Let us start with an example that triggers the issue.

byte[] src = ("A".repeat(128) + "=").getBytes(StandardCharsets.US_ASCII);
Base64.getDecoder().decode(src);

One hundred and twenty-eight letters A and one =. The string is not valid base64, and a correct decoder rejects it. OpenJDK writes past the end of the array anyway. The bug is CVE-2026-114938. It has been in the JDK since 17.0.6. The bad write is in hand-written assembly.

There are claims that it leads to a major security vulnerability. I have not checked. But I care about the issue because I am indirectly involved.

Base64 turns three bytes into four ASCII characters. Email attachments are base64, and a string often ends with =. Java sizes the output before it checks the input. One trailing = counts as one byte of padding, so the allocated length is

3 × ((129 + 3) / 4) − 1 = 98.

So decode allocates 98 bytes, then calls the decoding routine.

On an x86-64 machine with AVX-512 VBMI, the routine takes the first 128 characters as two blocks and writes 96 bytes. That loop follows the algorithm from Muła & Lemire (2020). The character A is the value 0, so each block becomes 48 zero bytes. The store is masked to 48 bytes. Both stay inside the array.

One character is left, the =. The tail asks how many output bytes a remainder of that length should produce. In integer arithmetic,

(1 / 4) × 3 = 0.

The last character is =, so it subtracts one. The length is now −1.

The tail writes with an AVX-512 masked store. A masked store writes a byte only when the matching bit in a mask is set. See my article Modern vector programming with masked loads and stores. The mask comes from bzhi, which keeps the low n bits of a word of ones. The count it reads is the low 8 bits of n. The low 8 bits of −1 are 255. A count of 255 is past 64, and bzhi then clears nothing: every bit stays set, and the store writes the whole 64-byte register.

The store starts where the main loop stopped, at offset 96. The array ends at offset 97. Two bytes land in the array. The other 62, offsets 98 through 159, land past the end.

A 98-byte array, and a 64-byte store that starts at offset 96. Two bytes land inside the array and 62 fall past the end.

The function then returns. Java sees a = with no data character in the current group and throws IllegalArgumentException, with the message “Input byte array has wrong 4-byte ending unit”. The write has already happened.

So that’s the bug. As you can see, it is not the fault of our fancy algorithm. Thankfully for me.

Java is somewhat behind. The fast decoder stops at a space or a line break. Email wraps every 76 characters, and crypto (PEM) every 64, so the fast path in Java ends at the first line since it cannot handle spaces or line breaks.

The algorithm in Lemire (2027) removes the ignorable characters inside the vector loop. A clean block of 64 characters is decoded directly. A block that contains whitespace is compacted, and the alphabet characters are decoded from a small buffer. Since the middle of 2025 that decoder has shipped in Safari and in the Chromium browsers (Chrome, Edge, Brave), and in Node.js 25, Deno and Bun.

I have a pull request that ports the same decoders to PHP. Decoding a 64 KiB MIME input is 30 times faster on an Intel Xeon Gold 6548N, and 39 times faster on an AMD EPYC 9R45.

References

Muła, W., & Lemire, D. (2020). Base64 encoding and decoding at almost the speed of a memory copy. Software: Practice and Experience, 50(2), 89–97. (arXiv)

Lemire, D. (2027). Fast forgiving base64 decoding with SIMD instructions. ACM Transactions on Architecture and Code Optimization, 24(1). (arXiv)

Cognitive radius

2026-11-17 00:51:07

The most interesting change AI brings to software work is not faster coding. It is the expansion of context or range… what I call radius.

For decades the job was defined by the limits of what one person could hold in their head. You tended to specialize. You got good at one or two systems, one or two types of problems because keeping more than that coherent was expensive. Context switching hurt. Onboarding to a new project took days or weeks.

Simply issuing a pull request to a new open-source repository took days. You had to learn the conventions. Learn the code base. Find out how to test it. Find out how to run benchmarks.

With a good model and the right tools, you can load a large amount of relevant information into the session: the structure of several repositories, the conventions of different teams, recent issues, design decisions, performance constraints, the history of related libraries. You can ask questions across them, generate changes that stay consistent with local style, and keep more projects alive at once without the usual collapse in quality.

The unit of work stops being “the project I am currently deep in” and becomes something closer to “the set of projects I can keep coherent right now.”

This is not the same as having an assistant write code for you. It is more. The gain comes from the ability to hold and operate on a larger working set. You still decide what matters. You still check the results. What changes is how much of the surrounding system you can bring into focus at the same time.

The practical consequences are already visible for people who treat this as a skill rather than a novelty:

  • You can enter a codebase you have not touched in months and become useful again in an hour instead of a day, because the model can reconstruct the local idioms and the reasons behind them from the history and tests.
  • You can maintain consistency across languages or frameworks. The cost of switching is lower when the model carries the surface details.

None of this is automatic. The model does not magically expand your capacity. You have to learn how to feed it the right context, how to structure the questions so the answers stay grounded, and how to verify the parts that matter. The people who get the most out of it treat the model as a high-bandwidth interface to a larger set of information.

Most discussion of AI in programming still focuses on the speed of generating individual functions or on the fear that juniors will not learn. Those are real topics, but they miss the larger shift. The constraint that shaped the profession for a long time, the limited amount of context a single developer could keep active, has loosened. What you can usefully own has grown. Learning to operate at that new scale is the part that is still under-discussed and under-practiced.

The cognitive radius of an engineer is the distance (in projects, technologies, or time) over which they can still produce consistent, well-informed work. AI has increased that radius.

Reading JSON at 277 GB/s in Python

2026-11-14 22:53:50

Many systems log their data as JSON, one record per line (NDJSON). GitHub, for example, publishes every public event on its site that way through GH Archive: about a gigabyte per hour. Suppose you want to know which repositories received the most stars over eight hours. That is 7.6 GB of JSON, 2.2 million events.

In Python, the obvious code reads each line with the json module:

for line in open("events.ndjson", "rb"):
    event = json.loads(line)
    if event["type"] == "WatchEvent":
        stars[event["repo"]["name"]] += 1

It runs at 0.28 GB/s: 27 seconds for this file. The orjson package is twice as fast. The msgspec package, which decodes only the fields you declare, is eight times as fast.

A server has many cores, but a Python process with the GIL runs Python code on one core at a time. The usual answer is a pool of processes: cut the file in pieces, give a piece to each process, and add up their counts. Each process returns a small table, so little has to come back.

Python 3.14 has a free-threaded build (python3.14t) without the GIL: threads run Python code at the same time. The fastsimdjson library is threaded. You give it the data and a function. It parses the records on many threads, and calls your function on the thread that parsed the record. You name the fields you want. It reads only what leads to them, and your function receives those values, not the whole record.

import collections
import fastsimdjson
def starred(event):
    kind, repo = event
    if kind == "WatchEvent":
        return repo
names = fastsimdjson.map_many(
    starred, data, threads=192,
    fields=["/type", "/repo/name"], drop_none=True)
stars = collections.Counter(names)

The variable data is the file, mapped in memory. The two paths are the event type and the repository name. The drop_none parameter leaves out the records that are not stars.

I ran this on an AWS instance (c8a.metal-48xl) with two AMD EPYC 9R45 processors, 192 cores, using Python 3.14.10, orjson 3.13 and msgspec 0.22. The file is mapped before the clock starts. On one core, fastsimdjson reads 8 GB/s, three and a half times as fast as msgspec.

Counting stars on one core

With 192 threads, fastsimdjson counts the stars in the 7.6 GB in 55 milliseconds: 138 GB/s. The best pool of msgspec processes reaches 116 GB/s, at 96 processes. More processes make it slower.

Counting stars on all cores

fastsimdjson is ahead at every worker count. The lead shrinks from three and a half times on one core to about 20%.

By number of workers

Counting is the best case for processes, because little comes back. Often you want the values themselves. I took six files of about a gigabyte from an existing benchmark (Pison), and pulled a field or two from every record. On the twitter file, fastsimdjson reaches 277 GB/s. Four of the six files pass 100 GB/s. The other two are slower: one stays near 20 GB/s, still about four times as fast as msgspec, and one is a tie.

Six queries

My source code is available.

Telling your number parser what to expect

2026-11-10 20:44:09

Our fast_float library parses numbers such as -731.51 into binary floating-point values. It is part of GCC’s C++ standard library, among others. It is fast: often a few nanoseconds per number.

Parsing a number means making decisions. Is there a sign? How many digits are there? Where is the decimal point? Is the processor rounding in the default mode? Each decision is a branch in the code. Modern processors do not wait to know the outcome of a branch: they guess, and they keep working. When they guess right, the branch is nearly free. When they guess wrong, they throw away the work done since the branch, and that costs 15 to 20 cycles.

When all your numbers look alike, the processor guesses right. When they do not, it guesses wrong often. Consider a column of gains and losses: the sign is random. The processor cannot guess it, and it pays for its mistakes on about half of the numbers.

You often know things about your data that the parser cannot know. So we are adding performance hints to fast_float. You pass them as a template argument:

using fast_float::performance_hints;
double value;
auto answer = fast_float::from_chars<performance_hints::unpredictable_sign>(
    input.data(), input.data() + input.size(), value);

There are three hints, and you may combine them with |:

  • unpredictable_sign: the signs come in random order. The parser then handles the sign without a branch.
  • short_numbers: each number is given alone (the range holds just the number) and most numbers have at most 8 characters, such as 42.5 or -3.75, in varying shapes. The parser then reads all 8 characters at once with a few bitwise operations, without a branch on the number of digits or the position of the decimal point.
  • assume_round_to_nearest: your program never changes the rounding mode, so the parser does not check it.

The hints never change the results, with one exception. If you promise assume_round_to_nearest and some code in your program changes the rounding mode, some results will be wrong.

The hints work with fast_float::from_chars, fast_float::from_chars_advanced and fast_float::integer_times_pow10. Without hints, nothing changes.

How much do they help? I generated four kinds of data, 100,000 numbers each:

  • price changes, two decimals and random signs, such as -731.51 or 42.07;
  • account balances, two decimals and random signs, but longer, such as -731271.51;
  • sensor readings, all positive, but in varying shapes, such as 7, 1042, 42.5 and 0.375;
  • regular prices, always the same shape, such as 12.34.

I parse them on an Intel Xeon Gold 6338 (Ice Lake) with GCC 15, and on an Apple M4 Max with Apple clang 17. I report millions of numbers parsed per second: higher is better.

Millions of numbers per second, Intel Ice Lake, GCC 15

Millions of numbers per second, Apple M4 Max, Apple clang 17

Each kind of data has its own best combination.

Price changes have random signs and short numbers. On the Intel processor, adding unpredictable_sign and assume_round_to_nearest goes from 73 to 105 million numbers per second. All three hints do as well (104 million), and they are best on the Apple processor, going from 153 to 345 million numbers per second: more than twice as fast. Without hints, the Intel processor mispredicts a branch on 60% of the numbers. With all three hints, the mispredictions disappear.

Account balances also have random signs, but the numbers are too long for short_numbers. Here unpredictable_sign with assume_round_to_nearest is the right choice: from 61 to 82 million numbers per second on the Intel processor, and from 132 to 199 million on the Apple processor. Adding short_numbers makes things worse on the Intel processor (70 million): the hint does not apply to these numbers, yet we pay for it.

Sensor readings have no sign, but their shapes vary. Here short_numbers is the key: from 67 to 92 million numbers per second on the Intel processor, and from 123 to 250 million on the Apple processor. The hint unpredictable_sign is harmful: there is no sign to predict, and the branch-free code is more work. On the Intel processor, it goes down to 57 million.

Regular prices are the easy case: all positive, always the same shape. The processor guesses everything right. On the Intel processor, the best choice is no hint at all (152 million numbers per second), and short_numbers makes things much slower (104 million). The Apple processor is different: short_numbers still helps, from 251 to 355 million numbers per second.

With Clang 21 on the Intel processor, the results are similar: the same hints win on the same data.

So the hints are not free. They trade branches, which are cheap when the processor guesses right, for more instructions. They pay off when the processor would guess wrong.

The hints are currently in a pull request.

My source code is available.

A performance trap: when store forwarding fails

2026-11-05 21:06:20

A store sits in the store buffer. A later load can take those bytes from the buffer instead of waiting for the data cache.

A store does not go straight into the cache. Waiting for that write would stall the processor. It sits in a store buffer, and a later load can take its bytes from that buffer. That is store forwarding.

Forwarding works when the load is the same size as the store, or smaller and completely inside it. It fails when the load is wider than the store, or when the two ranges only partly overlap. The load then waits until the store has reached the cache.

Here is an example in C. We have a buffer, that might have received data previously. Yet one byte is overwritten, and we try to load 8 bytes. It will typically cause a store forwarding failure.

uint64_t reload_byte(char *p, unsigned char v) {
    // Copy one byte from v into the buffer.
    memcpy(p, &v, 1);
    uint64_t w;
    // Copy eight bytes from the buffer into w.
    memcpy(&w, p, 8);
    return w;
}

A 4-byte store followed by an 8-byte load would cause the same issue. So would an 8-byte store followed by an 8-byte load that starts four bytes later: the ranges overlap.

You can sidestep the issue by working in a register, like the following example.

uint64_t update_byte(char *p, unsigned char v) {
    uint64_t x;
    // load from p to x
    memcpy(&x, p, 8);
    x = (x & ~0xffull) | v;
    // store the modified result to p
    memcpy(p, &x, 8);
    return x;
}

Further, if you store eight bytes and read those same eight back, a compiler might delete the reload.

I ran some experiments to see how many cycles various store/load operations take.

What the iteration does Xeon Gold 6548N Xeon 6975P-C EPYC 9R45 Graviton 5 Apple M4 Max
store 8, load 8, same address 1 1 1 5 5
store 8, load 4 at +4 5 5 7 6 5
store 4, load 8 19 19 14 5 5
store 4, then store 4, load 8 20 20 15 5 8

Cycles for a dependent store followed by a load

On the Xeons and the EPYC, a load that matches the store takes one cycle. A wider load takes 19 cycles on the Xeons and 14 on the EPYC.

On the M4 and Graviton 5, a wider load takes 5 cycles. Two stores bring the M4 to 8.

My source code is available.

Open source as a research method

2026-11-04 20:48:24

Many people still picture innovation as a line. The professor thinks and publishes. The engineer reads the paper and designs a product. The customer consumes it.

It rarely works that way. The industrial revolution was not set off by professors writing papers. It was driven by demand for cheaper and better clothing for the masses. Demand came first.

Michael Stonebraker made a related point in “Where We Have Failed” (2018). He argued that the database research community has lost its customer: “there are no ‘real world’ clients to keep us focused.” Without a customer, research drifts.

In 2019, JSON was everywhere, and parsing it was slow, at a few hundred megabytes per second. So we built simdjson. Today it parses JSON at gigabytes per second; I measured about 4.8 GB/s on twitter.json on a recent Intel Xeon.

We did not write the paper first. We wrote the code, then a blog post. We put it on GitHub and posted about it on social networks. Industry adopted it. Our first paper was published by the VLDB Journal, but it was more of an advertisement for our ideas and designs than the endpoint of the research.

Fred Brooks wrote that “the scientist builds in order to study; the engineer studies in order to build.” In my work, building is how I study.

Once simdjson parsed JSON quickly, a new bottleneck appeared: floating-point numbers. If everything else is fast, converting strings like 3.1416 into binary floats dominates the running time. That led to what is now called the Eisel-Lemire algorithm, which is exact and fast (Number Parsing at a Gigabyte per Second). It now sits inside the standard libraries of Rust, Go, .NET and C++ (through fast_float).

Then came the next bottleneck. Parsing JSON also means validating UTF-8, and security depends on getting that right. We got validation down to less than one instruction per byte. That code broke off into its own library, simdutf, which is now used by Node.js, Bun, Deno and web browsers for Unicode validation and transcoding.

I did not plan any of this from a mountaintop. Stonebraker said it well: “my ideas come from two sources: talking to real users with real problems and then trying to solve them.” Open source gives me those users. They show up with bug reports, adversarial inputs, exotic hardware and workloads I would never have imagined. Pull requests and code reviews are often a tougher form of peer review than most program committees. Reviewers who depend on your code read it more carefully than referees who skim your paper.

David Patterson, in “How to Build a Bad Research Center” (2014), wrote that for his successful projects, “the early indicators were not papers or awards, but non-researchers trying to use our ideas.” I agree.

Is open source still as important now that we have AI? If a model can write the code for you, why bother publishing yours?

What I see points the other way. More people than ever contribute to my projects, and more people than ever use my code and my ideas. AI tools seem to make it easier, not harder, for people to read an unfamiliar codebase, find a problem and send a fix.

When I delivered this message…

Last week (October 29, 2026), I was one of four invited speakers at the launch of SERIQ, the new Centre interuniversitaire de recherche en génie logiciel pour la société numérique. SERIQ continues the SEMTL (Software Engineering at Montreal) meetings that have brought together Montreal software engineering researchers for more than a decade. The new centre is backed by the Université de Montréal, Polytechnique Montréal, McGill University and the École de technologie supérieure, and it aims to cover all of Quebec. About a hundred people came to the Pavillon Jean-Coutu at the Université de Montréal for the event.

The other three guests were:

  • Lionel Briand (University of Ottawa and Lero, the Research Ireland centre for software at the University of Limerick), who spoke about how AI disrupts software testing strategies, and how it can also help fix them.
  • Marsha Chechik (University of Toronto), who spoke about designing the digital society, from human intent to reliable systems.
  • Yves Daoust (Ferme d’Hiver), an engineer who worked in telecommunications before founding a farming company. He looked back on forty years of software, from punched cards to AI agents, and asked why this moment is different.

Each of us had fifteen minutes. Afterwards, the four of us sat down for a panel on software research for society, moderated by Martin Robillard (McGill).

The panel at the SERIQ launch

My slides are online.