Пример #1
0
def __seek__(
    cpu,
    mem,
    kernel,
    func_signature=FunctionType(IntegerType(SysCallLocation), (
        AbstractDeclarator(IntegerType(SysCallLocation), SysCallLocation),
        AbstractDeclarator(IntegerType(SysCallLocation), SysCallLocation),
        AbstractDeclarator(IntegerType(SysCallLocation), SysCallLocation),
    ), SysCallLocation)):
    # int __seek__(int file_id, int offset, int whence);
    values = args(func_signature, cpu, mem)

    file_id, offset, whence = values
    return_value = Integer(-1, LocationNotSet)
    try:
        file_id = kernel.opened_files[file_id]
        file_id.seek(offset, whence)
        return_value = Integer(0, LocationNotSet)
    except KeyError as _:
        logger.warning(
            'trying to fseek on non-opened file_id {f}'.format(f=file_id))
    except Exception as ex:
        logger.warning('failed to fseek on file {f}, error: {m}'.format(
            f=getattr(file_id, 'name', file_id), m=ex))
    __return__(return_value, cpu, mem, kernel)
Пример #2
0
def __close__(
    cpu,
    mem,
    kernel,
    func_signature=FunctionType(
        IntegerType(SysCallLocation),
        (AbstractDeclarator(IntegerType(SysCallLocation), SysCallLocation), ),
        SysCallLocation)):
    # int __close__(int);  // returns 0 on success or -1 on failure
    # // returns 0 on success or -1 on failure.
    values = args(func_signature, cpu, mem)

    file_obj = file_id = next(values)
    return_value = Integer(-1, SysCallLocation)

    try:
        if file_id not in std_files:
            file_obj = kernel.opened_files.pop(file_id)
            file_obj.flush()
            file_obj.close()
        return_value = Integer(0, SysCallLocation)
    except KeyError as _:
        logger.warning(
            'trying to close a non-opened file_id {f}'.format(f=file_id))
    except Exception as ex:
        logger.warning('failed to close file {f}, error: {m}'.format(f=getattr(
            file_obj, 'name', file_obj),
                                                                     m=ex))
    __return__(return_value, cpu, mem, kernel)
Пример #3
0
def __write__(
    cpu,
    mem,
    kernel,
    func_signature=FunctionType(
        IntegerType(SysCallLocation),
        (AbstractDeclarator(IntegerType(SysCallLocation), SysCallLocation),
         AbstractDeclarator(
             PointerType(CharType(SysCallLocation), SysCallLocation),
             SysCallLocation),
         AbstractDeclarator(
             LongType(LongType(IntegerType(SysCallLocation), SysCallLocation),
                      SysCallLocation,
                      unsigned=True), SysCallLocation)), SysCallLocation)):
    # int  __write__(int file_id, char *buffer, unsigned long long number_of_bytes);
    # // returns 0 on success or -1 on failure.
    values = args(func_signature, cpu, mem)
    file_id, buffer_ptr, number_of_bytes = values
    return_value = Integer(-1, LocationNotSet)

    values = ''.join(imap(chr, __buffer__(buffer_ptr, number_of_bytes, mem)))
    try:
        file_id = kernel.opened_files[int(file_id)]
        file_id.write(values)
        return_value = Integer(0, LocationNotSet)
    except KeyError as _:
        logger.warning(
            'trying to write to a non-opened file_id {f}'.format(f=file_id))
    except Exception as ex:
        logger.warning('failed to write to file {f}, error: {m}'.format(
            f=getattr(file_id, 'name', file_id), m=ex))
        return_value = Integer(-1, LocationNotSet)
    __return__(return_value, cpu, mem, kernel)
