Section 3.35

Undefined versus unsupported semantics

Definition

Undefined and unsupported semantics are two reasons why a tool cannot say what an operation does; semantics means the exact definition of an operation’s effect. An operation has undefined semantics, called undefined behavior §4.12, when the specification of the language or instruction set imposes no requirement on what happens, as C does for signed integer overflow §4.15. An operation is unsupported when the specification defines its effect but a particular tool, such as an interpreter §2.13 or verifier, does not implement that definition. Undefined describes the language; unsupported describes one tool.

The idea

A verifier must show that a program never reads outside its arrays §14.5 before the program may run. Partway through, the verifier meets an instruction that has no entry in its table of rules. Two different situations look the same at this moment. In the first, the specification leaves the instruction’s effect open: no tool could promise anything about what follows, and a program that reaches the instruction is defective. In the second, the specification defines the effect exactly, but the verifier’s authors have not written a rule for it: the program may be safe, and only the verifier lacks the knowledge to show it. The two situations call for different reports, and neither allows a guess. A verifier that gives the instruction a convenient meaning, such as doing nothing, proves facts about a program that is not the program that runs.

A translator meets the same two problems: a garbled sentence in the original, which says nothing definite, and a sentence in a language the translator does not read, which has a meaning the translator cannot supply. An honest translator marks the first as a defect of the original and the second as beyond the translator’s skill, and invents a translation for neither. The analogy stops at the consequences. A garbled sentence is harmless, but a compiler may assume that undefined behavior never happens, and at run time any outcome may follow.

How it works

Three questions, asked in order, decide which case applies.

  1. What does the specification say? Look the operation up in the specification the program is written against, in the version the program targets: the language standard, or the definition of the instruction set architecture §4.38 (ISA), the instructions a processor or virtual machine §2.3 executes and their effects. The 2011 C standard (C11) distinguishes four classes of answer; its section 3.4 defines the last three.

    • Defined: one required result. Unsigned addition, for example, wraps around: the result is reduced modulo 2N2^N for an NN-bit type.

    • Implementation-defined: the implementation, meaning the compiler together with the machine it targets, chooses the result and must document the choice. The standard’s example is whether shifting a negative signed integer to the right copies the sign bit.

    • Unspecified: two or more results are allowed, and the implementation need not say which one occurs. The standard’s example is the order in which the arguments of a function call are evaluated.

    • Undefined: the standard imposes no requirement at all. The standard’s example is integer overflow, which in C concerns signed types, because unsigned arithmetic wraps. A note in the standard says that the outcome may range from ignoring the situation, with unpredictable results, to stopping the translation or the execution.

  2. Does the tool implement that meaning? A tool supports an operation when it has a decoder that recognizes the operation’s encoding and a rule that gives the operation’s effect in the tool’s own terms. In an interpreter, the rule computes the result. In a verifier, the rule is a transfer function: it turns what is known before the instruction into what is known after it. If either part is missing, the operation is unsupported by that tool.

  3. Is the operation inside the tool’s declared scope? A specification can allow implementations to leave instructions out. RFC 9669, the standard for the BPF (Berkeley Packet Filter) instruction set, sorts its instructions into conformance groups. Every implementation must support the group base32, which holds every instruction not assigned to another group. An implementation may also support other groups, such as divmul64 for 64-bit division, multiplication and modulo. An instruction from a group that an implementation leaves out is unsupported by that implementation, yet the standard still defines it fully.

The answers give three cases.

Defined and supported.

The tool applies its rule.

Defined but unsupported.

The tool reports the operation as unsupported, and its verdict on the program is unknown. Alternatively, the tool uses a sound overapproximation §19.2.5: a replacement rule that covers every outcome the specification allows, at a cost in precision. The simplest such rule forgets everything about the register the instruction writes. That rule is sound only when the specification shows that the instruction writes nothing else: no memory, no other register, no jump and no exception.

Undefined.

The program is at fault if an execution reaches the operation. The tool must not give the operation a convenient meaning, such as wrapping for signed overflow, unless its verdict states that it is about one particular compiler and its settings.

Worked example

Take a hypothetical checker for an instruction set whose specification defines a 16-bit byte swap, an instruction that exchanges the two lowest bytes of a register. The program under check does four things.

  1. Read a value from the input into register r1.

  2. Stop if r1 is greater than 15, so that afterwards r1 lies between 0 and 15.

  3. Byte-swap r1.

  4. Read the byte at position r1 of a 16-byte table, whose valid positions are 0 to 15.

With input 5, written 0x0005 in hexadecimal, the swap gives 0x0500, which is 5×256=12805 \times 256 = 1280. Step 4 reads position 1280, which is 1280−15=12651280 - 15 = 1265 positions past the last valid one.

