Ejemplo n.º 1
0
def libcore_patch_multi_trap_test(capsys):
  name = 'vamostest.library'
  impl = VamosTestLibrary()
  fd = read_lib_fd(name)
  ctx = _create_ctx()
  # create stub
  gen = LibStubGen()
  stub = gen.gen_stub(name, impl, fd, ctx)
  # now patcher
  traps = MockTraps()
  p = LibPatcherMultiTrap(ctx.mem, traps, stub)
  base_addr = 0x100
  p.patch_jump_table(base_addr)
  # lookup trap for function
  func = fd.get_func_by_name('PrintHello')
  bias = func.get_bias()
  func_addr = base_addr - bias
  op = ctx.mem.r16(func_addr)
  # its a trap
  assert op & 0xf000 == 0xa000
  # trigger
  traps.trigger(op)
  captured = capsys.readouterr()
  assert captured.out.strip().split('\n') == [
      'VamosTest: PrintHello()'
  ]
  # remove traps
  p.cleanup()
  assert traps.get_num_traps() == 0
Ejemplo n.º 2
0
def libcore_patch_multi_trap_test(capsys):
    name = 'vamostest.library'
    impl = VamosTestLibrary()
    fd = read_lib_fd(name)
    machine = MockMachine()
    ctx = LibCtx(machine)
    # create stub
    gen = LibStubGen()
    stub = gen.gen_stub(name, impl, fd, ctx)
    # now patcher
    alloc = MemoryAlloc(ctx.mem)
    traps = machine.get_traps()
    p = LibPatcherMultiTrap(alloc, traps, stub)
    base_addr = 0x100
    p.patch_jump_table(base_addr)
    # lookup trap for function
    func = fd.get_func_by_name('PrintHello')
    bias = func.get_bias()
    func_addr = base_addr - bias
    # check that jump table has jump + addr
    op = ctx.mem.r16(func_addr)
    assert op == op_jmp
    trap_addr = ctx.mem.r32(func_addr + 2)
    # check jump target is trap
    op = ctx.mem.r16(trap_addr)
    assert op & 0xf000 == 0xa000
    # trigger trap
    traps.trigger(op)
    captured = capsys.readouterr()
    assert captured.out.strip().split('\n') == ['VamosTest: PrintHello()']
    # remove traps
    p.cleanup()
    assert traps.get_num_traps() == 0
    assert alloc.is_all_free()
Ejemplo n.º 3
0
def libcore_impl_scan_vamos_test():
  fd = read_lib_fd('vamostest.library')
  impl = VamosTestLibrary()
  scanner = LibImplScanner()
  scanner.scan(impl, fd)
  assert scanner.get_num_valid_funcs() == 5
  assert scanner.get_num_missing_funcs() == 1
  assert scanner.get_num_error_funcs() == 0
  assert scanner.get_num_invalid_funcs() == 1
  # check funcs
  missing_funcs = scanner.get_missing_funcs()
  assert missing_funcs == {
    "Dummy" : fd.get_func_by_name("Dummy")
  }
  invalid_funcs = scanner.get_invalid_funcs()
  assert invalid_funcs == {
    "InvalidFunc" : impl.InvalidFunc
  }
  valid_funcs = scanner.get_valid_funcs()
  assert valid_funcs == {
    "PrintHello" : (fd.get_func_by_name("PrintHello"), impl.PrintHello),
    "PrintString" : (fd.get_func_by_name("PrintString"), impl.PrintString),
    "Add" : (fd.get_func_by_name("Add"), impl.Add),
    "Swap" : (fd.get_func_by_name("Swap"), impl.Swap),
    "RaiseError" : (fd.get_func_by_name("RaiseError"), impl.RaiseError)
  }
Ejemplo n.º 4
0
def libcore_profile_test():
    name = 'vamostest.library'
    fd = read_lib_fd(name)
    prof = LibProfile(name, fd)
    # func
    func_name = "Add"
    func = fd.get_func_by_name(func_name)
    func_prof = prof.get_func_prof(func.get_index())
    assert func_prof.get_name() == func_name
    # count
    func_prof.count(1.0)
    func_prof.count(2.0)
    func_prof.count(3.0)
    assert func_prof.get_num_calls() == 3
    assert func_prof.get_deltas() is None
    assert func_prof.get_sum_delta() == 6.0
    assert func_prof.get_avg_delta() == 2.0
    assert str(
        func_prof
    ) == "Add                        3 calls    6000.000 ms  avg    2000.000 ms"
    # dump
    assert prof.get_total() == (3, 6.0, 2.0)
    assert prof.get_total_str(
    ) == "LIB TOTAL                  3 calls    6000.000 ms  avg    2000.000 ms"
    prof.dump()
