Пример #1
0
def libnative_loader_load_ami_lib_test(buildlibnix):
    # path mgr mock
    lib_file = buildlibnix.make_lib('testnix')

    class PathMgrMock:
        def ami_to_sys_path(self, lock, ami_path, mustExist=True):
            if ami_path == 'LIBS:testnix.library':
                return lib_file

    pm = PathMgrMock()
    loader, segload, alloc, mem, sp, exec_lib = setup(pm)
    # load
    lib_base, seglist_baddr = loader.load_ami_lib("testnix.library", run_sp=sp)
    assert lib_base > 0
    assert seglist_baddr > 0
    info = segload.get_info(seglist_baddr)
    assert info
    assert info.ami_file == 'LIBS:testnix.library'
    assert info.sys_file == lib_file
    # we have to manually clean the lib here (as Exec FreeMem() does not work)
    lib = Library(mem, lib_base, alloc)
    lib.free()
    # cleanup
    segload.unload_seglist(seglist_baddr)
    assert segload.shutdown() == 0
    exec_lib.free()
    assert alloc.is_all_free()
Пример #2
0
def libnative_loader_load_ami_lib_test(buildlibnix):
  # path mgr mock
  lib_file = buildlibnix.make_lib('testnix')

  class PathMgrMock:
    def ami_to_sys_path(self, lock, ami_path, mustExist=True):
      if ami_path == 'LIBS:testnix.library':
        return lib_file
  pm = PathMgrMock()
  loader, segload, alloc, mem, sp, exec_lib = setup(pm)
  # load
  lib_base, seglist_baddr = loader.load_ami_lib("testnix.library", run_sp=sp)
  assert lib_base > 0
  assert seglist_baddr > 0
  info = segload.get_info(seglist_baddr)
  assert info
  assert info.ami_file == 'LIBS:testnix.library'
  assert info.sys_file == lib_file
  # we have to manually clean the lib here (as Exec FreeMem() does not work)
  lib = Library(mem, lib_base, alloc)
  lib.free()
  # cleanup
  segload.unload_seglist(seglist_baddr)
  assert segload.shutdown() == 0
  exec_lib.free()
  assert alloc.is_all_free()
Пример #3
0
def libnative_loader_load_sys_lib_test(buildlibnix):
  loader, segload, alloc, mem, sp, exec_lib = setup()
  # load
  lib_file = buildlibnix.make_lib('testnix')
  lib_base, seglist_baddr = loader.load_sys_lib(lib_file, run_sp=sp)
  assert lib_base > 0
  assert seglist_baddr > 0
  # we have to manually clean the lib here (as Exec FreeMem() does not work)
  lib = Library(mem, lib_base, alloc)
  lib.free()
  # cleanup
  segload.unload_seglist(seglist_baddr)
  assert segload.shutdown() == 0
  exec_lib.free()
  assert alloc.is_all_free()
Пример #4
0
def libnative_loader_load_sys_lib_test(buildlibnix):
    loader, segload, alloc, mem, sp, exec_lib = setup()
    # load
    lib_file = buildlibnix.make_lib('testnix')
    lib_base, seglist_baddr = loader.load_sys_lib(lib_file, run_sp=sp)
    assert lib_base > 0
    assert seglist_baddr > 0
    # we have to manually clean the lib here (as Exec FreeMem() does not work)
    lib = Library(mem, lib_base, alloc)
    lib.free()
    # cleanup
    segload.unload_seglist(seglist_baddr)
    assert segload.shutdown() == 0
    exec_lib.free()
    assert alloc.is_all_free()
