def libnative_libfuncs_close_library_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) segloader = SegmentLoader(alloc) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() # setup seglist seglist = SegList.alloc(alloc, [64]) segloader.register_seglist(seglist.get_baddr()) # setup expunge func def close_func(op, pc): # return my seglist cpu.w_reg(REG_D0, seglist.get_baddr()) trap_id = traps.setup(close_func, auto_rts=True) exp_addr = lib.get_addr() - 12 mem.w16(exp_addr, trap_id | 0xA000) # add lib lf = LibFuncs(machine, alloc) sl = lf.close_library(lib.get_addr(), segloader, run_sp=sp) assert seglist.get_baddr() == sl # cleanup lib.free() assert alloc.is_all_free() assert segloader.shutdown() == 0
def libnative_libfuncs_set_function_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib_addr = lib.get_addr() lib.setup() lib.fill_funcs(op_jmp, 0xCAFEBABE) assert lib.neg_size == 36 # patch function lvo = -30 addr = lib.get_addr() + lvo assert mem.r16(addr) == op_jmp assert mem.r32(addr + 2) == 0xCAFEBABE lf = LibFuncs(machine, alloc) old_addr = lf.set_function(lib_addr, lvo, 0xDEADBEEF) assert old_addr == 0xCAFEBABE assert mem.r16(addr) == op_jmp assert mem.r32(addr + 2) == 0xDEADBEEF assert lib.check_sum() # invalid function old_addr = lf.set_function(lib_addr, -36, 0) assert old_addr is None # cleanup lib.free() assert alloc.is_all_free()
def libnative_libfuncs_open_library_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() # setup open func def open_func(op, pc): # return my seglist cpu.w_reg(REG_D0, 0xcafebabe) trap_id = traps.setup(open_func, auto_rts=True) exp_addr = lib.get_addr() - 6 mem.w16(exp_addr, trap_id | 0xa000) # add lib lf = LibFuncs(machine, alloc) lib_base = lf.open_library(lib.get_addr(), run_sp=sp) assert lib_base == 0xcafebabe # cleanup lib.free() assert alloc.is_all_free()
def libnative_libfuncs_open_library_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() # setup open func def open_func(op, pc): # return my seglist cpu.w_reg(REG_D0, 0xCAFEBABE) trap_id = traps.setup(open_func, auto_rts=True) exp_addr = lib.get_addr() - 6 mem.w16(exp_addr, trap_id | 0xA000) # add lib lf = LibFuncs(machine, alloc) lib_base = lf.open_library(lib.get_addr(), run_sp=sp) assert lib_base == 0xCAFEBABE # cleanup lib.free() assert alloc.is_all_free()
def libnative_libfuncs_close_library_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() traps = machine.get_traps() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) segloader = SegmentLoader(alloc) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() # setup seglist seglist = SegList.alloc(alloc, [64]) segloader.register_seglist(seglist.get_baddr()) # setup expunge func def close_func(op, pc): # return my seglist cpu.w_reg(REG_D0, seglist.get_baddr()) trap_id = traps.setup(close_func, auto_rts=True) exp_addr = lib.get_addr() - 12 mem.w16(exp_addr, trap_id | 0xa000) # add lib lf = LibFuncs(machine, alloc) sl = lf.close_library(lib.get_addr(), segloader, run_sp=sp) assert seglist.get_baddr() == sl # cleanup lib.free() assert alloc.is_all_free() assert segloader.shutdown() == 0
def libnative_libfuncs_add_library_test(): machine = Machine() mem = machine.get_mem() alloc = MemoryAlloc.for_machine(machine) # setup exec lib exec_lib = ExecLibrary.alloc(alloc, "exec.library", "bla", 36) exec_lib.setup() mem.w32(4, exec_lib.get_addr()) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() mem.w32(lib.get_addr()-36, 0xdeadbeef) # check lib sum assert lib.sum == 0 # add lib lf = LibFuncs(machine, alloc) lf.add_library(lib.get_addr()) # check that lib was added assert len(exec_lib.lib_list) == 1 assert [a for a in exec_lib.lib_list] == [lib] assert lib.sum == 0xdeadbeef assert lf.find_library("my.library") == lib.get_addr() # cleanup lib.free() exec_lib.free() assert alloc.is_all_free()
def libnative_libfuncs_add_library_test(): machine = Machine() mem = machine.get_mem() alloc = MemoryAlloc.for_machine(machine) # setup exec lib exec_lib = ExecLibrary.alloc(alloc, "exec.library", "bla", 36) exec_lib.setup() mem.w32(4, exec_lib.get_addr()) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() mem.w32(lib.get_addr() - 36, 0xDEADBEEF) # check lib sum assert lib.sum == 0 # add lib lf = LibFuncs(machine, alloc) lf.add_library(lib.get_addr()) # check that lib was added assert len(exec_lib.lib_list) == 1 assert [a for a in exec_lib.lib_list] == [lib] assert lib.sum == 0xDEADBEEF assert lf.find_library("my.library") == lib.get_addr() # cleanup lib.free() exec_lib.free() assert alloc.is_all_free()
def libnative_libfuncs_set_function_test(): machine = Machine() mem = machine.get_mem() cpu = machine.get_cpu() sp = machine.get_ram_begin() - 4 alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib_addr = lib.get_addr() lib.setup() lib.fill_funcs(op_jmp, 0xcafebabe) assert lib.neg_size == 36 # patch function lvo = -30 addr = lib.get_addr() + lvo assert mem.r16(addr) == op_jmp assert mem.r32(addr+2) == 0xcafebabe lf = LibFuncs(machine, alloc) old_addr = lf.set_function(lib_addr, lvo, 0xdeadbeef) assert old_addr == 0xcafebabe assert mem.r16(addr) == op_jmp assert mem.r32(addr+2) == 0xdeadbeef assert lib.check_sum() # invalid function old_addr = lf.set_function(lib_addr, -36, 0) assert old_addr is None # cleanup lib.free() assert alloc.is_all_free()
def _create_library(self, info): name = info.get_name() id_str = info.get_id_string() neg_size = info.get_neg_size() pos_size = info.get_pos_size() library = Library.alloc(self.alloc, name, id_str, neg_size, pos_size) version = info.get_version() revision = info.get_revision() library.setup(version=version, revision=revision) return library
def _create_library(self, info, is_dev, fd): if is_dev: ltype = NodeType.NT_DEVICE else: ltype = NodeType.NT_LIBRARY name = info.get_name() id_str = info.get_id_string() neg_size = info.get_neg_size() pos_size = info.get_pos_size() library = Library.alloc(self.alloc, name, id_str, neg_size, pos_size, fd) version = info.get_version() revision = info.get_revision() library.setup(version=version, revision=revision, type=ltype) return library
def _create_library(self, info, is_dev): if is_dev: ltype = NodeType.NT_DEVICE else: ltype = NodeType.NT_LIBRARY name = info.get_name() id_str = info.get_id_string() neg_size = info.get_neg_size() pos_size = info.get_pos_size() library = Library.alloc(self.alloc, name, id_str, neg_size, pos_size) version = info.get_version() revision = info.get_revision() library.setup(version=version, revision=revision, type=ltype) return library
def libnative_libfuncs_sum_library_test(): machine = Machine() mem = machine.get_mem() alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() mem.w32(lib.get_addr() - 36, 0xdeadbeef) # sum lib lf = LibFuncs(machine, alloc) lf.sum_library(lib.get_addr()) assert lib.sum == 0xdeadbeef # cleanup lib.free() assert alloc.is_all_free()
def libnative_libfuncs_sum_library_test(): machine = Machine() mem = machine.get_mem() alloc = MemoryAlloc.for_machine(machine) # new lib lib = Library.alloc(alloc, "my.library", "bla", 36) lib.setup() mem.w32(lib.get_addr() - 36, 0xDEADBEEF) # sum lib lf = LibFuncs(machine, alloc) lf.sum_library(lib.get_addr()) assert lib.sum == 0xDEADBEEF # cleanup lib.free() assert alloc.is_all_free()
def atypes_library_label_test(mem_alloc): mem, alloc = mem_alloc name = "vamostest.library" id_str = "vamostest.library 0.1" fd = read_lib_fd("vamostest.library") neg_size = fd.get_neg_size() lib = Library.alloc(alloc, name, id_str, neg_size, fd=fd) # check for label if alloc.get_label_mgr(): assert lib._label assert isinstance(lib._label, LabelLib) assert lib._label.fd == fd else: assert not lib._label # done lib.free() assert alloc.is_all_free()
def atypes_libary_open_cnt_test(mem_alloc): mem, alloc = mem_alloc # alloc lib name = "my.library" id_str = "my.library 1.2" neg_size = 30 pos_size = LibraryStruct.get_size() lib = Library.alloc(alloc, name, id_str, neg_size) # test open cnt assert lib.get_open_cnt() == 0 lib.inc_open_cnt() assert lib.get_open_cnt() == 1 lib.dec_open_cnt() assert lib.get_open_cnt() == 0 # done lib.free() assert alloc.is_all_free()
def atypes_library_base_test(): mem = MockMemory() alloc = MemoryAlloc(mem) # alloc lib name = "my.library" id_str = "my.library 1.2" neg_size = 36 pos_size = LibraryStruct.get_size() lib = Library.alloc(alloc, name, id_str, neg_size) assert lib.get_name() == name assert lib.get_id_string() == id_str size = lib.get_size() assert pos_size == lib.get_pos_size() assert neg_size == lib.get_neg_size() assert size == pos_size + neg_size # lib setup flags = LibFlags(LibFlags.LIBF_SUMMING, LibFlags.LIBF_CHANGED) ltype = NodeType(NodeType.NT_DEVICE) pri = -3 ver = 1 rev = 2 lib.setup(version=ver, revision=rev, pri=pri, flags=flags, type=ltype) # check lib node = lib.get_node() assert node.get_succ() is None assert node.get_pred() is None assert node.get_type() == ltype assert node.get_pri() == pri assert lib.get_flags() == flags assert lib.get_pad() == 0 assert lib.get_neg_size() == neg_size assert lib.get_pos_size() == pos_size assert lib.get_version() == ver assert lib.get_revision() == rev assert lib.get_sum() == 0 assert lib.get_open_cnt() == 0 assert lib.get_name() == name assert lib.get_id_string() == id_str # fill funcs lib.fill_funcs() lib_base = lib.get_addr() assert mem.r16(lib_base - 6) == op_rts # done lib.free() assert alloc.is_all_free()
def atypes_library_base_test(mem_alloc): mem, alloc = mem_alloc # alloc lib name = "my.library" id_str = "my.library 1.2" neg_size = 36 pos_size = LibraryStruct.get_size() lib = Library.alloc(alloc, name, id_str, neg_size) assert lib.get_name() == name assert lib.get_id_string() == id_str size = lib.get_size() assert pos_size == lib.get_pos_size() assert neg_size == lib.get_neg_size() assert size == pos_size + neg_size # lib setup flags = LibFlags(LibFlags.LIBF_SUMMING, LibFlags.LIBF_CHANGED) ltype = NodeType(NodeType.NT_DEVICE) pri = -3 ver = 1 rev = 2 lib.setup(version=ver, revision=rev, pri=pri, flags=flags, type=ltype) # check lib node = lib.get_node() assert node.get_succ() is None assert node.get_pred() is None assert node.get_type() == ltype assert node.get_pri() == pri assert lib.get_flags() == flags assert lib.get_pad() == 0 assert lib.get_neg_size() == neg_size assert lib.get_pos_size() == pos_size assert lib.get_version() == ver assert lib.get_revision() == rev assert lib.get_sum() == 0 assert lib.get_open_cnt() == 0 assert lib.get_name() == name assert lib.get_id_string() == id_str # fill funcs lib.fill_funcs() lib_base = lib.get_addr() assert mem.r16(lib_base - 6) == op_rts # done lib.free() assert alloc.is_all_free()
def atypes_library_sum_test(mem_alloc): mem, alloc = mem_alloc # alloc lib name = "my.library" id_str = "my.library 1.2" neg_size = 30 pos_size = LibraryStruct.get_size() lib = Library.alloc(alloc, name, id_str, neg_size) # assume rounded neg size assert lib.get_neg_size() == 32 mem.w32(lib.addr-32, 0xdeadbeef) mem.w32(lib.addr-28, 0xcafebabe) my_sum = (0xdeadbeef + 0xcafebabe) & 0xffffffff lib_sum = lib.calc_sum() assert lib_sum == my_sum lib.update_sum() assert lib.get_sum() == my_sum assert lib.check_sum() # done lib.free() assert alloc.is_all_free()
def atypes_library_sum_test(mem_alloc): mem, alloc = mem_alloc # alloc lib name = "my.library" id_str = "my.library 1.2" neg_size = 30 pos_size = LibraryStruct.get_size() lib = Library.alloc(alloc, name, id_str, neg_size) # assume rounded neg size assert lib.get_neg_size() == 32 mem.w32(lib.addr - 32, 0xdeadbeef) mem.w32(lib.addr - 28, 0xcafebabe) my_sum = (0xdeadbeef + 0xcafebabe) & 0xffffffff lib_sum = lib.calc_sum() assert lib_sum == my_sum lib.update_sum() assert lib.get_sum() == my_sum assert lib.check_sum() # done lib.free() assert alloc.is_all_free()