Пример #4
0
def __open__(cpu,
             mem,
             kernel,
             func_signature=FunctionType(IntegerType(SysCallLocation), (
                 AbstractDeclarator(
                     PointerType(CharType(SysCallLocation), SysCallLocation),
                     SysCallLocation),
                 AbstractDeclarator(
                     PointerType(CharType(SysCallLocation), SysCallLocation),
                     SysCallLocation),
             ), SysCallLocation)):
    # int __open__(const char * file_path, const char *mode);  // returns file_id on success or -1 of failure.
    values = args(func_signature, cpu, mem)

    file_id = Integer(-1, LocationNotSet)
    file_path_ptr, file_mode_ptr = values
    file_mode = __str__(file_mode_ptr, mem)
    if file_path_ptr in std_files:
        file_id = file_path_ptr
    else:
        file_name = __str__(file_path_ptr, mem)
        try:
            file_obj = open(file_name, file_mode)
            file_id = file_obj.fileno()
            kernel.opened_files[file_id] = file_obj
        except Exception as ex:
            logger.warning('failed to open file {f}, error: {m}'.format(
                f=file_name, m=ex))
    __return__(file_id, cpu, mem, kernel)
Пример #5
0
def __tell__(
    cpu,
    mem,
    kernel,
    func_signature=FunctionType(
        IntegerType(SysCallLocation),
        (AbstractDeclarator(IntegerType(SysCallLocation), SysCallLocation), ),
        SysCallLocation)):
    # int __tell__(int);
    values = args(func_signature, cpu, mem)
    return_value = Integer(-1, LocationNotSet)
    file_id, = values

    try:
        file_id = kernel.opened_files[file_id]
        return_value = Integer(file_id.tell(), LocationNotSet)
    except KeyError as _:
        logger.warning(
            'trying to ftell on a non-opened file_id {f}'.format(f=file_id))
    except Exception as ex:
        logger.warning('failed to ftell on file {f}, error: {m}'.format(
            f=getattr(file_id, 'name', file_id), m=ex))
    __return__(return_value, cpu, mem, kernel)
Пример #6
0
def __read__(
    cpu,
    mem,
    kernel,
    func_signature=FunctionType(
        IntegerType(SysCallLocation),
        (AbstractDeclarator(IntegerType(SysCallLocation), SysCallLocation),
         AbstractDeclarator(
             PointerType(CharType(SysCallLocation), SysCallLocation),
             SysCallLocation),
         AbstractDeclarator(
             LongType(
                 LongType(IntegerType(SysCallLocation),
                          SysCallLocation,
                          unsigned=True), SysCallLocation), SysCallLocation)),
        SysCallLocation)):
    # int __read__(int file_id, char *dest, unsigned long long number_of_bytes);
    # // returns the number of elements read on success or -1 on failure
    values = args(func_signature, cpu, mem)

    file_id, dest_ptr, number_of_bytes = values
    return_value = Integer(-1, LocationNotSet)

    try:
        file_id = kernel.opened_files[file_id]
        values = file_id.read(number_of_bytes)
        for addr, value in izip(xrange(dest_ptr, dest_ptr + len(values)),
                                imap(ord, values)):
            mem[addr] = Byte(value, LocationNotSet)
        return_value = Integer(len(values), LocationNotSet)
    except KeyError as _:
        logger.warning(
            'trying to read from a non-opened file_id {f}'.format(f=file_id))
    except Exception as ex:
        logger.warning('failed to read from file {f}, error: {m}'.format(
            f=getattr(file_id, 'name', file_id), m=ex))
    __return__(return_value, cpu, mem, kernel)
Пример #7
0
def goto_statement(stmnt, symbol_table):
    labels, gotos, stack = imap(symbol_table.__getitem__, ('__ LABELS __', '__ GOTOS __', '__ stack __'))

    if stmnt.label in labels:  # Label previously defined either in current or previous scope ... nothing to do ...
        instr, stack_pointer = labels[stmnt.label]
        instrs = chain(
            update_stack(stack_pointer, stack.stack_pointer, loc(stmnt)),
            relative_jump(Offset(instr, loc(stmnt)), loc(stmnt))
        )
    else:
        # Label has yet to be defined ...
        # Basically we need to update the relative jump and the amount to which we need to update the stack ...
        # TODO: find/use a better approach, we can't use Address since it'll be translated ...
        alloc_instr = Allocate(loc(stmnt), Offset(Integer(0), loc(stmnt)))
        jump_instr = RelativeJump(loc(stmnt), Offset(None, loc(stmnt)))

        gotos[stmnt.label].append((alloc_instr, jump_instr, stack.stack_pointer))
        instrs = (alloc_instr, jump_instr)

    return instrs