示例#1
0
def load_server(ctx: ExecutionContext, serv_id: str) -> ServerDef:
    compare, succ = ServerDef.load_exists(ctx.singleton.db, serv_id)

    ok, (serv, *_) = ctx.singleton.db.transaction(compare=[compare], success=[succ], failure=[])

    if not ok:
        raise ExecutionError(f'Can not load server `{serv_id}`')

    server = ServerDef.deserialize_range(serv)

    if server is None:
        raise ExecutionError(f'Can not load server `{serv_id}`')

    return server
示例#2
0
    def fn(self, ctx: ExecutionContext) -> FollowUp:
        if len(ctx.stack) == 0:
            raise ExecutionError(
                f'`{ctx.thread.id}:{ctx.thread.ip}` Stack underflow')

        push_pop(ctx.stack[0], ctx)

        return FollowUp.new([ctx.thread.update(ip=ctx.nip)],
                            update_stacks=[ctx.stack[0]])
示例#3
0
    def fn(self, ctx: ExecutionContext) -> FollowUp:
        if len(ctx.args) != 1:
            raise ExecutionError(
                f'(j) `{ctx.thread.id}:{ctx.thread.ip}` too many args: {ctx.args}'
            )

        jmp = ctx.resolve_arg(0)

        return FollowUp.new([ctx.thread.update(ip=jmp)])
示例#4
0
    def fn(self, ctx: ExecutionContext, code: OpArg[str], ret: RefOpArg[Any],
           *args: OpArg[Any]):
        item = compile(code.get(), '<string>', mode='eval')

        try:
            res = eval(item, {'x': WrappedArgs(ctx, [ret] + list(args))})
        except ExecutionError:
            raise
        except Exception as e:
            raise ExecutionError("Failed to exr") from e

        ret.set(res)
示例#5
0
    def fn(self, ctx: ExecutionContext) -> FollowUp:
        # so a stack frame needs to load the parent stack frame
        # then update it

        # rtn = ctx.stack_get('psp')
        #
        # x = StackFrame.load(ctx.singleton.db, rtn)

        if len(ctx.stack) == 0:
            raise ExecutionError(
                f'`{ctx.thread.id}:{ctx.thread.ip}` Stack underflow')

        if len(ctx.args) and len(ctx.stack) < 2:
            raise ExecutionError(
                f'`{ctx.thread.id}:{ctx.thread.ip}` Stack underflow')
        elif len(ctx.args):
            push_pop(ctx.stack[-2], ctx)

        return FollowUp.new(
            [ctx.thread.update(ip=ctx.nip, sp=ctx.thread.sp[1:])],
            update_stacks=[ctx.stack[1]])
示例#6
0
    def fn(self, ctx: ExecutionContext) -> FollowUp:
        if len(ctx.args) != 2:
            raise ExecutionError(
                f'(fork) `{ctx.thread.id}:{ctx.thread.ip}` not enough args: {ctx.args}'
            )

        fork_id = ctx.resolve_arg(0)
        jmp = ctx.resolve_arg(1)

        return FollowUp.new([
            ctx.thread.update(ip=ctx.nip),
            ThreadContext.new(fork_id, jmp, ctx.thread.sp),
        ])
示例#7
0
    def fn(self, ctx: ExecutionContext) -> FollowUp:
        # if we had access to the meta-info, then we could easily remove it ourselves

        if len(ctx.stack) == 0:
            raise ExecutionError(
                f'`{ctx.thread.id}:{ctx.thread.ip}` Stack underflow')

        if len(ctx.args) and len(ctx.stack) < 2:
            raise ExecutionError(
                f'`{ctx.thread.id}:{ctx.thread.ip}` Stack underflow')
        elif len(ctx.args):
            push_pop(ctx.stack[-2], ctx)

        upd = []

        if len(ctx.stack) > 1:
            upd = [ctx.stack[1]]

        return FollowUp.new(
            [ctx.thread.update(ip=ctx.nip, sp=ctx.thread.sp[1:])],
            update_stacks=upd,
            delete_stacks=[ctx.stack[0]])
示例#8
0
    def fn(self, ctx: ExecutionContext):
        frz_id = ctx.resolve_arg(0)
        cmp, succ = FrozenThreadContext.load_exists(ctx.singleton.db, frz_id)

        ok, items = ctx.singleton.db.transaction(compare=[cmp], success=[succ])

        if not ok:
            raise ExecutionError(f'Could not find `{frz_id}`')

        frz = FrozenThreadContext.deserialize_range(items)

        ctx.thread.update(ip=frz.ctx.ip, sp=ctx.thread.sp + frz.ctx.sp)

        return FollowUp.new([ctx])
示例#9
0
    def fn(self, ctx: ExecutionContext, level: OpArg[str], format: OpArg[str],
           *args: OpArg[Any], **kwargs: OpArg[Any]):
        logger = logging.getLogger('OPCODE')

        try:
            level_str = level.get().upper()
            format_str = format.get()
            level_int = logging._nameToLevel[level_str]
        except KeyError:
            raise ExecutionError(f'Incorrect level: {level_str}')
        else:
            logger.log(
                level_int,
                format_str.format(*[x.get() for x in args],
                                  **{k: v.get()
                                     for k, v in kwargs.items()}))
示例#10
0
def push_pop(csf: StackFrame, ctx: ExecutionContext, args=None):
    if args is None:
        args = ctx.args

    for arg in args:
        if isinstance(arg, Identifier):
            csf.set(arg.name, ctx.stack_get(arg.name, arg.level))
        elif isinstance(arg, Constant):
            raise ExecutionError(
                f'(push) `{ctx.thread.id}:{ctx.thread.ip}` constant push requires an identifier'
            )
        elif isinstance(arg, Map):
            if isinstance(arg.to, Constant):
                val = arg.to.value
            elif isinstance(arg.to, Identifier):
                val = ctx.stack_get(arg.to.name, arg.to.level)
            else:
                raise NotImplementedError(f'{repr(arg)}')

            csf.set(arg.identifier.name, val)
        else:
            raise NotImplementedError(f'{repr(arg)}')