def atypes_process_bptr_test(): mem = MockMemory() alloc = MemoryAlloc(mem) # alloc proc proc = Process.alloc(alloc) # set list proc.seg_list = 0x100 assert proc.seg_list == BAddr(0x40) # check in mem seg list baddr struct = proc.get_type_struct() off = struct.pr_SegList_field.offset addr = proc.addr + off assert mem.r32(addr) == 0x40 # setup CLI cli = CLI.alloc(alloc) proc.cli = cli assert type(proc.cli) is CLI assert proc.cli == cli.addr # check in mem CLI baddr off = struct.pr_CLI_field.offset addr = proc.addr + off assert mem.r32(addr) == BAddr.from_addr(cli.addr).get_baddr() cli.free() # done proc.free() assert alloc.is_all_free()
def atypes_node_remove_test(): mem = MockMemory() text = 'hello, world!' mem.w_cstr(12, text) node = Node(mem, 0x80) node.setup(0x60, 0x100, NodeType.NT_DEVICE, -5, 12) node.remove()
def libtypes_process_bptr_test(): mem = MockMemory() alloc = MemoryAlloc(mem) # alloc proc proc = Process.alloc(alloc) # set list (as baddr) proc.seg_list.bptr = 0x40 assert proc.seg_list.bptr == 0x40 # check in mem seg list baddr off = proc.sdef.pr_SegList.offset addr = proc.addr + off assert mem.r32(addr) == 0x40 # setup CLI cli = CLI.alloc(alloc) proc.cli.ref = cli assert type(proc.cli.ref) is CLI assert proc.cli.aptr == cli.addr # check in mem CLI baddr off = proc.sdef.pr_CLI.offset addr = proc.addr + off assert mem.r32(addr) == cli.addr >> 2 cli.free() # done proc.free() assert alloc.is_all_free()
def atypes_node_str_test(): mem = MockMemory() text = 'hello, world!' mem.w_cstr(12, text) node = Node(mem, 0x42) node.setup(0x1234, 0x5678, NodeType.NT_DEVICE, -5, 12) assert str(node) == \ "[Node:@000042,p=005678,s=001234,NT_DEVICE,-5,'hello, world!']"
def mem_access_bptr_test(): mem = MockMemory() a = AccessStruct(mem, MyBCPLStruct, 0x42) # write/read addr a.w_s("bs_TestBptr", 44) assert a.r_s("bs_TestBptr") == 44 # check auto converted baddr assert mem.r32(0x42) == 11
def init_struct_test(): mem = MockMemory() # setup init table init_tab = 0x100 mem_ptr = 0x200 ib = InitStructBuilder(mem, init_tab) ib.init_byte(0, 21) ib.init_word(12, 0xDEAD) ib.init_long(34, 0xCAFEBABE) ib.init_struct(ib.SIZE_LONG, 40, [23, 42]) ib.init_struct(ib.SIZE_WORD, 64, [11, 7]) ib.init_struct(ib.SIZE_BYTE, 80, [1, 2, 3]) ib.end() # build struct i = InitStruct(mem) i.init_struct(init_tab, mem_ptr, 32) # check struct assert mem.r8(mem_ptr + 0) == 21 assert mem.r16(mem_ptr + 12) == 0xDEAD assert mem.r32(mem_ptr + 34) == 0xCAFEBABE assert mem.r32(mem_ptr + 40) == 23 assert mem.r32(mem_ptr + 44) == 42 assert mem.r16(mem_ptr + 64) == 11 assert mem.r16(mem_ptr + 66) == 7 assert mem.r8(mem_ptr + 80) == 1 assert mem.r8(mem_ptr + 81) == 2 assert mem.r8(mem_ptr + 82) == 3
def astructs_astruct_baddr_test(): mem = MockMemory() ms = MyStruct(mem, 0x10) # write int to baddr ms.ms_SegList.set(0x40) # bptr auto converts back to baddr assert ms.ms_SegList.get() == 0x40 # baddr is stored in mem assert mem.r32(0x14) == 0x40 # write baddr ms.ms_SegList.set(0x20) assert mem.r32(0x14) == 0x20
def libtypes_node_remove_test(): mem = MockMemory() text = "hello, world!" mem.w_cstr(12, text) node = Node(mem, 0x80, succ=0x60, pred=0x100, type=NodeType.NT_DEVICE, pri=-5, name=12) node.remove()
def atypes_cstring_empty_test(): mem = MockMemory() alloc = MemoryAlloc(mem) # empty string txt = "" cs = CString.alloc(alloc, txt) assert cs assert mem.r_cstr(cs.get_addr()) == txt assert cs.get_string() == txt assert cs == txt assert cs == cs.get_addr() cs.free() assert alloc.is_all_free()
def mem_cache_bstr_write_test(): mem = MemoryCache(0x100, 0x100) data = "hello, world" mem.w_bstr(0x100, data) assert mem.r_bstr(0x100) == data empty = "" mem.w_bstr(0x180, empty) assert mem.r_bstr(0x180) == empty # to main main_mem = MockMemory() mem.write_cache(main_mem) assert main_mem.r_bstr(0x100) == data assert main_mem.r_bstr(0x180) == empty
def atypes_cstring_base_test(): mem = MockMemory() alloc = MemoryAlloc(mem) # simple string txt = "hello, world!" cs = CString.alloc(alloc, txt) assert cs assert mem.r_cstr(cs.get_addr()) == txt assert cs.get_string() == txt assert cs == txt assert cs == cs.get_addr() assert cs == CString(mem, cs.get_addr()) cs.free() assert alloc.is_all_free()
def atypes_node_setup_test(): mem = MockMemory() text = 'hello, world!' mem.w_cstr(12, text) node = Node(mem, 0x42) node.setup(1234, 5678, NodeType.NT_DEVICE, -5, 12) # check node assert node.get_succ() == 1234 assert node.get_pred() == 5678 assert int(node.get_type()) == NodeType.NT_DEVICE assert node.get_pri() == -5 assert node.get_name(True) == 12 assert node.get_name() == text node.set_type(NodeType(NodeType.NT_DEVICE))
def atypes_bstring_empty_test(): mem = MockMemory() alloc = MemoryAlloc(mem) # empty string txt = "" bs = BString.alloc(alloc, txt) assert bs assert bs.get_baddr() << 2 == bs.get_addr() assert mem.r_bstr(bs.get_addr()) == txt assert bs.get_string() == txt assert bs == txt assert bs == bs.get_addr() bs.free() assert alloc.is_all_free()
def init_struct_test(): mem = MockMemory() # setup init table init_tab = 0x100 mem_ptr = 0x200 ib = InitStructBuilder(mem, init_tab) ib.init_byte(0, 21) ib.init_word(12, 0xdead) ib.init_long(34, 0xcafebabe) ib.init_struct(ib.SIZE_LONG, 40, [23, 42]) ib.init_struct(ib.SIZE_WORD, 64, [11, 7]) ib.init_struct(ib.SIZE_BYTE, 80, [1, 2, 3]) ib.end() # build struct i = InitStruct(mem) i.init_struct(init_tab, mem_ptr, 32) # check struct assert mem.r8(mem_ptr + 0) == 21 assert mem.r16(mem_ptr + 12) == 0xdead assert mem.r32(mem_ptr + 34) == 0xcafebabe assert mem.r32(mem_ptr + 40) == 23 assert mem.r32(mem_ptr + 44) == 42 assert mem.r16(mem_ptr + 64) == 11 assert mem.r16(mem_ptr + 66) == 7 assert mem.r8(mem_ptr + 80) == 1 assert mem.r8(mem_ptr + 81) == 2 assert mem.r8(mem_ptr + 82) == 3
def libtypes_node_str_test(): mem = MockMemory() text = "hello, world!" mem.w_cstr(12, text) node = Node(mem, 0x42, succ=0x1234, pred=0x5678, type=NodeType.NT_DEVICE, pri=-5, name=12) assert str( node ) == "[Node:@000042,p=005678,s=001234,NT_DEVICE,-5,'hello, world!']"
def atypes_bstring_base_test(): mem = MockMemory() alloc = MemoryAlloc(mem) # simple string txt = "hello, world!" bs = BString.alloc(alloc, txt) assert bs assert bs.get_baddr() << 2 == bs.get_addr() assert mem.r_bstr(bs.get_addr()) == txt assert bs.get_string() == txt assert bs == txt assert bs == bs.get_addr() assert bs == BString(mem, bs.get_addr()) bs.free() assert alloc.is_all_free()
def machine_mem_bstr_test(): mem = MockMemory() data = "hello, world" mem.w_bstr(0, data) assert mem.r_bstr(0) == data empty = "" mem.w_bstr(100, empty) assert mem.r_bstr(100) == empty
def setup(): mem = MockMemory(fill=23) traps = MockTraps() cpu = MockCPU() alloc = MemoryAlloc(mem) ctx = LibCtx(cpu, mem) return mem, traps, alloc, ctx
def astruct_astruct_baddr_test(): mem = MockMemory() ms = MyStruct(mem, 0x10) # write int (addr) to baddr ms.ms_SegList = 0x100 # baddr auto converts back to addr assert ms.ms_SegList == 0x100 # but its an BAddr assert type(ms.ms_SegList) is BAddr assert ms.ms_SegList == BAddr(0x40) # baddr is stored in mem assert mem.r32(0x14) == 0x40 # write baddr ms.ms_SegList = BAddr(0x20) assert ms.ms_SegList == BAddr(0x20) assert mem.r32(0x14) == 0x20
def mem_alloc_base_test(): mem = MockMemory() alloc = MemoryAlloc(mem) assert alloc.is_all_free() addr = alloc.alloc_mem(1024) alloc.free_mem(addr, 1024) assert alloc.is_all_free()
def libstructs_exec_task_test(): mem = MockMemory() task = TaskStruct(mem, 0x40) task.tc_Node.ln_Succ.set(42) task.tc_Node.ln_Pred.set(21) assert task.tc_Node.ln_Succ.get() == 42 assert task.tc_Node.ln_Pred.get() == 21
def mem_access_rw_sub_field_task_test(): mem = MockMemory() a = AccessStruct(mem, TaskStruct, 0x42) a.w_s("tc_Node.ln_Succ", 42) a.w_s("tc_Node.ln_Pred", 21) assert a.r_s("tc_Node.ln_Succ") == 42 assert a.r_s("tc_Node.ln_Pred") == 21
def mem_access_invalid_node_test(): mem = MockMemory() a = AccessStruct(mem, NodeStruct, 0x42) with pytest.raises(KeyError): a.w_s("bla", 12) with pytest.raises(KeyError): a.r_s("blub")
def atypes_list_alloc_test(): mem = MockMemory() alloc = MemoryAlloc(mem) l = List.alloc(alloc) assert type(l) is List assert l.get_size() == ListStruct.get_size() l.free()
def astructs_astruct_base_inst_test(): # check instance mem = MockMemory() ms = MyStruct(mem, 0x10) assert str(ms) == "[AStruct:My,@000010+00000c]" # field access fields = ms.sfields.get_fields() assert len(fields) == 4 field = ms.sfields.get_field_by_index(0) assert field.get_addr() == 0x10 assert ms.get("ms_Word") == field assert ms.sfields.find_field_by_offset(0) == (field, 0) assert ms.sfields.find_field_by_addr(0x10) == (field, 0) assert ms.sfields.find_field_def_by_addr(0x10) == (ms.sdef[0], 0) assert ms.ms_Word == field # alias name field = ms.ms_StackSize assert ms.stack_size == field # access ms.get("ms_Word").set(-3000) assert ms.get("ms_Word").get() == -3000 # access via __getattr__ ms.ms_Word.set(2000) assert ms.ms_Word.get() == 2000 # find field field = ms.ms_Word assert ms.sfields.find_sub_field_by_def(MyStruct.sdef.ms_Word) == field # try to assign field directly -> forbidden! with pytest.raises(AttributeError): ms.ms_Word = 42
def libtypes_node_alloc_min_test(): mem = MockMemory() alloc = MemoryAlloc(mem) node = MinNode.alloc(alloc) assert node.get_size() == MinNodeStruct.get_size() node.free() assert alloc.is_all_free()
def astructs_astruct_class_test(): mem = MockMemory() alloc = MemoryAlloc(mem) pc = PlainClass.alloc(alloc) assert type(pc) == PlainClass assert pc.foo() == "" # alloc a name pc.name.alloc_str(alloc, "hello") assert pc.foo() == "hello" # another struct pc2 = PlainClass.alloc(alloc, name="world!") assert type(pc2) == PlainClass assert pc2.name.str == "world!" assert pc2.foo() == "world!" pc.next.ref = pc2 assert pc.foo() == "hello world!" # more struct pc3 = PlainClass.alloc(alloc, name="what:", bname="ugh!", next=pc2) assert pc3.foo() == "what: world!" assert pc3.next.ref is pc2 assert pc3.name.str == "what:" assert pc3.bname.str == "ugh!" pc3.free() pc2.free() # clean up pc pc.name.free_str() pc.free() assert alloc.is_all_free()
def astructs_astruct_plain_setup_test(): mem = MockMemory() pc = PlainStruct(mem, 0x10, next=0x100, prev=0x200, name=0x120, bname=0x140) assert pc.next.aptr == 0x100 assert pc.prev.aptr == 0x200 assert pc.name.aptr == 0x120 assert pc.bname.aptr == 0x140
def atypes_resident_setup_test(): mem = MockMemory(fill=23) alloc = MemoryAlloc(mem) # alloc res2 = Resident.alloc(alloc, "bla.library", "blub") res2.setup( flags=ResidentFlags.RTF_AUTOINIT, version=42, type=NodeType.NT_DEVICE, pri=-7, init=0xDEADBEEF, ) # find resource res = Resident.find(mem, 0, 1024) assert res.get_match_word() == res.RTC_MATCHWORD assert res.get_match_tag() == res.get_addr() assert res.get_end_skip() >= res.get_addr() + res.get_type_size() assert res.get_flags() == ResidentFlags.RTF_AUTOINIT assert res.get_version() == 42 assert res.get_type() == NodeType.NT_DEVICE assert res.get_pri() == -7 assert res.get_name() == "bla.library" assert res.get_id_string() == "blub" assert res.get_init() == 0xDEADBEEF # free res2.free() assert alloc.is_all_free()
def atypes_node_setup_min_test(): mem = MockMemory() node = Node(mem, 0x42) node.setup_min(1234, 5678) # check node assert node.get_succ() == 1234 assert node.get_pred() == 5678
def libtypes_resident_setup_test(): mem = MockMemory(fill=23) alloc = MemoryAlloc(mem) # alloc res2 = Resident.alloc(alloc, name="bla.library", id_string="blub") res2.new_resident( flags=ResidentFlags.RTF_AUTOINIT, version=42, type=NodeType.NT_DEVICE, pri=-7, init=0xDEADBEEF, ) # find resource res = Resident.find(mem, 0, 1024) assert res.match_word.val == res.RTC_MATCHWORD assert res.match_tag.aptr == res.get_addr() assert res.end_skip.aptr >= res.get_addr() + res.get_size() assert res.flags.val == ResidentFlags.RTF_AUTOINIT assert res.version.val == 42 assert res.type.val == NodeType.NT_DEVICE assert res.pri.val == -7 assert res.name.str == "bla.library" assert res.id_string.str == "blub" assert res.init.aptr == 0xDEADBEEF # free res2.free() assert alloc.is_all_free()
def mem_cache_block_write_test(): mem = MemoryCache(0x100, 0x220) data = "hello, world!" mem.w_block(0x100, data) assert mem.r_block(0x100, len(data)) == data bdata = bytearray(data) mem.w_block(0x180, bdata) assert mem.r_block(0x180, len(bdata)) == bdata mem.clear_block(0x200, 100, 42) assert mem.r_block(0x200, 100) == chr(42) * 100 mem.copy_block(0x200, 0x300, 20) assert mem.r_block(0x300, 21) == chr(42) * 20 + chr(0) # write to main mem main_mem = MockMemory() mem.write_cache(main_mem) assert main_mem.r_block(0x100, len(data)) == data assert main_mem.r_block(0x180, len(bdata)) == bdata assert main_mem.r_block(0x200, 100) == chr(42) * 100 assert main_mem.r_block(0x300, 21) == chr(42) * 20 + chr(0)
def atypes_node_base_test(): mem = MockMemory() text = 'hello, world!' mem.w_cstr(12, text) node = Node(mem, 0x42) # set node node.set_succ(1234) node.set_pred(5678) node.set_type(NodeType.NT_LIBRARY) node.set_pri(-3) node.set_name(12) # check node assert node.get_succ() == 1234 assert node.get_pred() == 5678 assert int(node.get_type()) == NodeType.NT_LIBRARY assert node.get_type() == NodeType(NodeType.NT_LIBRARY) assert node.get_pri() == -3 assert node.get_name(True) == 12 assert node.get_name() == text
def mem_cache_bstr_read_test(): mem = MockMemory() mem = MemoryCache(0x100, 0x100) data = "hello, world" mem.w_bstr(0x100, data) assert mem.r_bstr(0x100) == data empty = "" mem.w_bstr(0x180, empty) assert mem.r_bstr(0x180) == empty # to cache cmem = MemoryCache(0x100, 0x100) cmem.read_cache(mem) assert cmem.r_bstr(0x100) == data assert cmem.r_bstr(0x180) == empty
def mem_cache_rwx_write_test(): mem = MemoryCache(0x100, 0x210) # build cache contents mem.w8(0x100, 42) assert mem.r8(0x100) == 42 mem.w16(0x200, 0xdead) assert mem.r16(0x200) == 0xdead mem.w32(0x300, 0xcafebabe) assert mem.r32(0x300) == 0xcafebabe mem.write(0, 0x101, 43) assert mem.read(0, 0x101) == 43 mem.write(1, 0x202, 0x1234) assert mem.read(1, 0x202) == 0x1234 mem.write(2, 0x304, 0x11223344) assert mem.read(2, 0x304) == 0x11223344 # write to main mem main_mem = MockMemory() mem.write_cache(main_mem) # check main mem assert main_mem.r8(0x100) == 42 assert main_mem.r16(0x200) == 0xdead assert main_mem.r32(0x300) == 0xcafebabe assert main_mem.read(0, 0x101) == 43 assert main_mem.read(1, 0x202) == 0x1234 assert main_mem.read(2, 0x304) == 0x11223344
def mem_cache_rwxs_write_test(): mem = MemoryCache(0x100, 0x210) # build cache contents mem.w8s(0x100, -42) assert mem.r8s(0x100) == -42 mem.w16s(0x200, -0x1ead) assert mem.r16s(0x200) == -0x1ead mem.w32s(0x300, -0x2afebabe) assert mem.r32s(0x300) == -0x2afebabe mem.writes(0, 0x101, -43) assert mem.reads(0, 0x101) == -43 mem.writes(1, 0x202, -0x1234) assert mem.reads(1, 0x202) == -0x1234 mem.writes(2, 0x304, -0x11223344) assert mem.reads(2, 0x304) == -0x11223344 # write to main mem main_mem = MockMemory() mem.write_cache(main_mem) # check main mem assert main_mem.r8s(0x100) == -42 assert main_mem.r16s(0x200) == -0x1ead assert main_mem.r32s(0x300) == -0x2afebabe assert main_mem.reads(0, 0x101) == -43 assert main_mem.reads(1, 0x202) == -0x1234 assert main_mem.reads(2, 0x304) == -0x11223344
def make_functions_ptr_test(): mem = MockMemory() # build an offset array ptr = 0 fptrs = [0x100, 0x202, 0x404, 0x808, 0x10000] for fptr in fptrs: mem.w32(ptr, fptr) ptr += 4 mem.w32s(ptr, -1) # build func table lib_base = 0x800 mf = MakeFuncs(mem) size = mf.make_functions(lib_base, 0) assert size == len(fptrs) * 6 # check jump table ptr = lib_base - 6 for fptr in fptrs: assert mem.r16(ptr) == op_jmp assert mem.r32(ptr+2) == fptr ptr -= 6
def make_functions_offset_test(): mem = MockMemory() # build an offset array ptr = 0 offsets = [-10, 2, 16, 32, 68, 100] for off in offsets: mem.w16s(ptr, off) ptr += 2 mem.w16s(ptr, -1) # build func table disp_base = 0x1000 lib_base = 0x800 mf = MakeFuncs(mem) size = mf.make_functions(lib_base, 0, disp_base) assert size == len(offsets) * 6 # check jump table ptr = lib_base - 6 for off in offsets: addr = disp_base + off assert mem.r16(ptr) == op_jmp assert mem.r32(ptr+2) == addr ptr -= 6
def machine_mem_rws_test(): mem = MockMemory() mem.w8s(0x100, 42) assert mem.r8s(0x100) == 42 mem.w8s(0x100, -23) assert mem.r8s(0x100) == -23 mem.w16s(0x200, 0x7ead) assert mem.r16s(0x200) == 0x7ead mem.w16s(0x200, -0x1000) assert mem.r16s(0x200) == -0x1000 mem.w32s(0x300, 0x1afebabe) assert mem.r32s(0x300) == 0x1afebabe mem.w32s(0x300, -0xafebabe) assert mem.r32s(0x300) == -0xafebabe mem.writes(0, 0x101, -43) assert mem.reads(0, 0x101) == -43 mem.writes(1, 0x202, -0x1234) assert mem.reads(1, 0x202) == -0x1234 mem.writes(2, 0x304, -0x11223344) assert mem.reads(2, 0x304) == -0x11223344 # invalid values with pytest.raises(struct.error): mem.w8s(0x100, 0x80) with pytest.raises(struct.error): mem.w8s(0x100, -0x81) # invalid values with pytest.raises(struct.error): mem.w16s(0x100, 0x8000) with pytest.raises(struct.error): mem.w16s(0x100, -0x8001) # invalid values with pytest.raises(struct.error): mem.w32s(0x100, 0x80000000) with pytest.raises(struct.error): mem.w32s(0x100, -0x80000001) # invalid type with pytest.raises(struct.error): mem.w8s(0x100, 'hello') # invalid type with pytest.raises(struct.error): mem.w16s(0x100, 'hello') # invalid type with pytest.raises(struct.error): mem.w32s(0x100, 'hello') # invalid width with pytest.raises(ValueError): mem.writes(7, 0x202, 12) with pytest.raises(ValueError): mem.reads(7, 0x202)
def machine_mem_block_test(): mem = MockMemory() data = "hello, world!" mem.w_block(0, data) assert mem.r_block(0, len(data)) == data bdata = bytearray(data) mem.w_block(0x100, bdata) assert mem.r_block(0x100, len(bdata)) == bdata mem.clear_block(0x200, 100, 42) assert mem.r_block(0x200, 100) == chr(42) * 100 mem.copy_block(0x200, 0x300, 20) assert mem.r_block(0x300, 21) == chr(42) * 20 + chr(0)
def machine_mem_rw_test(): mem = MockMemory() mem.w8(0x100, 42) assert mem.r8(0x100) == 42 mem.w16(0x200, 0xdead) assert mem.r16(0x200) == 0xdead mem.w32(0x300, 0xcafebabe) assert mem.r32(0x300) == 0xcafebabe mem.write(0, 0x101, 43) assert mem.read(0, 0x101) == 43 mem.write(1, 0x202, 0x1234) assert mem.read(1, 0x202) == 0x1234 mem.write(2, 0x304, 0x11223344) assert mem.read(2, 0x304) == 0x11223344 # invalid values with pytest.raises(ValueError): mem.w8(0x100, 0x100) with pytest.raises(ValueError): mem.w8(0x100, -1) # invalid values with pytest.raises(struct.error): mem.w16(0x100, 0x10000) with pytest.raises(struct.error): mem.w16(0x100, -2) # invalid values with pytest.raises(struct.error): mem.w32(0x100, 0x100000000) with pytest.raises(struct.error): mem.w32(0x100, -3) # invalid type with pytest.raises(ValueError): mem.w8(0x100, 'hello') # invalid type with pytest.raises(struct.error): mem.w16(0x100, 'hello') # invalid type with pytest.raises(struct.error): mem.w32(0x100, 'hello') # invalid width with pytest.raises(ValueError): mem.write(7, 0x202, 12) with pytest.raises(ValueError): mem.read(7, 0x202)