Ejemplo n.º 5
0
def libcore_profiler_main_profiler_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  cfg = ConfigDict({
      "enabled": True,
      "libs": {
          "names": [name],
          "calls": True
      },
      "output": {
          "file": None,
          "append": False,
          "dump": True
      }
  })
  mp = MainProfiler()
  prof = LibProfiler()
  assert mp.parse_config(cfg)
  assert mp.add_profiler(prof)
  mp.setup()
  assert prof.names == [name]
  assert prof.add_calls
  p = prof.create_profile(name, fd)
  assert p
  mp.shutdown()
Ejemplo n.º 6
0
def libcore_profiler_profiler_reuse_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfiler()
  prof.parse_config(ConfigDict({
      "names": [name],
      "calls": True
  }))
  prof.setup()
  p = prof.create_profile(name, fd)
  assert p
  f = p.get_func_by_name("Input")
  assert f
  idx = f.get_func_id()
  assert p.get_func_by_index(idx) == f
  f.count(1.0)
  # store/restore
  data = prof.get_data()
  prof2 = LibProfiler()
  assert prof2.set_data(data)
  prof2.setup()
  # reuse profile
  p2 = prof2.create_profile(name, fd)
  assert p == p2
  f2 = p2.get_func_by_name("Input")
  assert f2.get_num_calls() == 1
  f2.count(1.0)
  assert f2.get_num_calls() == 2
  idx = f2.get_func_id()
  assert p2.get_func_by_index(idx) == f2
Ejemplo n.º 7
0
def libcore_profile_data_samples_test():
  # from fd
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfileData(fd, True)
  # get func
  func_name = "Input"
  func = fd.get_func_by_name(func_name)
  idx = func.get_index()
  func_prof = prof.get_func_by_index(idx)
  assert func_prof
  assert func_prof.get_func_id() == idx
  # count
  func_prof.count(1.0)
  func_prof.count(2.0)
  func_prof.count(3.0)
  assert prof.get_total() == (3, 6.0, 2.0)
  assert prof.get_total_str() == \
      "LIB TOTAL                  3 calls    6000.000 ms" \
      "  avg    2000.000 ms"
  prof.remove_empty()
  # to/from dict
  data_dict = prof.get_data()
  prof2 = LibProfileData.from_dict(data_dict)
  assert prof == prof2
  # get func
  prof2.setup_func_table(fd)
  func_prof2 = prof2.get_func_by_index(func.get_index())
  assert func_prof == func_prof2
Ejemplo n.º 8
0
def libcore_profiler_main_profiler_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  cfg = ConfigDict({
      "enabled": True,
      "libs": {
          "names": [name],
          "calls": True
      },
      "output": {
          "file": None,
          "append": False,
          "dump": True
      }
  })
  mp = MainProfiler()
  prof = LibProfiler()
  assert mp.parse_config(cfg)
  assert mp.add_profiler(prof)
  mp.setup()
  assert prof.names == [name]
  assert prof.add_calls
  p = prof.create_profile(name, fd)
  assert p
  mp.shutdown()
Ejemplo n.º 9
0
def libcore_impl_scan_vamos_extra_args_test():
    name = "vamostest.library"
    fd = read_lib_fd(name)
    impl = VamosTestLibrary()

    # setup test function with annotated args
    # also test name replacement with _ if it collides with Python
    def PrintString(self, ctx, str_: str):
        pass

    impl.PrintString = PrintString.__get__(impl, impl.__class__)

    # scan impl
    scanner = LibImplScanner()
    res = scanner.scan(name, impl, fd)

    # check valid funcs
    valid_func = res.get_func_by_name("ExecutePy")
    assert valid_func.name == "ExecutePy"
    assert valid_func.fd_func == fd.get_func_by_name("ExecutePy")
    assert valid_func.method == impl.ExecutePy
    assert valid_func.tag == LibImplScan.TAG_VALID
    assert valid_func.extra_args == [
        LibImplFuncArg("argc", REG_D0, int),
        LibImplFuncArg("argv", REG_A0, int),
    ]

    valid_func = res.get_func_by_name("PrintString")
    assert valid_func.name == "PrintString"
    assert valid_func.fd_func == fd.get_func_by_name("PrintString")
    assert valid_func.method == impl.PrintString
    assert valid_func.tag == LibImplScan.TAG_VALID
    assert valid_func.extra_args == [
        LibImplFuncArg("str", REG_A0, str),
    ]
Ejemplo n.º 10
0
def libcore_impl_scan_checked_vamos_test():
    name = "vamostest.library"
    fd = read_lib_fd(name)
    impl = VamosTestLibrary()
    scanner = LibImplScanner()
    with pytest.raises(VamosInternalError):
        scanner.scan_checked(name, impl, fd)
