<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://prince776.dev/feed.xml" rel="self" type="application/atom+xml" /><link href="https://prince776.dev/" rel="alternate" type="text/html" /><updated>2026-11-27T08:49:34+00:00</updated><id>https://prince776.dev/feed.xml</id><title type="html">Prince Gupta</title><subtitle>Software engineer. Writing about low-level systems, codebases, and building things from scratch.</subtitle><author><name>Prince Gupta</name></author><entry><title type="html">Proving Mercurial CPU Cores Offline</title><link href="https://prince776.dev/blog/proving-mercurial-cores-offline/" rel="alternate" type="text/html" title="Proving Mercurial CPU Cores Offline" /><published>2026-11-18T00:00:00+00:00</published><updated>2026-11-18T00:00:00+00:00</updated><id>https://prince776.dev/blog/proving-mercurial-cores-offline</id><content type="html" xml:base="https://prince776.dev/blog/proving-mercurial-cores-offline/"><![CDATA[<h2 id="background">Background</h2>

<p>A few months ago I wrote about <a href="https://www.rubrik.com/blog/architecture/26/6/when-a-cpu-instruction-lies-debugging-silent-data-corruption-in-the-cloud">a CPU instruction that lied</a> on the Rubrik engineering blog.</p>

<p>For over a year, <code class="language-plaintext highlighter-rouge">AES-GCM</code> encryption in our storage engine occasionally produced a wrong auth tag. The culprit was a single cloud VM whose <code class="language-plaintext highlighter-rouge">PCLMULQDQ</code> instruction (the carry-less multiply behind <code class="language-plaintext highlighter-rouge">GHASH</code>) sometimes returned one extra bit. A carry-less multiplication producing a ghost carry like:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Operand A     : 0xc57ded290fecde69b4c724259c54dcf0
Operand B     : 0xf7db6ff66057b68c9b60371f02762cc4
Expected      : 0x556f27e77e03aa93a418d6d4672077c0
Actual        : 0x556f27e77e03ab93a418d6d4672077c0
</code></pre></div></div>

<p>Because the fault was inside <code class="language-plaintext highlighter-rouge">GHASH</code>, only the auth tag came out wrong. The ciphertext itself was fine (see the <a href="https://www.rubrik.com/blog/architecture/26/6/when-a-cpu-instruction-lies-debugging-silent-data-corruption-in-the-cloud">original blog</a> for more details on data recovery).</p>

<figure class="diagram gf" aria-labelledby="gf-cap">
  <svg viewBox="0 10 760 186" role="img" aria-label="Plaintext goes through AES-CTR to produce correct ciphertext. The ciphertext then feeds GHASH, a chain of multiply-by-H steps that produces the tag. A one-bit fault in the second multiply carries through the rest of the chain, so only the tag is wrong.">
    <defs>
      <marker id="gf-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
        <path d="M0,0 L10,5 L0,10 z" class="dg-head" />
      </marker>
      <marker id="gf-arrow-bad" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
        <path d="M0,0 L10,5 L0,10 z" class="gf-head-bad" />
      </marker>
    </defs>

    <!-- Encryption: plaintext -> AES-CTR -> ciphertext -->
    <rect class="dg-box" x="20" y="24" width="110" height="36" rx="5" />
    <text class="dg-t" x="75" y="46">Plaintext</text>
    <line class="dg-l" x1="130" y1="42" x2="168" y2="42" marker-end="url(#gf-arrow)" />
    <rect class="dg-box" x="170" y="24" width="100" height="36" rx="5" />
    <text class="dg-t" x="220" y="46">AES-CTR</text>
    <line class="dg-l" x1="270" y1="42" x2="298" y2="42" marker-end="url(#gf-arrow)" />
    <rect class="dg-box gf-ok" x="300" y="24" width="120" height="36" rx="5" />
    <text class="dg-t" x="360" y="46">Ciphertext ✓</text>

    <!-- Authentication: ciphertext -> GHASH (×H chain) -> tag -->
    <line class="dg-l" x1="360" y1="60" x2="360" y2="128" marker-end="url(#gf-arrow)" />
    <text class="dg-t dg-lab gf-grouplab" x="648" y="178">GHASH</text>
    <rect class="gf-group" x="300" y="104" width="360" height="80" rx="8" />
    <rect class="dg-box" x="320" y="130" width="80" height="34" rx="5" />
    <text class="dg-t" x="360" y="151">×H</text>
    <line class="dg-l" x1="400" y1="147" x2="438" y2="147" marker-end="url(#gf-arrow)" />
    <rect class="dg-box gf-bad" x="440" y="130" width="80" height="34" rx="5" />
    <text class="dg-t gf-bad-t" x="480" y="151">×H</text>
    <line class="gf-l-bad" x1="520" y1="147" x2="558" y2="147" marker-end="url(#gf-arrow-bad)" />
    <rect class="dg-box gf-bad" x="560" y="130" width="80" height="34" rx="5" />
    <text class="dg-t gf-bad-t" x="600" y="151">×H</text>
    <line class="gf-l-bad" x1="640" y1="147" x2="678" y2="147" marker-end="url(#gf-arrow-bad)" />
    <rect class="dg-box gf-tag" x="680" y="130" width="70" height="34" rx="5" />
    <text class="dg-t gf-tag-t" x="715" y="151">Tag ✗</text>

    <!-- The fault -->
    <g transform="translate(480 112)" class="gf-burst">
      <path d="M0,-9 L2.5,-2.5 L9,-2.5 L4,1.8 L5.8,8.5 L0,4.2 L-5.8,8.5 L-4,1.8 L-9,-2.5 L-2.5,-2.5 z" />
    </g>
    <text class="dg-t gf-note" x="480" y="94">one bit flipped</text>
  </svg>
  <figcaption id="gf-cap">The fault hit one multiply inside GHASH. Every later multiply carried the error into the tag. Encryption never goes through GHASH, so the ciphertext stayed correct.</figcaption>
