Skip to content

Releases

4

Choose a tag to compare

@pg83 pg83 released this 12 Nov 18:46

strdup and strndup are gone from derived.c: they were the archive's only reference outward, malloc, and the archive is one object, so a C library that takes it for its string functions linked the allocator into every program, the startup's memcpy was enough. In ix that brought tcmalloc and libc++abi with their thread-locals, and a Go program linked externally without cgo, which sets FS itself and expects its g alone at FS-8, trapped at its first instruction. The archive now takes no name from outside; the C library keeps its own strdup and strndup, made of strlen and memcpy.

The two functions sat at the end of .text: every other function keeps its address and its bytes, but the three after them (two_way, memmem, strstr). Their specs and malloc's contract left the prover with them.

Validation: cut in a hurry for an ix rebuild; the C test, the coverage gate and the proof chain's replay on this code follow.

3

3

Choose a tag to compare

@pg83 pg83 released this 12 Nov 16:50

strnlen is a slot of the table now, no longer memchr through the table in derived.c: memchr's blocks for the NUL, the offset for the answer (n when there is none), and past the two blocks in line a kernel of its own, strnlen_large, made of memchr_large's blocks. struct str_table has 22 slots where it had 20, so a loader that counts them, as pizdos's does, must count 22. strncat, strlcat and strndup call the slot; strlcpy and strpbrk choose by a select, not by a branch the programs' calls mispredict.

Zen 5 runs evex512. evex512r differed from it in memset alone, rep stosb for every size, and on the programs' own calls it lost on 18 of 20 and tied on 2; it goes, with the STR_FSRS bit and the cpuid leaves only it read. For Broadwell the small copies and fills of sse2, sse42 and avx2 test their size classes in rising order, one taken branch at most; avx2's memset stores up to 256 bytes itself; and avx2's fills from 8 MiB on are non-temporal stores closed by an sfence.

The compiler builds the library for pizdos's wasm2o as well (--abi wasm2o, ./build libstr_wasm2o): the convention of the code wasm2o compiles, for its loader to link. That build found a bug: a function whose stack addresses escape made a tail call, and str_host's struct on the stack was overwritten; such a call is no longer a tail call. A vector operation's address now folds into its memory operand (eight functions lose an instruction or two), and the compiler emits non-temporal vector stores and sfence.

The lab measures real programs too: dev/lab/pizdos.py runs pizdos's compiler compiling itself, its decoders and CPython with this library linked in, counts their cycles, and records their calls as traces for kbench. The workload has the derived functions in its mix and 1% of its copies and fills on a power-law tail from 4 KiB to 8 MiB, as the programs make them. README's table is on it: 0.65, 0.81 and 0.96 of glibc's time on Zen 5, Skylake-SP and Broadwell. A call costs more there than in release 2's workload, so the two tables do not compare.

The proof chain ran entirely on this release's code: every function in every configuration (116 of 116), and the derived functions by the contracts of the slots they call, all of them but memmem and strstr, which are tested. New are the proofs of strnlen and its kernel, non-temporal stores and sfence in the symbolic machine, and a proof of evex's fill_large of its own. The first runs found two gaps in the specs, closed now: strnlen's lacked the obligation that the answer is n itself when there is no NUL, and strnlen_large's let its from wrap below s. The chain had three faults of its own: the nodes were not sent common.h, --dist swept the hosts' answer cache, and the prover's threads overflowed the 128 KiB stacks of the hosts' musl. strnlen_large in avx2 and in evex has one solver query each that takes 34 and 48 minutes, past the chain's default limit of 30 (-Dproof_timeout); both were proved with a limit of two hours.

Validation: on an AVX-512 host, the C test on every configuration and 176 one-slot mixes, the compiler's self-check, the decoder round-trip and the wasm2o contract; line and branch coverage 100% (1596 of 1596 lines, 1418 of 1418 branches); the whole proof chain, 262 nodes, proved; CI green with gcc and clang.

2

2

Choose a tag to compare

@pg83 pg83 released this 10 Nov 17:11

Our own versions of the string functions made of the primitives, so that what a C library makes of them does not matter: bcmp, bcopy, bzero, explicit_bzero, mempcpy, strnlen, strncat, strlcpy, strlcat, strpbrk, strsep, strtok, strtok_r, strdup, strndup, memmem and strstr (derived.c), each over the str_* entry points: any configuration, any mix of slots, with malloc the one name taken from outside. memmem and strstr scan for the needle's first byte and compare where it is found, and go on by two-way when that does more work than it advances: linear in the haystack whatever the needle.

The archive is one object with one .text laid out by the compiler. A function's symbol is its entry: until now it was the start of its section, so every call first ran the pad that puts the entry at its phase in its 64-byte line (two NOPs in avx2's memcpy, four in memcmp_large). Calls between functions are resolved in the object, and only the ABI is global: the entry points, the new functions, the host's choice, str_table and the configurations.

Two compiler bugs the new functions found are fixed: a spilled value made by a copy and a two-address operation stored nothing back (memmem's bound), and a bool widened from a fused compare could take the compared value's register.

The new functions but memmem and strstr are proved by the contracts of the slots they call; memmem and strstr are tested on every needle and haystack of small alphabets, plainly and past the scan's budget, and on periodic needles. The proof chain last ran on release 1's code: strcat, memset_rep and strcspn in the avx2 family changed since and are not re-proved yet.

Validation: the C test on every configuration and 200 one-slot mixes, the compiler's self-check and the decoder round-trip; line and branch coverage 100% (1421 of 1421 lines, 1244 of 1244 branches).

1

1

Choose a tag to compare

@pg83 pg83 released this 10 Nov 14:48

First numbered release. String and memory functions for x86-64 under the MIT license: memcpy, memmove, memset, memcmp, memchr, strlen, strcpy, strcat, strcmp, strspn and strcspn, as the entry points str_memcpy and so on (a libc build renames them with -Dstr_memcpy=memcpy and the like). One cpuid at startup picks the configuration of the whole table of primitives: sse2, sse42, avx2, evex (256-bit AVX-512 first blocks over the AVX2 kernels, Skylake-SP), evex512 (the 512-bit ones, Zen 4) or evex512r (evex512 with rep stosb for memset where a short rep stosb starts fast, Zen 5). The functions are written in a small C dialect with builtin SIMD operations and compiled by the project's own optimizer in Python (dev/opt: from the AST through SSA passes, instruction selection, register allocation and layout to the ELF object), so ./build -j8 libstr needs Python alone.

The linked machine code is proved: each configuration's copy of every function and of the kernels behind it, against the function's spec, by symbolic execution with SMT solvers (dev/proof). Proved are the result, every store inside the destination with the byte due there, and every load within the pages of its object, for every length (the loops by induction), every position of the pointers in a page and, for strspn and strcspn, every set. ./build proof is 128 proofs, all proved on this commit.

Validation: CI green (style, the C test at every level its runner has, the decoder round-trip); ./build proof on the lab hosts, 128 of 128 proved; on the lab stand's workload (dev/lab) the mixed trace takes 0.48 of glibc's time on Skylake-SP and 0.90 on Broadwell-EP, at parity with the previous compiler's code (clang and a post pass) measured in the same binary.