Ejemplo n.º 11
0
def libcore_profile_data_samples_test():
  # from fd
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfileData(fd, True)
  # get func
  func_name = "Input"
  func = fd.get_func_by_name(func_name)
  idx = func.get_index()
  func_prof = prof.get_func_by_index(idx)
  assert func_prof
  assert func_prof.get_func_id() == idx
  # count
  func_prof.count(1.0)
  func_prof.count(2.0)
  func_prof.count(3.0)
  assert prof.get_total() == (3, 6.0, 2.0)
  assert prof.get_total_str() == \
      "LIB TOTAL                  3 calls    6000.000 ms" \
      "  avg    2000.000 ms"
  prof.remove_empty()
  # to/from dict
  data_dict = prof.get_data()
  prof2 = LibProfileData.from_dict(data_dict)
  assert prof == prof2
  # get func
  prof2.setup_func_table(fd)
  func_prof2 = prof2.get_func_by_index(func.get_index())
  assert func_prof == func_prof2
Ejemplo n.º 12
0
def libcore_profile_data_test():
    # from fd
    name = "dos.library"
    fd = read_lib_fd(name)
    prof = LibProfileData(fd)
    # get func
    func_name = "Input"
    func = fd.get_func_by_name(func_name)
    idx = func.get_index()
    func_prof = prof.get_func_by_index(idx)
    assert func_prof
    assert func_prof.get_func_id() == idx
    assert prof.get_func_by_name(func_name) == func_prof
    # count
    func_prof.count(1.0)
    func_prof.count(2.0)
    func_prof.count(3.0)
    assert prof.get_total() == (3, 6.0, 2.0)
    assert (
        prof.get_total_str() == "LIB TOTAL                  3 calls    6000.000 ms"
        "  avg    2000.000 ms"
    )
    prof.remove_empty()
    assert [x for x in prof.get_all_funcs()] == [(func_name, func_prof)]
    # to/from dict
    data_dict = prof.get_data()
    prof2 = LibProfileData.from_dict(data_dict)
    assert prof == prof2
    # get func
    prof2.setup_func_table(fd)
    func_prof2 = prof2.get_func_by_index(idx)
    assert func_prof == func_prof2
Ejemplo n.º 13
0
def libcore_impl_scan_checked_vamos_test():
  name = 'vamostest.library'
  fd = read_lib_fd(name)
  impl = VamosTestLibrary()
  scanner = LibImplScanner()
  with pytest.raises(VamosInternalError):
    scanner.scan_checked(name, impl, fd)
Ejemplo n.º 14
0
def libcore_profiler_profiler_reuse_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfiler()
  prof.parse_config(ConfigDict({
      "names": [name],
      "calls": True
  }))
  prof.setup()
  p = prof.create_profile(name, fd)
  assert p
  f = p.get_func_by_name("Input")
  assert f
  idx = f.get_func_id()
  assert p.get_func_by_index(idx) == f
  f.count(1.0)
  # store/restore
  data = prof.get_data()
  prof2 = LibProfiler()
  assert prof2.set_data(data)
  prof2.setup()
  # reuse profile
  p2 = prof2.create_profile(name, fd)
  assert p == p2
  f2 = p2.get_func_by_name("Input")
  assert f2.get_num_calls() == 1
  f2.count(1.0)
  assert f2.get_num_calls() == 2
  idx = f2.get_func_id()
  assert p2.get_func_by_index(idx) == f2
Ejemplo n.º 15
0
 def create_lib(self, info, ctx, impl=None, lib_cfg=None, check=False):
     name = info.get_name()
     if name.endswith(".device"):
         is_dev = True
     elif name.endswith(".library"):
         is_dev = False
     else:
         raise ValueError("create_lib: %s is neither lib nor dev!" % name)
     # get fd: either read from fd or fake one
     fd = read_lib_fd(name, self.fd_dir)
     if fd is None:
         fd = self._generate_fake_fd(name, lib_cfg)
     # if impl is available scan it
     scan = None
     if impl:
         scanner = LibImplScanner()
         if check:
             scan = scanner.scan_checked(name, impl, fd, True)
         else:
             scan = scanner.scan(name, impl, fd, True)
     # add profile?
     if self.profiler:
         # get some impl information
         if scan:
             impl_funcs = scan.get_all_funcs()
         else:
             impl_funcs = None
         profile = self.profiler.create_profile(name, fd, impl_funcs)
     else:
         profile = None
     # create stub
     if scan is None:
         stub = self.stub_gen.gen_fake_stub(name, fd, ctx, profile)
         struct = LibraryStruct
     else:
         stub = self.stub_gen.gen_stub(scan, ctx, profile)
         struct = impl.get_struct_def()
     # adjust info pos/neg size
     if info.pos_size == 0:
         info.pos_size = struct.get_size()
     if info.neg_size == 0:
         info.neg_size = fd.get_neg_size()
     # allocate and init lib
     library = self._create_library(info, is_dev, fd)
     addr = library.get_addr()
     # patcher
     patcher = LibPatcherMultiTrap(self.alloc, self.traps, stub)
     patcher.patch_jump_table(addr)
     # fix lib sum
     library.update_sum()
     # create vamos lib and combine all pieces
     vlib = VLib(
         library, info, struct, fd, impl, stub, ctx, patcher, profile, is_dev
     )
     return vlib