Пример #5
0
def libnative_makelib_test():
    machine = Machine()
    mem = machine.get_mem()
    sp = machine.get_ram_begin() - 4
    alloc = MemoryAlloc.for_machine(machine)

    # build lib info in memory
    vectors = 0x100
    mem.w32(vectors, 0x400)
    mem.w32(vectors + 4, 0x600)
    mem.w32(vectors + 8, 0x800)
    mem.w32(vectors + 12, 0xFFFFFFFF)
    name_addr = 0x1F0
    mem.w_cstr(name_addr, "bla.library")

    init_tab = 0x200
    ib = InitStructBuilder(mem, init_tab)
    name_off = LibraryStruct.get_field_offset_for_path("lib_Node.ln_Name")
    ib.init_long(name_off, name_addr)
    ib.end()

    init_func = 0x300
    mem.w16(init_func, op_rts)

    pos_size = LibraryStruct.get_size()

    # make library
    ml = MakeLib(machine, alloc)
    lib_base, mem_obj = ml.make_library(vectors,
                                        init_tab,
                                        init_func,
                                        pos_size,
                                        0,
                                        run_sp=sp)
    assert lib_base != 0

    # check library
    lib = Library(mem, lib_base)
    assert lib.get_name() == "bla.library"
    assert lib.get_pos_size() == 36
    assert lib.get_neg_size() == 20  # round_long(3*6)
    assert mem.r32(lib_base - 4) == 0x400
    assert mem.r32(lib_base - 10) == 0x600
    assert mem.r32(lib_base - 16) == 0x800

    # cleanup
    alloc.free_memory(mem_obj)
    assert alloc.is_all_free()
Пример #6
0
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
Пример #7
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()
Пример #8
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()
Пример #9
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()
Пример #10
0
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()
Пример #11
0
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
Пример #12
0
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()
Пример #13
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()
Пример #14
0
 def add_library(self, lib_base, exec_lib=None):
     lib = Library(self.mem, lib_base)
     lib.node.type = NodeType.NT_LIBRARY
     self.sum_library(lib_base)
     if exec_lib is None:
         exec_addr = self.mem.r32(4)
         exec_lib = ExecLibrary(self.mem, exec_addr)
     exec_lib.lib_list.enqueue(lib.node)
Пример #15
0
def libnative_makelib_test():
  machine = Machine()
  mem = machine.get_mem()
  sp = machine.get_ram_begin() - 4
  alloc = MemoryAlloc.for_machine(machine)

  # build lib info in memory
  vectors = 0x100
  mem.w32(vectors, 0x400)
  mem.w32(vectors+4, 0x600)
  mem.w32(vectors+8, 0x800)
  mem.w32(vectors+12, 0xffffffff)
  name_addr = 0x1f0
  mem.w_cstr(name_addr, "bla.library")

  init_tab = 0x200
  ib = InitStructBuilder(mem, init_tab)
  name_off = LibraryStruct.get_field_offset_for_path("lib_Node.ln_Name")
  ib.init_long(name_off, name_addr)
  ib.end()

  init_func = 0x300
  mem.w16(init_func, op_rts)

  pos_size = LibraryStruct.get_size()

  # make library
  ml = MakeLib(machine, alloc)
  lib_base, mem_obj = ml.make_library(
      vectors, init_tab, init_func, pos_size, 0,
      run_sp=sp)
  assert lib_base != 0

  # check library
  lib = Library(mem, lib_base)
  assert lib.get_name() == "bla.library"
  assert lib.get_pos_size() == 36
  assert lib.get_neg_size() == 20 # round_long(3*6)
  assert mem.r32(lib_base - 4) == 0x400
  assert mem.r32(lib_base - 10) == 0x600
  assert mem.r32(lib_base - 16) == 0x800

  # cleanup
  alloc.free_memory(mem_obj)
  assert alloc.is_all_free()
Пример #16
0
 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
