Intel x86-64 Model
The x86-64 front end, --arch x86-64, reads a static Intel-syntax assembly listing of one function and turns it into a BV<64> SPL model with bit-vector instruction semantics. saple analyze then matches the x86 pattern library against every path of the function and asks Z3 whether each pattern's constraint can hold on that path.
--arch x86-64.asm extension selects the x86-64 front end on its own; a listing saved as .txt or .s does not. Give --arch x86-64 to both saple translate and saple analyze. When the front end cannot run inside saple analyze (a .txt listing without --arch, an unknown --arch, a listing the front end rejects), analyze prints the error on standard error but still reports No vulnerabilities detected. ([] in JSON) and exits 0.
Input: a listing#
- One instruction per line, Intel syntax (
mov dword [rbp-4], edi,imul eax, eax, 0x40). label:on a line of its own defines a jump target.- Blank lines and comments starting with
;or#(whole-line or trailing) are skipped. - A jump target that is a bare name must be a label of the listing or a curated libc callee (
jmp mallocis a tail call);jmp footo an undefined name is an error at that line. A call to any other name (call foo,call foo@plt,call __stack_chk_fail) is accepted and becomescall_unknown().
Instructions get synthetic addresses 0x1000, 0x1010, … (one per instruction), which appear in the comments of the model.
What the model contains#
- Bit-vector semantics. A
BV<64>schema with the 16 general registers,RIP,RFLAGSand a byte memoryMemory : Array(BV<64>, BV<8>); one transition per instruction form, with the flag effects of the Intel SDM (each transition cites its SDM section). - Control flow. One process equation per basic block. A conditional jump becomes a guarded choice over the flag fields,
([(RFLAGS.ZF == 0)] B1 + [!(RFLAGS.ZF == 0)] B2); a backward jump names an earlier equation, so loops are recursive equations;retends the path withdone. An indirect jump continues at the start of any block or leaves the function. - Data flow. The analyzer encodes each path in SSA form: every register and memory write gets a new version, so a pattern's binders are tied to the values the instructions actually compute along that path, and the guards of the conditional jumps taken on the path become path conditions.
- Library calls. Calls to the curated libc callees
malloc,calloc,realloc,free,memcpy,memmove,memset,strcpy,strncpy,strlenandstrcmp(an@pltsuffix is accepted) become transitions such ascall_malloc(RDI)that take their SysV argument registers and havoc what the callee may change. Any other call — a label of the listing, another named function, a register, an address — is not inlined: it becomescall_unknown(), which havocsRAX,RCX,RDX,RSI,RDI,R8–R11, the status flags andMemory. - Unsupported instructions. An instruction without modelled semantics becomes a transition
unsupported_<mnemonic>_<hash>()that havocs everything the instruction may write. The model over-approximates such an instruction; it does not stop the analysis. - Inputs. Registers have no initial value: the function's inputs are symbolic. The analysis assumes a SysV entry stack pointer and prints that assumption with every report.
Example: CWE-190 before malloc#
The listing demo/corpus/cwe-190/vulnerable/imul32_overflow_before_malloc.txt multiplies a 32-bit argument by 64 and passes the product to malloc. A large argument wraps the 32-bit product, so the allocation is too small.
push rbp
mov rbp, rsp
sub rsp, 32
mov dword [rbp-4], edi
mov eax, dword [rbp-4]
imul eax, eax, 0x40
mov edi, eax
call malloc
mov qword [rbp-16], rax
mov rax, qword [rbp-16]
mov rsp, rbp
pop rbp
ret1. Translate#
saple translate demo/corpus/cwe-190/vulnerable/imul32_overflow_before_malloc.txt \
--arch x86-64 -o imul.splwarning: static listing: the initial registers are symbolic (no initial values were observed); the analysis assumes a SysV entry stack pointer
Wrote SPL to imul.spl
Translated: 1 agents, 1 behaviors, 1 warningsThe process of the generated model, one action per instruction (the transitions and the schema are left out here):
agent program() {
attributes { }
process {
B0 =
// step 0 @ 0x1000: push rbp
push(RBP) .
// step 1 @ 0x1010: mov rbp, rsp
mov(RBP, RSP) .
// step 2 @ 0x1020: sub rsp, 32
sub_op(RSP, 32) .
// step 3 @ 0x1030: mov dword [rbp-4], edi
mov_st((RBP + 0xfffffffffffffffc:BV<64>), RDI, 32) .
// step 4 @ 0x1040: mov eax, dword [rbp-4]
mov_mem(RAX, (RBP + 0xfffffffffffffffc:BV<64>), 32) .
// step 5 @ 0x1050: imul eax, eax, 0x40
imul32(RAX, RAX, 64) .
// step 6 @ 0x1060: mov edi, eax
mov32(RDI, RAX) .
// step 7 @ 0x1070: call malloc
call_malloc(RDI) .
// step 8 @ 0x1080: mov qword [rbp-16], rax
mov_st((RBP + 0xfffffffffffffff0:BV<64>), RAX, 64) .
// step 9 @ 0x1090: mov rax, qword [rbp-16]
mov_mem(RAX, (RBP + 0xfffffffffffffff0:BV<64>), 64) .
// step 10 @ 0x10a0: mov rsp, rbp
mov(RSP, RBP) .
// step 11 @ 0x10b0: pop rbp
pop(RBP) .
// step 12 @ 0x10c0: ret
done ;
}
}and the semantics of the multiply, as emitted:
transition imul32(dst_reg, src_reg, multiplier) {
guard: true ;
effect: let full = (sign_extend(extract<31,0>(src_reg), 32) * sign_extend(extract<31,0>(multiplier), 32)) ;
let result = extract<31,0>(full) ;
let ovf = ~(sign_extend(result, 32) == full) ;
dst_reg := zero_extend(result, 32) ;
RFLAGS.CF := ovf ;
RFLAGS.OF := ovf ;
}2. Analyze#
Run from the repository root, so that --pattern-set v0.6 finds patterns/x86-cwe-v0.6.spl:
saple analyze imul.spl --pattern-set=v0.6Output (the banner goes to standard error):
Assumption: x86-64 SysV entry: RSP@0 is a user-space stack pointer, 0x10000 <= RSP@0 < 0x800000000000 and RSP@0 % 16 == 8
2 finding(s):
1. [STRUCTURAL] CRITICAL — Pattern 'cwe_190_imul32_wraparound_before_malloc_v6' structurally matches behavior (constraint undecided: width mismatch: BV<64> against BV<96>)
Pattern: cwe_190_imul32_wraparound_before_malloc_v6
Trace: imul32 → call_malloc
2. [STRUCTURAL] CRITICAL — Pattern 'cwe_190_imul32_wraparound_before_malloc_bv' structurally matches behavior (constraint satisfiable)
Pattern: cwe_190_imul32_wraparound_before_malloc_bv
Trace: imul32 → call_mallocThe run exits with status 1. Finding 1 comes from the library's older _v6 pattern, written for an integer schema: its constraint does not type on a bit-vector model, so it is reported undecided with a width mismatch. Such a finding says nothing about the program and does not affect the exit status. The _bv pattern is the one that applies.
Analyzing the listing directly#
saple analyze also accepts the listing itself and translates it in memory:
saple analyze demo/corpus/cwe-190/vulnerable/imul32_overflow_before_malloc.txt \
--arch x86-64 --pattern-set=v0.6The findings are the same, plus the front end's warning about the symbolic initial registers, reported as a MEDIUM finding without a pattern.
A guarded variant#
demo/corpus/cwe-190/safe/checked_before_calloc.txt compares the argument with 0x100000 and jumps past the multiply (ja overflow_handler) when it is larger. On the only path that reaches calloc the guard bounds the argument, the product cannot wrap, and the _bv pattern's constraint is unsatisfiable: the report holds no _bv finding and the run exits with status 0.
Limits#
- A path is matched structurally up to 50 steps and decided over its SSA encoding up to 256 steps; each pattern checks at most
--path-budgetpaths (256 by default). Work cut short by any of these — for example a loop unrolled past the cap — makes the pattern UNKNOWN for that part. It is never silently dropped. - Calls to functions other than the curated callees are havocs, not inlined bodies.
- The listing is one function; entry into the middle of a block is not modelled.