Ejemplo n.º 16
0
def libcore_profiler_profiler_set_get_data_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfiler(names=[name])
  prof.setup()
  p = prof.create_profile(name, fd)
  data = prof.get_data()
  prof2 = LibProfiler()
  assert prof2.set_data(data)
  p2 = prof2.get_profile(name)
  assert p == p2
Ejemplo n.º 17
0
 def create_lib(self, info, ctx, impl=None, lib_cfg=None, check=False):
   name = info.get_name()
   if name.endswith('.device'):
     is_dev = True
   elif name.endswith('.library'):
     is_dev = False
   else:
     raise ValueError("create_lib: %s is neither lib nor dev!" % name)
   # get fd: either read from fd or fake one
   fd = read_lib_fd(name, self.fd_dir)
   if fd is None:
     fd = self._generate_fake_fd(name, lib_cfg)
   # if impl is available scan it
   scan = None
   if impl:
     scanner = LibImplScanner()
     if check:
       scan = scanner.scan_checked(name, impl, fd, True)
     else:
       scan = scanner.scan(name, impl, fd, True)
   # add profile?
   if self.profiler:
     # get some impl information
     if scan:
       func_tags = scan.get_func_tags()
     else:
       func_tags = None
     profile = self.profiler.create_profile(name, fd, func_tags)
   else:
     profile = None
   # create stub
   if scan is None:
     stub = self.stub_gen.gen_fake_stub(name, fd, ctx, profile)
     struct = LibraryStruct
   else:
     stub = self.stub_gen.gen_stub(scan, ctx, profile)
     struct = impl.get_struct_def()
   # adjust info pos/neg size
   if info.pos_size == 0:
     info.pos_size = struct.get_size()
   if info.neg_size == 0:
     info.neg_size = fd.get_neg_size()
   # allocate and init lib
   library = self._create_library(info, is_dev, fd)
   addr = library.get_addr()
   # patcher
   patcher = LibPatcherMultiTrap(self.alloc, self.traps, stub)
   patcher.patch_jump_table(addr)
   # fix lib sum
   library.update_sum()
   # create vamos lib and combine all pieces
   vlib = VLib(library, info, struct, fd, impl,
               stub, ctx, patcher, profile, is_dev)
   return vlib
Ejemplo n.º 18
0
def get_jump_table(with_fd=False, **kwargs):
    machine = MockMachine()
    mem = machine.get_mem()
    name = 'vamostest.library'
    fd = read_lib_fd(name)
    neg_size = fd.get_neg_size()
    lib_base = neg_size + 0x100
    if not with_fd:
        fd = None
    jt = LibJumpTable(mem, lib_base, neg_size, fd=fd, **kwargs)
    return jt
Ejemplo n.º 19
0
def get_jump_table(with_fd=False, **kwargs):
  machine = MockMachine()
  mem = machine.get_mem()
  name = 'vamostest.library'
  fd = read_lib_fd(name)
  neg_size = fd.get_neg_size()
  lib_base = neg_size + 0x100
  if not with_fd:
    fd = None
  jt = LibJumpTable(mem, lib_base, neg_size, fd=fd, **kwargs)
  return jt
Ejemplo n.º 20
0
def libcore_profiler_profiler_set_get_data_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfiler(names=[name])
  prof.setup()
  p = prof.create_profile(name, fd)
  data = prof.get_data()
  prof2 = LibProfiler()
  assert prof2.set_data(data)
  p2 = prof2.get_profile(name)
  assert p == p2
Ejemplo n.º 21
0
def libcore_profile_profiler_test():
    name = 'vamostest.library'
    fd = read_lib_fd(name)
    c = LibProfilerConfig(profiling=True, libs=[name])
    p = LibProfiler(c)
    assert not p.get_profile(name)
    prof = p.create_profile(name, fd)
    assert prof
    assert prof == p.get_profile(name)
    assert p.get_all_lib_names() == [name]
    assert p.get_num_libs() == 1
