<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Emma's Blog - rust</title><link>https://emmatyping.dev/</link><description/><atom:link href="https://emmatyping.dev/feeds/rust/rss.xml" rel="self"/><lastBuildDate>Tue, 14 Oct 2025 00:00:00 -0700</lastBuildDate><item><title>Finding a miscompilation in Rust/LLVM</title><link>https://emmatyping.dev/finding-a-miscompilation-in-rustllvm.html</link><description>&lt;p&gt;Among my friends I have a reputation for &lt;del&gt;causing&lt;/del&gt; stumbling across esoteric error messages. Whether that is &lt;code&gt;SSL read: I/O error: Success&lt;/code&gt; (caused by a layered SSH connection hangup on Windows), or that time I tried installing NixOS on my laptop and &lt;code&gt;os-prober&lt;/code&gt; failed to start (this was several years ago, so I am sure it is no longer an issue). I attribute these oddities to my curiosity, particularly around trying things that may or may not work and seeing if they do. Recently, I was trying to complete an item from &lt;a href="https://notes.emmatyping.dev/share/ossTODO"&gt;my OSS TODO list&lt;/a&gt; when I came across a bug that stumped me for several days. Turns out sometimes even compilers have bugs...&lt;/p&gt;
&lt;p&gt;My goal was to build CPython with Rust implementations of common compression libraries to see if the Rust libraries could be supported. &lt;strong&gt;C&lt;/strong&gt;Python relies on &lt;strong&gt;C&lt;/strong&gt; code to do many performance sensitive activities such as &lt;a href="https://docs.python.org/3.14/library/math.html"&gt;&lt;code&gt;math&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://docs.python.org/3.14/library/compression.html"&gt;&lt;code&gt;compression&lt;/code&gt;&lt;/a&gt;. I had recently read about the &lt;a href="https://trifectatech.org/"&gt;Trifecta Tech Foundation&lt;/a&gt;'s initiative to re-write popular compression libraries in Rust. So far as of September 2025, they have pure-Rust re-implementations of &lt;a href="https://github.com/trifectatechfoundation/zlib-rs"&gt;zlib&lt;/a&gt; (the library used for zip and gzip files), and &lt;a href="https://github.com/trifectatechfoundation/libbzip2-rs"&gt;bzip2&lt;/a&gt; that are available for use.&lt;/p&gt;
&lt;p&gt;These Rust libraries not only bring increased memory safety, they're also &lt;a href="https://trifectatechfoundation.github.io/zlib-rs-bench/"&gt;as fast or faster than their C counter-parts&lt;/a&gt;. Additionally, zlib-rs is widely deployed in Firefox, to the point that it may have &lt;a href="https://github.com/trifectatechfoundation/zlib-rs/issues/306"&gt;tripped over a CPU hardware bug(!)&lt;/a&gt;. So I had confidence that at least zlib-rs would work out of the box.&lt;/p&gt;
&lt;p&gt;To add support for these libraries to CPython, I made &lt;a href="https://github.com/emmatyping/cpython/tree/build-with-rust-compression-libs"&gt;a branch with changes to the autoconf script&lt;/a&gt; to search for the Rust libraries through &lt;code&gt;pkg-config&lt;/code&gt;. I built &lt;a href="https://github.com/trifectatechfoundation/zlib-rs/tree/main/libz-rs-sys-cdylib"&gt;zlib-rs's C library&lt;/a&gt; with &lt;code&gt;RUSTFLAGS="-Ctarget-cpu=native"&lt;/code&gt; for maximum speed, and then pointed CPython's build process to the built zlib_rs library. Everything built just fine. Next, I wanted to run the CPython zlib test suite to verify zlib-rs was working correctly. I mostly did this to make sure I had built things properly, I had no doubts the tests would pass.&lt;/p&gt;
&lt;p&gt;&lt;img alt="A screenshot of test failures. The test_wbits and test_combine_no_iv tests in test_zlib failed." src="https://emmatyping.dev/static/zlib_test_failure.png" /&gt;&lt;/p&gt;
&lt;p&gt;And yet. I was shocked! zlib-rs is used in Firefox, cargo, and many other widely used tools and applications. Hard to believe it would have a glaring bug that would be surfaced by CPython's test suite. At first I assumed I had somehow made a mistake when building. I realized I had used my system zlib header when building, so maybe there was some weirdness with symbol compatibility?? No, re-building CPython pointing to the zlib-rs include directory didn't fix it.
I tried running &lt;code&gt;cargo test&lt;/code&gt; in the zlib-rs directory to make sure there wasn't something wrong I could catch there. No failures occurred.&lt;/p&gt;
&lt;p&gt;At this point I was convinced it was probably a bug with how I was building things, or a bug in the cdylib (Rust lingo for "C library") wrapping zlib-rs since test Rust tests passed but the tests in CPython failed. To make my testing simpler, I captured the state of the &lt;a href="https://github.com/python/cpython/blob/c50d794c7bb81f31d1b977e63d0faba0b926a168/Lib/test/test_zlib.py#L169-L174"&gt;&lt;code&gt;test_zlib.test_combine_no_iv&lt;/code&gt; test&lt;/a&gt; using PDB and wrote a C program which does the same thing as the test, with deterministic inputs:&lt;/p&gt;
&lt;div class="codehilite" style="background: #0d1117"&gt;&lt;pre style="line-height: 125%;"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span style="color: #8b949e; font-weight: bold; font-style: italic"&gt;#include&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #8b949e; font-style: italic"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style="color: #8b949e; font-weight: bold; font-style: italic"&gt;#include&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #8b949e; font-style: italic"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;
&lt;span style="color: #8b949e; font-weight: bold; font-style: italic"&gt;#include&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #8b949e; font-style: italic"&gt;&amp;quot;zlib.h&amp;quot;&lt;/span&gt;

