gitmyhub

nullok

Shell ★ 11 updated 10y ago

Scripts that I used to write a blog post about section 7.24.1(2) of C11

This repository contains tools for exploring how C compilers behave when they're told whether function inputs can be null pointers or not. It's built around a specific rule in the C11 standard that says functions in the standard library assume all pointers you pass to them are valid (not null). The author created these scripts to test what actually happens to compiled code when you remove that assumption.

The way it works is fairly straightforward: the scripts compile several open-source software packages twice—once with the standard "pointers can't be null" assumption in place, and once with that assumption removed. Then it captures the disassembly (the low-level machine instructions generated by the compiler) for both versions and writes them to files so you can compare them side by side. The difference between these two versions shows what optimizations or code changes the compiler was making based on that null-pointer assumption.

To run this yourself, you need to manually set up two modified versions of your C standard library header files: one that keeps the original "nonnull" annotations (the standard assumption) and one that strips them out. The scripts then use these to recompile various packages. The whole process takes a while since it's rebuilding multiple software projects, but it's mostly automated—you just run one main script and let it work through the list.

This is useful for compiler developers, performance enthusiasts, or anyone curious about how the C standard's assumptions translate into real performance implications. The blog post linked in the README goes into more depth about why this matters: the compiler can make different optimization choices depending on whether it knows a pointer can never be null. By seeing the actual assembly output, you can understand exactly what those optimizations are and whether they're worth the trade-offs.