</figure>

<p>Often we would see the effects of the corruption long after it happened; it was truly silent, especially in the early days. By then the machine was often gone: twice, the cloud provider recycled a suspect node before we could cordon it. So after root-causing and recovering all the data, I wanted to explore <strong>if it’s possible to attribute a Silent Data Corruption to a faulty CPU without actually capturing the machine</strong>.</p>

<p>It turns out you often can, and with a level of certainty that’s hard to argue with.</p>

<p><em>This experiment was done as part of my work at Rubrik; views are my own.</em></p>

<h2 id="mercurial-cores">Mercurial cores</h2>

<p>Google calls these <a href="https://sigops.org/s/conferences/hotos/2021/papers/hotos21-s01-hochschild.pdf">mercurial cores</a>: CPU cores that, for some instructions and some inputs, silently compute the wrong answer. Meta has reported <a href="https://arxiv.org/abs/2102.11245">the same phenomenon at fleet scale</a>. They don’t crash. They don’t fail standard stress tests. They just occasionally lie.</p>

<p>In a cloud environment like AKS, at a large scale, your system would be running on several different machines, increasing the likelihood of encountering a mercurial core. It’s not a matter of if, but when.</p>

<h2 id="the-idea">The idea</h2>

<p>If we emulate how a CPU executes a program (i.e., emulate the underlying assembly code), and then inject the right fault at the right time, we can reproduce the corrupted output. So the problem becomes a brute force search problem.</p>

<p>We can do it like this:</p>

<ol>
  <li>Take the exact instruction sequence that produced the output (<code class="language-plaintext highlighter-rouge">AES-GCM</code> here).</li>
  <li>Run it in an emulator where you control every instruction’s result.</li>
  <li>Define a set of plausible hardware faults — bit flips, word corruptions, stuck bits, on inputs, outputs, or specific registers.</li>
  <li>Inject each fault, one at a time, at every instruction, and check whether the emulated output matches the corrupted output exactly.</li>
</ol>

<figure class="diagram fs" aria-labelledby="fs-cap">
  <div class="fs-tags">
    <div class="fs-row"><span class="fs-label">observed</span><code class="fs-tag" data-observed=""></code></div>
    <div class="fs-row"><span class="fs-label">emulated</span><code class="fs-tag" data-emulated=""></code></div>
  </div>

  <p class="fs-step">1. Pick an instruction to fault</p>
  <div class="fs-trace" data-trace="" role="group" aria-label="Instructions"></div>

  <div class="fs-controls">
    <div class="fs-ctl">
      <span class="fs-step">2. Where</span>
      <div class="fs-seg" data-sites="" role="group" aria-label="Fault site"></div>
    </div>
    <label class="fs-ctl">
      <span class="fs-step">3. Flip bit <output data-bitval="">0</output></span>
      <input type="range" min="0" max="127" value="0" data-bit="" />
    </label>
  </div>

  <p class="fs-status" data-status="" aria-live="polite"></p>
  <button type="button" class="fs-find" data-find="">Find the match for me</button>

  <figcaption id="fs-cap">Each choice is one candidate fault. The emulator re-runs the encryption with that fault and compares the tag it gets with the one the faulty machine produced. Digits in brick don't match.</figcaption>
