Section 3.10

Bitmask abstract value (tnum)

Full name: tnum: tracked number, also called tristate number.

Definition

A bitmask abstract value is a summary of a fixed-width integer that records whether each bit is known to be 0, known to be 1, or unknown. It is stored as two integers: a value, whose 1 bits are the bits known to be 1, and a mask, whose 1 bits are the unknown bits. It stands for every integer that matches the value at all known positions. Unlike an interval, it can express “always a multiple of 4”. The Linux kernel calls it a tnum (tracked or tristate number).

The idea

A program computes an offset as x&12x \mathbin{\&} 12, the bitwise AND of an unknown value xx with 12, which keeps a bit of xx only where 12 has a 1. In binary, 12 is 1100, so the two lowest bits of the result are always 0, and the offset is always a multiple of 4. A later 4-byte read at that offset therefore meets alignment §4.22, the requirement that an address be a multiple of the access size, provided the base address is also a multiple of 4. Interval analysis §3.9, which keeps only a minimum and a maximum, summarizes the offset as [0,12][0, 12]. That interval contains 1, 2, 3, 5 and other values that are not multiples of 4, so it cannot prove the alignment.

The bitmask summary writes a number with three possible symbols per position: 0, 1, or *\ast for unknown. The offset above is **00\ast\ast00 in four bits. A pattern with kk unknown positions stands for 2k2^k numbers, so **00\ast\ast00 stands for the 22=42^2 = 4 numbers 0, 4, 8 and 12. The unknown positions are independent of each other: the summary cannot say that two unknown bits always differ, so the set {1,2}\{1, 2\}, binary 01 and 10, can only be summarized as **\ast\ast, which also includes 0 and 3.

How it works

A bitmask abstract value has these parts.

Common summaries follow directly. A known constant cc is (c,0)(c, 0). A completely unknown value is (0,2w−1)(0, 2^w - 1), every bit unknown. A byte loaded from memory into a 64-bit register has the value 0x0 and the mask 0xff: the top 56 bits are known to be 0 and the low 8 bits are unknown. The Linux kernel’s documentation uses this example. The smallest number a summary allows is vv, with every unknown bit 0, and the largest is v|mv \mathbin{|} m, the bitwise OR of vv and mm, with every unknown bit 1.

Each operation has a transfer function, a rule that computes the summary of the result from the summaries of the operands. For bitwise operations §4.50 each bit position can be handled on its own.

Addition cannot be handled bit by bit, because a carry out of a lower position changes higher positions. The rule the Linux kernel uses computes two sums for operands (va,ma)(v_a, m_a) and (vb,mb)(v_b, m_b): s0=va+vbs_0 = v_a + v_b, the sum when every unknown bit is 0, and s1=(va+ma)+(vb+mb)s_1 = (v_a + m_a) + (v_b + m_b), the sum when every unknown bit is 1. A result bit is marked unknown when it differs between s0s_0 and s1s_1 or is unknown in either operand. Every other bit takes its value from s0s_0. The rule is sound: every sum of two numbers that the operands stand for is a number that the result stands for.

Analyzers usually keep a bitmask summary together with intervals and let each sharpen the other. Linux checks every eBPF §2.1 (extended Berkeley Packet Filter) program, a small program loaded into the kernel, with the eBPF verifier §3.3, which keeps for each number a tnum together with signed and unsigned minimum and maximum values. Value 0 with mask 0xff implies an unsigned maximum of 255, and an unsigned range [0,3][0, 3] implies that every bit above the lowest two is known to be 0.

Worked example

Use 4-bit numbers, written in binary. The input xx is unknown: (v,m)=(𝟶𝟶𝟶𝟶,𝟷𝟷𝟷𝟷)(v, m) = (\texttt{0000}, \texttt{1111}). The constant 12 is (𝟷𝟷𝟶𝟶,𝟶𝟶𝟶𝟶)(\texttt{1100}, \texttt{0000}). For AND, the result value is the AND of the two values, 𝟶𝟶𝟶𝟶&𝟷𝟷𝟶𝟶=𝟶𝟶𝟶𝟶\texttt{0000} \mathbin{\&} \texttt{1100} = \texttt{0000}. A result bit is unknown when it can be 1 in both operands but is not known to be 1 in both. The bits that can be 1 are v|mv \mathbin{|} m: 1111 for xx and 1100 for 12, and 𝟷𝟷𝟷𝟷&𝟷𝟷𝟶𝟶=𝟷𝟷𝟶𝟶\texttt{1111} \mathbin{\&} \texttt{1100} = \texttt{1100}. The result is (𝟶𝟶𝟶𝟶,𝟷𝟷𝟶𝟶)(\texttt{0000}, \texttt{1100}), the pattern **00\ast\ast00, which stands for 0, 4, 8 and 12. Each is a multiple of 4: 0/4=00 / 4 = 0, 4/4=14 / 4 = 1, 8/4=28 / 4 = 2 and 12/4=312 / 4 = 3. The summary also gives the range: the smallest value is v=0v = 0 and the largest is v|m=𝟷𝟷𝟶𝟶=12v \mathbin{|} m = \texttt{1100} = 12.

