Bit-vector Operations
This page lists the operations on bit-vectors: arithmetic and comparison, bit selection, extension, multi-byte memory access and flag arithmetic. The types themselves (BV<N> and its aliases, typed literals, byte-addressed memory, and how to model registers and flags) are described under Bit-vector types. The solver checks bit-vector constraints with modular (wrap-around) arithmetic, and both front ends emit bit-vector SPL.
Operators#
+, -, *, &, |, ^, ~, <<, >>>, == and != work on bit-vectors of equal width. Where signedness matters, use the explicit operators:
| Unsigned | Signed | Meaning | Precedence level |
|---|---|---|---|
<u <=u >u >=u | <s <=s >s >=s | Comparison | with the comparisons |
*u /u %u | *s /s %s | Multiply, divide, remainder | with * / % |
Multiplication modulo 2N is the same for both signednesses, so *u and *s compute the same value. In bit-vector pattern constraints, >>> is the logical right shift; >> is not encoded there. The SMT-LIB names bvnot, bvand, bvor, bvxor, bvshl, bvlshr, bvashr, bvadd, bvsub and bvmul are also accepted as functions in bit-vector pattern constraints.
*s, *u, /s, %u, <u … as one operator whenever the letter is not followed by a letter, digit or _. So a *s) is a signed multiply, not a * s. Put a space after the operator when the right operand is a one-letter name s or u: write a * s.
Extract and concat#
extract<hi, lo>(e) selects bits hi down to lo (inclusive) and has width hi - lo + 1. Both bounds are integer literals. concat(a, b, …) joins bit-vectors, the first argument in the most significant position.
let low = extract<31, 0>(RAX) ; // BV<32>
let sign = extract<63, 63>(RAX) ; // BV<1>
let wide = concat(extract<7, 0>(RBX), low) ; // BV<40>Zero and sign extension#
zero_extend(e, k) and sign_extend(e, k) widen e by k bits: k is the number of bits added, an integer literal, not the target width. zero_extend(x, 32) turns a BV<32> into a BV<64>.
The older built-ins zext(e, w) and sext(e, w) take the target width w instead; see the built-in index.
Multi-byte memory access#
Memory(a) is the byte at address a. Memory<N>(a) reads N bytes little-endian: it is concat(Memory(a + N-1), …, Memory(a + 1), Memory(a)). The same form on the left of := writes N bytes, byte i of the value going to a + i. N must be at least 1.
schema Machine {
attributes {
RSP, RAX : BV<64> ;
Memory : Array(BV<64>, BV<8>) ;
}
}
transition load64() {
guard: true ;
effect: RAX := Memory<8>(RSP) ;
}
transition store32() {
guard: true ;
effect: Memory<4>(RSP) := extract<31, 0>(RAX) ;
}Where operations are evaluated#
Pattern constraints and the front ends' models are checked with the solver, which handles the operations as described above. The concrete walk of saple analyze, which checks the properties of a hand-written model, evaluates fewer:
| Operation | In the concrete walk |
|---|---|
+ - * & | ^ ~ << >> >>> | Evaluated, wrapping at the width (>> is the arithmetic shift, >>> the logical one) |
== != | Evaluated |
extract, concat, zero_extend, sign_extend | Evaluated, at most 64 bits; concat joins its first two arguments only |
< <= > >= and the u / s comparisons | Always false on bit-vectors, in a guard, a property or an if condition |
/ % and the u / s multiply, divide and remainder | Unknown value |
Memory(a), Memory<N>(a) | Not kept: a store changes nothing, a load is unknown |
So a hand-written model whose properties depend on a comparison or a carry should compute it with the evaluated operations, as in Flag arithmetic.
Flag arithmetic#
Flags are BV<1> attributes (Types › Flags). A comparison is a Bool; turn it into a flag with a conditional expression:
ZF := if (result == 0x0:BV<64>) then 0x1:BV<1> else 0x0:BV<1> ;For a 32-bit add of a and b with result r, the sign and zero flags read the result:
N := extract<31,31>(r) ; // sign bit
Z := if (r == 0x0:BV<32>) then 0x1:BV<1> else 0x0:BV<1> ; // zeroThe carry is the bit that does not fit. Add the operands one bit wider and take the top bit:
let wide = zero_extend(a, 1) + zero_extend(b, 1) ; // 33 bits: no carry is lost
let r = extract<31,0>(wide) ; // the 32-bit result
C := extract<32,32>(wide) ; // carry out of bit 31Signed overflow happens when both operands have the same sign and the result's sign differs:
V := ~(extract<31,31>(a) ^ extract<31,31>(b)) & (extract<31,31>(a) ^ extract<31,31>(r)) ;These forms use only operations the concrete walk evaluates. The x86-64 front end writes the carry of a subtraction with <u instead, RFLAGS.CF := (if (dst_in <u src_in) then 0x1:BV<1> else 0x0:BV<1>), which the solver evaluates. Types › Flags has a complete model that computes all four flags.
Security use: integer wrap-around (CWE-190) is stated with these operators; see Pattern Libraries › CWE-190.