angr.analyses.decompiler.known_patterns.ctype_tables¶
The glibc ctype macros: isspace, isdigit, … and tolower/toupper.
<ctype.h> classifies a character by indexing a locale table rather than by
comparing ranges:
#define __isctype(c, type) ((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) type)
so isspace(c) compiles to a call to __ctype_b_loc, a load of the table
pointer it returns, a 16-bit indexed load, and a mask. Every predicate is that
one shape with a different constant, which is why they are generated here rather
than written out.
Measured over the -O2 benchmark corpus, the eleven predicates account for 7,894
macro-oracle sites across 24 projects, wider project coverage than any family
KnownPatterns covers today, with tolower and toupper adding 1,588 more.
The callee is what makes the whole family self-guarding: nothing else calls
__ctype_b_loc. PCallResult binds the table to that call
without consuming it, which matters because the compiler hoists the call out of
loops and indexes the table from every predicate in the function.
Two byte lanes. glibc’s _ISbit(n) is n < 8 ? (1 << n) << 8 : (1 << n) >> 8,
so eight of the twelve masks live in the high byte of the table entry and four in
the low byte, and gcc tests a byte rather than the 16-bit word. Hence three
spellings per predicate rather than one: the mask as written against the whole
entry, and, for a high mask, an 8-bit slice at byte 1 against the mask’s high
byte (test $0x20,%ah) or that byte loaded on its own
(testb $0x20,1(%rax,%rdx,2)); for a low mask, an 8-bit slice at byte 0. The
slice spellings are not interchangeable with the first: structural matching skips
a Convert and does not skip an Extract.
Calibrated against tests/x86_64/decompiler/known_patterns_ctype (gcc 12.2.0 -O2).
- angr.analyses.decompiler.known_patterns.ctype_tables.CTYPE_B_LOC = frozenset({'___ctype_b_loc', '__ctype_b_loc'})¶
glibc’s
__ctype_b_locunder every spelling a binary may know it by.
- angr.analyses.decompiler.known_patterns.ctype_tables.CTYPE_PREDICATES: tuple[tuple[str, int], ...] = (('isupper', 256), ('islower', 512), ('isalpha', 1024), ('isdigit', 2048), ('isxdigit', 4096), ('isspace', 8192), ('isprint', 16384), ('isgraph', 32768), ('isblank', 1), ('iscntrl', 2), ('ispunct', 4), ('isalnum', 8))¶
(macro name, mask). The values are glibc’senumfrom <ctype.h>, computed by_ISbit(n) = n < 8 ? (1 << n) << 8 : (1 << n) >> 8.