"""Import stubs for the simplexml / stringtable differential test (tools/port/test_xml.py). Behaviour follows 2009 Darwin libc / libstdc++ (gcc 4.0) on i386: - ctype in the "C" locale (isspace is true only for 9..13 and 32; negative values other than EOF are not space), - strchr also finds the terminating NUL when searching for 0, strncmp returns the unsigned byte difference, - printf/scanf: the conversions used by the SDK (%d %s %c %u %x ...), sscanf returns EOF on an input failure before the first conversion, - std::list node hook/unhook and the red-black tree helpers are ports of libstdc++'s list.cc / tree.cc. All stubs are registered into emu.EXTRA_STUBS at the bottom of this file with setdefault, so an equivalent stub that another area's file registered first is kept (emu.py's own loader would let the last file win, and it skips names starting with "__" such as the libstdc++ "__Z..." symbols). The functions therefore carry an "x" prefix. """ import re import struct from unicorn.x86_const import UC_X86_REG_ESP def s32(v): v &= 0xffffffff return v - (1 << 32) if v & 0x80000000 else v # ---- ctype / string --------------------------------------------------------------------------------------- def x_isspace(g): (c,) = g._args(1) c = s32(c) # _CTYPE_S = 0x4000 in Darwin's rune table; only ASCII entries are set in the C locale. g._ret(0x4000 if c in (9, 10, 11, 12, 13, 32) else 0) def x_strchr(g): s, c = g._args(2) c &= 0xff i = 0 while True: b = g.uc.mem_read(s + i, 1)[0] if b == c: g._ret(s + i) return if b == 0: g._ret(0) return i += 1 def x_strncmp(g): a, b, n = g._args(3) for i in range(n): x = g.uc.mem_read(a + i, 1)[0] y = g.uc.mem_read(b + i, 1)[0] if x != y: g._ret(x - y) return if x == 0: break g._ret(0) def x_strstr(g): h, n = g._args(2) hay = g.read_cstr(h) needle = g.read_cstr(n) i = hay.find(needle) g._ret(h + i if i >= 0 else 0) # ---- printf / scanf --------------------------------------------------------------------------------------- # FreeBSD-derived vfprintf (Darwin): an unknown conversion character is printed as itself, no argument used. _SPEC = re.compile(rb"%([-+ #0']*)(\*|\d+)?(?:\.(\*|\d*))?(hh|h|ll|l|q|L|j|z|t)?(.)", re.S) def format_c(g, fmt, args): """printf-style formatting of the bytes `fmt`; `args` yields 32-bit stack words (i386 va_list).""" out = bytearray() pos = 0 while True: m = _SPEC.search(fmt, pos) if not m: out += fmt[pos:] return bytes(out) out += fmt[pos:m.start()] pos = m.end() flags, width, prec, length, conv = m.groups() flags = flags.decode().replace("'", '') if conv == b'%': out += b'%' continue if width == b'*': width = s32(next(args)) if width < 0: flags += '-' width = -width else: width = int(width) if width else None if prec == b'*': prec = s32(next(args)) prec = prec if prec >= 0 else None elif prec is not None: prec = int(prec) if prec else 0 if conv in b'di': v = next(args) if length in (b'll', b'q', b'j'): v |= next(args) << 32 v = v - (1 << 64) if v & (1 << 63) else v else: v = s32(v) spec = '%' + flags + (str(width) if width else '') + ('.%d' % prec if prec is not None else '') + 'd' out += (spec % v).encode() elif conv in b'ouxX': v = next(args) if length in (b'll', b'q', b'j'): v |= next(args) << 32 spec = '%' + flags + (str(width) if width else '') + ('.%d' % prec if prec is not None else '') + \ (conv.decode() if conv != b'u' else 'd') out += (spec % v).encode() elif conv == b'c': s = bytes([next(args) & 0xff]) out += s.rjust(width or 0) if '-' not in flags else s.ljust(width or 0) elif conv == b's': p = next(args) s = g.read_cstr(p) if p else b'(null)' if prec is not None: s = s[:prec] out += s.rjust(width or 0) if '-' not in flags else s.ljust(width or 0) elif conv == b'p': out += b'0x%x' % next(args) elif conv == b'n': next(args) else: out += conv def arg_words(g, ptr): while True: yield g.u32(ptr) ptr += 4 def x_vsnprintf(g): buf, size, fmt, va = g._args(4) s = format_c(g, g.read_cstr(fmt), arg_words(g, va)) if size: n = min(len(s), size - 1) g.uc.mem_write(buf, s[:n] + b'\0') g._ret(len(s)) def x_vasprintf(g): ret, fmt, va = g._args(3) s = format_c(g, g.read_cstr(fmt), arg_words(g, va)) p = g.malloc(len(s) + 1) g.uc.mem_write(p, s + b'\0') g.w32(ret, p) g._ret(len(s)) def x_sscanf(g): src, fmt = g._args(2) esp_args = arg_words(g, g.uc.reg_read(UC_X86_REG_ESP) + 12) s = g.read_cstr(src) f = g.read_cstr(fmt) si = 0 fi = 0 count = 0 ws = b' \t\n\v\f\r' def result(input_failure): return -1 if (input_failure and count == 0) else count while fi < len(f): fc = f[fi:fi + 1] if fc in ws: while si < len(s) and s[si:si + 1] in ws: si += 1 fi += 1 continue if fc != b'%': if si >= len(s): g._ret(result(True)); return if s[si:si + 1] != fc: g._ret(result(False)); return si += 1 fi += 1 continue m = re.match(rb'%(\*)?(\d+)?(hh|h|l|ll)?([dis%])', f[fi:]) if not m: raise ValueError('unsupported scanf format %r' % f) fi += m.end() suppress, width, _length, conv = m.groups() if conv == b'%': while si < len(s) and s[si:si + 1] in ws: si += 1 if si >= len(s): g._ret(result(True)); return if s[si:si + 1] != b'%': g._ret(result(False)); return si += 1 continue while si < len(s) and s[si:si + 1] in ws: si += 1 if si >= len(s): g._ret(result(True)); return width = int(width) if width else 1 << 30 if conv == b's': start = si while si < len(s) and si - start < width and s[si:si + 1] not in ws: si += 1 if not suppress: p = next(esp_args) g.uc.mem_write(p, s[start:si] + b'\0') count += 1 continue start = si if si < len(s) and si - start < width and s[si:si + 1] in b'+-': si += 1 dstart = si while si < len(s) and si - start < width and 48 <= s[si] <= 57: si += 1 if si == dstart: g._ret(result(False)); return if not suppress: p = next(esp_args) g.uc.mem_write(p, struct.pack(' members through __jump_table stubs although it defines them itself; dyld # binds a coalesced symbol to the main executable's definition, so the stub continues at the local code. COALESCED_LOCAL = [ '__ZNSt8_Rb_treeI3strSt4pairIKS0_S0_ESt10_Select1stIS3_ESt4lessIS0_ESaIS3_EE4findERS2_', '__ZNSt8_Rb_treeI3strSt4pairIKS0_S0_ESt10_Select1stIS3_ESt4lessIS0_ESaIS3_EE11upper_boundERS2_', '__ZNSt8_Rb_treeI3strSt4pairIKS0_S0_ESt10_Select1stIS3_ESt4lessIS0_ESaIS3_EE5eraseESt17_Rb_tree_iteratorIS3_ESB_', '__ZNSt8_Rb_treeI3strSt4pairIKS0_S0_ESt10_Select1stIS3_ESt4lessIS0_ESaIS3_EE5eraseERS2_', ] def make_local_jump(name): def stub(g): from unicorn.x86_const import UC_X86_REG_EIP g.uc.reg_write(UC_X86_REG_EIP, g.symbols[name]) return stub # ---- registration of the "__Z..." names (skipped by emu.py's loader) --------------------------------------- def register_cxx(): import sys names = {k[1:]: v for k, v in globals().items() if k.startswith('x_') and callable(v)} names.update({k: v for k, v in globals().items() if k.startswith('__Z') and callable(v)}) for n in COALESCED_LOCAL: names[n] = make_local_jump(n) for mod in list(sys.modules.values()): stubs = getattr(mod, 'EXTRA_STUBS', None) if isinstance(stubs, dict): for k, v in names.items(): stubs.setdefault(k, v) register_cxx()