Four checkers reach four different verdicts.

Checkers B, C and D are sound; only checker D tells the program’s author what is wrong.

Undefined behavior is a different case. In C with a 32-bit int, whose largest value is 2,147,483,647, computing x + 1 for that value of x is a signed integer overflow. A checker that models the addition as wrapping computes −2,147,483,648-2{,}147{,}483{,}648 and may then prove that the function returns a negative number. That proof describes no real program: a compiler may produce code that wraps, code that traps, or code simplified on the assumption that the overflow never happens. The correct report is a violation: undefined behavior is reachable with input 2,147,483,647.

Stated precisely

Fix a specification and a property PP to be proved, such as “every read stays inside its table”. For an operation oo and a machine state ss in which oo runs, let Spec(o,s)\mathrm{Spec}(o, s) be the set of outcomes the specification permits: one outcome for a defined operation, the permitted choices for an unspecified or implementation-defined one, and every conceivable outcome for an undefined one. A tool keeps a description AA of the states possible before oo, and its rule for oo produces a description A′A' of the states possible after oo. The rule is sound when, for every state ss that AA allows, every outcome in Spec(o,s)\mathrm{Spec}(o, s) is a state that A′A' allows.

Three consequences follow.

  1. An unsupported operation has no rule, so no conclusion about later states can be drawn. The tool must answer unknown, or adopt a replacement rule that meets the soundness §3.7 condition and is justified from the specification.

  2. For an undefined operation, the only sound rule is “any state at all, with any effect on the outside world”. A tool that proves PP must therefore show that no execution reaches the operation, report a violation when it finds an execution that does, and answer unknown otherwise.

  3. The verdict proved is allowed only if every operation that some execution can reach had a sound rule and PP held. The verdict violation needs an execution that the specification permits and that breaks PP. Every other ending is unknown, reported with its reason.

Limits and common mistakes

The word undefined means different things in different specifications. The Arm architecture reference manuals mark some encodings undefined, and such an encoding must raise an exception: its behavior is defined, and the instruction simply does not exist. Behavior those manuals leave open is marked unpredictable. Read each specification’s own definitions.

An operation can be undefined at one layer and defined at another. RFC 9669 defines BPF arithmetic to wrap and division by zero to give zero, while C leaves signed overflow and division by zero undefined. A BPF interpreter written in C must therefore use C’s unsigned arithmetic and test the divisor before dividing; otherwise the interpreter adds undefined behavior that the BPF program never had. Keeping an interpreter’s meaning equal to the specification is called semantic fidelity §2.38.

A test run does not show that an operation is defined. Undefined behavior can give the expected result in every test and a different result after a compiler upgrade.

An unknown instruction can break the decoder. BPF instructions are 8 bytes long, except the wide immediate instruction §2.10, which takes 16. A decoder that misreads an unknown encoding may misjudge where the next instruction starts, which makes every later verdict unreliable. Reserved fields carry a similar risk: a field that one version of a specification requires to be zero may gain a meaning in a later version, so a tool that ignores such a field may accept an instruction it does not understand.

Compilers and verifiers use undefined behavior in opposite ways. A compiler may assume that undefined behavior never happens and optimize on that assumption. A verifier that proves the absence of undefined behavior must check it instead.

Questions answered

How should a proof engine report an instruction it cannot soundly interpret?

A proof engine should report an instruction it cannot soundly interpret as a separate third outcome, unsupported or unknown, which is neither a proof of safety nor a demonstrated violation. The report names the instruction, by position and encoding, and the reason: the specification defines the instruction, but the engine has no sound rule for it. The program’s verdict is then unknown, and a gate that admits only proved programs refuses the program without calling it unsafe.

The engine must not skip the instruction, treat it as doing nothing, assume that it cannot execute, or give it a convenient meaning; each of these yields a proof about a different program from the one that runs. Two alternatives are acceptable. The engine may replace the instruction by a sound overapproximation, such as “the destination register may now hold any value”, when the specification shows that the instruction has no other effect; later conclusions then lose precision but stay true. Or the engine may prove soundly that no execution reaches the instruction, provided its decoder still knows the instruction’s length.

Undefined behavior is reported differently. If an execution can reach an operation for which the specification imposes no requirement, such as signed integer overflow in C, the program is at fault, and the engine reports a violation, together with an input that reaches the operation when the engine has found one.

Related entries: Opcode (§2.8), Signed integer overflow (§4.15), Conservative refusal (§3.36), Undefined behavior (UB) (§4.12), Soundness (§3.7), Interpreter semantic fidelity (§2.38), Overapproximation versus underapproximation (§19.2.5). Further reading: [78, 160].