Vellvm

Better Support for UB


  • Home
  • About
  • Github
  • Running Tests
  • Structure
  • Research
  • FAQ

Better Support for UB

Recent work on Vellvm makes the semantics detect many more of the cases where the LLVM LangRef says a program has immediate undefined behavior, instead of computing some value or failing with an “unsupported” error. Along the way, several parts of the semantics were brought closer to LLVM. All of the changes are carried through the infinite-to-finite refinement proof (Theory/I2F), which still holds with no admitted lemmas.

A test suite for undefined behavior

The new tests/ub directory holds executable tests for the immediate-UB clauses of the LangRef: one file per instruction, plus files for call attributes, intrinsics, and free. Each UB case is written

; ASSERT UB <line>: call i32 @f(...)

where <line> is the instruction that should be undefined. Most UB cases also have a well-defined control that differs only slightly, often by being poison rather than UB.

A companion script, tests/ub/compare.py, runs every case on its own under Vellvm, llubi, and clang at -O0 to -O3 and with AddressSanitizer, and produces a side-by-side comparison. Vellvm currently agrees with 328 of the 363 assertions, which is comparable to or better than llubi. The tests/ub/README.md file records the remaining gaps.

Semantic changes

undef is now poison

Vellvm 3.0 already removed undefined values from the semantics, but it still denoted an undef constant as freeze poison, an arbitrary fixed value. undef is now simply poison, in line with LLVM’s plan to remove undef. So br i1 undef and switch i32 undef are UB, as the LangRef requires. One consequence differs from today’s LangRef: or i8 undef, 255 is poison rather than 255.

The null pointer

  • Nothing is ever allocated at address 0. Functions, globals, allocas, and heap blocks now all compare unequal to null.
  • Null is recognized by its address. Provenance is ignored, so inttoptr i64 0 also counts as null.
  • Calls through null or poison function pointers are UB. Calling any other non-pointer value is an error.
  • free(null) does nothing, as in C, and free(poison) is UB.

Alignment

  • An explicit alignment that is too large is UB, as the LangRef says (“Overestimating the alignment results in undefined behavior”). This is checked on load, store, atomicrmw, and cmpxchg.
  • An alloca without align now gets its type’s natural (preferred) alignment, as in LLVM, rather than a fixed 8 bytes.
  • Vectors get LLVM’s default alignment: their size in bytes, rounded up to a power of two (so <4 x i32> is 16-byte aligned).

Accesses with no align annotation are not checked yet.

Function and value attributes

  • Value attributes are now enforced. nonnull, align N, range(a, b), and nofpclass turn a violating value into poison. noundef makes a value that is not well defined UB. dereferenceable(n) and dereferenceable_or_null(n) are UB when the n bytes cannot be accessed. They are checked on call-site arguments and return values (call and invoke), and on the parameters and return value of a defined function. A direct call also gets the attributes its callee declares, so an attribute split between a declaration and the call site is combined, and the attributes of a function that is only declared apply too.
  • Load metadata. !noundef, !nonnull, !align, and !nofpclass on a load act like the corresponding attributes on the loaded value. !range (on loads and on call and invoke results) makes a value outside the ranges poison, and !dereferenceable / !dereferenceable_or_null are UB when the bytes cannot be accessed.
  • Function attributes. A noreturn function that returns, or a nounwind function that unwinds, is UB, whether the attribute is on the function’s definition or on the call site. So is a nocreateundeforpoison function that returns undef or poison for well-defined arguments.
  • Calling conventions. Calling a function with a calling convention that does not match its definition is UB. This is checked when the function is entered, so it also covers calls through function pointers.
  • Attribute groups and numbered metadata. Attributes given through attribute groups (#0) and metadata given as numbered nodes (!align !1), as clang emits them, are now resolved, so they are checked too.

Still missing: the attributes that constrain what memory a pointer may access (memory(...), readonly, readnone, captures, nofree, noalias, initializes, writable), which we expect a provenance model with access permissions to cover.

Instructions

  • alloca with a poison element count is UB at the alloca itself, as the LangRef requires.
  • srem of INT_MIN by -1 is UB, as sdiv already was.
  • icmp comparing poison with a pointer yields poison.
  • indirectbr is now supported, together with blockaddress constants. It jumps to the listed block whose address it is given, and it is UB on a poison or null address, or on an address that is not one of its listed blocks (including a block of another function).
  • getelementptr with nusw or nuw gives poison when the offset computation wraps, as the LangRef requires. inbounds implies nusw, and an inbounds offset from null is poison. Going out of the bounds of a real object is not detected yet, because the memory model does not record object bounds.

Intrinsics and library functions

  • llvm.memmove is now supported (overlapping copies allowed).
  • memcpy, memmove, and memset with a poison length are UB. Poison pointers are UB unless the length is 0, in which case the call does nothing. memset with a poison fill value stores poison bytes and is not UB.
  • Opaque-pointer names. The names current LLVM emits (llvm.memcpy.p0.p0.i64, llvm.memset.p0.i64, …) are handled directly, rather than through shims in libll, so their UB is reported at the program’s own call.
  • llvm.assume is now a built-in intrinsic. A false or poison condition is UB, and so is a violated "nonnull", "noundef", "align", "dereferenceable", or "dereferenceable_or_null" operand bundle.

Parser

The parser now accepts all the syntax used in tests/ub, including blockaddress, attribute groups, numbered metadata, the getelementptr flags, nofpclass, and fast-math flags on fcmp, select, and phi, as well as trunc nsw nuw. Fast-math flags and the trunc flags are parsed but do not yet change the semantics.

Test harness

  • ASSERT UB <line> asserts that a call has undefined behavior.
  • ASSERT EQ accepts poison as the expected value, and an unexpected poison result is now reported as an ordinary assertion failure.

src/ml/README.md documents the full assertion syntax.

Code organization

The checks that annotations impose on values now live in a new file, Semantics/Attributes.v, and Denotation.v applies them at the points where the LangRef does. This keeps the denotation focused on the structure of the semantics. Most checks are pure functions. The few that must read memory, such as dereferenceable, are written generically over the events they use. The refinement lemmas for the pure checks live in Theory/I2F/I2F_attributes.v.


Last Modified: Oct 29, 2026 4:40 pm