</figure>
<script>
(function () {
  var fig = document.currentScript.previousElementSibling;
  var OBS = "3f9c07d2a1e4b85c6d10f7a29e43bc81";
  var TRACE = ["movdqu","pxor","aesenc","aesenc","aesenc","aesenclast","pxor","pshufb","movdqa",
               "pclmulqdq","pclmulqdq","pclmulqdq","pxor","psrldq","pslldq","pxor","pclmulqdq",
               "pshufd","pxor","pclmulqdq","pxor","pxor","pshufb","movdqu"];
  var SITES = ["dst", "src1", "src2"];
  var MATCH = { insn: 10, site: "dst", bit: 72 }; // illustrative
  var state = { insn: -1, site: "dst", bit: 0 };

  var $ = function (s) { return fig.querySelector(s); };
  var emuEl = $("[data-emulated]"), statusEl = $("[data-status]");
  var bitEl = $("[data-bit]"), bitVal = $("[data-bitval]");
  $("[data-observed]").textContent = OBS;

  // Deterministic stand-in for "run the emulator with this fault":
  // a different pseudo-random tag per candidate, except the real match.
  function tagFor(key) {
    var h = 2166136261;
    for (var i = 0; i < key.length; i++) { h ^= key.charCodeAt(i); h = Math.imul(h, 16777619); }
    var s = "";
    for (var j = 0; j < 32; j++) {
      h ^= h << 13; h ^= h >>> 17; h ^= h << 5;
      s += "0123456789abcdef"[(h >>> 0) % 16];
    }
    return s;
  }

  var chips = TRACE.map(function (op, i) {
    var b = document.createElement("button");
    b.type = "button"; b.textContent = op; b.setAttribute("aria-pressed", "false");
    b.title = "Instruction " + (i + 1);
    b.addEventListener("click", function () { state.insn = i; render(); });
    $("[data-trace]").appendChild(b);
    return b;
  });
  var siteBtns = SITES.map(function (s) {
    var b = document.createElement("button");
    b.type = "button"; b.textContent = s;
    b.addEventListener("click", function () { state.site = s; render(); });
    $("[data-sites]").appendChild(b);
    return b;
  });
  bitEl.addEventListener("input", function () { state.bit = +bitEl.value; render(); });
  $("[data-find]").addEventListener("click", function () {
    state.insn = MATCH.insn; state.site = MATCH.site; state.bit = MATCH.bit; render();
  });

  function render() {
    chips.forEach(function (c, i) { c.setAttribute("aria-pressed", i === state.insn ? "true" : "false"); });
    siteBtns.forEach(function (b) { b.setAttribute("aria-pressed", b.textContent === state.site ? "true" : "false"); });
    bitEl.value = state.bit; bitVal.textContent = state.bit;

    var tag, hit = false;
    if (state.insn < 0) {
      tag = tagFor("no-fault");
    } else {
      hit = state.insn === MATCH.insn && state.site === MATCH.site && state.bit === MATCH.bit;
      tag = hit ? OBS : tagFor(state.insn + ":" + state.site + ":" + state.bit);
    }
    var diff = 0;
    emuEl.innerHTML = tag.split("").map(function (c, i) {
      if (c === OBS[i]) return c;
      diff++; return '<span class="fs-diff">' + c + "</span>";
    }).join("");

    fig.classList.toggle("fs-matched", hit);
    if (state.insn < 0) {
      statusEl.textContent = "No fault injected yet: the emulator computes the correct tag, which doesn't match what the faulty machine produced.";
    } else if (hit) {
      statusEl.textContent = "✓ Exact match. A single flipped bit in this pclmulqdq's result reproduces the corrupted tag.";
    } else {
      statusEl.textContent = "✗ No match: " + diff + " of 32 hex digits differ. " +
        (TRACE.length * SITES.length * 128 - 1).toLocaleString() + " other candidates to try.";
    }
  }
  render();
})();
</script>

<p>If some fault reproduces the corrupted output bit-for-bit, you have a concrete, checkable explanation: <em>instruction N, executing <code class="language-plaintext highlighter-rouge">PCLMULQDQ</code>, returned its result with bit <code class="language-plaintext highlighter-rouge">72</code> flipped</em>. If the search space is reasonable and the output is large enough, the chance of that happening by coincidence is essentially zero. <a href="#how-sure-can-you-be">More on that below</a>.</p>