Пример #17
0
def libnative_mgr_test(buildlibnix):
    if buildlibnix.flavor not in ('gcc', 'gcc-dbg'):
        pytest.skip("only single base lib supported")
    log_libmgr.setLevel(logging.INFO)
    machine, alloc, sp, mem, exec_lib = setup()
    # load
    lib_file = buildlibnix.make_lib('testnix')

    class PathMgrMock:
        def ami_to_sys_path(self, lock, ami_path, mustExist=True):
            if ami_path == 'LIBS:testnix.library':
                return lib_file

    pm = PathMgrMock()
    segload = SegmentLoader(alloc, pm)
    mgr = ALibManager(machine, alloc, segload)
    # open_lib
    lib_base = mgr.open_lib("testnix.library", run_sp=sp)
    assert lib_base > 0
    assert mgr.is_base_addr(lib_base)
    lib_info = mgr.get_lib_info_for_name("testnix.library")
    assert lib_info
    assert lib_info.is_base_addr(lib_base)
    # close lib
    seglist = mgr.close_lib(lib_base, run_sp=sp)
    assert seglist == 0
    lib_info = mgr.get_lib_info_for_name("testnix.library")
    assert lib_info
    assert not lib_info.is_base_addr(lib_base)
    # expunge lib
    left = mgr.shutdown(run_sp=sp)
    assert left == 0
    assert not mgr.is_base_addr(lib_base)
    lib_info = mgr.get_lib_info_for_name("testnix.library")
    assert not lib_info
    # we have to manually clean the lib here (as Exec FreeMem() does not work)
    lib = Library(mem, lib_base, alloc)
    lib.free()
    # cleanup
    exec_lib.free()
    assert alloc.is_all_free()
Пример #18
0
def libnative_mgr_test(buildlibnix):
  if buildlibnix.flavor not in ('gcc', 'gcc-dbg'):
    pytest.skip("only single base lib supported")
  log_libmgr.setLevel(logging.INFO)
  machine, alloc, sp, mem, exec_lib = setup()
  # load
  lib_file = buildlibnix.make_lib('testnix')

  class PathMgrMock:
    def ami_to_sys_path(self, lock, ami_path, mustExist=True):
      if ami_path == 'LIBS:testnix.library':
        return lib_file

  pm = PathMgrMock()
  segload = SegmentLoader(alloc, pm)
  mgr = ALibManager(machine, alloc, segload)
  # open_lib
  lib_base = mgr.open_lib("testnix.library", run_sp=sp)
  assert lib_base > 0
  assert mgr.is_base_addr(lib_base)
  lib_info = mgr.get_lib_info_for_name("testnix.library")
  assert lib_info
  assert lib_info.is_base_addr(lib_base)
  # close lib
  seglist = mgr.close_lib(lib_base, run_sp=sp)
  assert seglist == 0
  lib_info = mgr.get_lib_info_for_name("testnix.library")
  assert lib_info
  assert not lib_info.is_base_addr(lib_base)
  # expunge lib
  left = mgr.shutdown(run_sp=sp)
  assert left == 0
  assert not mgr.is_base_addr(lib_base)
  lib_info = mgr.get_lib_info_for_name("testnix.library")
  assert not lib_info
  # we have to manually clean the lib here (as Exec FreeMem() does not work)
  lib = Library(mem, lib_base, alloc)
  lib.free()
  # cleanup
  exec_lib.free()
  assert alloc.is_all_free()
Пример #19
0
  def make_library(self, vectors_addr, init_struct_addr, init_func_addr,
                   pos_size, seglist_baddr, label_name=None, run_sp=None):
    """Exec's MakeLibrary

       return lib_base, mem_obj or 0, None
    """
    neg_size, offsets = self._calc_neg_size(vectors_addr)
    neg_size = self._round_long(neg_size)
    pos_size = self._round_long(pos_size)
    size = neg_size + pos_size

    # allocate lib mem
    if label_name is None:
      label_name = "MakeLibrary"
    mobj = self.alloc.alloc_memory(label_name, size)
    addr = mobj.addr
    lib_base = addr + neg_size

    # init funcs
    mf = MakeFuncs(self.mem)
    if offsets:
      mf.make_functions(lib_base, vectors_addr+2, vectors_addr)
    else:
      mf.make_functions(lib_base, vectors_addr)

    # lib object and set neg/pos size
    lib = Library(self.mem, lib_base)
    lib.set_neg_size(neg_size)
    lib.set_pos_size(pos_size)

    # init struct?
    if init_struct_addr != 0:
      i = InitStruct(self.mem)
      i.init_struct(init_struct_addr, lib_base, 0)

    # init func?
    if init_func_addr != 0:
      lib_base = self.run_init(init_func_addr, lib_base, seglist_baddr,
                               label_name, run_sp)

    return lib_base, mobj
