angr.analyses.decompiler.known_patterns.posix_macros¶
Single-expression glibc/POSIX macros that the preprocessor inlines into every caller, so they survive into decompiled output as bare bit arithmetic.
<sys/stat.h> file-type predicates all expand through one helper:
#define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask))
with __S_IFMT == 0170000 and one of the seven __S_IFxxx values, so each
predicate is a two-node expression (mode & 0xf000) == <S_IFxxx>. The mask is
the same in every libc and in the Windows CRT (_S_IFMT), and the pairing of
that mask with exactly one of the seven legal type values is self-guarding, so
these are enabled by default.
<sys/wait.h> status decoders (glibc bits/waitstatus.h):
#define __WTERMSIG(status) ((status) & 0x7f)
#define __WIFEXITED(status) (__WTERMSIG(status) == 0)
#define __WIFSIGNALED(status) (((signed char) (((status) & 0x7f) + 1) >> 1) > 0)
#define __WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
Only the first three leave enough residue to identify. gcc rewrites
WIFSIGNALED into (signed char)((status & 0x7f) + 1) > 1 (the >> 1 and
the > 0 fold together), which is what is matched here; the signed compare is
required, since the (signed char) cast is the whole point of the idiom.
WIFSTOPPED narrows to a bare (char)status == 0x7f (the & 0xff is
absorbed by the byte-sized compare) and WTERMSIG is a bare status & 0x7f;
both are common enough shapes to be opt-in. WEXITSTATUS / WSTOPSIG
(identical macros in glibc) are not matchable at all on x86: gcc compiles
(status & 0xff00) >> 8 to a single movzbl %ah and the decompiler renders
it as a plain byte-sized virtual variable, leaving no shift or mask to match.
WIFSIGNALED does not match on x86-32, and should not be made to. There
the (signed char) cast lifts to an Extract rather than a Convert, and
gcc takes the inverted branch, so the AIL reads
Extract(((status & 0x7f) + 1), 8bits@0) CmpLE 1<8>
The missing Extract node in the DSL is the smaller half of the problem: that
expression is !WIFSIGNALED(status), and a KnownPattern replaces the matched
expression by the value of its synthesized call, so there is no way to spell the
negation. A pattern matching this shape and emitting WIFSIGNALED(status)
would silently swap the two arms of every wait-status branch on 32-bit: the
“child was killed by signal N” arm would become the “exited normally” one. The
x86-32 sites (0 of 13 busybox functions) are therefore a deliberate gap until the
pattern model can express a negated result.
<sys/sysmacros.h> major() is an extern __inline function that -O2
inlines; its glibc encoding scatters the major number over bits 8-19 and 32-63:
__major = ((__dev & 0x00000000000fff00u) >> 8);
__major |= ((__dev & 0xfffff00000000000u) >> 32);
which gcc reassociates into ((dev >> 8) & 0xfff) | ((dev >> 32) & 0xfffff000)
on 64-bit targets, and into a funnel-shift pair on 32-bit ones (see
_build_major32). That constant quartet is unique enough to enable by default.
minor() and makedev() compile to Insert/Extract AIL expressions
for which the pattern DSL has no node, so they are not defined here.
Every macro argument is matched as a virtual variable (optionally behind a
compiler-inserted narrowing Convert), never as a Load. The consequence is that a
caller reading the mode straight out of a struct stat field at the point of
use does not match at all: PVVar.match returns None on a Load, so the finder
never produces a match. (It does not reach _rewrite_callsite and fail
there; that error can only occur for patterns whose parameter capture is not a
bare PVVar.) Measured on real -O2 code this costs less than it sounds: struct
stat is almost always a stack local, so st.st_mode is lifted to a stack vvar
that matches; only pointer dereferences such as inode->i_mode stay Loads.
Calibrated against tests/x86_64/decompiler/known_patterns_glibc_macros (gcc 12.2 -O2; re-checked at -O1/-O3, and against glibc 2.36 headers).
- angr.analyses.decompiler.known_patterns.posix_macros.macro_operand(cap)¶
A macro’s scalar argument: a virtual variable, possibly narrowed by the compiler, or a field read.
PVVar does not see through Convert wrappers on its own, and binding the pre-narrowing vvar keeps the synthesized call argument correctly typed.
The
PLoadarm is what makesS_ISDIR(inode->i_mode)matchable at all. Before the outliner could lift a parameter expression into a temporary, a capture had to be a virtual variable, so the same macro was recognized when its argument was a stack local and invisible when it was a field of a struct reached through a pointer, a split with no meaning to a reader. The mask and the type constant still carry the identification; the argument’s shape never did.