<p>Crypto code makes this unusually tractable. Constant-time implementations have no data-dependent branches, so the instruction trace for a given input length is fixed. Every input of that length runs the same straight-line sequence, and “inject a fault at step N” is well defined.</p>

<h2 id="the-setup">The setup</h2>

<p>I had some leftover samples from the original investigation: a set of synthetic inputs we had encrypted on the faulty node, along with the corrupted outputs it produced.</p>

<h3 id="extracting-the-code">Extracting the code</h3>

<p>The first step was getting the exact instructions our <code class="language-plaintext highlighter-rouge">AES-GCM</code> path executed in production.</p>

<p>I had Claude pull the relevant assembly out of our production binaries — the <code class="language-plaintext highlighter-rouge">AES-NI</code> rounds, the <code class="language-plaintext highlighter-rouge">GHASH</code> multiply-and-reduce sequence, and the glue around them.</p>

<p>It’s important this is the <em>actual</em> code that ran, not a reference implementation; a different but equivalent implementation would have a different fault surface.</p>

<h3 id="the-emulator">The emulator</h3>

<p>I wrote a small emulator in Go. It’s very much makeshift: it only implements the instructions this code path needs — the SSE/AVX moves and shuffles, XORs and shifts, <code class="language-plaintext highlighter-rouge">AESENC</code>/<code class="language-plaintext highlighter-rouge">AESENCLAST</code>, and <code class="language-plaintext highlighter-rouge">PCLMULQDQ</code> — operating on a register file and a bit of memory.</p>

<p>The one design decision that mattered: every instruction goes through a hook that can corrupt its operands or result. Roughly:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c">// Illustrative, not the actual code.</span>
<span class="k">type</span> <span class="n">Fault</span> <span class="k">struct</span> <span class="p">{</span>
	<span class="n">Step</span>    <span class="kt">int</span>        <span class="c">// which executed instruction</span>
	<span class="n">Site</span>    <span class="n">Site</span>       <span class="c">// Src1, Src2, Dst, or a specific register</span>
	<span class="n">Pattern</span> <span class="n">Corruption</span> <span class="c">// BitFlip, ByteFlip, WordFlip, StuckAt0, StuckAt1, ...</span>
	<span class="n">Index</span>   <span class="kt">int</span>        <span class="c">// which bit / byte / word</span>
<span class="p">}</span>

