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 , the bitwise AND of an unknown value with 12, which keeps a bit of 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 . 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 for unknown. The offset above is in four bits. A pattern with unknown positions stands for numbers, so stands for the 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 , binary 01 and 10, can only be summarized as , which also includes 0 and 3.
How it works
A bitmask abstract value has these parts.
Width : the number of bits, such as 64 for a 64-bit register. All arithmetic is taken modulo .
Value : a -bit integer whose 1 bits are the bits known to be 1.
Mask : a -bit integer whose 1 bits are the unknown bits.
A bit that is 0 in both and is known to be 0. No bit may be 1 in both, since a bit cannot be known to be 1 and unknown at once: .
Common summaries follow directly. A known constant is . A completely unknown value is , 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 , with every unknown bit 0, and the largest is , the bitwise OR of and , 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.
AND: a result bit is known to be 1 when both operand bits are known to be 1, known to be 0 when either operand bit is known to be 0, and unknown otherwise.
OR: a result bit is known to be 1 when either operand bit is known to be 1, known to be 0 when both are known to be 0, and unknown otherwise.
Shift left by a constant : and both shift left by places, and the new low bits are known to be 0.
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 and : , the sum when every unknown bit is 0, and , the sum when every unknown bit is 1. A result bit is marked unknown when it differs between and or is unknown in either operand. Every other bit takes its value from . 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 implies that every bit above the lowest two is known to be 0.
Worked example
Use 4-bit numbers, written in binary. The input is unknown: . The constant 12 is . For AND, the result value is the AND of the two values, . 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 : 1111 for and 1100 for 12, and . The result is , the pattern , which stands for 0, 4, 8 and 12. Each is a multiple of 4: , , and . The summary also gives the range: the smallest value is and the largest is .
The membership test confirms this: a number belongs to when its bits outside the mask equal . The bits outside are 0011. For 4: , so 4 belongs. For 5: , so 5 does not.
The offset is aligned; the address is aligned only if the base is too. With base address 1000, which is , 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 be 0 or 1, the summary , and add the constant 1, the summary . 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 , giving , which stands for 0 and 1 and misses the real result 2: the naive rule is unsound. The kernel’s rule computes and . These differ in the bits 0011. Adding the operand’s unknown bit 0001 leaves the mask at , and the value is with those bits cleared, . The result 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 . Write , and for bitwise AND, OR and exclusive OR (1 where the two bits differ), and for the complement of within bits. A bitmask abstract value is a pair of -bit integers with . It stands for the set which has members when has bits set to 1. Its smallest member is and its largest is .
For operands and , with additions taken modulo , the AND rule, the addition rule and the join used where two paths meet are where and . Each rule is sound: for every and , the concrete result or lies in the set that the abstract result stands for, and .
Alignment: if the lowest bits of are 0, that is , every member of is a multiple of . After for a constant , every bit that is 0 in is known to be 0 in the result, whatever is. So if the lowest bits of are 0, the result is a multiple of .
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 with a constant , a bitmask summary proves that every bit position where has a 0 is 0 in the result, whatever is. So if the lowest bits of are 0, the result is a multiple of , and as a byte offset it is aligned to bytes. For example, AND with 12, binary 1100, leaves the pattern , where marks an unknown bit: the result is one of 0, 4, 8 and 12, all multiples of 4. The bits where has a 1 are copied from : known if they were known in , unknown otherwise. The summary also bounds the result: it is at most .
This proves the alignment of the offset only. The address, base plus offset, is a multiple of 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].