Example #1
0
 def open_full(engine: Engine):
     try:
         file = engine.read_string()
         flags = map_open_flags(engine.read_int(), Flags, os_)
         mode = map_file_mode(engine.read_int(), Flags, os_)
         engine.write_int(os_.open(file, flags, mode))
     except OSError as e:
         engine.write_int(-1)
         engine.write_int(map_errno(int(str(e)), errno, Flags))
Example #2
0
 def read_full(engine: Engine):
     try:
         fd = engine.read_int()
         len_ = engine.read_int()
         buf = os_.read(fd, len_)
         engine.write_int(0)
         engine.write_memory(buf)
     except OSError as e:
         engine.write_int(map_errno(int(str(e)), errno, Flags))
Example #3
0
 def lseek(engine: Engine):
     try:
         file = engine.read_int()
         ptr = engine.read_int()
         dir_ = engine.read_int()
         engine.write_int(os_.lseek(file, ptr, dir_))
     except OSError as e:
         engine.write_int(-1)
         engine.write_int(map_errno(int(str(e)), errno, Flags))
Example #4
0
 def open_unchecked(engine: Engine):
     file = engine.read_string()
     try:
         flags = map_open_flags(engine.read_int(), Flags, os_)
         mode = map_file_mode(engine.read_int(), Flags, os_)
         target_fd = engine.read_int()
         unchecked_map[target_fd] = os_.open(file, flags, mode)
     except OSError as e:
         print("Error opening file {} : {}".format(file, e),
               file=sys.stderr)
Example #5
0
    def read_buffered(engine: Engine):
        try:
            fd = engine.read_int()
            len_ = engine.read_int()

            cur = os_.lseek(fd, 0, os_.SEEK_CUR)
            end = os_.lseek(fd, 0, os_.SEEK_END)

            available = end - cur
            read_len = min(available, len_)

            buf = os_.read(fd, read_len)
            engine.write_int(0)
            engine.write_memory(buf)
        except OSError as e:
            engine.write_int(map_errno(int(str(e)), errno, Flags))
Example #6
0
 def isatty(engine: Engine):
     try:
         fd = engine.read_int()
         isatty_ = os_.isatty(fd)
         engine.write_int(0)
         engine.write_int(isatty_)
     except OSError as e:
         engine.write_int(map_errno(int(str(e)), errno, Flags))
Example #7
0
 def fstat(engine: Engine):
     try:
         fd = engine.read_int()
         st = os_.fstat(fd)
         engine.write_int(0)
         write_stat(engine, st)
     except OSError as e:
         engine.write_int(map_errno(int(str(e)), errno, Flags))
Example #8
0
 def write_full(engine: Engine):
     try:
         fd = engine.read_int()
         dt = engine.read_memory()
         engine.write_int(os_.write(fd, dt))
     except OSError as e:
         engine.write_int(-1)
         engine.write_int(map_errno(int(str(e)), errno, Flags))
Example #9
0
 def close_full(engine: Engine):
     try:
         fd = engine.read_int()
         os_.close(fd)
         engine.write_int(0)
     except OSError as e:
         engine.write_int(-1)
         engine.write_int(map_errno(int(str(e)), errno, Flags))
Example #10
0
 def close_unchecked(engine: Engine):
     target_fd = engine.read_int()
     try:
         os_.close(unchecked_map[target_fd])
     except OSError as e:
         print("Error closing file {} : {}".format(target_fd, e),
               file=sys.stderr)
     except KeyError:
         pass
Example #11
0
    def invoke(self, engine: Engine, macro_expansion: MacroExpansion):
        file = macro_expansion.file
        line = macro_expansion.line

        type_and_level = engine.read_byte()
        args = macro_expansion.args[3:]

        level = level_t[(type_and_level[0] & 0b11100000) >> 5]
        type_ = type_t[type_and_level[0] & 0b11111]

        cond_or_length = engine.read_int()
        condition = bool(cond_or_length)
        func = getattr(self.reporter, type_)

        if type_ == "checkpoint":
            func(file, line)
        elif type_ == "log":
            func(file, line, args[0])
        elif type_ in ["message", "plain"]:
            func(file, line, level, condition, args[0])
        elif type_ in ["equal", "not_equal", "ge", "le", "greater", "lesser"]:
            func(file, line, level, condition, args[0], args[1])
        elif type_ in ["close", "close_relative"]:
            func(file, line, level, condition, args[0], args[1], args[2])
        elif type_ in ["close", "close_relative"]:
            func(file, line, level, condition, args[0], args[1], args[2])
        elif type_ == "report":
            func(file, line, condition)
        elif type_ == "critical":
            func(file, line, level)
        elif type_ == "call":
            name = args[1][3:-8] if args[1] else args[0]
            func(file, line, level, condition, name)
        elif type_ == 'loop':
            func(file, line, level)
        elif type_ == 'ranged':
            func(file, line, level, cond_or_length, args)
        elif type_ == 'predicate':
            args_a = [arg.strip() for arg in str(args[1][2:-2]).split(',')
                      ] if args else None
            func(file, line, level, cond_or_length, args[0], args_a)
        else:
            raise Exception("Unknown check type {}".format(type_))
Example #12
0
p = Popen(args.binary,
          stdin=PIPE,
          stdout=PIPE,
          stderr=sys.stdout.buffer,
          close_fds=True)

engine = Engine(input=p.stdout, output=p.stdin, serial_info=serial_info)

assert engine.init_marker.file.endswith('write.c')
assert engine.init_marker.line == 25

engine.write_byte(b'0')
assert engine.read_byte() == b'9'

engine.write_int(22)
assert engine.read_int() == 44

assert engine.write_string("str") == 3
assert engine.read_string() == "tr"

engine.write_int(11)
assert engine.read_int() == 33

assert engine.write_string("overflow") == 6
assert engine.read_string() == "erfl"

engine.write_int(4)
assert engine.read_int() == 16

assert engine.write_memory(b'\x00\x01\x02\x03\x04') == 4
assert engine.read_memory() == b'\x03\x02\x01\x00'
Example #13
0
 def write_unchecked(engine: Engine):
     fd = engine.read_int()
     dt = engine.read_memory()
     os_.write(fd, dt)
Example #14
0
 def invoke(self, engine: Engine, macro_expansion: MacroExpansion):
     l = engine.read_int()
     global argv_
     argv_ = [engine.read_string() for i in range(l)]
Example #15
0
                    nargs='+',
                    help="Include folders for the preprocessor",
                    default=[])
parser.add_argument('-D',
                    '--define',
                    nargs='+',
                    help="Defines for the preprocessor",
                    default=[])

args = parser.parse_args()

serial_info = generate(args.binary, args.define, args.include)

p = Popen(args.binary, stdin=PIPE, stdout=PIPE, close_fds=True)
engine = Engine(input=p.stdout, output=p.stdin, serial_info=serial_info)

assert engine.init_marker.file.endswith('read.c')
assert engine.init_marker.line == 22

assert engine.read_byte() == b'a'
assert engine.read_int() == 42
assert engine.read_string() == "test-string"

main_ptr = engine.read_int() - engine.base_pointer
assert [sym.address for sym in serial_info.symbols
        if sym.name == 'main'][0] == main_ptr

assert int.from_bytes(engine.read_memory(), engine.endianness) == 42
assert engine.read_int() == 1234

assert engine.run() == 42