<span class="k">func</span> <span class="p">(</span><span class="n">m</span> <span class="o">*</span><span class="n">Machine</span><span class="p">)</span> <span class="n">step</span><span class="p">(</span><span class="n">i</span> <span class="kt">int</span><span class="p">,</span> <span class="n">inst</span> <span class="n">Inst</span><span class="p">,</span> <span class="n">f</span> <span class="o">*</span><span class="n">Fault</span><span class="p">)</span> <span class="p">{</span>
	<span class="k">if</span> <span class="n">f</span> <span class="o">!=</span> <span class="no">nil</span> <span class="o">&amp;&amp;</span> <span class="n">f</span><span class="o">.</span><span class="n">Step</span> <span class="o">==</span> <span class="n">i</span> <span class="o">&amp;&amp;</span> <span class="n">f</span><span class="o">.</span><span class="n">Site</span><span class="o">.</span><span class="n">IsInput</span><span class="p">()</span> <span class="p">{</span>
		<span class="n">m</span><span class="o">.</span><span class="n">corrupt</span><span class="p">(</span><span class="n">inst</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
	<span class="p">}</span>
	<span class="n">m</span><span class="o">.</span><span class="n">exec</span><span class="p">(</span><span class="n">inst</span><span class="p">)</span>
	<span class="k">if</span> <span class="n">f</span> <span class="o">!=</span> <span class="no">nil</span> <span class="o">&amp;&amp;</span> <span class="n">f</span><span class="o">.</span><span class="n">Step</span> <span class="o">==</span> <span class="n">i</span> <span class="o">&amp;&amp;</span> <span class="n">f</span><span class="o">.</span><span class="n">Site</span><span class="o">.</span><span class="n">IsOutput</span><span class="p">()</span> <span class="p">{</span>
		<span class="n">m</span><span class="o">.</span><span class="n">corrupt</span><span class="p">(</span><span class="n">inst</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
	<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Before trusting it, I checked it against Go’s <code class="language-plaintext highlighter-rouge">crypto/aes</code> + <code class="language-plaintext highlighter-rouge">crypto/cipher</code> on many random inputs: with no fault injected, the emulator’s ciphertext and tag must match bit-for-bit.</p>

<h3 id="fault-models">Fault models</h3>

<p>At the time I was also chasing a different bug, so I built the emulator to support many kinds of corruption rather than just single bit flips: corruption of an instruction’s inputs, its output, or a specific register; and patterns like single-bit flips, whole-word flips, and others.</p>

<h3 id="the-search">The search</h3>

<p>Then brute force:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">f</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">allFaults</span><span class="p">(</span><span class="n">trace</span><span class="p">,</span> <span class="n">models</span><span class="p">)</span> <span class="p">{</span>
	<span class="n">out</span> <span class="o">:=</span> <span class="n">emu</span><span class="o">.</span><span class="n">Run</span><span class="p">(</span><span class="n">trace</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">f</span><span class="p">)</span>
	<span class="k">if</span> <span class="n">bytes</span><span class="o">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">corrupted</span><span class="p">)</span> <span class="p">{</span>
		<span class="n">matches</span> <span class="o">=</span> <span class="nb">append</span><span class="p">(</span><span class="n">matches</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
	<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>It’s embarrassingly parallel and cacheable (every candidate shares the same run up to its faulty step), so you can brute force far more faults than you’d expect.</p>

<h2 id="the-result">The result</h2>

<p><strong>It reproduced the corrupted outputs exactly.</strong></p>

<p>For every sample, the search landed on the same fault: a single <code class="language-plaintext highlighter-rouge">PCLMULQDQ</code> in the <code class="language-plaintext highlighter-rouge">GHASH</code> multiply, returning its result with bit <code class="language-plaintext highlighter-rouge">72</code> set. That’s the same ghost carry from the example above. Nothing else matched.</p>

<p>It took us over a year to pin this on one instruction. The emulator got there from the inputs and outputs alone.</p>

<h2 id="how-sure-can-you-be">How sure can you be?</h2>

<p>Very sure, as long as the fault model is narrow.</p>

<p>Look at it from the skeptic’s side. Suppose the corruption was caused by something else entirely, like a software bug or a memory error. Then to any single candidate fault, the observed 128-bit tag is just a random number, with a 1 in 2¹²⁸ chance of matching. Even across 16 million candidates (2²⁴), the chance of a coincidental match is about 1 in 2¹⁰⁴. That’s less likely than winning the Powerball jackpot three times in a row.</p>

<p><strong><a href="/scratchpad/how-sure-fault-replay/">How sure can you be? The details →</a></strong></p>

<h2 id="why-now-ai-made-this-practical">Why now: AI made this practical</h2>

<p>None of this is a new idea. Fault injection in emulators is decades old. What’s changed is the cost.</p>

<p>Building a custom emulator for one production code path used to take weeks: extracting the right instructions, implementing each opcode exactly, adding fault models, and validating it all against a reference. For a bug that may never come back, that’s hard to justify.</p>

<p>With Claude, it took a couple of days. Claude extracted the assembly and wrote most of the opcodes and fault models, and I spent my time verifying it against Go’s crypto library. When throwaway tools get this cheap, a lot of ideas that weren’t worth the effort suddenly are.</p>

<h2 id="limits">Limits</h2>

<ul>
  <li><strong>You need the exact inputs.</strong> Key, nonce, AAD, plaintext, and the binary that ran.</li>
  <li><strong>One fault at a time.</strong> Multi-fault searches blow up fast (more on that with the other bug :p).</li>
  <li><strong>Only the faults you model.</strong> Anything outside your fault model goes unnoticed.</li>
  <li><strong>It doesn’t identify the host.</strong> You still need logs to know which machine it was.</li>
</ul>

<h2 id="conclusion">Conclusion</h2>

<p>Silent data corruption is painful to debug because by the time you notice it, the faulty machine is usually gone. But a faulty CPU gets a specific instruction wrong in a specific way, and that leaves a fingerprint in the output. With the exact inputs and a small emulator, you can find that fingerprint without the machine.</p>

<p>So next time you hit a corruption you can’t explain, hold on to the inputs and the bad output before writing it off as a cosmic ray. You might be able to prove what actually happened.</p>

<p>This methodology and emulator also helped me catch another mercurial core causing SDC: the different bug I mentioned earlier. More on that in a future post.</p>]]></content><author><name>Prince Gupta</name></author><summary type="html"><![CDATA[Can you prove a Silent Data Corruption (SDC) was caused by a faulty CPU, without having the faulty machine?]]></summary></entry></feed>