&lt;span style="color: #ff7b72"&gt;int&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #d2a8ff; font-weight: bold"&gt;main&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;()&lt;/span&gt;
&lt;span style="color: #e6edf3"&gt;{&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #ff7b72"&gt;unsigned&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72"&gt;char&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;a[&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;32&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;]&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;{&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x88&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x64&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x15&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xce&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x5e&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x3b&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x8d&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x35&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xdb&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xd2&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xb5&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xfa&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x8e&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xa7&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x73&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x10&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x66&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x83&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x1b&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xd1&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xde&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x0f&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x25&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x86&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xeb&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xe5&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x42&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x44&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xad&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x62&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xff&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x11&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;};&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;uInt&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk_a&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;crc32(&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;a,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;32&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;);&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #ff7b72"&gt;unsigned&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72"&gt;char&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;b[&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;64&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;]&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;{&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x31&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xb8&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xce&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x94&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x4d&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x2b&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xb9&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x7e&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xd5&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x81&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x7f&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xc2&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x40&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xbf&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x3d&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xa5&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x25&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xa5&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xf9&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xdf&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x53&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x68&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xc4&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xf6&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xbe&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x06&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x7d&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xf3&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xc7&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xdc&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x5b&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x84&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xce&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xd2&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xb2&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xeb&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x87&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x62&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x60&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xe3&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x10&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x05&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x64&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x59&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x15&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xc4&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x2d&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x78&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xc8&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xf3&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x14&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x38&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x87&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x39&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xb3&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x58&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;                        &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xb5&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x95&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x07&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x25&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xd9&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xc1&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0xac&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0x04&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;};&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;uInt&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk_b&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;crc32(&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;b,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;64&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;);&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #ff7b72"&gt;unsigned&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72"&gt;char&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;buff[&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;96&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;];&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;memcpy(buff,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;a,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;32&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;);&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;memcpy(buff&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;+&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;32&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;b,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;64&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;);&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;uInt&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;crc32(&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;buff,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;96&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;);&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;uInt&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk_combine&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;crc32_combine(chk_a,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk_b,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;64&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;);&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;printf(&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;chk (%u) = chk_combine (%u)? %s&lt;/span&gt;&lt;span style="color: #79c0ff"&gt;\n&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk_combine,&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;==&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;chk_combine&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;?&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;True&amp;quot;&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;:&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;False&amp;quot;&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;);&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;&lt;span style="color: #ff7b72"&gt;return&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;(&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;0&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;);&lt;/span&gt;
&lt;span style="color: #e6edf3"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This program also failed. Hm, okay, not an issue with CPython at least. I then translated the above test into Rust to add to the zlib-rs test suite, since the Rust tests passed. If it failed I could more easily debug the issue.&lt;/p&gt;
&lt;div class="codehilite" style="background: #0d1117"&gt;&lt;pre style="line-height: 125%;"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span style="color: #79c0ff; font-weight: bold"&gt;diff --git a/zlib-rs/src/crc32/combine.rs b/zlib-rs/src/crc32/combine.rs&lt;/span&gt;
&lt;span style="color: #79c0ff; font-weight: bold"&gt;index 40e3745..65c0143 100644&lt;/span&gt;
&lt;span style="color: #ffa198; background-color: #490202"&gt;--- a/zlib-rs/src/crc32/combine.rs&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+++ b/zlib-rs/src/crc32/combine.rs&lt;/span&gt;
&lt;span style="color: #79c0ff"&gt;@@ -66,6 +66,26 @@ mod test {&lt;/span&gt;

&lt;span style="color: #6e7681"&gt; &lt;/span&gt;   use crate::crc32;

&lt;span style="color: #56d364; background-color: #0f5323"&gt;+    #[test]&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+    fn test_crc32_combine_no_iv() {&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+        for _ in 0..1000 {&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            let a: &amp;amp;[u8] = &amp;amp;[0x88, 0x64, 0x15, 0xce, 0x5e, 0x3b, 0x8d, 0x35, 0xdb, 0xd2, 0xb5, 0xfa, 0x8e, 0xa7, 0x73, 0x10, 0x66, 0x83, 0x1b, 0xd1, 0xde, 0x0f, 0x25, 0x86, 0xeb, 0xe5, 0x42, 0x44, 0xad, 0x62, 0xff, 0x11];&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            let b: &amp;amp;[u8] = &amp;amp;[0x31, 0xb8, 0xce, 0x94, 0x4d, 0x2b, 0xb9, 0x7e, 0xd5, 0x81, 0x7f, 0xc2, 0x40, 0xbf, 0x3d, 0xa5, 0x25, 0xa5, 0xf9, 0xdf, 0x53, 0x68, 0xc4, 0xf6, 0xbe, 0x06, 0x7d, 0xf3, 0xc7, 0xdc, 0x5b, 0x84, 0xce, 0xd2, 0xb2, 0xeb, 0x87, 0x62, 0x60, 0xe3, 0x10, 0x05, 0x64, 0x59, 0x15, 0xc4, 0x2d, 0x78, 0xc8, 0xf3, 0x14, 0x38, 0x87, 0x39, 0xb3, 0x58, 0xb5, 0x95, 0x07, 0x25, 0xd9, 0xc1, 0xac, 0x04];&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            let both: &amp;amp;[u8] = &amp;amp;[0x88, 0x64, 0x15, 0xce, 0x5e, 0x3b, 0x8d, 0x35, 0xdb, 0xd2, 0xb5, 0xfa, 0x8e, 0xa7, 0x73, 0x10, 0x66, 0x83, 0x1b, 0xd1, 0xde, 0x0f, 0x25, 0x86, 0xeb, 0xe5, 0x42, 0x44, 0xad, 0x62, 0xff, 0x11, 0x31, 0xb8, 0xce, 0x94, 0x4d, 0x2b, 0xb9, 0x7e, 0xd5, 0x81, 0x7f, 0xc2, 0x40, 0xbf, 0x3d, 0xa5, 0x25, 0xa5, 0xf9, 0xdf, 0x53, 0x68, 0xc4, 0xf6, 0xbe, 0x06, 0x7d, 0xf3, 0xc7, 0xdc, 0x5b, 0x84, 0xce, 0xd2, 0xb2, 0xeb, 0x87, 0x62, 0x60, 0xe3, 0x10, 0x05, 0x64, 0x59, 0x15, 0xc4, 0x2d, 0x78, 0xc8, 0xf3, 0x14, 0x38, 0x87, 0x39, 0xb3, 0x58, 0xb5, 0x95, 0x07, 0x25, 0xd9, 0xc1, 0xac, 0x04];&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            let chk_a = crc32(0, &amp;amp;a);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            assert_eq!(chk_a, 101488544);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            let chk_b = crc32(0, &amp;amp;b);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            assert_eq!(chk_b, 2995985109);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            let combined = crc32_combine(chk_a, chk_b, 64);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            assert_eq!(combined, 2546675245);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            let chk_both = crc32(0, &amp;amp;both);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            assert_eq!(chk_both, 3010918023);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+            assert_eq!(combined, chk_both);&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+        }&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+    }&lt;/span&gt;
&lt;span style="color: #56d364; background-color: #0f5323"&gt;+&lt;/span&gt;
&lt;span style="color: #6e7681"&gt; &lt;/span&gt;   #[test]
&lt;span style="color: #6e7681"&gt; &lt;/span&gt;   fn test_crc32_combine() {
&lt;span style="color: #6e7681"&gt; &lt;/span&gt;       ::quickcheck::quickcheck(test as fn(_) -&amp;gt; _);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Running &lt;code&gt;cargo test&lt;/code&gt; passed! I was at my wits end! How could the C code fail but the Rust code succeed??&lt;/p&gt;
&lt;p&gt;I felt like I had enough information that I reported the issue to zlib-rs. Let me interrupt this story to mention that I really want to thank Folkert de Vries (maintainer of zlib-rs) for help debugging this. They were extremely friendly and helpful in figuring out what was going wrong. Folkert responded to my issue that my C program sample works for them!
Why would my machine be any different? I was running in the WSL at the time, maybe that could cause weirdness? I decided to write up a Containerfile to ensure I had a clean environment:&lt;/p&gt;
&lt;div class="codehilite" style="background: #0d1117"&gt;&lt;pre style="line-height: 125%;"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span style="color: #ff7b72"&gt;FROM&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;ubuntu:24.04&lt;/span&gt;

&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;apt-get&lt;span style="color: #6e7681"&gt; &lt;/span&gt;update&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;\&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;    &lt;/span&gt;apt-get&lt;span style="color: #6e7681"&gt; &lt;/span&gt;install&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-y&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;\&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;        &lt;/span&gt;build-essential&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;\&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;        &lt;/span&gt;curl&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;\&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;        &lt;/span&gt;git&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;\&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;        &lt;/span&gt;pkg-config&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;\&lt;/span&gt;
&lt;span style="color: #6e7681"&gt;        &lt;/span&gt;libssl-dev

&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;curl&lt;span style="color: #6e7681"&gt; &lt;/span&gt;https://sh.rustup.rs&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-sSf&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;|&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;bash&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-s&lt;span style="color: #6e7681"&gt; &lt;/span&gt;--&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-y
&lt;span style="color: #ff7b72"&gt;ENV&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;PATH&lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;/root/.cargo/bin:${&lt;/span&gt;&lt;span style="color: #79c0ff"&gt;PATH&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;}&amp;quot;&lt;/span&gt;
&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;curl&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-sSL&lt;span style="color: #6e7681"&gt; &lt;/span&gt;https://apt.llvm.org/llvm-snapshot.gpg.key&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;|&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;apt-key&lt;span style="color: #6e7681"&gt; &lt;/span&gt;add&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-
&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;echo&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;deb http://apt.llvm.org/noble/ llvm-toolchain-noble-20 main&amp;quot;&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&amp;gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;/etc/apt/sources.list.d/llvm.list
&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;apt-get&lt;span style="color: #6e7681"&gt; &lt;/span&gt;update&lt;span style="color: #6e7681"&gt;  &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;apt-get&lt;span style="color: #6e7681"&gt; &lt;/span&gt;upgrade&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-y&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;apt-get&lt;span style="color: #6e7681"&gt; &lt;/span&gt;install&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-y&lt;span style="color: #6e7681"&gt; &lt;/span&gt;clang-20
&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;cargo&lt;span style="color: #6e7681"&gt; &lt;/span&gt;install&lt;span style="color: #6e7681"&gt; &lt;/span&gt;cargo-c
&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;mkdir&lt;span style="color: #6e7681"&gt; &lt;/span&gt;/scratch
&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;git&lt;span style="color: #6e7681"&gt; &lt;/span&gt;clone&lt;span style="color: #6e7681"&gt; &lt;/span&gt;https://github.com/trifectatechfoundation/zlib-rs.git&lt;span style="color: #6e7681"&gt; &lt;/span&gt;/scratch/zlib-rs
&lt;span style="color: #ff7b72"&gt;COPY&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;./test.c&lt;span style="color: #6e7681"&gt; &lt;/span&gt;/scratch/zlib-rs/libz-rs-sys-cdylib/test.c
&lt;span style="color: #ff7b72"&gt;WORKDIR&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;/scratch/zlib-rs/libz-rs-sys-cdylib&lt;/span&gt;
&lt;span style="color: #ff7b72"&gt;ENV&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;RUSTFLAGS&lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;-Ctarget-cpu=native&amp;quot;&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;#&lt;span style="color: #6e7681"&gt; &lt;/span&gt;comment&lt;span style="color: #6e7681"&gt; &lt;/span&gt;this&lt;span style="color: #6e7681"&gt; &lt;/span&gt;out&lt;span style="color: #6e7681"&gt; &lt;/span&gt;to&lt;span style="color: #6e7681"&gt; &lt;/span&gt;fix&lt;span style="color: #6e7681"&gt; &lt;/span&gt;the&lt;span style="color: #6e7681"&gt; &lt;/span&gt;bug
&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;cargo&lt;span style="color: #6e7681"&gt; &lt;/span&gt;cbuild&lt;span style="color: #6e7681"&gt; &lt;/span&gt;--release
&lt;span style="color: #ff7b72"&gt;RUN&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;clang-20&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-o&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;test&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;test.c&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-I&lt;span style="color: #6e7681"&gt; &lt;/span&gt;./include/&lt;span style="color: #6e7681"&gt; &lt;/span&gt;-static&lt;span style="color: #6e7681"&gt; &lt;/span&gt;./target/x86_64-unknown-linux-gnu/release/libz_rs.a
&lt;span style="color: #ff7b72"&gt;ENV&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #79c0ff"&gt;LD_LIBRARY_PATH&lt;/span&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;target/x86_64-unknown-linux-gnu/release/&amp;quot;&lt;/span&gt;
&lt;span style="color: #ff7b72"&gt;ENTRYPOINT&lt;/span&gt;&lt;span style="color: #6e7681"&gt; &lt;/span&gt;&lt;span style="color: #e6edf3"&gt;[&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;quot;./test&amp;quot;&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;While experimenting with setting up this container, I found a lead at last! If I compiled with &lt;code&gt;RUSTFLAGS="-Ctarget-cpu=native"&lt;/code&gt;, the program gave the wrong results. If I compiled &lt;em&gt;without&lt;/em&gt; using native code generation, the program worked correctly. Bizarre!!&lt;/p&gt;
&lt;p&gt;Backing up a bit, let me explain what &lt;code&gt;RUSTFLAGS="-Ctarget-cpu=native"&lt;/code&gt; actually does (if you know already, please skip to the next paragraph). Compilers like &lt;code&gt;rustc&lt;/code&gt; have feature flags for each target (aka OS + CPU architecture family) which allows them to optionally emit code that uses features of processors. For example, most x86 processors have &lt;code&gt;sse2&lt;/code&gt;, and ARM64 processors have NEON or SVE. Newer processes usually come with newer features which provide optimized implementations of some useful thing, for example some x86 processors has optimized implementations of SHA hashing. Since not all computers have every feature, these need to be opted into at compile time. In the case of &lt;code&gt;RUSTFLAGS="-Ctarget-cpu=native"&lt;/code&gt; I'm telling Rust "use all the features for my current processor." This is a way to eke out the most performance from a program. But in this case, it meant I had a bug on my hands! Folkert (maintainer of zlib-rs) suggested I try to narrow down exactly which instruction set extension was causing the issue. After a bit of binary searching, I found out it was &lt;code&gt;avx512vl&lt;/code&gt;. AVX is an extension to provide &lt;a href="https://en.wikipedia.org/wiki/Single_instruction,_multiple_data"&gt;SIMD&lt;/a&gt; and AVX512-VL is an extension which allows interoperability between 128/256-bit wide SIMD and faster 512-bit wide SIMD. This made a lot of sense in some ways, after all, I have an AMD R9 9950X, and one of it's features is AVX512 support! But how exactly did these AVX512 instructions get into the final binary?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;:&lt;br&gt; As pointed out in a message on Mastodon, AVX512-VL is actually 11 years old! It was first introduced in Intel AVX512 implementations. However, AVX512 support in Rust is relatively new.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So enabling AVX512 was the culprit for the bug in crc32 calculations. Skimming over the zlib-rs code, I was a bit surprised to find that it does not explicitly use AVX-512 &lt;em&gt;anywhere&lt;/em&gt;! In fact it uses the older SSE4.1 instruction set (presumably for maximum portability). So why was AVX512-VL causing these issues? Unfortunately, I don't know for sure. But I have a theory.&lt;/p&gt;
&lt;p&gt;Rust uses LLVM as it's default backend (the bit of the compiler that emits instructions/binaries). LLVM probably realized it could use AVX512-VL instructions (available on my machine) to speed up the SSE4.1 code that zlib-rs is using. However, AVX512-VL is new enough that there was a bug in the compiler - a miscompilation - and the wrong code was emitted. I haven't found a smoking gun issue but &lt;a href="https://github.com/llvm/llvm-project/issues?q=is%3Aissue%20state%3Aclosed%20avx512vl"&gt;it is probably one of these&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I am happy to report that this issue does not present itself with Rust 1.90+ or the latest release of zlib-rs. Many thanks again to Folkert for not only helping figure out the source of the issue, but also adding a mitigation to zlib-rs and cutting a new release to work around the miscompilation! Now the CPython test suite passes when linked against zlib-rs and I can continue my experiments...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Emma Smith</dc:creator><pubDate>Tue, 14 Oct 2025 00:00:00 -0700</pubDate><guid>tag:emmatyping.dev,2025-10-14:/finding-a-miscompilation-in-rustllvm.html</guid><category>misc</category><category>python</category><category>rust</category><category>compression</category></item><item><title>New Rust crate: generational-arena-dom</title><link>https://emmatyping.dev/new-rust-crate-generational-arena-dom.html</link><description>&lt;p&gt;I've just published a new crate someone may find interesting. I recently had a take-home assessment where I used the &lt;a href="https://servo.org/"&gt;Servo project's&lt;/a&gt; &lt;code&gt;html5ever&lt;/code&gt; HTML parser crate. &lt;a href="https://github.com/servo/html5ever"&gt;&lt;code&gt;html5ever&lt;/code&gt;&lt;/a&gt; is the main crate that servo uses to parse HTML content on the web. This crate is very customizable, and you have to bring your own implementation of the DOM (which means you can handle memory management however you want!). They provide an example implementation of the DOM that uses &lt;code&gt;Rc&amp;lt;RefCell&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt;s all over, which is a bit annoying to use with Rust's borrow checking model. This becomes particularly frustrating when dealing with frequent DOM mutations, as was the case in my project.&lt;/p&gt;
&lt;p&gt;Fortunately, I recalled the benefits of using arenas for ergonomic memory management when working with trees. I also had recently read about &lt;a href="https://verdagon.dev/blog/hybrid-generational-memory"&gt;Vale's generational arena usage&lt;/a&gt;, and I was inspired to build a DOM implementation based on a generational arena design. Generational arenas are nice because they don't suffer from &lt;a href="https://en.wikipedia.org/wiki/ABA_problem"&gt;the ABA problem&lt;/a&gt;, so it is thread-safe to add, update, and delete nodes in the DOM. I ended up coming across the &lt;a href="https://crates.io/crates/generational-indextree"&gt;generational-indextree&lt;/a&gt; crate, which uses tokens instead of references to refer to tree members, which made working with mutable elements of the DOM much easier!&lt;/p&gt;
&lt;p&gt;Anyway, I encourage anyone who is interested to learn more to check out the project &lt;a href="https://github.com/emmatyping/generational-arena-dom"&gt;on my github&lt;/a&gt; or &lt;a href="https://crates.io/crates/generational-arena-dom"&gt;on the project page on crates.io&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Emma Smith</dc:creator><pubDate>Sat, 08 Jul 2023 00:00:00 -0700</pubDate><guid>tag:emmatyping.dev,2023-07-08:/new-rust-crate-generational-arena-dom.html</guid><category>misc</category><category>html</category><category>rust</category></item><item><title>Rust CLI tools apt repo</title><link>https://emmatyping.dev/rust-cli-tools-apt-repo.html</link><description>&lt;blockquote&gt;
&lt;p&gt;tl;dr
Go to &lt;a href="http://apt.cli.rs"&gt;http://apt.cli.rs&lt;/a&gt; and follow the instructions to add the apt repo&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I guess its only fair to start my new blogging kick by catching up on a project I'd worked on several months ago which is pretty much "done." I really like several Rust tools, such as &lt;a href="https://github.com/BurntSushi/ripgrep"&gt;&lt;code&gt;ripgrep&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://github.com/sharkdp/hyperfine"&gt;&lt;code&gt;hyperfine&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://github.com/sharkdp/fd"&gt;&lt;code&gt;fd&lt;/code&gt;&lt;/a&gt;. I wanted an easy way to install them on Debian-based systems that may not have them in the official repos, so I needed to create my own apt repo. I was originally considering making a ppa, or private package archive, since I mainly use Ubuntu, but I decided I wanted these tools available on Debian as well, so my only option was an apt repo.&lt;/p&gt;
&lt;p&gt;It turns out that apt repos are really simple, you just need to serve a directory with e.g. nginx. It took me a while to find a tool I liked using for making the apt repo, however. I started with trying &lt;code&gt;reprepro&lt;/code&gt;, but I found it was more annoying to use than I wanted, so I ended up using &lt;a href="https://www.aptly.info/"&gt;&lt;code&gt;aptly&lt;/code&gt;&lt;/a&gt;, a newer apt repo management tool.&lt;/p&gt;
&lt;p&gt;When setting up the repo, my criteria for inclusion were:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the tool must be able to build &lt;code&gt;*.deb&lt;/code&gt; packages. This is the package format for Debian/Ubuntu so definitely required&lt;/li&gt;
&lt;li&gt;the tool must build those packages. I wanted to use the official/same binaries as those in the Github release&lt;/li&gt;
&lt;li&gt;the binary packages must be statically musl-linked. Since various versions of Debian/Ubuntu use different versions of glibc, you can get version compatibility errors if you don't statically link. Statically linking to musl is &lt;em&gt;much&lt;/em&gt; easier than glibc, so this seemed to be the best route to avoid compatibility errors.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With these criteria in hand, I then set up the apt repo with the CLI tools that fit and wrote a script to try updating all of the packages, pulling from the official Github releases. I have subscribed to releases on Github from all the repos that are currently included in the apt repo so all I have to do is run the script when a new release comes out from one of the repos. I may end up setting up a cronjob that runs this update script, but I'm weighing whether it is worth any potential risks...&lt;/p&gt;
&lt;p&gt;Anyway, if you are interested in using this apt repo, head on over to &lt;a href="http://apt.cli.rs"&gt;https://apt.cli.rs&lt;/a&gt;, I've written up the commands you need to get started using the apt repo.&lt;/p&gt;
&lt;p&gt;If you have suggestions for Rust CLI tools that should be included, please &lt;a href="https://github.com/emmatyping/apt.cli.rs/issues/new"&gt;open an issue on the github&lt;/a&gt;! If a package doesn't build musl-linked Debian packages, consider opening an issue on that project to add it. However, please &lt;em&gt;do not&lt;/em&gt; spam maintainers asking for these packages to be built. I unfortunately do not have time to contribute to the packaging of every great Rust CLI tool, but I can add them to the apt repo if they are packaged already!&lt;/p&gt;
&lt;p&gt;Anyway, hopefully someone else finds it useful!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Emma Smith</dc:creator><pubDate>Sat, 26 Nov 2022 00:00:00 -0800</pubDate><guid>tag:emmatyping.dev,2022-11-26:/rust-cli-tools-apt-repo.html</guid><category>misc</category><category>rust</category><category>packaging</category></item></channel></rss>