Ejemplo n.º 22
0
def libcore_stub_gen_fake_base_test():
  name = 'vamostest.library'
  fd = read_lib_fd(name)
  ctx = _create_ctx()
  # create stub
  gen = LibStubGen()
  stub = gen.gen_fake_stub(name, fd, ctx)
  _check_stub(stub)
  # call func
  stub.PrintHello()
  stub.Dummy()
  stub.Swap()
Ejemplo n.º 23
0
def libcore_profile_profiler_default_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfiler(names=[name])
  prof.setup()
  p = prof.create_profile(name, fd)
  assert p
  assert prof.create_profile('bla', fd) is None
  assert prof.get_profile(name) == p
  assert prof.get_num_libs() == 1
  assert prof.get_all_lib_names() == [name]
  prof.shutdown()
Ejemplo n.º 24
0
def libcore_stub_gen_fake_base_test():
    name = 'vamostest.library'
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    # create stub
    gen = LibStubGen()
    stub = gen.gen_fake_stub(name, fd, ctx)
    _check_stub(stub)
    # call func
    stub.PrintHello()
    stub.Dummy()
    stub.Swap()
Ejemplo n.º 25
0
def libcore_profile_profiler_default_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfiler(names=[name])
  prof.setup()
  p = prof.create_profile(name, fd)
  assert p
  assert prof.create_profile('bla', fd) is None
  assert prof.get_profile(name) == p
  assert prof.get_num_libs() == 1
  assert prof.get_all_lib_names() == [name]
  prof.shutdown()
Ejemplo n.º 26
0
def libcore_profiler_profiler_config_test():
    name = 'dos.library'
    fd = read_lib_fd(name)
    prof = LibProfiler()
    prof.parse_config(ConfigDict({"names": [name], "calls": True}))
    prof.setup()
    p = prof.create_profile(name, fd)
    data = prof.get_data()
    prof2 = LibProfiler()
    assert prof2.set_data(data)
    p2 = prof2.get_profile(name)
    assert p == p2
Ejemplo n.º 27
0
def setup_tm():
  machine = MockMachine()
  lm = machine.get_label_mgr()
  tm = TraceManager(machine)
  lm.add_label(LabelRange("range", 0x100, 0x100))
  lm.add_label(LabelStruct("node", 0x200, NodeStruct))
  lm.add_label(LabelLib("fake.library", 0x320, 0x20,
                        LibraryStruct.get_size(), LibraryStruct))
  fd = read_lib_fd("vamostest.library")
  neg_size = fd.get_neg_size()
  lm.add_label(LabelLib("vamostest.library", 0x400, neg_size,
                        LibraryStruct.get_size(), LibraryStruct, fd))
  return tm
Ejemplo n.º 28
0
def libcore_stub_gen_exc_default_test():
    name = 'vamostest.library'
    impl = VamosTestLibrary()
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    # create stub
    gen = LibStubGen()
    stub = gen.gen_stub(name, impl, fd, ctx)
    _check_stub(stub)
    # call func
    ctx.mem.w_cstr(0, 'RuntimeError')
    with pytest.raises(RuntimeError):
        stub.RaiseError()
Ejemplo n.º 29
0
 def open_lib(self, lib_name, cwd_lock=None, run_sp=None, progdir_lock=None):
     log_libmgr.info(
         "[native] +open_lib: lib_name='%s', cwd=%s, progdir=%s",
         lib_name,
         cwd_lock,
         progdir_lock,
     )
     # multict lib base name
     base_name = self.loader.get_lib_base_name(lib_name)
     # find library
     lib_info = self._find_info(base_name)
     log_libmgr.info("find: '%s' -> %s", base_name, lib_info)
     # not found... try to load it
     if not lib_info:
         log_libmgr.info("loading native lib: '%s'", lib_name)
         load_addr, seglist_baddr = self.loader.load_ami_lib(
             lib_name, cwd_lock, run_sp, progdir_lock
         )
         # even loading failed... abort
         if load_addr == 0:
             log_libmgr.info("[native] -open_lib: load failed!")
             return 0
         log_libmgr.info("loaded: @%06x  seglist: @%06x", load_addr, seglist_baddr)
         info = self.segloader.get_info(seglist_baddr)
         log_libmgr.info("loaded: %s", info)
         # try to load associated fd (if available)
         fd = read_lib_fd(base_name, add_std_calls=False)
         log_libmgr.info("loaded FD: '%s' -> %r", base_name, fd is not None)
         # store original load addr and name in info
         lib_info = self._add_info(base_name, load_addr, seglist_baddr, fd)
         loaded = True
     else:
         load_addr = lib_info.get_load_addr()
         loaded = False
     # got lib: open lib... may return new base!
     log_libmgr.debug("[native] call open lib: load_addr=%06x", load_addr)
     lib_base = self.funcs.open_library(load_addr, run_sp)
     # save base addr
     if lib_base > 0:
         lib_info.add_base_addr(lib_base)
     elif loaded:
         # remove lib info again
         self.lib_infos.remove(lib_info)
     log_libmgr.info(
         "[native] -open_lib: load_addr=@%06x, lib_base=@%06x, %s",
         load_addr,
         lib_base,
         lib_info,
     )
     # return base
     return lib_base