The membership test confirms this: a number belongs to (v,m)(v, m) when its bits outside the mask equal vv. The bits outside m=𝟷𝟷𝟶𝟶m = \texttt{1100} are 0011. For 4: 𝟶𝟷𝟶𝟶&𝟶𝟶𝟷𝟷=𝟶𝟶𝟶𝟶=v\texttt{0100} \mathbin{\&} \texttt{0011} = \texttt{0000} = v, so 4 belongs. For 5: 𝟶𝟷𝟶𝟷&𝟶𝟶𝟷𝟷=𝟶𝟶𝟶𝟷≠v\texttt{0101} \mathbin{\&} \texttt{0011} = \texttt{0001} \ne v, so 5 does not.

The offset is aligned; the address is aligned only if the base is too. With base address 1000, which is 4×2504 \times 250, the possible addresses are 1000, 1004, 1008 and 1012, all multiples of 4. With base 1001 they are 1001, 1005, 1009 and 1013, each of which leaves remainder 1 when divided by 4. Alignment also says nothing about bounds: a 4-byte read at offset 12 covers bytes 12 to 15, which fits inside a 16-byte object but not inside a 12-byte one.

Addition shows why carries matter. Let xx be 0 or 1, the summary (𝟶𝟶𝟶𝟶,𝟶𝟶𝟶𝟷)(\texttt{0000}, \texttt{0001}), and add the constant 1, the summary (𝟶𝟶𝟶𝟷,𝟶𝟶𝟶𝟶)(\texttt{0001}, \texttt{0000}). The true results are 1 and 2, binary 0001 and 0010. A naive rule that adds bit by bit says that bit 0 is unknown and bit 1 is 0+0=00 + 0 = 0, giving (𝟶𝟶𝟶𝟶,𝟶𝟶𝟶𝟷)(\texttt{0000}, \texttt{0001}), which stands for 0 and 1 and misses the real result 2: the naive rule is unsound. The kernel’s rule computes s0=𝟶𝟶𝟶𝟶+𝟶𝟶𝟶𝟷=𝟶𝟶𝟶𝟷s_0 = \texttt{0000} + \texttt{0001} = \texttt{0001} and s1=(𝟶𝟶𝟶𝟶+𝟶𝟶𝟶𝟷)+(𝟶𝟶𝟶𝟷+𝟶𝟶𝟶𝟶)=𝟶𝟶𝟷𝟶s_1 = (\texttt{0000} + \texttt{0001}) + (\texttt{0001} + \texttt{0000}) = \texttt{0010}. These differ in the bits 0011. Adding the operand’s unknown bit 0001 leaves the mask at m=𝟶𝟶𝟷𝟷m = \texttt{0011}, and the value is s0s_0 with those bits cleared, v=𝟶𝟶𝟶𝟶v = \texttt{0000}. The result (𝟶𝟶𝟶𝟶,𝟶𝟶𝟷𝟷)(\texttt{0000}, \texttt{0011}) stands for 0, 1, 2 and 3. It covers both real results, and it also admits 0 and 3, because the summary cannot say that the two low bits always differ.

Stated precisely

Fix a width ww. Write &\mathbin{\&}, |\mathbin{|} and ⊕\oplus for bitwise AND, OR and exclusive OR (1 where the two bits differ), and m¯\overline{m} for the complement of mm within ww bits. A bitmask abstract value is a pair (v,m)(v, m) of ww-bit integers with v&m=0v \mathbin{\&} m = 0. It stands for the set γ(v,m)={x:0≤x≤2w−1,x&m¯=v},\gamma(v, m) = \{ x : 0 \le x \le 2^w - 1,\ x \mathbin{\&} \overline{m} = v \}, which has 2k2^k members when mm has kk bits set to 1. Its smallest member is vv and its largest is v|mv \mathbin{|} m.

