Types
SPL has a small static type system: scalar types, bit-vectors, collections, user-defined enumerations and algebraic datatypes. Types appear on attributes, transition parameters, quantifier variables and datatype fields.
Scalar types#
| Type | Description | Literals |
|---|---|---|
Int | Integer | 0, 42, -1, 0xFF, 0b1010 |
Real | Floating-point number | 3.14, 1.0e10 |
Bool | Boolean | true, false |
String, Str | Text (Str is an alias) | "hello" |
Void | Unit type | — |
- A decimal literal must fit a 64-bit signed integer; a larger one is a parse error. Write a 64-bit pattern as a hex bit-vector literal,
0xffffffffffffffff:BV<64>. - A hex or binary literal without a
:BV<N>suffix is anInt. One above0x7fffffffffffffffis read as a negativeInt(two's complement), so give large constants the suffix. - The exponent of a real literal needs a fraction:
1.0e3, not1e3.
1.5f32 and 1.5f64, but the parser does not accept them today, and there is no Float32 / Float64 type keyword. Use Real.
schema S { attributes { r : Real = 1.5f32 ; } }Bit-vector types#
A bit-vector is a value of a fixed number of bits: a machine word, a register, a flag, a packed protocol field, a byte of memory. Arithmetic on it wraps around.
| Type | Meaning |
|---|---|
BV<N> | Bit-vector of width N |
QWord | BV<64> |
DWord | BV<32> |
Word | BV<16> |
Byte | BV<8> |
Bits(N) | Older spelling of BV<N>, still accepted |
- The width
Nis a decimal integer literal:BV<0x40>is a parse error. - Use widths from 1 to 64. The parser also accepts
BV<0>and widths above 64, but a typed literal holds at most 64 bits and the concrete walk ofsaple analyzecomputes with 64-bit values. - The aliases name types only. A literal's suffix is always
:BV<N>:0xff:QWordis a parse error.
Typed literals#
A hex or binary literal followed by :BV<N> is a bit-vector of width N: 0xff:BV<8>, 0b1010:BV<4>, 0x1:BV<1>, 0xffffffffffffffff:BV<64>.
- A decimal literal takes no suffix:
5:BV<8>is a parse error. Write0x5:BV<8>. - The digits must fit in 64 bits. The parser does not check them against
N(0x1ff:BV<8>is accepted), so keep them within the width. - Without the suffix, a hex or binary literal is an
Int. Above0x7fffffffffffffffit is read as a negativeInt, and a decimal literal above9223372036854775807is a parse error (see Scalar types). Give 64-bit constants the suffix. - Next to a bit-vector, an untyped integer takes the bit-vector's width: with
acc : BV<8>,acc + 1adds0x1:BV<8>.
Arithmetic wraps around modulo 2N. Incrementing the 8-bit value 0xff gives 0x0; the operators, extract, concat and extension are described on Bit-vector Operations.
schema Machine {
attributes {
acc : BV<8> = 0xff:BV<8> ;
mask : BV<4> = 0b1010:BV<4> ;
word : DWord = 0xdeadbeef:BV<32> ;
Memory : Array(BV<64>, BV<8>) ;
}
}
transition inc() { guard: true ; effect: acc := acc + 1 ; }
agent Counter() {
attributes { }
process {
Main = inc() . done ;
}
}
system Wrap {
schema: Machine ;
agents { Counter() ; }
properties {
nonZero : acc != 0x0:BV<8> ;
}
}saple analyze wrap.spl 1 finding(s):
1. [PROPERTY] HIGH — Property 'nonZero' violated
Property: nonZero
Trace: inc
Witness: Memory=Unconstrained, acc=Bits(0, 8), mask=Bits(10, 4), word=Bits(3735928559, 32)The witness shows each bit-vector as Bits(value, width), the value in decimal: acc=Bits(0, 8) is the wrapped 0x0:BV<8>, and mask=Bits(10, 4) is 0b1010. Memory is Unconstrained because the walk does not keep memory contents (below).
Byte-addressed memory#
Array(BV<64>, BV<8>), an SMT array (see Collection types), maps 64-bit addresses to bytes. It is the memory of a machine model and is usually named Memory. Memory(a), or Memory[a], is the byte at address a, and Memory<N>(a) reads or writes N bytes little-endian (Multi-byte memory access).
Memory unchanged, and a load gives an unknown value. Pattern constraints and the front ends' models reason about memory through the solver.
Collection types#
| Type | Description |
|---|---|
Set(T) | Set of elements of type T |
Array(T, n) | Fixed-size array of n elements of type T (n is an integer literal) |
Array(K, V) | SMT array from keys of type K to values of type V, for example a byte-addressed memory Array(BV<64>, BV<8>) |
Queue(T) | FIFO queue of elements of type T |
The second argument of Array decides the form: an integer literal gives the sized array, a type gives the SMT array.
Enumeration types#
An enumeration declares a finite set of named members, separated by commas and terminated by ;:
type NodeState = { IDLE, ACTIVE, BLOCKED } ;
type Light = { RED, YELLOW, GREEN } ;An enumeration can type an attribute or a parameter. A parameter typed with an enumeration lets a pattern constraint name a member directly (c == RED); the members resolve to their positions in the declaration.
type Light = { RED, YELLOW, GREEN } ;
// A traffic light switches to colour c.
transition switchTo(c : Light) {
guard: true ;
effect: changes += 1 ;
}
// The first two switches on a path go to RED, then GREEN.
pattern redThenGreen {
signature { switchTo(a) . switchTo(b) . done }
constraints { a == RED && b == GREEN }
severity: LOW ;
}Algebraic datatypes#
A datatype declares constructors, each with zero or more named, typed fields; constructors are separated by |. A message on a channel, for example:
datatype Message = Empty | Data(seq: BV<32>, len: BV<16>) | Ack(seq: BV<32>) ;Modelling machine state#
A machine model keeps its registers, flags and memory as schema attributes of bit-vector type, and writes each instruction as a transition that reads and writes them. The order of the instructions is the process: a chain of actions, or one equation per basic block. The two front ends emit models of this shape; see Intel x86-64 Model and AArch64 Model.
General registers#
Each general register is a BV<64> attribute. The compact forms declare a register file in a few lines:
X0..X30 : BV<64> ; // X0, X1, …, X30, unknown at the start
RAX, RBX : QWord = 0x0:BV<64> ; // two registers, both 0
X1 = 0x4a0080:BV<64> ; // override the initial value of X1A register declared without an initial value is unknown when the concrete walk starts, and a guard or property that reads an unknown value is false there. Give an initial value to the registers that guards and properties read, or set one for a run with --input NAME=VALUE after the file name.
Sub-registers#
A narrower view of a register is not a separate attribute: read it with extract and write it back into the full register. On x86-64, EAX is extract<31,0>(RAX), AX is extract<15,0>(RAX), AL is extract<7,0>(RAX) and AH is extract<15,8>(RAX). On AArch64, W0 is extract<31,0>(X0).
What a narrow write does to the rest of the register depends on the architecture. On x86-64, a 32-bit write zero-extends into the full register, while an 8- or 16-bit write keeps the other bits. On AArch64, a write to a W register zero-extends. Both front ends model 32-bit writes as zero_extend(…, 32).
schema Regs {
attributes {
RAX, RBX : QWord = 0x0:BV<64> ;
RAX = 0xffffffffffffffff:BV<64> ;
RBX = 0x1122334455667788:BV<64> ;
step : Int = 0 ;
}
}
// mov al, bl: an 8-bit write keeps bits 63..8
transition mov_al_bl() {
guard: true ;
effect: RAX := concat(extract<63,8>(RAX), extract<7,0>(RBX)) ;
step += 1 ;
}
// mov ax, bx: a 16-bit write keeps bits 63..16
transition mov_ax_bx() {
guard: true ;
effect: RAX := concat(extract<63,16>(RAX), extract<15,0>(RBX)) ;
step += 1 ;
}
// mov eax, ebx: a 32-bit write clears bits 63..32
transition mov_eax_ebx() {
guard: true ;
effect: RAX := zero_extend(extract<31,0>(RBX), 32) ;
step += 1 ;
}
agent Program() {
attributes { }
process {
Main = mov_al_bl() . mov_ax_bx() . mov_eax_ebx() . done ;
}
}
system Views {
schema: Regs ;
agents { Program() ; }
properties {
afterAL : step == 1 => RAX == 0xffffffffffffff88:BV<64> ;
afterAX : step == 2 => RAX == 0xffffffffffff7788:BV<64> ;
afterEAX : step == 3 => RAX == 0x0000000055667788:BV<64> ;
lowByte : extract<7,0>(RAX) == 0x88:BV<8> ;
}
}saple analyze subregs.spl No vulnerabilities detected.Every property holds after every step. With 0xffffffff55667788:BV<64> in afterEAX, as if the 32-bit write kept the upper half, the report has a [PROPERTY] finding for afterEAX.
concat. Nest it for three parts, as in an AH write: concat(concat(extract<63,16>(RAX), v), extract<7,0>(RAX)).
Flags#
A flag is a BV<1> attribute. The AArch64 condition flags are four attributes, N, Z, C, V : BV<1>. In havoc(…) and in a regset, the name NZCV stands for all four. A comparison is a Bool, so set a flag from one with a conditional expression:
Z := if (r == 0x0:BV<32>) then 0x1:BV<1> else 0x0:BV<1> ;The model below computes the flags of a 32-bit add, then moves the stack pointer. It uses only operations the concrete walk evaluates (see Flag arithmetic):
schema Cpu {
attributes {
X0..X30 : BV<64> ; // general registers, unknown at the start
SP : BV<64> = 0x7ffffff000:BV<64> ;
PC : BV<64> = 0x401000:BV<64> ;
N, Z, C, V : BV<1> = 0x0:BV<1> ; // condition flags
X1 = 0xffffffff:BV<64> ; // overrides
X2 = 0x1:BV<64> ;
}
}
// adds w0, w1, w2: a 32-bit add that sets the flags
transition adds_w0_w1_w2() {
guard: true ;
effect: let a = extract<31,0>(X1) ;
let b = extract<31,0>(X2) ;
let wide = zero_extend(a, 1) + zero_extend(b, 1) ; // 33 bits
let r = extract<31,0>(wide) ;
N := extract<31,31>(r) ;
Z := if (r == 0x0:BV<32>) then 0x1:BV<1> else 0x0:BV<1> ;
C := extract<32,32>(wide) ;
V := ~(extract<31,31>(a) ^ extract<31,31>(b)) & (extract<31,31>(a) ^ extract<31,31>(r)) ;
X0 := zero_extend(r, 32) ; // writing W0 clears bits 63..32
PC := PC + 0x4:BV<64> ;
}
// sub sp, sp, #16
transition sub_sp_16() {
guard: true ;
effect: SP := SP - 0x10:BV<64> ;
PC := PC + 0x4:BV<64> ;
}
agent Program() {
attributes { }
process {
Main = adds_w0_w1_w2() . sub_sp_16() . done ;
}
}
system Run {
schema: Cpu ;
agents { Program() ; }
properties {
noCarry : C == 0x0:BV<1> ;
}
}saple analyze cpu.spl 2 finding(s):
1. [PROPERTY] HIGH — Property 'noCarry' violated
Property: noCarry
Trace: adds_w0_w1_w2
Witness: C=Bits(1, 1), N=Bits(0, 1), PC=Bits(4198404, 64), SP=Bits(549755809792, 64), V=Bits(0, 1), X0=Bits(0, 64), X1=Bits(4294967295, 64), X10=Unconstrained
2. [PROPERTY] HIGH — Property 'noCarry' violated
Property: noCarry
Trace: adds_w0_w1_w2 → sub_sp_16
Witness: C=Bits(1, 1), N=Bits(0, 1), PC=Bits(4198408, 64), SP=Bits(549755809776, 64), V=Bits(0, 1), X0=Bits(0, 64), X1=Bits(4294967295, 64), X10=Unconstrained0xffffffff + 0x1 wraps to 0 in 32 bits, so the add sets Z and C, and writing W0 leaves X0 at 0. PC advances by 4 per instruction (4198404 is 0x401004), and sub_sp_16 moves SP down by 16 (549755809776 is 0x7fffffeff0). The property is checked after every action, so it is reported once per step. The witness lists the first eight attributes in alphabetical order; X10=Unconstrained is a register that was never given a value.
x86-64 flags
The x86-64 front end declares RFLAGS : BV<64>, and it keeps each status flag as its own one-bit location written with the field syntax: RFLAGS.CF, RFLAGS.PF, RFLAGS.AF, RFLAGS.ZF, RFLAGS.SF and RFLAGS.OF (popf also writes RFLAGS.DF). These locations are not bits of the RFLAGS attribute, which no instruction writes. A conditional jump becomes a guard over them:
// From the model that `saple translate --arch x86-64` writes for `sub rsp, 0x20` … `je out`
transition sub_op(dst, src) {
guard: true ;
effect: let dst_in = dst ;
let src_in = src ;
let result = (dst - src) ;
dst := result ;
RFLAGS.CF := (if (dst_in <u src_in) then 0x1:BV<1> else 0x0:BV<1>) ;
…
RFLAGS.SF := extract<63,63>(result) ;
RFLAGS.ZF := (if (result == 0x0:BV<64>) then 0x1:BV<1> else 0x0:BV<1>) ;
…
}
…
([(RFLAGS.ZF == 1)] B2 + [!(RFLAGS.ZF == 1)] B1) ;The solver evaluates <u; the concrete walk does not (see Where operations are evaluated). In a hand-written model, declare such flags as BV<1> attributes, for example ZF : BV<1> = 0x0:BV<1>. A name with a dot cannot be declared in a schema, so RFLAGS.ZF would start with no value.
RAX and RFLAGS as BV<64> is analysed as a model of the x86-64 front end. The concrete walk then leaves guards undecided (it follows both branches) and treats the register arguments of actions as unknown. Give a hand-written model other names, or no RFLAGS attribute.
Program counter#
The program counter is an ordinary BV<64> attribute. The order of the instructions is given by the process, not by the counter. The AArch64 front end declares PC with the recorded start address and sets it only in its branch actions (branch_taken(target), branch_fall(next)). The x86-64 front end declares RIP, but no transition writes it: its control flow is one equation per basic block.
Stack pointer#
The stack pointer is a BV<64> register like the others (SP on AArch64, RSP on x86-64), and the stack lives in Memory. The x86-64 front end's push decrements RSP by 8 and then writes the eight bytes of the value at the new RSP. For a static listing, the analysis assumes a SysV entry stack pointer (see Intel x86-64 Model). The AArch64 front end declares SP with its recorded value.
Register groups: regset#
regset name = { … } ; names a group of registers for havoc, for example the registers a call may change. Its items are attribute names, ranges such as X0..X18, NZCV and other regsets.
schema Cpu {
attributes {
X0..X30 : BV<64> = 0x0:BV<64> ;
N, Z, C, V : BV<1> = 0x0:BV<1> ;
}
}
// AAPCS64: a call may change X0..X18, X30 and the flags
regset caller_saved = { X0..X18, X30, NZCV } ;
transition call_helper() { guard: true ; effect: havoc(caller_saved) ; }
agent Program() {
attributes { }
process {
Main = call_helper() . done ;
}
}
system Calls {
schema: Cpu ;
agents { Program() ; }
properties {
calleeSavedKept : X19 == 0x0:BV<64> ;
}
}saple analyze regset.spl No vulnerabilities detected.X19 is not in the group, so the call keeps it. A property on X0 instead is reported, with X0=Unconstrained in the witness. A regset name can be used only inside havoc(…) or another regset. Anywhere else, as in X0 := caller_saved, it is a parse error: regset `caller_saved` can only be used inside `havoc(…)` or another regset.
Register enumerations#
For a pattern constraint to say which register an action works on, declare the register names as an enumeration and pass them as arguments. A binder bound to such an argument can be compared with a member: r == RSP holds when the operand is RSP.
type X86Reg = { RAX, RBX, RCX, RDX, RSI, RDI, RSP, RBP } ;
schema Stack {
attributes {
depth : Int = 0 ;
}
}
transition push(r : X86Reg) { guard: true ; effect: depth += 1 ; }
agent Program() {
attributes { }
process {
Main = push(RSP) . done ;
}
}
pattern pushesStackPointer {
signature { push(r) . done }
constraints { r == RSP }
severity: LOW ;
}
system Frames {
schema: Stack ;
agents { Program() ; }
}saple analyze regenum.spl 2 finding(s):
1. [STRUCTURAL] LOW — Pattern 'pushesStackPointer' structurally matches behavior (constraint satisfiable)
Pattern: pushesStackPointer
Trace: push
2. [CONCRETE] LOW — Pattern 'pushesStackPointer' confirmed with concrete inputs
Pattern: pushesStackPointer
Trace: push
Witness: depth=1, r=6- With
push(RBP)instead, the report isNo vulnerabilities detected.Members compare by their position:r=6in the witness isRSP. - SAPLE treats an enumeration whose name ends in
Regas a list of registers. A binder bound to one of its members stands for the register's value, except in a comparison with a register name such asr == RSP. With another enumeration name, the binder stands for the member's position. - The x86-64 front end declares
type X86Reg = { RAX, RBX, …, R15, RIP, RFLAGS }, but the parameters of its transitions are untyped (mov32(dst, src)).