Пример #20
0
 def _check_version(self, name, addr, open_ver):
     # check version
     lib = Library(self.mem, addr)
     lib_ver = lib.version
     if lib_ver < open_ver:
         log_libmgr.warning("lib '%s' has too low version: %d < %d", name,
                            lib_ver, open_ver)
         return 0
     else:
         log_libmgr.info("lib '%s' version %d ok for open version %d", name,
                         lib_ver, open_ver)
         return addr
Пример #21
0
 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
Пример #22
0
 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
Пример #23
0
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()
Пример #24
0
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()
Пример #25
0
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()
Пример #26
0
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()
Пример #27
0
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()
Пример #28
0
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()
Пример #29
0
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()
Пример #30
0
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()
Пример #31
0
    def make_library(
        self,
        vectors_addr,
        init_struct_addr,
        init_func_addr,
        pos_size,
        seglist_baddr,
        label_name=None,
        run_sp=None,
    ):
        """Exec's MakeLibrary

        return lib_base, mem_obj or 0, None
        """
        neg_size, offsets = self._calc_neg_size(vectors_addr)
        neg_size = self._round_long(neg_size)
        pos_size = self._round_long(pos_size)
        size = neg_size + pos_size

        # allocate lib mem
        if label_name is None:
            label_name = "MakeLibrary"
        mobj = self.alloc.alloc_memory(label_name, size)
        addr = mobj.addr
        lib_base = addr + neg_size

        # init funcs
        mf = MakeFuncs(self.mem)
        if offsets:
            mf.make_functions(lib_base, vectors_addr + 2, vectors_addr)
        else:
            mf.make_functions(lib_base, vectors_addr)

        # lib object and set neg/pos size
        lib = Library(self.mem, lib_base)
        lib.set_neg_size(neg_size)
        lib.set_pos_size(pos_size)

        # init struct?
        if init_struct_addr != 0:
            i = InitStruct(self.mem)
            i.init_struct(init_struct_addr, lib_base, 0)

        # init func?
        if init_func_addr != 0:
            lib_base = self.run_init(
                init_func_addr, lib_base, seglist_baddr, label_name, run_sp
            )

        return lib_base, mobj
Пример #32
0
 def set_function(self, lib_base, lvo, new_func_addr):
     """return old func addr or None if patch failed"""
     lib = Library(self.mem, lib_base)
     neg_size = lib.neg_size
     if lvo < 0:
         lvo = -lvo
     # check lvo range
     if lvo >= neg_size:
         return None
     # check that jmp is at lvo
     addr = lib_base - lvo
     jmp = self.mem.r16(addr)
     if jmp != op_jmp:
         return None
     # set new function
     old_func = self.mem.r32(addr + 2)
     self.mem.w32(addr + 2, new_func_addr)
     # sum lib
     self.sum_library(lib_base)
     return old_func
