1 #include "../../include/ktap_types.h"
2 #include "../../include/ktap_opcodes.h"
4 #include "../cparser.h"
8 cp_csymbol_state csym_state;
10 #define cs_nr (csym_state.cs_nr)
11 #define cs_arr_size (csym_state.cs_arr_size)
12 #define cs_arr (csym_state.cs_arr)
14 csymbol *cp_id_to_csym(int id)
20 typedef struct cp_ctype_entry {
21 char name[MAX_TYPE_NAME_LEN];
25 #define DEFAULT_CTYPE_ARR_SIZE 100
27 static int cte_arr_size;
28 static cp_ctype_entry *cte_arr;
31 /* stack to help maintain state during parsing */
32 typedef struct cp_ctype_stack {
35 cp_ctype_entry *stack;
39 static ctype_stack cts;
41 #define ct_stack(id) (&(cts.stack[id]))
42 #define ct_stack_ct(id) (&(cts.stack[id].ct))
46 int cp_ctype_reg_csymbol(csymbol *cs);
49 size_t ctype_size(const struct cp_ctype *ct)
51 if (ct->pointers - ct->is_array) {
52 return sizeof(void*) * (ct->is_array ? ct->array_size : 1);
54 } else if (!ct->is_defined || ct->type == VOID_TYPE) {
55 cp_error("can't calculate size of an undefined type");
57 } else if (ct->variable_size_known) {
58 assert(ct->is_variable_struct && !ct->is_array);
59 return ct->base_size + ct->variable_increment;
60 } else if (ct->is_variable_array || ct->is_variable_struct) {
61 cp_error("internal error: calc size of variable type with "
65 return ct->base_size * (ct->is_array ? ct->array_size : 1);
69 #define MAX_STACK_SIZE 100
70 int ctype_stack_grow(int size)
72 struct cp_ctype_entry *new_st;
74 assert(cts.size + size < MAX_STACK_SIZE);
76 new_st = realloc(cts.stack, (cts.size+size)*sizeof(cp_ctype_entry));
87 int ctype_stack_free_space()
89 return cts.size - cts.top;
92 void ctype_stack_reset()
97 /* push ctype to stack, create new csymbol if needed */
98 void cp_push_ctype_with_name(struct cp_ctype *ct, const char *name, int nlen)
101 struct cp_ctype *nct;
103 if (ctype_stack_free_space() < 1)
106 /* we have to check pointer here because does type lookup by name
107 * before parsing '*', and for pointers, ct will always be the
110 for (i = 0; i < cte_nr; i++) {
111 nct = &(cte_arr[i].ct);
112 if (nct->type == ct->type &&
113 nct->pointers == ct->pointers) {
119 /* pointer type not found
120 * create a new pointer symbol for this type */
121 /* associate ctype with new csymbol */
122 ct->ffi_cs_id = cp_symbol_build_pointer(ct);
123 /* register wit new pointer name */
124 cp_ctype_reg_type(csym_name(ct_ffi_cs(ct)), ct);
126 /* pointer type already registered, reinstantiate ct */
130 memset(ct_stack(cts.top), 0, sizeof(cp_ctype_entry));
131 ct_stack(cts.top)->ct = *ct;
133 strncpy(ct_stack(cts.top)->name, name, nlen);
137 void cp_push_ctype(struct cp_ctype *ct)
139 cp_push_ctype_with_name(ct, NULL, 0);
142 void cp_set_defined(struct cp_ctype *ct)
146 /* @TODO: update ctypes and cdatas that were created before the
147 * definition came in */
150 void cp_ctype_dump_stack()
155 printf("---------------------------\n");
156 printf("start of ctype stack (%d) dump: \n", cts.top);
157 for (i = 0; i < cts.top; i++) {
159 printf("[%d] -> cp_ctype: %d, sym_type: %d, pointer: %d "
160 "symbol_id: %d, name: %s\n",
162 csym_type(ct_ffi_cs(ct)), ct->pointers, ct->ffi_cs_id,
167 int ctype_reg_table_grow()
169 cp_ctype_entry *new_arr;
171 new_arr = realloc(cte_arr, sizeof(cp_ctype_entry)*cte_arr_size*2);
173 cp_error("failed to allocate memory for ctype array\n");
175 cte_arr_size = cte_arr_size * 2;
179 /* return index in csymbol array */
180 int cp_ctype_reg_csymbol(csymbol *cs)
182 if (cs_nr >= cs_arr_size) {
184 cs_arr = realloc(cs_arr, cs_arr_size*sizeof(csymbol));
186 cp_error("failed to extend csymbol array!\n");
195 void __cp_symbol_dump_struct(csymbol *cs)
199 csymbol_struct *stcs = csym_struct(cs);
201 printf("=== [%s] definition ==================\n", csym_name(cs));
202 for (i = 0; i < stcs->memb_nr; i++) {
203 printf("\t(%d) ", i);
204 printf("csym_id: %d, ", stcs->members[i].id);
205 ncs = &cs_arr[stcs->members[i].id];
206 printf("name: %s, ffi_ctype: %d, %s\n",
207 stcs->members[i].name, ncs->type, csym_name(ncs));
211 void cp_symbol_dump_struct(int id)
213 __cp_symbol_dump_struct(&cs_arr[id]);
216 int cp_symbol_build_struct(const char *stname)
218 int i, id, memb_size;
221 struct_member *st_membs;
222 csymbol_struct *stcs;
224 if (cts.top <= 0 || !stname) {
225 cp_error("invalid struct definition.\n");
229 st_membs = malloc(memb_size*sizeof(struct_member));
231 cp_error("failed to allocate memory for struct members.\n");
232 memset(st_membs, 0, memb_size*sizeof(struct_member));
234 nst.type = FFI_STRUCT;
235 strcpy(nst.name, stname);
237 stcs = csym_struct(&nst);
238 stcs->memb_nr = memb_size;
239 stcs->members = st_membs;
241 for (i = 0; i < memb_size; i++) {
245 strcpy(st_membs[i].name, cte->name);
246 st_membs[i].id = ct_stack_ct(i)->ffi_cs_id;
249 id = cp_ctype_reg_csymbol(&nst);
256 /* build pointer symbol from given csymbol */
257 int cp_symbol_build_pointer(struct cp_ctype *ct)
261 csymbol *ref_cs = ct_ffi_cs(ct);
263 /* TODO: Check correctness of multi-level pointer 24.11.2013(unihorn) */
264 memset(&ncspt, 0, sizeof(csymbol));
265 ncspt.type = FFI_PTR;
266 ret = sprintf(ncspt.name, "%s *", csym_name(ref_cs));
267 assert(ret < MAX_TYPE_NAME_LEN);
269 csym_set_ptr_deref_id(&ncspt, ct->ffi_cs_id);
270 id = cp_ctype_reg_csymbol(&ncspt);
275 void __cp_symbol_dump_func(csymbol *cs)
279 csymbol_func *fcs = csym_func(cs);
281 printf("=== [%s] function definition =============\n", csym_name(cs));
282 ncs = cp_csymf_ret(fcs);
283 printf("address: %p\n", fcs->addr);
284 printf("return type: \n");
285 printf("\tcsym_id: %d, ffi_ctype: %d, %s\n",
286 fcs->ret_id, ncs->type, csym_name(ncs));
287 printf("args type (%d): \n", fcs->arg_nr);
288 for (i = 0; i < csymf_arg_nr(fcs); i++) {
289 printf("\t (%d) ", i);
290 printf("csym_id: %d, ", fcs->arg_ids[i]);
291 ncs = cp_csymf_arg(fcs, i);
292 printf("ffi_ctype: %d, %s\n", ncs->type, csym_name(ncs));
296 void cp_symbol_dump_func(int id)
298 __cp_symbol_dump_func(&cs_arr[id]);
301 int cp_symbol_build_func(struct cp_ctype *type, const char *fname, int fn_size)
303 int i = 1, arg_nr, id;
308 if (cts.top == 0 || fn_size < 0 || !fname) {
309 cp_error("invalid function definition.\n");
312 argsym_id_arr = NULL;
313 memset(&nfcs, 0, sizeof(csymbol));
314 csym_type(&nfcs) = FFI_FUNC;
316 strncpy(csym_name(&nfcs), fname, fn_size);
318 fcs = csym_func(&nfcs);
319 fcs->has_var_arg = type->has_var_arg;
320 /* Type needed for handling variable args handle */
321 if (fcs->has_var_arg && !ctype_lookup_type("void *"))
322 cp_symbol_build_pointer(ctype_lookup_type("void"));
324 /* Fetch start address of function */
325 fcs->addr = (void *)find_kernel_symbol(csym_name(&nfcs));
327 cp_error("wrong function address for %s\n", csym_name(&nfcs));
329 /* bottom of the stack is return type */
330 fcs->ret_id = ct_stack_ct(0)->ffi_cs_id;
332 /* the rest is argument type */
334 /* function takes no argument */
337 arg_nr = cts.top - 1;
338 argsym_id_arr = malloc(arg_nr * sizeof(int));
340 cp_error("failed to allocate memory for function args.\n");
341 for (i = 0; i < arg_nr; i++) {
342 argsym_id_arr[i] = ct_stack_ct(i+1)->ffi_cs_id;
345 fcs->arg_nr = arg_nr;
346 fcs->arg_ids = argsym_id_arr;
348 id = cp_ctype_reg_csymbol(&nfcs);
350 /* clear stack since we have consumed all the ctypes */
356 struct cp_ctype *cp_ctype_reg_type(char *name, struct cp_ctype *ct)
358 if (cte_nr >= cte_arr_size)
359 ctype_reg_table_grow();
361 memset(cte_arr[cte_nr].name, 0, MAX_TYPE_NAME_LEN);
362 strcpy(cte_arr[cte_nr].name, name);
364 cte_arr[cte_nr].ct = *ct;
367 return &(cte_arr[cte_nr-1].ct);
371 /* TODO: used for size calculation */
372 static ffi_type ffi_int_type(ktap_state *ks, int size, bool sign)
396 kp_error(ks, "Error: Have not support int type of size %d\n", size);
400 /* NEVER reach here, silence compiler */
406 static inline void ct_set_type(struct cp_ctype *ct, int type, int is_unsigned)
409 ct->is_unsigned = is_unsigned;
412 static void init_builtin_type(struct cp_ctype *ct, ffi_type ftype)
417 csym_type(&cs) = ftype;
418 strncpy(csym_name(&cs), ffi_type_name(ftype), CSYM_NAME_MAX_LEN);
419 cs_id = cp_ctype_reg_csymbol(&cs);
421 memset(ct, 0, sizeof(*ct));
422 ct->ffi_cs_id = cs_id;
424 case FFI_VOID: ct_set_type(ct, VOID_TYPE, 0); break;
425 case FFI_UINT8: ct_set_type(ct, INT8_TYPE, 1); break;
426 case FFI_INT8: ct_set_type(ct, INT8_TYPE, 0); break;
427 case FFI_UINT16: ct_set_type(ct, INT16_TYPE, 1); break;
428 case FFI_INT16: ct_set_type(ct, INT16_TYPE, 0); break;
429 case FFI_UINT32: ct_set_type(ct, INT32_TYPE, 1); break;
430 case FFI_INT32: ct_set_type(ct, INT32_TYPE, 0); break;
431 case FFI_UINT64: ct_set_type(ct, INT64_TYPE, 1); break;
432 case FFI_INT64: ct_set_type(ct, INT64_TYPE, 0); break;
435 ct->base_size = ffi_type_size(ftype);
436 ct->align_mask = ffi_type_align(ftype) - 1;
441 * lookup and register builtin C type on demand
442 * You should ensure that the type with name doesn't appear in
443 * csymbol table before calling.
445 struct cp_ctype *ctype_lookup_builtin_type(char *name)
449 if (!strncmp(name, "void", sizeof("void"))) {
450 init_builtin_type(&ct, FFI_VOID);
451 return cp_ctype_reg_type("void", &ct);
452 } else if (!strncmp(name, "int8_t", sizeof("int8_t"))) {
453 init_builtin_type(&ct, FFI_INT8);
454 return cp_ctype_reg_type("int8_t", &ct);
455 } else if (!strncmp(name, "uint8_t", sizeof("uint8_t"))) {
456 init_builtin_type(&ct, FFI_UINT8);
457 return cp_ctype_reg_type("uint8_t", &ct);
458 } else if (!strncmp(name, "int16_t", sizeof("int16_t"))) {
459 init_builtin_type(&ct, FFI_INT16);
460 return cp_ctype_reg_type("int16_t", &ct);
461 } else if (!strncmp(name, "uint16_t", sizeof("uint16_t"))) {
462 init_builtin_type(&ct, FFI_UINT16);
463 return cp_ctype_reg_type("uint16_t", &ct);
464 } else if (!strncmp(name, "int32_t", sizeof("int32_t"))) {
465 init_builtin_type(&ct, FFI_INT32);
466 return cp_ctype_reg_type("int32_t", &ct);
467 } else if (!strncmp(name, "uint32_t", sizeof("uint32_t"))) {
468 init_builtin_type(&ct, FFI_UINT32);
469 return cp_ctype_reg_type("uint32_t", &ct);
470 } else if (!strncmp(name, "int64_t", sizeof("int64_t"))) {
471 init_builtin_type(&ct, FFI_INT64);
472 return cp_ctype_reg_type("int64_t", &ct);
473 } else if (!strncmp(name, "uint64_t", sizeof("uint64_t"))) {
474 init_builtin_type(&ct, FFI_UINT64);
475 return cp_ctype_reg_type("uint64_t", &ct);
477 /* no builtin type matched */
482 /* start ctype reg table */
483 struct cp_ctype *ctype_lookup_type(char *name)
488 for (i = 0; i < cte_nr; i++) {
490 if (!strcmp(name, cte_arr[i].name))
494 /* see if it's a builtin C type
495 * return NULL if still no match */
496 return ctype_lookup_builtin_type(name);
499 cp_csymbol_state *ctype_get_csym_state(void)
504 #define DEFAULT_STACK_SIZE 20
505 #define DEFAULT_SYM_ARR_SIZE 20
508 cts.size = DEFAULT_STACK_SIZE;
510 cts.stack = malloc(sizeof(cp_ctype_entry)*DEFAULT_STACK_SIZE);
513 cs_arr_size = DEFAULT_SYM_ARR_SIZE;
514 cs_arr = malloc(sizeof(csymbol)*DEFAULT_SYM_ARR_SIZE);
515 memset(cs_arr, 0, sizeof(csymbol)*DEFAULT_SYM_ARR_SIZE);
518 cte_arr_size = DEFAULT_CTYPE_ARR_SIZE;
519 cte_arr = malloc(sizeof(cp_ctype_entry)*DEFAULT_CTYPE_ARR_SIZE);
533 for (i = 0; i < cs_nr; i++) {
535 if (csym_type(cs) == FFI_FUNC) {
536 if (csym_func(cs)->arg_ids)
537 free(csym_func(cs)->arg_ids);
538 } else if (csym_type(cs) == FFI_STRUCT) {
539 if (csym_struct(cs)->members)
540 free(csym_struct(cs)->members);