Ejemplo n.º 30
0
def libcore_impl_scan_vamos_test():
  name = 'vamostest.library'
  fd = read_lib_fd(name)
  impl = VamosTestLibrary()
  # inject error func

  def PrintHello(self, ctx, wrong):
    pass
  impl.PrintHello = PrintHello.__get__(impl, impl.__class__)
  # scan impl
  scanner = LibImplScanner()
  res = scanner.scan(name, impl, fd)
  assert res.get_name() == name
  assert res.get_impl() == impl
  assert res.get_fd() == fd
  assert res.get_num_valid_funcs() == 4
  assert res.get_num_missing_funcs() == 1
  assert res.get_num_error_funcs() == 1
  assert res.get_num_invalid_funcs() == 1
  # check funcs
  missing_funcs = res.get_missing_funcs()
  assert missing_funcs == {
      "Dummy": fd.get_func_by_name("Dummy")
  }
  missing_names = res.get_missing_func_names()
  assert missing_names == ["Dummy"]

  invalid_funcs = res.get_invalid_funcs()
  assert invalid_funcs == {
      "InvalidFunc": impl.InvalidFunc
  }
  invalid_names = res.get_invalid_func_names()
  assert invalid_names == ["InvalidFunc"]

  error_funcs = res.get_error_funcs()
  assert error_funcs == {
      "PrintHello": (fd.get_func_by_name("PrintHello"), impl.PrintHello)
  }
  error_names = res.get_error_func_names()
  assert error_names == ["PrintHello"]

  valid_funcs = res.get_valid_funcs()
  assert valid_funcs == {
      "PrintString": (fd.get_func_by_name("PrintString"), impl.PrintString),
      "Add": (fd.get_func_by_name("Add"), impl.Add),
      "Swap": (fd.get_func_by_name("Swap"), impl.Swap),
      "RaiseError": (fd.get_func_by_name("RaiseError"), impl.RaiseError)
  }
  valid_names = res.get_valid_func_names()
  assert valid_names == ["Add", "PrintString", "RaiseError", "Swap"]
Ejemplo n.º 31
0
def libcore_stub_gen_fake_profile_test():
  name = 'vamostest.library'
  fd = read_lib_fd(name)
  ctx = _create_ctx()
  profile = LibProfileData(fd)
  # create stub
  gen = LibStubGen()
  stub = gen.gen_fake_stub(name, fd, ctx, profile)
  _check_stub(stub)
  # call func
  stub.PrintHello()
  stub.Dummy()
  stub.Swap()
  _check_profile(fd, profile)
Ejemplo n.º 32
0
def libcore_stub_gen_fake_profile_test():
    name = 'vamostest.library'
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    profile = LibProfileData(fd)
    # create stub
    gen = LibStubGen()
    stub = gen.gen_fake_stub(name, fd, ctx, profile)
    _check_stub(stub)
    # call func
    stub.PrintHello()
    stub.Dummy()
    stub.Swap()
    _check_profile(fd, profile)
Ejemplo n.º 33
0
def libcore_profiler_profiler_config_test():
  name = 'dos.library'
  fd = read_lib_fd(name)
  prof = LibProfiler()
  prof.parse_config(ConfigDict({
      "names": [name],
      "calls": True
  }))
  prof.setup()
  p = prof.create_profile(name, fd)
  data = prof.get_data()
  prof2 = LibProfiler()
  assert prof2.set_data(data)
  p2 = prof2.get_profile(name)
  assert p == p2
Ejemplo n.º 34
0
def setup_tm():
    machine = MockMachine()
    lm = machine.get_label_mgr()
    tm = TraceManager(machine)
    lm.add_label(LabelRange("range", 0x100, 0x100))
    lm.add_label(LabelStruct("node", 0x200, NodeStruct))
    lm.add_label(
        LabelLib("fake.library", 0x320, 0x20, LibraryStruct.get_size(),
                 LibraryStruct))
    fd = read_lib_fd("vamostest.library")
    neg_size = fd.get_neg_size()
    lm.add_label(
        LabelLib("vamostest.library", 0x400, neg_size,
                 LibraryStruct.get_size(), LibraryStruct, fd))
    return tm
