Skip to content

Commit 87f473d

Browse files
committed
add post contents for symbolic verifiction of condition code
1 parent 1e4dba3 commit 87f473d

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

_posts/Assembly/2025-03-17-EFLAGS-Registers-in-x86.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,56 @@ def x86g_calculate_condition(state, cond, cc_op, cc_dep1, cc_dep2, cc_ndep):
351351
return pc_calculate_condition_simple(state, cond, cc_op, cc_dep1, cc_dep2, cc_ndep, platform="X86")
352352
else:
353353
return pc_calculate_condition(state, cond, cc_op, cc_dep1, cc_dep2, cc_ndep, platform="X86")
354+
# in arm, the computation is like:
355+
356+
def armg_calculate_condition(state, cond_n_op, cc_dep1, cc_dep2, cc_dep3):
357+
concrete_cond_n_op = op_concretize(cond_n_op)
358+
359+
cond = concrete_cond_n_op >> 4
360+
cc_op = concrete_cond_n_op & 0xF
361+
inv = cond & 1
362+
363+
concrete_cond = op_concretize(cond)
364+
flag = None
365+
366+
# NOTE: adding constraints afterwards works here *only* because the constraints are actually useless, because we
367+
# require cc_op to be unique. If we didn't, we'd need to pass the constraints into any functions called after the
368+
# constraints were created.
369+
370+
if concrete_cond == ARMCondAL:
371+
flag = claripy.BVV(1, 32)
372+
elif concrete_cond in [ARMCondEQ, ARMCondNE]:
373+
zf = armg_calculate_flag_z(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
374+
flag = inv ^ zf
375+
elif concrete_cond in [ARMCondHS, ARMCondLO]:
376+
cf = armg_calculate_flag_c(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
377+
flag = inv ^ cf
378+
elif concrete_cond in [ARMCondMI, ARMCondPL]:
379+
nf = armg_calculate_flag_n(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
380+
flag = inv ^ nf
381+
elif concrete_cond in [ARMCondVS, ARMCondVC]:
382+
vf = armg_calculate_flag_v(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
383+
flag = inv ^ vf
384+
elif concrete_cond in [ARMCondHI, ARMCondLS]:
385+
cf = armg_calculate_flag_c(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
386+
zf = armg_calculate_flag_z(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
387+
flag = inv ^ (cf & ~zf)
388+
elif concrete_cond in [ARMCondGE, ARMCondLT]:
389+
nf = armg_calculate_flag_n(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
390+
vf = armg_calculate_flag_v(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
391+
flag = inv ^ (1 & ~(nf ^ vf))
392+
elif concrete_cond in [ARMCondGT, ARMCondLE]:
393+
nf = armg_calculate_flag_n(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
394+
vf = armg_calculate_flag_v(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
395+
zf = armg_calculate_flag_z(state, cc_op, cc_dep1, cc_dep2, cc_dep3)
396+
flag = inv ^ (1 & ~(zf | (nf ^ vf)))
397+
398+
if flag is not None:
399+
return flag
400+
401+
l.error("Unrecognized condition %d in armg_calculate_condition", concrete_cond)
402+
raise SimCCallError("Unrecognized condition %d in armg_calculate_condition" % concrete_cond)
403+
354404
```
355405
The code above is to **check whether the x86 Carry Flag (CF) would cause a conditional branch** to be taken — i.e., _“Would a `jb` (jump if below) or `jc` (jump if carry) actually occur?”_
356406

@@ -367,6 +417,7 @@ This makes sense in context when:
367417
So the code here is to check whether the cf will trigger a branch here, not to extract the cflags.
368418

369419
```python
420+
cf_normal = (claripy.LShR(eflags, s_ccall.data["X86"]["CondBitOffsets"]["G_CC_SHIFT_C"]) & 1 ) # normal cf
370421
cf_shl = claripy.Extract(0, 0,
371422
s_ccall.x86g_calculate_condition(
372423
state,

_posts/Assembly/2025-11-15-angr-condition-code-verification.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tags: [assembly,python,angr] # TAG names should always be lowercase
66
published: false
77
---
88

9-
> Problem: I need to verify different condition codes between guset and hose symbolic machines, but how to verify them across different isas?
9+
> Problem: I need to verify different condition codes between guest and hose symbolic machines, but how to verify them across different isas?
1010
1111
# Overviews
1212

0 commit comments

Comments
 (0)