static void ktap_init_arguments(ktap_state *ks, int argc, char **argv) { const ktap_value *gt = kp_table_getint(hvalue(&G(ks)->registry), KTAP_RIDX_GLOBALS); ktap_table *global_tbl = hvalue(gt); ktap_table *arg_tbl = kp_table_new(ks); ktap_value arg_tblval; ktap_value arg_tsval; int i; setsvalue(&arg_tsval, kp_tstring_new(ks, "arg")); sethvalue(&arg_tblval, arg_tbl); kp_table_setvalue(ks, global_tbl, &arg_tsval, &arg_tblval); if (!argc) return; kp_table_resize(ks, arg_tbl, 100, 100); for (i = 0; i < argc; i++) { int res; ktap_value val; if (!kstrtoint(argv[i], 10, &res)) { setnvalue(&val, res); } else setsvalue(&val, kp_tstring_new(ks, argv[i])); kp_table_setint(ks, arg_tbl, i, &val); } }
/* function for register library */ void kp_register_lib(ktap_state *ks, const char *libname, const ktap_Reg *funcs) { int i; ktap_table *target_tbl; const ktap_value *gt = kp_table_getint(hvalue(&G(ks)->registry), KTAP_RIDX_GLOBALS); /* lib is null when register baselib function */ if (libname == NULL) target_tbl = hvalue(gt); else { ktap_value key, val; target_tbl = kp_table_new(ks); kp_table_resize(ks, target_tbl, 0, sizeof(*funcs) / sizeof(ktap_Reg)); setsvalue(&key, kp_tstring_new(ks, libname)); sethvalue(&val, target_tbl); kp_table_setvalue(ks, hvalue(gt), &key, &val); } for (i = 0; funcs[i].name != NULL; i++) { ktap_value func_name, cl; setsvalue(&func_name, kp_tstring_new(ks, funcs[i].name)); setfvalue(&cl, funcs[i].func); kp_table_setvalue(ks, target_tbl, &func_name, &cl); cfunction_cache_add(ks, &cl); } }
static int ktap_lib_execname(ktap_State *ks) { Tstring *ts = kp_tstring_new(ks, current->comm); setsvalue(ks->top, ts); incr_top(ks); return 1; }
int luaK_stringK(FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); return addk(fs, &o, &o); }
/* support other architecture pt_regs showing */ static void event_regstr(ktap_state *ks, struct ktap_event *e, StkId ra) { struct pt_regs *regs = e->regs; char str[256] = {0}; #if defined(CONFIG_X86_32) snprintf(str, sizeof(str), "{ax: 0x%lx, orig_ax: 0x%lx, bx: 0x%lx, cx: 0x%lx, dx: 0x%lx, " "si: 0x%lx, di: 0x%lx, bp: 0x%lx, ds: 0x%lx, es: 0x%lx, fs: 0x%lx, " "gs: 0x%lx, ip: 0x%lx, cs: 0x%lx, flags: 0x%lx, sp: 0x%lx, ss: 0x%lx}\n", regs->ax, regs->orig_ax, regs->bx, regs->cx, regs->dx, regs->si, regs->di, regs->bp, regs->ds, regs->es, regs->fs, regs->gs, regs->ip, regs->cs, regs->flags, regs->sp, regs->ss); #elif defined(CONFIG_X86_64) /* x86_64 pt_regs doesn't have ds, es, fs or gs. */ snprintf(str, sizeof(str), "{ax: 0x%lx, orig_ax: 0x%lx, bx: 0x%lx, cx: 0x%lx, dx: 0x%lx, " "si: 0x%lx, di: 0x%lx, r8: 0x%lx, r9: 0x%lx, r10: 0x%lx, r11: 0x%lx, " "r12: 0x%lx, r13: 0x%lx, r14: 0x%lx, r15: 0x%lx, bp: 0x%lx, ip: 0x%lx, " "cs: 0x%lx, flags: 0x%lx, sp: 0x%lx, ss: 0x%lx}\n", regs->ax, regs->orig_ax, regs->bx, regs->cx, regs->dx, regs->si, regs->di, regs->r8, regs->r9, regs->r10, regs->r11, regs->r12, regs->r13, regs->r14, regs->r15, regs->bp, regs->ip, regs->cs, regs->flags, regs->sp, regs->ss); #endif setsvalue(ra, kp_tstring_new_local(ks, str)); }
int codegen_stringK(FuncState *fs, Tstring *s) { Tvalue o; setsvalue(&o, s); return addk(fs, &o, &o); }
int codegen_stringK(ktap_funcstate *fs, ktap_string *s) { ktap_value o; setsvalue(&o, s); return addk(fs, &o, &o); }
static int load_constants(struct load_state *S, ktap_proto *f) { int i,n; n = READ_INT(S); f->sizek = n; f->k = NEW_VECTOR(S, n * sizeof(ktap_value)); for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i=0; i < n; i++) { ktap_value *o = &f->k[i]; int t = READ_CHAR(S); switch (t) { case KTAP_TNIL: setnilvalue(o); break; case KTAP_TBOOLEAN: setbvalue(o, READ_CHAR(S)); break; case KTAP_TNUMBER: /* * todo: kernel not support fp, check double when * loading */ setnvalue(o, READ_NUMBER(S)); break; case KTAP_TSTRING: setsvalue(o, READ_STRING(S)); break; default: kp_error(S->ks, "ktap: load_constants: " "unknow ktap_value\n"); return -1; } } n = READ_INT(S); f->p = NEW_VECTOR(S, n * sizeof(ktap_proto)); f->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { f->p[i] = kp_newproto(S->ks); if (load_function(S, f->p[i])) return -1; } return 0; }
static void luaR_next_helper(lua_State *L, const luaR_entry *pentries, int pos, TValue *key, TValue *val) { setnilvalue(key); setnilvalue(val); if (pentries[pos].key.type != LUA_TNIL) { /* Found an entry */ if (pentries[pos].key.type == LUA_TSTRING) setsvalue(L, key, luaS_newro(L, pentries[pos].key.id.strkey)) else setnvalue(key, (lua_Number)pentries[pos].key.id.numkey) setobj2s(L, val, &pentries[pos].value); } }
int luaK_stringK (FuncState *fs, TString *s) { TValue o; #if LUA_REFCOUNT lua_State *L = fs->L; int ret; setsvalue2n(fs->L, &o, s); ret = addk(fs, &o, &o); setnilvalue(&o); return ret; #else setsvalue(fs->L, &o, s); return addk(fs, &o, &o); #endif /* LUA_REFCOUNT */ }
int luaK_numberK (FuncState *fs, lua_Number r) { int n; lua_State *L = fs->ls->L; TValue o; setnvalue(&o, r); if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */ /* use raw representation as key to avoid numeric problems */ setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r))); n = addk(fs, L->top - 1, &o); L->top--; } else n = addk(fs, &o, &o); /* regular case */ return n; }
static int ktap_lib_user_string(ktap_State *ks) { unsigned long addr = nvalue(GetArg(ks, 1)); char str[256] = {0}; int ret; pagefault_disable(); ret = __copy_from_user_inatomic((void *)str, (const void *)addr, 256); (void) &ret; /* Silence compiler warning. */ pagefault_enable(); str[255] = '\0'; setsvalue(ks->top, kp_tstring_new(ks, str)); incr_top(ks); return 1; }
int codegen_numberK(FuncState *fs, ktap_Number r) { int n; Tvalue o, s; setnvalue(&o, r); if (r == 0 || ktap_numisnan(NULL, r)) { /* handle -0 and NaN */ /* use raw representation as key to avoid numeric problems */ setsvalue(&s, ktapc_ts_newlstr((char *)&r, sizeof(r))); // incr_top(L); n = addk(fs, &s, &o); // L->top--; } else n = addk(fs, &o, &o); /* regular case */ return n; }
static int addk (FuncState *fs, TValue *k, TValue *v) { lua_State *L = fs->L; TValue *idx = luaH_set(L, fs->h, k); #ifdef LUA_TINT /* Note: Integer-valued LUA_TNUMBER's are handled as in unpatched Lua (below) */ if (ttype(idx)==LUA_TINT) { int i; # ifdef LNUM_INT64 lua_assert( (int)ivalue(idx) == ivalue(idx) ); /* make sure no data is lost in the casting */ # endif i= (int)ivalue(idx); lua_assert(luaO_rawequalObj(&fs->f->k[i], v)); return i; } else if (ttype(idx)==LUA_TNUMBER) { #else if (ttisnumber(idx)) { #endif int i= cast_int(nvalue_fast(idx)); lua_assert(luaO_rawequalObj(&fs->f->k[i], v)); return i; } else { /* constant not found; create a new entry */ Proto *f = fs->f; int oldsize = f->sizek; setivalue(idx, fs->nk); luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, MAXARG_Bx, "constant table overflow"); while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[fs->nk], v); luaC_barrier(L, f, v); return fs->nk++; } } int luaK_stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->L, &o, s); return addk(fs, &o, &o); }
static void ktap_concat(ktap_state *ks, int start, int end) { int i, len = 0; StkId top = ks->ci->u.l.base; ktap_string *ts; char *ptr, *buffer; for (i = start; i <= end; i++) { if (!ttisstring(top + i)) { kp_error(ks, "cannot concat non-string\n"); setnilvalue(top + start); return; } len += rawtsvalue(top + i)->tsv.len; } if (len >= KTAP_PERCPU_BUFFER_SIZE) { kp_error(ks, "Error: too long string concatenation\n"); return; } preempt_disable_notrace(); buffer = kp_percpu_data(KTAP_PERCPU_DATA_BUFFER); ptr = buffer; for (i = start; i <= end; i++) { int len = rawtsvalue(top + i)->tsv.len; strncpy(ptr, svalue(top + i), len); ptr += len; } ts = kp_tstring_newlstr(ks, buffer, len); setsvalue(top + start, ts); preempt_enable_notrace(); }
void MCExecPoint::setstaticbytes(const void *p_bytes, uindex_t p_length) { setsvalue(MCString((const char *)p_bytes, p_length)); }
void MCExecPoint::setstaticcstring(const char *p_string) { setsvalue(p_string); }
/* ** Add a string to list of constants and return its index. */ int luaK_stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); return addk(fs, &o, &o); /* use string itself as key */ }
static void ktap_execute(ktap_state *ks) { int exec_count = 0; ktap_callinfo *ci; ktap_lclosure *cl; ktap_value *k; unsigned int instr, opcode; StkId base; /* stack pointer */ StkId ra; /* register pointer */ int res, nresults; /* temp varible */ ci = ks->ci; newframe: cl = CLVALUE(ci->func); k = cl->p->k; base = ci->u.l.base; mainloop: /* main loop of interpreter */ /* dead loop detaction */ if (exec_count++ == 10000) { if (G(ks)->mainthread != ks) { kp_error(ks, "non-mainthread executing too much, " "please try to enlarge execution limit\n"); return; } cond_resched(); if (signal_pending(current)) { flush_signals(current); return; } exec_count = 0; } instr = *(ci->u.l.savedpc++); opcode = GET_OPCODE(instr); /* ra is target register */ ra = RA(instr); switch (opcode) { case OP_MOVE: setobj(ra, base + GETARG_B(instr)); break; case OP_LOADK: setobj(ra, k + GETARG_Bx(instr)); break; case OP_LOADKX: setobj(ra, k + GETARG_Ax(*ci->u.l.savedpc++)); break; case OP_LOADBOOL: setbvalue(ra, GETARG_B(instr)); if (GETARG_C(instr)) ci->u.l.savedpc++; break; case OP_LOADNIL: { int b = GETARG_B(instr); do { setnilvalue(ra++); } while (b--); break; } case OP_GETUPVAL: { int b = GETARG_B(instr); setobj(ra, cl->upvals[b]->v); break; } case OP_GETTABUP: { int b = GETARG_B(instr); gettable(ks, cl->upvals[b]->v, RKC(instr), ra); base = ci->u.l.base; break; } case OP_GETTABLE: gettable(ks, RB(instr), RKC(instr), ra); base = ci->u.l.base; break; case OP_SETTABUP: { int a = GETARG_A(instr); settable(ks, cl->upvals[a]->v, RKB(instr), RKC(instr)); base = ci->u.l.base; break; } case OP_SETUPVAL: { ktap_upval *uv = cl->upvals[GETARG_B(instr)]; setobj(uv->v, ra); break; } case OP_SETTABLE: settable(ks, ra, RKB(instr), RKC(instr)); base = ci->u.l.base; break; case OP_NEWTABLE: { int b = GETARG_B(instr); int c = GETARG_C(instr); ktap_table *t = kp_table_new(ks); sethvalue(ra, t); if (b != 0 || c != 0) kp_table_resize(ks, t, fb2int(b), fb2int(c)); break; } case OP_SELF: { StkId rb = RB(instr); setobj(ra+1, rb); gettable(ks, rb, RKC(instr), ra); base = ci->u.l.base; break; } case OP_ADD: arith_op(ks, NUMADD); break; case OP_SUB: arith_op(ks, NUMSUB); break; case OP_MUL: arith_op(ks, NUMMUL); break; case OP_DIV: /* divide 0 checking */ if (!nvalue(RKC(instr))) { kp_error(ks, "divide 0 arith operation\n"); return; } arith_op(ks, NUMDIV); break; case OP_MOD: /* divide 0 checking */ if (!nvalue(RKC(instr))) { kp_error(ks, "mod 0 arith operation\n"); return; } arith_op(ks, NUMMOD); break; case OP_POW: kp_error(ks, "ktap don't support pow arith in kernel\n"); return; case OP_UNM: { ktap_value *rb = RB(instr); if (ttisnumber(rb)) { ktap_number nb = nvalue(rb); setnvalue(ra, NUMUNM(nb)); } break; } case OP_NOT: res = isfalse(RB(instr)); setbvalue(ra, res); break; case OP_LEN: { int len = kp_objlen(ks, RB(instr)); if (len < 0) return; setnvalue(ra, len); break; } case OP_CONCAT: { int b = GETARG_B(instr); int c = GETARG_C(instr); ktap_concat(ks, b, c); break; } case OP_JMP: dojump(ci, instr, 0); break; case OP_EQ: { ktap_value *rb = RKB(instr); ktap_value *rc = RKC(instr); if ((int)equalobj(ks, rb, rc) != GETARG_A(instr)) ci->u.l.savedpc++; else donextjump(ci); base = ci->u.l.base; break; } case OP_LT: if (lessthan(ks, RKB(instr), RKC(instr)) != GETARG_A(instr)) ci->u.l.savedpc++; else donextjump(ci); base = ci->u.l.base; break; case OP_LE: if (lessequal(ks, RKB(instr), RKC(instr)) != GETARG_A(instr)) ci->u.l.savedpc++; else donextjump(ci); base = ci->u.l.base; break; case OP_TEST: if (GETARG_C(instr) ? isfalse(ra) : !isfalse(ra)) ci->u.l.savedpc++; else donextjump(ci); break; case OP_TESTSET: { ktap_value *rb = RB(instr); if (GETARG_C(instr) ? isfalse(rb) : !isfalse(rb)) ci->u.l.savedpc++; else { setobj(ra, rb); donextjump(ci); } break; } case OP_CALL: { int b = GETARG_B(instr); int ret; nresults = GETARG_C(instr) - 1; if (b != 0) ks->top = ra + b; ret = precall(ks, ra, nresults); if (ret) { /* C function */ if (nresults >= 0) ks->top = ci->top; base = ci->u.l.base; break; } else { /* ktap function */ ci = ks->ci; /* this flag is used for return time, see OP_RETURN */ ci->callstatus |= CIST_REENTRY; goto newframe; } break; } case OP_TAILCALL: { int b = GETARG_B(instr); if (b != 0) ks->top = ra+b; if (precall(ks, ra, -1)) /* C function? */ base = ci->u.l.base; else { int aux; /* * tail call: put called frame (n) in place of * caller one (o) */ ktap_callinfo *nci = ks->ci; /* called frame */ ktap_callinfo *oci = nci->prev; /* caller frame */ StkId nfunc = nci->func; /* called function */ StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ StkId lim = nci->u.l.base + CLVALUE(nfunc)->p->numparams; /* close all upvalues from previous call */ if (cl->p->sizep > 0) function_close(ks, oci->u.l.base); /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobj(ofunc + aux, nfunc + aux); /* correct base */ oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct top */ oci->top = ks->top = ofunc + (ks->top - nfunc); oci->u.l.savedpc = nci->u.l.savedpc; /* remove new frame */ ci = ks->ci = oci; /* restart ktap_execute over new ktap function */ goto newframe; } break; } case OP_RETURN: { int b = GETARG_B(instr); if (b != 0) ks->top = ra+b-1; if (cl->p->sizep > 0) function_close(ks, base); b = poscall(ks, ra); /* if it's called from external invocation, just return */ if (!(ci->callstatus & CIST_REENTRY)) return; ci = ks->ci; if (b) ks->top = ci->top; goto newframe; } case OP_FORLOOP: { ktap_number step = nvalue(ra+2); /* increment index */ ktap_number idx = NUMADD(nvalue(ra), step); ktap_number limit = nvalue(ra+1); if (NUMLT(0, step) ? NUMLE(idx, limit) : NUMLE(limit, idx)) { ci->u.l.savedpc += GETARG_sBx(instr); /* jump back */ setnvalue(ra, idx); /* update internal index... */ setnvalue(ra+3, idx); /* ...and external index */ } break; } case OP_FORPREP: { const ktap_value *init = ra; const ktap_value *plimit = ra + 1; const ktap_value *pstep = ra + 2; if (!ktap_tonumber(init, ra)) { kp_error(ks, KTAP_QL("for") " initial value must be a number\n"); return; } else if (!ktap_tonumber(plimit, ra + 1)) { kp_error(ks, KTAP_QL("for") " limit must be a number\n"); return; } else if (!ktap_tonumber(pstep, ra + 2)) { kp_error(ks, KTAP_QL("for") " step must be a number\n"); return; } setnvalue(ra, NUMSUB(nvalue(ra), nvalue(pstep))); ci->u.l.savedpc += GETARG_sBx(instr); break; } case OP_TFORCALL: { StkId cb = ra + 3; /* call base */ setobj(cb + 2, ra + 2); setobj(cb + 1, ra + 1); setobj(cb, ra); ks->top = cb + 3; /* func. + 2 args (state and index) */ kp_call(ks, cb, GETARG_C(instr)); base = ci->u.l.base; ks->top = ci->top; instr = *(ci->u.l.savedpc++); /* go to next instruction */ ra = RA(instr); } /*go through */ case OP_TFORLOOP: if (!ttisnil(ra + 1)) { /* continue loop? */ setobj(ra, ra + 1); /* save control variable */ ci->u.l.savedpc += GETARG_sBx(instr); /* jump back */ } break; case OP_SETLIST: { int n = GETARG_B(instr); int c = GETARG_C(instr); int last; ktap_table *h; if (n == 0) n = (int)(ks->top - ra) - 1; if (c == 0) c = GETARG_Ax(*ci->u.l.savedpc++); h = hvalue(ra); last = ((c - 1) * LFIELDS_PER_FLUSH) + n; if (last > h->sizearray) /* needs more space? */ kp_table_resizearray(ks, h, last); for (; n > 0; n--) { ktap_value *val = ra+n; kp_table_setint(ks, h, last--, val); } /* correct top (in case of previous open call) */ ks->top = ci->top; break; } case OP_CLOSURE: { /* need to use closure cache? (multithread contention issue)*/ ktap_proto *p = cl->p->p[GETARG_Bx(instr)]; pushclosure(ks, p, cl->upvals, base, ra); break; } case OP_VARARG: { int b = GETARG_B(instr) - 1; int j; int n = (int)(base - ci->func) - cl->p->numparams - 1; if (b < 0) { /* B == 0? */ b = n; /* get all var. arguments */ checkstack(ks, n); /* previous call may change the stack */ ra = RA(instr); ks->top = ra + n; } for (j = 0; j < b; j++) { if (j < n) { setobj(ra + j, base - n + j); } else setnilvalue(ra + j); } break; } case OP_EXTRAARG: return; case OP_EVENT: { struct ktap_event *e = ks->current_event; if (unlikely(!e)) { kp_error(ks, "invalid event context\n"); return; } setevalue(ra, e); break; } case OP_EVENTNAME: { struct ktap_event *e = ks->current_event; if (unlikely(!e)) { kp_error(ks, "invalid event context\n"); return; } setsvalue(ra, kp_tstring_new(ks, e->call->name)); break; } case OP_EVENTARG: if (unlikely(!ks->current_event)) { kp_error(ks, "invalid event context\n"); return; } kp_event_getarg(ks, ra, GETARG_B(instr)); break; case OP_LOAD_GLOBAL: { ktap_value *cfunc = cfunction_cache_get(ks, GETARG_C(instr)); setobj(ra, cfunc); } break; case OP_EXIT: return; } goto mainloop; }
int ICACHE_FLASH_ATTR luaK_stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->L, &o, s); return addk(fs, &o, &o); }
static void pushstring(lua_State *L, TString *s) { TValue o; setsvalue(L, &o, s); LIF(A,pushobject)(L, &o); }
static int ktap_lib_arch(ktap_State *ks) { setsvalue(ks->top, kp_tstring_new(ks, utsname()->machine)); incr_top(ks); return 1; }
static int ktap_lib_kernel_v(ktap_State *ks) { setsvalue(ks->top, kp_tstring_new(ks, utsname()->release)); incr_top(ks); return 1; }
int FuncState::luaK_stringK (/*FuncState *fs,*/ TString *s) { TValue o; setsvalue(ls->L, &o, s); return addk(&o, &o); }
void MCExecPoint::setstaticmcstring(const MCString& p_mcstring) { setsvalue(p_mcstring); }
void MCExecPoint::setboolean(Boolean p_value) { setsvalue(p_value ? MCtruemcstring : MCfalsemcstring); }
void pushString(lua_State *luaState, TString *str) { TValue o; setsvalue(luaState, &o, str); pushObject(luaState, &o); }