void kp_cdata_ptr_get(ktap_state_t *ks, ktap_cdata_t *cd, ktap_val_t *key, ktap_val_t *val) { ktap_number idx; csymbol *cs; size_t size; char *addr; csymbol_id cs_id; if (!is_number(key)) { kp_error(ks, "array index should be number\n"); return; } idx = nvalue(key); if (unlikely(idx < 0 || (cd_ptr_nmemb(cd) >= 0 && idx >= cd_ptr_nmemb(cd)))) { kp_error(ks, "array index out of bound\n"); return; } cs_id = csym_ptr_deref_id(cd_csym(ks, cd)); cs = id_to_csym(ks, cs_id); size = csym_size(ks, cs); addr = cd_ptr(cd); addr += size * idx; kp_cdata_init(ks, val, addr, -1, cs_id); kp_cdata_pack(ks, val, addr, cs); }
void kp_cdata_record_get(ktap_state_t *ks, ktap_cdata_t *cd, ktap_val_t *key, ktap_val_t *val) { const char *mb_name; csymbol *cs, *mb_cs; csymbol_struct *csst; struct_member *mb; char *addr; csymbol_id mb_cs_id; if (!is_shrstring(key)) { kp_error(ks, "struct member name should be string\n"); return; } mb_name = svalue(key); cs = cd_csym(ks, cd); csst = csym_struct(cs); mb = csymst_mb_by_name(ks, csst, mb_name); if (mb == NULL) { kp_error(ks, "struct member %s doesn't exist\n", mb_name); return; } mb_cs_id = mb->id; mb_cs = id_to_csym(ks, mb_cs_id); addr = cd_record(cd); addr += csym_record_mb_offset_by_name(ks, cs, mb_name); kp_cdata_init(ks, val, addr, mb->len, mb_cs_id); if (mb->len < 0) kp_cdata_pack(ks, val, addr, mb_cs); }
void kp_cdata_ptr_set(ktap_state_t *ks, ktap_cdata_t *cd, ktap_val_t *key, ktap_val_t *val) { ktap_number idx; csymbol *cs; size_t size; char *addr; if (!is_number(key)) { kp_error(ks, "array index should be number\n"); return; } idx = nvalue(key); if (unlikely(idx < 0 || (cd_ptr_nmemb(cd) >= 0 && idx >= cd_ptr_nmemb(cd)))) { kp_error(ks, "array index out of bound\n"); return; } cs = csym_ptr_deref(ks, cd_csym(ks, cd)); if (kp_cdata_type_match(ks, cs, val)) { kp_error(ks, "array member should be %s type\n", csym_name(cs)); return; } size = csym_size(ks, cs); addr = cd_ptr(cd); addr += size * idx; kp_cdata_unpack(ks, addr, cs, val); }
void kp_cdata_record_set(ktap_state *ks, ktap_cdata *cd, ktap_value *key, ktap_value *val) { const char *mb_name; csymbol *cs, *mb_cs; csymbol_struct *csst; struct_member *mb; char *addr; if (!is_shrstring(key)) { kp_error(ks, "struct member name should be string\n"); return; } mb_name = svalue(key); cs = cd_csym(ks, cd); csst = csym_struct(cs); mb = csymst_mb_by_name(ks, csst, mb_name); if (mb == NULL) { kp_error(ks, "struct member %s doesn't exist\n", mb_name); return; } mb_cs = id_to_csym(ks, mb->id); if (kp_cdata_type_match(ks, mb_cs, val)) { kp_error(ks, "struct member should be %s type\n", csym_name(mb_cs)); return; } addr = cd_struct(cd); addr += csym_record_mb_offset_by_name(ks, cs, mb_name); kp_cdata_unpack(ks, addr, mb_cs, val); }
static void kp_cdata_value(ktap_state_t *ks, ktap_val_t *val, void **out_addr, size_t *out_size, void **temp) { ktap_cdata_t *cd; csymbol *cs; ffi_type type; switch (ttypenv(val)) { case KTAP_TYPE_BOOLEAN: *out_addr = &bvalue(val); *out_size = sizeof(int); return; case KTAP_TYPE_LIGHTUSERDATA: *out_addr = pvalue(val); *out_size = sizeof(void *); return; case KTAP_TYPE_NUMBER: *out_addr = &nvalue(val); *out_size = sizeof(ktap_number); return; case KTAP_TYPE_STRING: *temp = (void *)svalue(val); *out_addr = temp; *out_size = sizeof(void *); return; } cd = cdvalue(val); cs = cd_csym(ks, cd); type = csym_type(cs); *out_size = csym_size(ks, cs); switch (type) { case FFI_VOID: kp_error(ks, "Error: Cannot copy data from void type\n"); return; case FFI_UINT8: case FFI_INT8: case FFI_UINT16: case FFI_INT16: case FFI_UINT32: case FFI_INT32: case FFI_UINT64: case FFI_INT64: *out_addr = &cd_int(cd); return; case FFI_PTR: *out_addr = &cd_ptr(cd); return; case FFI_STRUCT: case FFI_UNION: *out_addr = cd_record(cd); return; case FFI_FUNC: case FFI_UNKNOWN: kp_error(ks, "Error: internal error for csymbol %s\n", csym_name(cs)); return; } }
static csymbol *ffi_get_arg_csym(ktap_state_t *ks, csymbol_func *csf, int idx) { StkId arg; csymbol *cs; if (idx < csymf_arg_nr(csf)) return csymf_arg(ks, csf, idx); arg = kp_arg(ks, idx + 1); cs = id_to_csym(ks, ffi_get_csym_id(ks, "void *")); switch (ttypenv(arg)) { case KTAP_TYPE_LIGHTUSERDATA: case KTAP_TYPE_BOOLEAN: case KTAP_TYPE_NUMBER: case KTAP_TYPE_STRING: return cs; case KTAP_TYPE_CDATA: return cd_csym(ks, cdvalue(arg)); default: kp_error(ks, "Error: Cannot get type for arg %d\n", idx); return cs; } }
/* Notice: Even if the types are matched, there may exist the lost of * data in the unpack process due to precision */ int kp_cdata_type_match(ktap_state_t *ks, csymbol *cs, ktap_val_t *val) { ffi_type type; type = csym_type(cs); if (type == FFI_FUNC) goto error; switch (ttypenv(val)) { case KTAP_TYPE_LIGHTUSERDATA: if (type != FFI_PTR) goto error; break; case KTAP_TYPE_BOOLEAN: case KTAP_TYPE_NUMBER: if (type != FFI_UINT8 && type != FFI_INT8 && type != FFI_UINT16 && type != FFI_INT16 && type != FFI_UINT32 && type != FFI_INT32 && type != FFI_UINT64 && type != FFI_INT64) goto error; break; case KTAP_TYPE_STRING: if (type != FFI_PTR && type != FFI_UINT8 && type != FFI_INT8) goto error; break; case KTAP_TYPE_CDATA: if (cs != cd_csym(ks, cdvalue(val))) goto error; break; default: goto error; } return 0; error: return -1; }