Пример #33
0
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()
Пример #34
0
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()
Пример #35
0
  def init_resident(self, resident_addr, seg_list_baddr,
                    label_name=None, run_sp=None, exec_lib=None):
    """Implement Exec's InitResident
       Returns lib_base, mem_obj or lib_base, 0 or 0, None
    """
    res = Resident(self.mem, resident_addr)
    if not res.is_valid():
      return 0, None

    if label_name is None:
      label_name = "%s_init" % res.name

    auto_init = res.flags.has_bits(ResidentFlags.RTF_AUTOINIT)
    if auto_init:
      ai = res.get_auto_init()

      # create lib without calling init
      ml = MakeLib(self.machine, self.alloc)
      lib_base, mem_obj = ml.make_library(ai.functions, ai.init_struct,
                                          0, ai.pos_size,
                                          seg_list_baddr,
                                          label_name=label_name, run_sp=run_sp)

      # fill lib
      lib = Library(self.mem, lib_base)
      flags = LibFlags.LIBF_CHANGED | LibFlags.LIBF_SUMUSED
      lib.setup(version=res.version, type=res.type, flags=flags)
      lib.name = res.name
      lib.id_string = res.id_string

      # now call init
      if ai.init_func != 0:
        run_name = "InitResident:%s" % res.name
        lib_base = ml.run_init(
            ai.init_func, lib_base, seg_list_baddr, run_name, run_sp)

      if lib_base == 0:
        return 0, None

      # add lib to exec list
      rtype = res.type
      if rtype == NodeType.NT_LIBRARY:
        lf = LibFuncs(self.machine, self.alloc)
        lf.add_library(lib_base, exec_lib)
      elif rtype == NodeType.NT_DEVICE:
        # TODO
        raise NotImplementedError("InitResource(NT_DEVICE)")
      elif rtype == NodeType.NT_RESOURCE:
        # TODO
        raise NotImplementedError("InitResident(NT_RESOURCE)")

    else:
      # no auto init, lib_base, or mem_obj
      lib_base = 0
      mem_obj = None
      # call init func
      init_func = res.init
      if init_func != 0:
        ml = MakeLib(self.machine, self.alloc)
        lib_base = ml.run_init(
            init_func, lib_base, seg_list_baddr, label_name, run_sp)

    return lib_base, mem_obj
Пример #36
0
    def init_resident(self,
                      resident_addr,
                      seg_list_baddr,
                      label_name=None,
                      run_sp=None,
                      exec_lib=None):
        """Implement Exec's InitResident
        Returns lib_base, mem_obj or lib_base, 0 or 0, None
        """
        res = Resident(self.mem, resident_addr)
        if not res.is_valid():
            return 0, None

        if label_name is None:
            label_name = "%s_init" % res.name

        auto_init = res.flags.has_bits(ResidentFlags.RTF_AUTOINIT)
        if auto_init:
            ai = res.get_auto_init()

            # create lib without calling init
            ml = MakeLib(self.machine, self.alloc)
            lib_base, mem_obj = ml.make_library(
                ai.functions,
                ai.init_struct,
                0,
                ai.pos_size,
                seg_list_baddr,
                label_name=label_name,
                run_sp=run_sp,
            )

            # fill lib
            lib = Library(self.mem, lib_base)
            flags = LibFlags.LIBF_CHANGED | LibFlags.LIBF_SUMUSED
            lib.setup(version=res.version, type=res.type, flags=flags)
            lib.name = res.name
            lib.id_string = res.id_string

            # now call init
            if ai.init_func != 0:
                run_name = "InitResident:%s" % res.name
                lib_base = ml.run_init(ai.init_func, lib_base, seg_list_baddr,
                                       run_name, run_sp)

            if lib_base == 0:
                return 0, None

            # add lib to exec list
            rtype = res.type
            if rtype == NodeType.NT_LIBRARY:
                lf = LibFuncs(self.machine, self.alloc)
                lf.add_library(lib_base, exec_lib)
            elif rtype == NodeType.NT_DEVICE:
                # TODO
                raise NotImplementedError("InitResource(NT_DEVICE)")
            elif rtype == NodeType.NT_RESOURCE:
                # TODO
                raise NotImplementedError("InitResident(NT_RESOURCE)")

        else:
            # no auto init, lib_base, or mem_obj
            lib_base = 0
            mem_obj = None
            # call init func
            init_func = res.init
            if init_func != 0:
                ml = MakeLib(self.machine, self.alloc)
                lib_base = ml.run_init(init_func, lib_base, seg_list_baddr,
                                       label_name, run_sp)

        return lib_base, mem_obj
Пример #37
0
 def sum_library(self, lib_base):
   lib = Library(self.mem, lib_base)
   lib.flags.clr_bits(LibFlags.LIBF_CHANGED | LibFlags.LIBF_SUMUSED)
   return lib.update_sum()
Пример #38
0
 def sum_library(self, lib_base):
     lib = Library(self.mem, lib_base)
     lib.flags.clr_bits(LibFlags.LIBF_CHANGED | LibFlags.LIBF_SUMUSED)
     return lib.update_sum()