Ejemplo n.º 35
0
def libcore_impl_scan_vamos_test():
    name = 'vamostest.library'
    fd = read_lib_fd(name)
    impl = VamosTestLibrary()

    # inject error func

    def PrintHello(self, ctx, wrong):
        pass

    impl.PrintHello = PrintHello.__get__(impl, impl.__class__)
    # scan impl
    scanner = LibImplScanner()
    res = scanner.scan(name, impl, fd)
    assert res.get_name() == name
    assert res.get_impl() == impl
    assert res.get_fd() == fd
    assert res.get_num_valid_funcs() == 4
    assert res.get_num_missing_funcs() == 1
    assert res.get_num_error_funcs() == 1
    assert res.get_num_invalid_funcs() == 1
    # check funcs
    missing_funcs = res.get_missing_funcs()
    assert missing_funcs == {"Dummy": fd.get_func_by_name("Dummy")}
    missing_names = res.get_missing_func_names()
    assert missing_names == ["Dummy"]

    invalid_funcs = res.get_invalid_funcs()
    assert invalid_funcs == {"InvalidFunc": impl.InvalidFunc}
    invalid_names = res.get_invalid_func_names()
    assert invalid_names == ["InvalidFunc"]

    error_funcs = res.get_error_funcs()
    assert error_funcs == {
        "PrintHello": (fd.get_func_by_name("PrintHello"), impl.PrintHello)
    }
    error_names = res.get_error_func_names()
    assert error_names == ["PrintHello"]

    valid_funcs = res.get_valid_funcs()
    assert valid_funcs == {
        "PrintString": (fd.get_func_by_name("PrintString"), impl.PrintString),
        "Add": (fd.get_func_by_name("Add"), impl.Add),
        "Swap": (fd.get_func_by_name("Swap"), impl.Swap),
        "RaiseError": (fd.get_func_by_name("RaiseError"), impl.RaiseError)
    }
    valid_names = res.get_valid_func_names()
    assert valid_names == ["Add", "PrintString", "RaiseError", "Swap"]
Ejemplo n.º 36
0
def libcore_stub_gen_fake_log_test(caplog):
    caplog.set_level(logging.INFO)
    name = 'vamostest.library'
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    log_missing = logging.getLogger('missing')
    log_valid = logging.getLogger('valid')
    # create stub
    gen = LibStubGen(log_missing=log_missing, log_valid=log_valid)
    stub = gen.gen_fake_stub(name, fd, ctx)
    _check_stub(stub)
    # call func
    stub.PrintHello()
    stub.Dummy()
    stub.Swap()
    _check_log_fake(caplog)
Ejemplo n.º 37
0
def libcore_stub_gen_fake_log_test(caplog):
  caplog.set_level(logging.INFO)
  name = 'vamostest.library'
  fd = read_lib_fd(name)
  ctx = _create_ctx()
  log_missing = logging.getLogger('missing')
  log_valid = logging.getLogger('valid')
  # create stub
  gen = LibStubGen(log_missing=log_missing, log_valid=log_valid)
  stub = gen.gen_fake_stub(name, fd, ctx)
  _check_stub(stub)
  # call func
  stub.PrintHello()
  stub.Dummy()
  stub.Swap()
  _check_log_fake(caplog)
Ejemplo n.º 38
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()
Ejemplo n.º 39
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()
Ejemplo n.º 40
0
def gen_prof(add_samples=False):
    name = 'vamostest.library'
    fd = read_lib_fd(name)
    cfg = LibProfilerConfig(profiling=True,
                            all_libs=True,
                            add_samples=add_samples)
    p = LibProfiler(cfg)
    lp = p.create_profile(name, fd)
    assert lp
    # get func
    func_name = "Add"
    func = fd.get_func_by_name(func_name)
    fp = lp.get_func_prof(func.get_index())
    fp.count(1.0)
    fp.count(2.0)
    fp.count(3.0)
    return p, lp, fp
Ejemplo n.º 41
0
def libcore_stub_gen_fake_log_profile_test(caplog):
    caplog.set_level(logging.INFO)
    name = "vamostest.library"
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    log_missing = logging.getLogger("missing")
    log_valid = logging.getLogger("valid")
    profile = LibProfileData(fd)
    # create stub
    gen = LibStubGen(log_missing=log_missing, log_valid=log_valid)
    stub = gen.gen_fake_stub(name, fd, ctx, profile)
    _check_stub(stub)
    # call func
    stub.PrintHello()
    stub.Dummy()
    stub.Swap()
    _check_log_fake(caplog)
    _check_profile(fd, profile)
Ejemplo n.º 42
0
def libcore_stub_gen_exc_default_test(capsys):
    name = 'vamostest.library'
    impl = VamosTestLibrary()
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    # create stub
    gen = LibStubGen()
    stub = gen.gen_stub(name, impl, fd, ctx)
    _check_stub(stub)
    # call func
    ctx.mem.w_cstr(0, 'RuntimeError')
    with pytest.raises(RuntimeError):
        stub.RaiseError()
    # check for traceback
    captured = capsys.readouterr()
    lines = captured.err.strip().split('\n')
    assert lines[0] == 'Traceback (most recent call last):'
    assert lines[-1] == 'RuntimeError: VamosTest'
