"""Import stubs for the TMemoryFile / TMemDataSource differential test (tools/port/test_memfile.py). Behaves like 2009 Darwin i386 (libSystem, libstdc++ of gcc 4.0): - _vsnprintf: C99 semantics (returns the full length, writes at most n-1 chars plus NUL), integer/string subset. - _pthread_mutex_lock/_unlock: return 0 and count the calls in g.mutex_log (the test checks lock/unlock pairs). - std::_Rb_tree_* free functions of libstdc++ (tree.cc), transcribed from the gcc 4.0 source. - Weak template instantiations that the binary imports through its jump table although it defines them itself (e.g. std::_Rb_tree>::erase(const str&)) are bound to the binary's own copy, as dyld coalesces them to the executable's definition. emu.py's loader skips names that start with '__' (all C++ imports), so the C++ ones are registered into EXTRA_STUBS directly at load time (see h_register at the bottom). """ import re import struct from unicorn.x86_const import UC_X86_REG_EIP RED, BLACK = 0, 1 # ---- libc ------------------------------------------------------------------------------------------------------ def h_format(g, fmt, ap): out = bytearray() i = 0 while i < len(fmt): c = fmt[i] if c != 0x25: out.append(c); i += 1; continue m = re.match(rb'%([-+ #0]*)(\d*)(?:\.(\d+))?(hh|h|ll|l|z|j|t)?([diouxXcs%])', fmt[i:]) if not m: raise RuntimeError(f'_vsnprintf: unsupported format {fmt[i:]!r}') flags, width, prec, length, conv = m.groups() i += m.end() if conv == b'%': out += b'%'; continue if length == b'll': v = g.u32(ap) | (g.u32(ap + 4) << 32); ap += 8; bits = 64 else: v = g.u32(ap); ap += 4; bits = 32 if conv in (b'd', b'i'): if v & (1 << (bits - 1)): v -= 1 << bits body = str(abs(v)).encode() sign = b'-' if v < 0 else (b'+' if b'+' in flags else (b' ' if b' ' in flags else b'')) elif conv == b'u': body, sign = str(v).encode(), b'' elif conv == b'o': body, sign = format(v, 'o').encode(), b'' elif conv in (b'x', b'X'): body, sign = format(v, 'x' if conv == b'x' else 'X').encode(), b'' elif conv == b'c': body, sign = bytes([v & 0xff]), b'' else: # s body, sign = (g.read_cstr(v) if v else b'(null)'), b'' if prec is not None: body = body[:int(prec)] if conv not in (b's', b'c') and prec is not None: body = body.rjust(int(prec), b'0') w = int(width) if width else 0 pad = w - len(sign) - len(body) if pad > 0: if b'-' in flags: body = sign + body + b' ' * pad; sign = b'' elif b'0' in flags and prec is None and conv not in (b's', b'c'): body = sign + b'0' * pad + body; sign = b'' else: body = b' ' * pad + sign + body; sign = b'' out += sign + body return bytes(out) def _vsnprintf(g): buf, n, fmt, ap = g._args(4) text = h_format(g, g.read_cstr(fmt), ap) if n: part = text[:n - 1] g.uc.mem_write(buf, part + b'\0') g._ret(len(text)) def _pthread_mutex_lock(g): (m,) = g._args(1) g.__dict__.setdefault('mutex_log', []).append(('lock', m)) g._ret(0) def _pthread_mutex_unlock(g): (m,) = g._args(1) g.__dict__.setdefault('mutex_log', []).append(('unlock', m)) g._ret(0) # ---- libstdc++ tree.cc (gcc 4.0) ----------------------------------------------------------------------------- # _Rb_tree_node_base: +0 color, +4 parent, +8 left, +0xc right. def h_col(g, x): return g.u32(x) def h_par(g, x): return g.u32(x + 4) def h_left(g, x): return g.u32(x + 8) def h_right(g, x): return g.u32(x + 12) def h_scol(g, x, v): g.w32(x, v) def h_spar(g, x, v): g.w32(x + 4, v) def h_sleft(g, x, v): g.w32(x + 8, v) def h_sright(g, x, v): g.w32(x + 12, v) def h_rotate_left(g, x, root_slot): y = h_right(g, x) h_sright(g, x, h_left(g, y)) if h_left(g, y): h_spar(g, h_left(g, y), x) h_spar(g, y, h_par(g, x)) if x == g.u32(root_slot): g.w32(root_slot, y) elif x == h_left(g, h_par(g, x)): h_sleft(g, h_par(g, x), y) else: h_sright(g, h_par(g, x), y) h_sleft(g, y, x) h_spar(g, x, y) def h_rotate_right(g, x, root_slot): y = h_left(g, x) h_sleft(g, x, h_right(g, y)) if h_right(g, y): h_spar(g, h_right(g, y), x) h_spar(g, y, h_par(g, x)) if x == g.u32(root_slot): g.w32(root_slot, y) elif x == h_right(g, h_par(g, x)): h_sright(g, h_par(g, x), y) else: h_sleft(g, h_par(g, x), y) h_sright(g, y, x) h_spar(g, x, y) def h_increment(g, x): if h_right(g, x): x = h_right(g, x) while h_left(g, x): x = h_left(g, x) else: y = h_par(g, x) while x == h_right(g, y): x = y y = h_par(g, y) if h_right(g, x) != y: x = y return x def h_decrement(g, x): if h_col(g, x) == RED and h_par(g, h_par(g, x)) == x: x = h_right(g, x) elif h_left(g, x): y = h_left(g, x) while h_right(g, y): y = h_right(g, y) x = y else: y = h_par(g, x) while x == h_left(g, y): x = y y = h_par(g, y) x = y return x def rb_increment(g): (x,) = g._args(1); g._ret(h_increment(g, x)) def rb_decrement(g): (x,) = g._args(1); g._ret(h_decrement(g, x)) def rb_insert_and_rebalance(g): insert_left, x, p, header = g._args(4) insert_left &= 0xff root_slot = header + 4 h_spar(g, x, p); h_sleft(g, x, 0); h_sright(g, x, 0); h_scol(g, x, RED) if insert_left: h_sleft(g, p, x) if p == header: g.w32(header + 4, x) g.w32(header + 12, x) elif p == g.u32(header + 8): g.w32(header + 8, x) else: h_sright(g, p, x) if p == g.u32(header + 12): g.w32(header + 12, x) while x != g.u32(root_slot) and h_col(g, h_par(g, x)) == RED: xpp = h_par(g, h_par(g, x)) if h_par(g, x) == h_left(g, xpp): y = h_right(g, xpp) if y and h_col(g, y) == RED: h_scol(g, h_par(g, x), BLACK); h_scol(g, y, BLACK); h_scol(g, xpp, RED) x = xpp else: if x == h_right(g, h_par(g, x)): x = h_par(g, x) h_rotate_left(g, x, root_slot) h_scol(g, h_par(g, x), BLACK); h_scol(g, xpp, RED) h_rotate_right(g, xpp, root_slot) else: y = h_left(g, xpp) if y and h_col(g, y) == RED: h_scol(g, h_par(g, x), BLACK); h_scol(g, y, BLACK); h_scol(g, xpp, RED) x = xpp else: if x == h_left(g, h_par(g, x)): x = h_par(g, x) h_rotate_right(g, x, root_slot) h_scol(g, h_par(g, x), BLACK); h_scol(g, xpp, RED) h_rotate_left(g, xpp, root_slot) h_scol(g, g.u32(root_slot), BLACK) g._ret(0) def rb_rebalance_for_erase(g): z, header = g._args(2) root_slot, left_slot, right_slot = header + 4, header + 8, header + 12 y = z x = 0 x_parent = 0 if h_left(g, y) == 0: x = h_right(g, y) elif h_right(g, y) == 0: x = h_left(g, y) else: y = h_right(g, y) while h_left(g, y): y = h_left(g, y) x = h_right(g, y) if y != z: h_spar(g, h_left(g, z), y) h_sleft(g, y, h_left(g, z)) if y != h_right(g, z): x_parent = h_par(g, y) if x: h_spar(g, x, h_par(g, y)) h_sleft(g, h_par(g, y), x) h_sright(g, y, h_right(g, z)) h_spar(g, h_right(g, z), y) else: x_parent = y if g.u32(root_slot) == z: g.w32(root_slot, y) elif h_left(g, h_par(g, z)) == z: h_sleft(g, h_par(g, z), y) else: h_sright(g, h_par(g, z), y) h_spar(g, y, h_par(g, z)) cy, cz = h_col(g, y), h_col(g, z) h_scol(g, y, cz); h_scol(g, z, cy) y = z else: x_parent = h_par(g, y) if x: h_spar(g, x, h_par(g, y)) if g.u32(root_slot) == z: g.w32(root_slot, x) elif h_left(g, h_par(g, z)) == z: h_sleft(g, h_par(g, z), x) else: h_sright(g, h_par(g, z), x) if g.u32(left_slot) == z: if h_right(g, z) == 0: g.w32(left_slot, h_par(g, z)) else: m = x while h_left(g, m): m = h_left(g, m) g.w32(left_slot, m) if g.u32(right_slot) == z: if h_left(g, z) == 0: g.w32(right_slot, h_par(g, z)) else: m = x while h_right(g, m): m = h_right(g, m) g.w32(right_slot, m) if h_col(g, y) != RED: while x != g.u32(root_slot) and (x == 0 or h_col(g, x) == BLACK): if x == h_left(g, x_parent): w = h_right(g, x_parent) if h_col(g, w) == RED: h_scol(g, w, BLACK); h_scol(g, x_parent, RED) h_rotate_left(g, x_parent, root_slot) w = h_right(g, x_parent) if (h_left(g, w) == 0 or h_col(g, h_left(g, w)) == BLACK) and \ (h_right(g, w) == 0 or h_col(g, h_right(g, w)) == BLACK): h_scol(g, w, RED) x = x_parent x_parent = h_par(g, x_parent) else: if h_right(g, w) == 0 or h_col(g, h_right(g, w)) == BLACK: h_scol(g, h_left(g, w), BLACK); h_scol(g, w, RED) h_rotate_right(g, w, root_slot) w = h_right(g, x_parent) h_scol(g, w, h_col(g, x_parent)); h_scol(g, x_parent, BLACK) if h_right(g, w): h_scol(g, h_right(g, w), BLACK) h_rotate_left(g, x_parent, root_slot) break else: w = h_left(g, x_parent) if h_col(g, w) == RED: h_scol(g, w, BLACK); h_scol(g, x_parent, RED) h_rotate_right(g, x_parent, root_slot) w = h_left(g, x_parent) if (h_right(g, w) == 0 or h_col(g, h_right(g, w)) == BLACK) and \ (h_left(g, w) == 0 or h_col(g, h_left(g, w)) == BLACK): h_scol(g, w, RED) x = x_parent x_parent = h_par(g, x_parent) else: if h_left(g, w) == 0 or h_col(g, h_left(g, w)) == BLACK: h_scol(g, h_right(g, w), BLACK); h_scol(g, w, RED) h_rotate_left(g, w, root_slot) w = h_left(g, x_parent) h_scol(g, w, h_col(g, x_parent)); h_scol(g, x_parent, BLACK) if h_left(g, w): h_scol(g, h_left(g, w), BLACK) h_rotate_right(g, x_parent, root_slot) break if x: h_scol(g, x, BLACK) g._ret(y) def h_forward_to_binary(name): """A weak symbol the executable imports from itself: jump to its own definition (dyld coalescing).""" def stub(g): g.uc.reg_write(UC_X86_REG_EIP, g.symbols[name]) return stub h_MAP = 'I3strSt4pairIKS0_S0_ESt10_Select1stIS3_ESt4lessIS0_ESaIS3_EE' CXX_STUBS = { '__ZSt18_Rb_tree_incrementPSt18_Rb_tree_node_base': rb_increment, '__ZSt18_Rb_tree_incrementPKSt18_Rb_tree_node_base': rb_increment, '__ZSt18_Rb_tree_decrementPSt18_Rb_tree_node_base': rb_decrement, '__ZSt29_Rb_tree_insert_and_rebalancebPSt18_Rb_tree_node_baseS0_RS_': rb_insert_and_rebalance, '__ZSt28_Rb_tree_rebalance_for_erasePSt18_Rb_tree_node_baseRS_': rb_rebalance_for_erase, } for h_n in ['__ZNSt8_Rb_tree' + h_MAP + '5eraseERS2_', '__ZNSt8_Rb_tree' + h_MAP + '5eraseESt17_Rb_tree_iteratorIS3_ESB_', '__ZNSt8_Rb_tree' + h_MAP + '11upper_boundERS2_', '__ZNSt8_Rb_tree' + h_MAP + '4findERS2_']: CXX_STUBS[h_n] = h_forward_to_binary(h_n) def h_register(): import sys f = sys._getframe(1) while f is not None: if 'EXTRA_STUBS' in f.f_globals and '_load_extra_stubs' in f.f_globals: f.f_globals['EXTRA_STUBS'].update(CXX_STUBS) return f = f.f_back h_register()