Skip to content

Commit

Permalink
examples/callgraph: correlate indirect callers and callees via type
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Jun 8, 2024
1 parent e76f408 commit 719f5d6
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions examples/callgraph/callgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <toywasm/expr_parser.h>
#include <toywasm/leb128.h>
#include <toywasm/name.h>
#include <toywasm/type.h>

static void
fatal(int error)
{
fprintf(stderr, "fatal error %d: %s", error, strerror(error));
exit(1);
}

void
dump_table_type(uint32_t tableidx, const char *typestr)
{
printf("subgraph cluster_table_%" PRIu32 " { \"table%" PRIu32
"_%s\" [label=\"%s\",shape=box]; label=table_%" PRIu32
"; color=purple }\n",
tableidx, tableidx, typestr, typestr, tableidx);
}

static void
dump_calls(const struct module *m, uint32_t i, struct nametable *table)
{
Expand All @@ -21,6 +38,10 @@ dump_calls(const struct module *m, uint32_t i, struct nametable *table)
struct name callee_func_name;
uint32_t callee;
uint32_t tableidx;
uint32_t typeidx;
const struct functype *ft;
char *typestr;
int ret;
switch (insn[0]) {
case 0x10: /* call */
imm = &insn[1];
Expand All @@ -32,11 +53,18 @@ dump_calls(const struct module *m, uint32_t i, struct nametable *table)
break;
case 0x11: /* call_indirect */
imm = &insn[1];
read_leb_u32_nocheck(&imm); /* typeidx */
typeidx = read_leb_u32_nocheck(&imm);
tableidx = read_leb_u32_nocheck(&imm);
printf("f%" PRIu32 " -> table%" PRIu32
" [color=purple]\n",
funcidx, tableidx);
ft = &m->types[typeidx];
ret = functype_to_string(&typestr, ft);
if (ret != 0) {
fatal(ret);
}
dump_table_type(tableidx, typestr);
printf("f%" PRIu32 " -> \"table%" PRIu32
"_%s\" [color=purple]\n",
funcidx, tableidx, typestr);
functype_string_free(typestr);
break;
#if defined(TOYWASM_ENABLE_WASM_TAILCALL)
/* XXX implement tail-call instructions */
Expand All @@ -61,6 +89,7 @@ dump_calls(const struct module *m, uint32_t i, struct nametable *table)
void
callgraph(const struct module *m)
{
int ret;
uint32_t i;
struct nametable table;
nametable_init(&table);
Expand All @@ -77,10 +106,6 @@ callgraph(const struct module *m)
printf("f%" PRIu32 " [label=\"%.*s\",color=%s]\n", i,
CSTR(&func_name), color);
}
for (i = 0; i < m->ntables; i++) {
printf("table%" PRIu32 " [shape=parallelogram,color=purple]\n",
i);
}
/*
* here we only implement active elements with funcidxes.
* ie. the case where we don't need to execute exprs.
Expand All @@ -97,9 +122,20 @@ callgraph(const struct module *m)
}
uint32_t j;
for (j = 0; j < e->init_size; j++) {
printf("table%" PRIu32 " -> f%" PRIu32
uint32_t funcidx = e->funcs[j];
uint32_t tableidx = e->table;
const struct functype *ft =
module_functype(m, funcidx);
char *typestr;
ret = functype_to_string(&typestr, ft);
if (ret != 0) {
fatal(ret);
}
dump_table_type(tableidx, typestr);
printf("\"table%" PRIu32 "_%s\" -> f%" PRIu32
" [color=purple]\n",
e->table, e->funcs[j]);
tableidx, typestr, funcidx);
functype_string_free(typestr);
}
}
for (i = 0; i < m->nimports; i++) {
Expand Down

0 comments on commit 719f5d6

Please sign in to comment.