Ejemplo n.º 43
0
def _create_stub(do_profile=False, do_log=False):
    name = 'vamostest.library'
    impl = VamosTestLibrary()
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    if do_profile:
        profile = LibProfileData(fd)
    else:
        profile = None
    if do_log:
        log_missing = logging.getLogger('missing')
        log_valid = logging.getLogger('valid')
    else:
        log_missing = None
        log_valid = None
    # create stub
    gen = LibStubGen(log_missing=log_missing, log_valid=log_valid)
    stub = gen.gen_stub(name, impl, fd, ctx, profile)
    return stub
Ejemplo n.º 44
0
def libcore_stub_gen_log_profile_test(caplog):
    caplog.set_level(logging.INFO)
    name = 'vamostest.library'
    impl = VamosTestLibrary()
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    log_missing = logging.getLogger('missing')
    log_valid = logging.getLogger('valid')
    profile = LibProfileData(fd)
    # create stub
    gen = LibStubGen(log_missing=log_missing, log_valid=log_valid)
    stub = gen.gen_stub(name, impl, fd, ctx, profile)
    _check_stub(stub)
    # call func
    stub.PrintHello()
    stub.Dummy()
    stub.Swap()
    _check_log(caplog)
    _check_profile(fd, profile)
Ejemplo n.º 45
0
def libcore_stub_gen_multi_arg(capsys):
    caplog.set_level(logging.INFO)
    name = 'vamostest.library'
    impl = VamosTestLibrary()
    fd = read_lib_fd(name)
    ctx = _create_ctx()
    log_missing = logging.getLogger('missing')
    log_valid = logging.getLogger('valid')
    profile = LibProfile(name, fd)
    # create stub
    gen = LibStubGen(log_missing=log_missing, log_valid=log_valid)
    stub = gen.gen_stub(name, impl, fd, ctx, profile)
    _check_stub(stub)
    # call func
    stub.PrintHello(1, 2, a=3)
    stub.Dummy(3, b='hello')
    stub.Swap('hugo', None, c=3)
    _check_log(caplog)
    _check_profile(fd, profile)
Ejemplo n.º 46
0
def _create_stub(do_profile=False, do_log=False):
  name = 'vamostest.library'
  impl = VamosTestLibrary()
  fd = read_lib_fd(name)
  scanner = LibImplScanner()
  scan = scanner.scan(name, impl, fd)
  ctx = _create_ctx()
  if do_profile:
    profile = LibProfileData(fd)
  else:
    profile = None
  if do_log:
    log_missing = logging.getLogger('missing')
    log_valid = logging.getLogger('valid')
  else:
    log_missing = None
    log_valid = None
  # create stub
  gen = LibStubGen(log_missing=log_missing, log_valid=log_valid)
  stub = gen.gen_stub(scan, ctx, profile)
  return stub
Ejemplo n.º 47
0
def libcore_patch_multi_trap_test(capsys):
  name = 'vamostest.library'
  impl = VamosTestLibrary()
  fd = read_lib_fd(name)
  machine = MockMachine()
  ctx = LibCtx(machine)
  # create stub
  scanner = LibImplScanner()
  scan = scanner.scan(name, impl, fd)
  gen = LibStubGen()
  stub = gen.gen_stub(scan, ctx)
  # now patcher
  alloc = MemoryAlloc(ctx.mem)
  traps = machine.get_traps()
  p = LibPatcherMultiTrap(alloc, traps, stub)
  base_addr = 0x100
  p.patch_jump_table(base_addr)
  # lookup trap for function
  func = fd.get_func_by_name('PrintHello')
  bias = func.get_bias()
  func_addr = base_addr - bias
  # check that jump table has jump + addr
  op = ctx.mem.r16(func_addr)
  assert op == op_jmp
  trap_addr = ctx.mem.r32(func_addr + 2)
  # check jump target is trap
  op = ctx.mem.r16(trap_addr)
  assert op & 0xf000 == 0xa000
  # trigger trap
  traps.trigger(op)
  captured = capsys.readouterr()
  assert captured.out.strip().split('\n') == [
      'VamosTest: PrintHello()'
  ]
  # remove traps
  p.cleanup()
  assert traps.get_num_traps() == 0
  assert alloc.is_all_free()
Ejemplo n.º 48
0
def _create_scan():
  name = 'vamostest.library'
  impl = VamosTestLibrary()
  fd = read_lib_fd(name)
  scanner = LibImplScanner()
  return scanner.scan(name, impl, fd, True)