For operands a=(va,ma)a = (v_a, m_a) and b=(vb,mb)b = (v_b, m_b), with additions taken modulo 2w2^w, the AND rule, the addition rule and the join used where two paths meet are AND♯(a,b)=(va&vb,(va|ma)&(vb|mb)&va&vb¯),ADD♯(a,b)=(s0&μ¯,μ),μ=(s0⊕s1)|ma|mb,a⊔b=(va&ν¯,ν),ν=ma|mb|(va⊕vb),\begin{aligned} \mathrm{AND}^\sharp(a, b) &= \bigl(v_a \mathbin{\&} v_b,\ (v_a \mathbin{|} m_a) \mathbin{\&} (v_b \mathbin{|} m_b) \mathbin{\&} \overline{v_a \mathbin{\&} v_b}\bigr), \\ \mathrm{ADD}^\sharp(a, b) &= \bigl(s_0 \mathbin{\&} \overline{\mu},\ \mu\bigr), \quad \mu = (s_0 \oplus s_1) \mathbin{|} m_a \mathbin{|} m_b, \\ a \sqcup b &= \bigl(v_a \mathbin{\&} \overline{\nu},\ \nu\bigr), \quad \nu = m_a \mathbin{|} m_b \mathbin{|} (v_a \oplus v_b), \end{aligned} where s0=va+vbs_0 = v_a + v_b and s1=(va+ma)+(vb+mb)s_1 = (v_a + m_a) + (v_b + m_b). Each rule is sound: for every x∈γ(a)x \in \gamma(a) and y∈γ(b)y \in \gamma(b), the concrete result x&yx \mathbin{\&} y or (x+y)mod2w(x + y) \bmod 2^w lies in the set that the abstract result stands for, and γ(a)∪γ(b)⊆γ(a⊔b)\gamma(a) \cup \gamma(b) \subseteq \gamma(a \sqcup b).

Alignment: if the lowest kk bits of v|mv \mathbin{|} m are 0, that is (v|m)&(2k−1)=0(v \mathbin{|} m) \mathbin{\&} (2^k - 1) = 0, every member of γ(v,m)\gamma(v, m) is a multiple of 2k2^k. After x&cx \mathbin{\&} c for a constant cc, every bit that is 0 in cc is known to be 0 in the result, whatever xx is. So if the lowest kk bits of cc are 0, the result is a multiple of 2k2^k.

Limits and common mistakes

An unknown bit is not a 0 bit. A 1 in the mask means that the bit can have either value, and every member of the set must be treated as possible.

Besides relations between bits, the summary cannot express most ranges: the range 0 to 10 needs all four low bits unknown, which admits 11 to 15. That is why analyzers combine bitmask summaries with intervals.

Carries spread unknown bits. Adding two values with unknown low bits can make many higher bits unknown, and multiplying by an unknown value usually leaves almost every bit unknown.

The width must match the instruction. In eBPF a 32-bit operation on a 64-bit register sets the upper 32 bits to 0, and the summary must model that; a complement taken over the wrong width gives wrong known bits.

Known low bits prove the alignment of an offset. They do not prove the alignment of the address unless the base is known to be aligned, and they do not prove that the access stays in bounds, that the object is still alive, or that the program may use the pointer (the Pointer provenance §4.24 and Object lifetime §4.1 entries).

Questions answered

What can a bitmask prove about alignment after AND with a constant?

After x&cx \mathbin{\&} c with a constant cc, a bitmask summary proves that every bit position where cc has a 0 is 0 in the result, whatever xx is. So if the lowest kk bits of cc are 0, the result is a multiple of 2k2^k, and as a byte offset it is aligned to 2k2^k bytes. For example, AND with 12, binary 1100, leaves the pattern **00\ast\ast00, where *\ast marks an unknown bit: the result is one of 0, 4, 8 and 12, all multiples of 4. The bits where cc has a 1 are copied from xx: known if they were known in xx, unknown otherwise. The summary also bounds the result: it is at most cc.

This proves the alignment of the offset only. The address, base plus offset, is a multiple of 2k2^k only when the base is known to be one too: base 1000 with offsets 0, 4, 8 and 12 gives multiples of 4, base 1001 does not. Alignment proves neither that the access stays inside its object nor that the object is still alive.

Related entries: Alignment (§4.22), Software fault isolation (SFI) (§2.41), Interval analysis (§3.9), Bit operations: truth tables, masks, shifts, and rotations (§4.50). Further reading: [2].