Rizin
unix-like reverse engineering framework and cli tools
wasm.c File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_lib.h>
#include <rz_bin.h>
#include "wasm.h"

Go to the source code of this file.

Typedefs

typedef size_t(* ConsumeFcn) (const ut8 *p, const ut8 *max, ut32 *out_value)
 
typedef void *(* ParseEntryFcn) (RzBuffer *b, ut64 max)
 

Functions

static ut32 consume_r (RzBuffer *b, ut64 max, size_t *n_out, ConsumeFcn consume_fcn)
 
static size_t consume_u32_r (RzBuffer *b, ut64 max, ut32 *out)
 
static size_t consume_u7_r (RzBuffer *b, ut64 max, ut8 *out)
 
static size_t consume_s7_r (RzBuffer *b, ut64 max, st8 *out)
 
static size_t consume_u1_r (RzBuffer *b, ut64 max, ut8 *out)
 
static size_t consume_str_r (RzBuffer *b, ut64 max, size_t sz, char *out)
 
static size_t consume_init_expr_r (RzBuffer *b, ut64 max, ut8 eoc, void *out)
 
static size_t consume_locals_r (RzBuffer *b, ut64 max, RzBinWasmCodeEntry *out)
 
static size_t consume_limits_r (RzBuffer *b, ut64 max, struct rz_bin_wasm_resizable_limits_t *out)
 
static RzListrz_bin_wasm_get_sections_by_id (RzList *sections, ut8 id)
 
static void rz_bin_wasm_free_types (RzBinWasmTypeEntry *ptr)
 
static void rz_bin_wasm_free_codes (RzBinWasmCodeEntry *ptr)
 
static RzListget_entries_from_section (RzBinWasmObj *bin, RzBinWasmSection *sec, ParseEntryFcn parse_entry, RzListFree free_entry)
 
static void * parse_type_entry (RzBuffer *b, ut64 max)
 
static void * parse_import_entry (RzBuffer *b, ut64 max)
 
static void * parse_export_entry (RzBuffer *b, ut64 max)
 
static void * parse_code_entry (RzBuffer *b, ut64 max)
 
static void * parse_data_entry (RzBuffer *b, ut64 max)
 
static bool parse_namemap (RzBuffer *b, ut64 max, RzIDStorage *map, ut32 *count)
 
static void * parse_custom_name_entry (RzBuffer *b, ut64 max)
 
static void * parse_memory_entry (RzBuffer *b, ut64 max)
 
static void * parse_table_entry (RzBuffer *b, ut64 max)
 
static void * parse_global_entry (RzBuffer *b, ut64 max)
 
static void * parse_element_entry (RzBuffer *b, ut64 max)
 
static RzListrz_bin_wasm_get_type_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_import_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_export_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_code_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_data_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzBinWasmStartEntryrz_bin_wasm_get_start (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_memory_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_table_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_global_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_element_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
static RzListrz_bin_wasm_get_custom_name_entries (RzBinWasmObj *bin, RzBinWasmSection *sec)
 
RzBinWasmObjrz_bin_wasm_init (RzBinFile *bf, RzBuffer *buf)
 
void rz_bin_wasm_destroy (RzBinFile *bf)
 
RzListrz_bin_wasm_get_sections (RzBinWasmObj *bin)
 
ut32 rz_bin_wasm_get_entrypoint (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_imports (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_exports (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_types (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_tables (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_memories (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_globals (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_elements (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_codes (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_datas (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_custom_names (RzBinWasmObj *bin)
 
const char * rz_bin_wasm_get_function_name (RzBinWasmObj *bin, ut32 idx)
 

Typedef Documentation

◆ ConsumeFcn

typedef size_t(* ConsumeFcn) (const ut8 *p, const ut8 *max, ut32 *out_value)

Definition at line 12 of file wasm.c.

◆ ParseEntryFcn

typedef void*(* ParseEntryFcn) (RzBuffer *b, ut64 max)

Definition at line 13 of file wasm.c.

Function Documentation

◆ consume_init_expr_r()

static size_t consume_init_expr_r ( RzBuffer b,
ut64  max,
ut8  eoc,
void *  out 
)
static

Definition at line 93 of file wasm.c.

93  {
94  if (!b || max >= rz_buf_size(b) || rz_buf_tell(b) > max) {
95  return 0;
96  }
97  size_t res = 0;
98  ut8 cur;
99  if (!rz_buf_read8(b, &cur)) {
100  return 0;
101  }
102  while (rz_buf_tell(b) <= max && cur != eoc) {
103  if (!rz_buf_read8(b, &cur)) {
104  return 0;
105  }
106  res++;
107  }
108  if (cur != eoc) {
109  return 0;
110  }
111  return res + 1;
112 }
int max
Definition: enough.c:225
uint8_t ut8
Definition: lh5801.h:11
RZ_API ut64 rz_buf_tell(RZ_NONNULL RzBuffer *b)
Return the current cursor position.
Definition: buf.c:1238
RZ_API bool rz_buf_read8(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut8 *result)
Read a byte at the cursor in the buffer.
Definition: buf.c:860
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define b(i)
Definition: sha256.c:42

References b, max, rz_buf_read8(), rz_buf_size(), and rz_buf_tell().

Referenced by parse_data_entry(), parse_element_entry(), and parse_global_entry().

◆ consume_limits_r()

static size_t consume_limits_r ( RzBuffer b,
ut64  max,
struct rz_bin_wasm_resizable_limits_t out 
)
static

Definition at line 144 of file wasm.c.

144  {
145  if (!b || max >= rz_buf_size(b) || rz_buf_tell(b) > max || !out) {
146  return 0;
147  }
148  ut32 i = rz_buf_tell(b);
149  if (!(consume_u7_r(b, max, &out->flags))) {
150  return 0;
151  }
152  if (!(consume_u32_r(b, max, &out->initial))) {
153  return 0;
154  }
155  if (out->flags && (!(consume_u32_r(b, max, &out->maximum)))) {
156  return 0;
157  }
158  return (size_t)(rz_buf_tell(b) - i);
159 }
lzma_index ** i
Definition: index.h:629
static size_t consume_u32_r(RzBuffer *b, ut64 max, ut32 *out)
Definition: wasm.c:41
static size_t consume_u7_r(RzBuffer *b, ut64 max, ut8 *out)
Definition: wasm.c:50
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
uint32_t ut32

References b, consume_u32_r(), consume_u7_r(), i, max, out, rz_buf_size(), and rz_buf_tell().

Referenced by parse_import_entry(), parse_memory_entry(), and parse_table_entry().

◆ consume_locals_r()

static size_t consume_locals_r ( RzBuffer b,
ut64  max,
RzBinWasmCodeEntry out 
)
static

Definition at line 114 of file wasm.c.

114  {
115  ut64 cur = rz_buf_tell(b);
116  if (!b || max >= rz_buf_size(b) || cur > max) {
117  return 0;
118  }
119  ut32 count = out->local_count;
120  if (count > 0) {
121  if (!(out->locals = RZ_NEWS0(struct rz_bin_wasm_local_entry_t, count))) {
122  return 0;
123  }
124  }
125  ut32 j = 0;
126  while (rz_buf_tell(b) <= max && j < count) {
127  if (!(consume_u32_r(b, max, &out->locals[j].count))) {
128  goto beach;
129  }
130  if (!(consume_s7_r(b, max, (st8 *)&out->locals[j].type))) {
131  goto beach;
132  }
133  j++;
134  }
135  if (j != count) {
136  goto beach;
137  }
138  return j;
139 beach:
140  RZ_FREE(out->locals);
141  return 0;
142 }
static size_t consume_s7_r(RzBuffer *b, ut64 max, st8 *out)
Definition: wasm.c:59
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
#define RZ_FREE(x)
Definition: rz_types.h:369
#define st8
Definition: rz_types_base.h:16
Definition: wasm.h:154
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References b, consume_s7_r(), consume_u32_r(), count, max, out, rz_buf_size(), rz_buf_tell(), RZ_FREE, RZ_NEWS0, st8, and ut64().

Referenced by parse_code_entry().

◆ consume_r()

static ut32 consume_r ( RzBuffer b,
ut64  max,
size_t n_out,
ConsumeFcn  consume_fcn 
)
static

Definition at line 16 of file wasm.c.

16  {
17  rz_return_val_if_fail(b && n_out && consume_fcn, 0);
18 
19  size_t n;
20  ut32 tmp;
21  ut64 cur = rz_buf_tell(b);
22  if (max >= rz_buf_size(b) || cur > max) {
23  return 0;
24  }
25  // 16 bytes are enough to store 128bits values
26  ut8 *buf = RZ_NEWS(ut8, 16);
27  if (!buf) {
28  return 0;
29  }
30  rz_buf_read(b, buf, 16);
31  if (!(n = consume_fcn(buf, buf + max + 1, &tmp))) {
32  free(buf);
33  return 0;
34  }
35  rz_buf_seek(b, cur + n, RZ_BUF_SET);
36  *n_out = n;
37  free(buf);
38  return tmp;
39 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
int n
Definition: mipsasm.c:19
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API st64 rz_buf_seek(RZ_NONNULL RzBuffer *b, st64 addr, int whence)
Modify the current cursor position in the buffer.
Definition: buf.c:1166
#define RZ_BUF_SET
Definition: rz_buf.h:14
RZ_API st64 rz_buf_read(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
#define RZ_NEWS(x, y)
Definition: rz_types.h:283

References b, free(), max, n, rz_buf_read(), rz_buf_seek(), RZ_BUF_SET, rz_buf_size(), rz_buf_tell(), RZ_NEWS, rz_return_val_if_fail, autogen_x86imm::tmp, and ut64().

Referenced by consume_s7_r(), consume_u1_r(), consume_u32_r(), and consume_u7_r().

◆ consume_s7_r()

static size_t consume_s7_r ( RzBuffer b,
ut64  max,
st8 out 
)
static

Definition at line 59 of file wasm.c.

59  {
60  size_t n = 0;
62  if (out) {
63  *out = (st8)(((tmp & 0x10000000) << 7) | (tmp & 0x7f));
64  }
65  return n;
66 }
size_t(* ConsumeFcn)(const ut8 *p, const ut8 *max, ut32 *out_value)
Definition: wasm.c:12
static ut32 consume_r(RzBuffer *b, ut64 max, size_t *n_out, ConsumeFcn consume_fcn)
Definition: wasm.c:16
RZ_API size_t read_i32_leb128(const ut8 *p, const ut8 *max, st32 *out_val)
Definition: uleb128.c:211

References b, consume_r(), max, n, out, read_i32_leb128(), st8, and autogen_x86imm::tmp.

Referenced by consume_locals_r(), parse_import_entry(), parse_table_entry(), and parse_type_entry().

◆ consume_str_r()

static size_t consume_str_r ( RzBuffer b,
ut64  max,
size_t  sz,
char *  out 
)
static

Definition at line 77 of file wasm.c.

77  {
78  ut64 cur = rz_buf_tell(b);
79  if (!b || max >= rz_buf_size(b) || cur > max) {
80  return 0;
81  }
82  if (!(cur + sz - 1 <= max)) {
83  return 0;
84  }
85  if (sz > 0) {
87  } else {
88  *out = 0;
89  }
90  return sz;
91 }
#define RZ_BIN_WASM_STRING_LENGTH
Definition: wasm.h:18
#define RZ_MIN(x, y)

References b, max, out, RZ_BIN_WASM_STRING_LENGTH, rz_buf_read(), rz_buf_size(), rz_buf_tell(), RZ_MIN, and ut64().

Referenced by parse_custom_name_entry(), parse_export_entry(), parse_import_entry(), parse_namemap(), and rz_bin_wasm_get_sections().

◆ consume_u1_r()

static size_t consume_u1_r ( RzBuffer b,
ut64  max,
ut8 out 
)
static

Definition at line 68 of file wasm.c.

68  {
69  size_t n = 0;
71  if (out) {
72  *out = (ut8)(tmp & 0x1);
73  }
74  return n;
75 }
#define ut8
Definition: dcpu16.h:8
RZ_API size_t read_u32_leb128(const ut8 *p, const ut8 *max, ut32 *out_val)
Definition: uleb128.c:186

References b, consume_r(), max, n, out, read_u32_leb128(), autogen_x86imm::tmp, and ut8.

Referenced by parse_global_entry(), parse_import_entry(), and parse_type_entry().

◆ consume_u32_r()

static size_t consume_u32_r ( RzBuffer b,
ut64  max,
ut32 out 
)
static

◆ consume_u7_r()

static size_t consume_u7_r ( RzBuffer b,
ut64  max,
ut8 out 
)
static

Definition at line 50 of file wasm.c.

50  {
51  size_t n = 0;
53  if (out) {
54  *out = (ut8)(tmp & 0x7f);
55  }
56  return n;
57 }

References b, consume_r(), max, n, out, read_u32_leb128(), autogen_x86imm::tmp, and ut8.

Referenced by consume_limits_r(), parse_custom_name_entry(), parse_export_entry(), parse_global_entry(), parse_import_entry(), parse_type_entry(), and rz_bin_wasm_get_sections().

◆ get_entries_from_section()

static RzList* get_entries_from_section ( RzBinWasmObj bin,
RzBinWasmSection sec,
ParseEntryFcn  parse_entry,
RzListFree  free_entry 
)
static

Definition at line 236 of file wasm.c.

236  {
237  rz_return_val_if_fail(sec && bin, NULL);
238 
239  RzList *ret = rz_list_newf(free_entry);
240  if (!ret) {
241  return NULL;
242  }
243  RzBuffer *b = bin->buf;
245  ut32 r = 0;
246  ut64 max = rz_buf_tell(b) + sec->payload_len - 1;
247  if (!(max < rz_buf_size(b))) {
248  goto beach;
249  }
250  while (rz_buf_tell(b) <= max && r < sec->count) {
251  void *entry = parse_entry(b, max);
252  if (!entry) {
253  goto beach;
254  }
255 
256  if (!rz_list_append(ret, entry)) {
257  free_entry(entry);
258  // should this jump to beach?
259  }
260  r++;
261  }
262  return ret;
263 beach:
264  RZ_LOG_ERROR("wasm: failed to read entries for section %s\n", sec->name);
265  return ret;
266 }
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
Definition: malloc.c:26
unsigned char * buf
Definition: gzjoin.c:83
Definition: zipcmp.c:77
char name[RZ_BIN_WASM_STRING_LENGTH]
Definition: wasm.h:78

References b, bin::buf, count, max, rz_bin_wasm_section_t::name, NULL, rz_bin_wasm_section_t::payload_data, rz_bin_wasm_section_t::payload_len, r, rz_buf_seek(), RZ_BUF_SET, rz_buf_size(), rz_buf_tell(), rz_list_append(), rz_list_newf(), RZ_LOG_ERROR, rz_return_val_if_fail, and ut64().

Referenced by rz_bin_wasm_get_code_entries(), rz_bin_wasm_get_data_entries(), rz_bin_wasm_get_element_entries(), rz_bin_wasm_get_export_entries(), rz_bin_wasm_get_global_entries(), rz_bin_wasm_get_import_entries(), rz_bin_wasm_get_memory_entries(), rz_bin_wasm_get_table_entries(), and rz_bin_wasm_get_type_entries().

◆ parse_code_entry()

static void* parse_code_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 392 of file wasm.c.

392  {
394  if (!ptr) {
395  return NULL;
396  }
397  if (!(consume_u32_r(b, max, &ptr->body_size))) {
398  goto beach;
399  }
400  ut32 j = rz_buf_tell(b);
401  if (!(rz_buf_tell(b) + ptr->body_size - 1 <= max)) {
402  goto beach;
403  }
404  if (!(consume_u32_r(b, max, &ptr->local_count))) {
405  goto beach;
406  }
407  if (consume_locals_r(b, max, ptr) < ptr->local_count) {
408  goto beach;
409  }
410  ptr->code = rz_buf_tell(b);
411  ptr->len = ptr->body_size - ptr->code + j;
412  rz_buf_seek(b, ptr->len - 1, RZ_BUF_CUR); // consume bytecode
413  rz_buf_read(b, &ptr->byte, 1);
414  if (ptr->byte != RZ_BIN_WASM_END_OF_CODE) {
415  goto beach;
416  }
417  return ptr;
418 
419 beach:
421  return NULL;
422 }
static void rz_bin_wasm_free_codes(RzBinWasmCodeEntry *ptr)
Definition: wasm.c:228
static size_t consume_locals_r(RzBuffer *b, ut64 max, RzBinWasmCodeEntry *out)
Definition: wasm.c:114
#define RZ_BIN_WASM_END_OF_CODE
Definition: wasm.h:19
#define RZ_BUF_CUR
Definition: rz_buf.h:15
#define RZ_NEW0(x)
Definition: rz_types.h:284
ut32 body_size
Definition: wasm.h:167
ut32 local_count
Definition: wasm.h:168

References b, rz_bin_wasm_code_t::body_size, rz_bin_wasm_code_t::byte, rz_bin_wasm_code_t::code, consume_locals_r(), consume_u32_r(), rz_bin_wasm_code_t::len, rz_bin_wasm_code_t::local_count, max, NULL, RZ_BIN_WASM_END_OF_CODE, rz_bin_wasm_free_codes(), RZ_BUF_CUR, rz_buf_read(), rz_buf_seek(), rz_buf_tell(), and RZ_NEW0.

Referenced by rz_bin_wasm_get_code_entries().

◆ parse_custom_name_entry()

static void* parse_custom_name_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 485 of file wasm.c.

485  {
487  size_t i;
488  if (!(ptr = RZ_NEW0(RzBinWasmCustomNameEntry))) {
489  return NULL;
490  }
491 
492  if (!(consume_u7_r(b, max, &ptr->type))) {
493  goto beach;
494  };
495 
496  if (!(consume_u32_r(b, max, &ptr->size))) {
497  goto beach;
498  };
499 
500  switch (ptr->type) {
502  ptr->mod_name = RZ_NEW0(struct rz_bin_wasm_name_t);
503  if (!ptr->mod_name) {
504  goto beach;
505  }
506  if (!(consume_u32_r(b, max, &ptr->mod_name->len))) {
507  goto beach;
508  }
509 
510  if (!(consume_str_r(b, max, ptr->mod_name->len, (char *)ptr->mod_name->name))) {
511  goto beach;
512  }
513 
514  ptr->mod_name->name[ptr->mod_name->len] = 0;
515  break;
518  if (!ptr->func) {
519  goto beach;
520  }
521 
522  ptr->func->names = rz_id_storage_new(0, UT32_MAX);
523 
524  if (!ptr->func->names) {
525  goto beach;
526  }
527 
528  if (!parse_namemap(b, max, ptr->func->names, &ptr->func->count)) {
529  goto beach;
530  }
531  break;
534  if (!ptr->local) {
535  goto beach;
536  }
537  if (!(consume_u32_r(b, max, &ptr->local->count))) {
538  free(ptr->local);
539  goto beach;
540  }
541 
542  ptr->local->locals = rz_list_new();
543 
544  for (i = 0; i < ptr->local->count; i++) {
546  if (!local_name) {
547  free(ptr->local);
548  free(ptr);
549  return NULL;
550  }
551 
552  if (!(consume_u32_r(b, max, &local_name->index))) {
553  rz_list_free(ptr->local->locals);
554  free(ptr->local);
555  free(local_name);
556  goto beach;
557  }
558 
559  local_name->names = rz_id_storage_new(0, UT32_MAX);
560  if (!local_name->names) {
561  rz_list_free(ptr->local->locals);
562  free(ptr->local);
563  free(local_name);
564  goto beach;
565  }
566 
567  if (!parse_namemap(b, max, local_name->names, &local_name->names_count)) {
568  rz_id_storage_free(local_name->names);
569  rz_list_free(ptr->local->locals);
570  free(ptr->local);
571  free(local_name);
572  goto beach;
573  }
574 
575  if (!rz_list_append(ptr->local->locals, local_name)) {
576  free(local_name);
577  goto beach;
578  };
579  }
580  break;
581  }
582 
583  return ptr;
584 beach:
585  free(ptr);
586  return NULL;
587 }
static size_t consume_str_r(RzBuffer *b, ut64 max, size_t sz, char *out)
Definition: wasm.c:77
static bool parse_namemap(RzBuffer *b, ut64 max, RzIDStorage *map, ut32 *count)
Definition: wasm.c:447
@ RZ_BIN_WASM_NAMETYPE_Local
Definition: wasm.h:55
@ RZ_BIN_WASM_NAMETYPE_Module
Definition: wasm.h:53
@ RZ_BIN_WASM_NAMETYPE_Function
Definition: wasm.h:54
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
RZ_API RzIDStorage * rz_id_storage_new(ut32 start_id, ut32 last_id)
Definition: idpool.c:80
RZ_API void rz_id_storage_free(RzIDStorage *storage)
Definition: idpool.c:270
#define UT32_MAX
Definition: rz_types_base.h:99
Definition: wasm.h:204
ut8 type
Definition: wasm.h:205
struct rz_bin_wasm_name_t * mod_name
Definition: wasm.h:210
RzBinWasmCustomNameFunctionNames * func
Definition: wasm.h:211
RzBinWasmCustomNameLocalNames * local
Definition: wasm.h:212
ut32 size
Definition: wasm.h:206
ut8 name[RZ_BIN_WASM_STRING_LENGTH]
Definition: wasm.h:71

References b, consume_str_r(), consume_u32_r(), consume_u7_r(), rz_bin_wasm_custom_name_function_names_t::count, rz_bin_wasm_custom_name_local_names_t::count, free(), rz_bin_wasm_custom_name_entry_t::func, i, rz_bin_wasm_custom_name_local_name_t::index, rz_bin_wasm_name_t::len, rz_bin_wasm_custom_name_entry_t::local, rz_bin_wasm_custom_name_local_names_t::locals, max, rz_bin_wasm_custom_name_entry_t::mod_name, rz_bin_wasm_name_t::name, rz_bin_wasm_custom_name_function_names_t::names, rz_bin_wasm_custom_name_local_name_t::names, rz_bin_wasm_custom_name_local_name_t::names_count, NULL, parse_namemap(), RZ_BIN_WASM_NAMETYPE_Function, RZ_BIN_WASM_NAMETYPE_Local, RZ_BIN_WASM_NAMETYPE_Module, rz_id_storage_free(), rz_id_storage_new(), rz_list_append(), rz_list_free(), rz_list_new(), RZ_NEW0, rz_bin_wasm_custom_name_entry_t::size, rz_bin_wasm_custom_name_entry_t::type, and UT32_MAX.

Referenced by rz_bin_wasm_get_custom_name_entries().

◆ parse_data_entry()

static void* parse_data_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 424 of file wasm.c.

424  {
426  if (!ptr) {
427  return NULL;
428  }
429  if (!(consume_u32_r(b, max, &ptr->index))) {
430  goto beach;
431  }
433  goto beach;
434  }
435  if (!(consume_u32_r(b, max, &ptr->size))) {
436  goto beach;
437  }
438  ptr->data = rz_buf_tell(b);
439  rz_buf_seek(b, ptr->size, RZ_BUF_CUR);
440  return ptr;
441 
442 beach:
443  free(ptr);
444  return NULL;
445 }
static size_t consume_init_expr_r(RzBuffer *b, ut64 max, ut8 eoc, void *out)
Definition: wasm.c:93
struct rz_bin_wasm_init_expr_t offset
Definition: wasm.h:179

References b, consume_init_expr_r(), consume_u32_r(), rz_bin_wasm_data_t::data, free(), rz_bin_wasm_data_t::index, rz_bin_wasm_init_expr_t::len, max, NULL, rz_bin_wasm_data_t::offset, RZ_BIN_WASM_END_OF_CODE, RZ_BUF_CUR, rz_buf_seek(), rz_buf_tell(), RZ_NEW0, and rz_bin_wasm_data_t::size.

Referenced by rz_bin_wasm_get_data_entries().

◆ parse_element_entry()

static void* parse_element_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 643 of file wasm.c.

643  {
645  if (!ptr) {
646  return NULL;
647  }
648  if (!(consume_u32_r(b, max, &ptr->index))) {
649  goto beach;
650  }
652  goto beach;
653  }
654  if (!(consume_u32_r(b, max, &ptr->num_elem))) {
655  goto beach;
656  }
657  ut32 j = 0;
658  while (rz_buf_tell(b) <= max && j < ptr->num_elem) {
659  // TODO: allocate space and fill entry
660  if (!(consume_u32_r(b, max, NULL))) {
661  goto beach;
662  }
663  }
664  return ptr;
665 
666 beach:
667  free(ptr);
668  return NULL;
669 }

References b, consume_init_expr_r(), consume_u32_r(), free(), rz_bin_wasm_element_t::index, max, NULL, rz_bin_wasm_element_t::num_elem, RZ_BIN_WASM_END_OF_CODE, rz_buf_tell(), and RZ_NEW0.

Referenced by rz_bin_wasm_get_element_entries().

◆ parse_export_entry()

static void* parse_export_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 369 of file wasm.c.

369  {
371  if (!ptr) {
372  return NULL;
373  }
374  if (!(consume_u32_r(b, max, &ptr->field_len))) {
375  goto beach;
376  }
377  if (consume_str_r(b, max, ptr->field_len, ptr->field_str) < ptr->field_len) {
378  goto beach;
379  }
380  if (!(consume_u7_r(b, max, &ptr->kind))) {
381  goto beach;
382  }
383  if (!(consume_u32_r(b, max, &ptr->index))) {
384  goto beach;
385  }
386  return ptr;
387 beach:
388  free(ptr);
389  return NULL;
390 }
char field_str[RZ_BIN_WASM_STRING_LENGTH]
Definition: wasm.h:145

References b, consume_str_r(), consume_u32_r(), consume_u7_r(), rz_bin_wasm_export_t::field_len, rz_bin_wasm_export_t::field_str, free(), rz_bin_wasm_export_t::index, rz_bin_wasm_export_t::kind, max, NULL, and RZ_NEW0.

Referenced by rz_bin_wasm_get_export_entries().

◆ parse_global_entry()

static void* parse_global_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 622 of file wasm.c.

622  {
624  if (!ptr) {
625  return NULL;
626  }
627  if (!(consume_u7_r(b, max, (ut8 *)&ptr->content_type))) {
628  goto beach;
629  }
630  if (!(consume_u1_r(b, max, &ptr->mutability))) {
631  goto beach;
632  }
634  goto beach;
635  }
636  return ptr;
637 
638 beach:
639  free(ptr);
640  return NULL;
641 }
static size_t consume_u1_r(RzBuffer *b, ut64 max, ut8 *out)
Definition: wasm.c:68
rz_bin_wasm_value_type_t content_type
Definition: wasm.h:138

References b, consume_init_expr_r(), consume_u1_r(), consume_u7_r(), rz_bin_wasm_global_t::content_type, free(), max, rz_bin_wasm_global_t::mutability, NULL, RZ_BIN_WASM_END_OF_CODE, and RZ_NEW0.

Referenced by rz_bin_wasm_get_global_entries().

◆ parse_import_entry()

static void* parse_import_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 312 of file wasm.c.

312  {
314  if (!ptr) {
315  return NULL;
316  }
317  if (!(consume_u32_r(b, max, &ptr->module_len))) {
318  goto beach;
319  }
320  if (consume_str_r(b, max, ptr->module_len, ptr->module_str) < ptr->module_len) {
321  goto beach;
322  }
323  if (!(consume_u32_r(b, max, &ptr->field_len))) {
324  goto beach;
325  }
326  if (consume_str_r(b, max, ptr->field_len, ptr->field_str) < ptr->field_len) {
327  goto beach;
328  }
329  if (!(consume_u7_r(b, max, &ptr->kind))) {
330  goto beach;
331  }
332  switch (ptr->kind) {
333  case 0: // Function
334  if (!(consume_u32_r(b, max, &ptr->type_f))) {
335  goto beach;
336  }
337  break;
338  case 1: // Table
339  if (!(consume_s7_r(b, max, (st8 *)&ptr->type_t.elem_type))) {
340  goto beach;
341  }
342  if (!(consume_limits_r(b, max, &ptr->type_t.limits))) {
343  goto beach;
344  }
345  break;
346  case 2: // Memory
347  if (!(consume_limits_r(b, max, &ptr->type_m.limits))) {
348  goto beach;
349  }
350  break;
351  case 3: // Global
352  if (!(consume_s7_r(b, max, (st8 *)&ptr->type_g.content_type))) {
353  goto beach;
354  }
355  if (!(consume_u1_r(b, max, (ut8 *)&ptr->type_g.mutability))) {
356  goto beach;
357  }
358  break;
359  default:
360  goto beach;
361  }
362  return ptr;
363 
364 beach:
365  free(ptr);
366  return NULL;
367 }
static size_t consume_limits_r(RzBuffer *b, ut64 max, struct rz_bin_wasm_resizable_limits_t *out)
Definition: wasm.c:144
rz_bin_wasm_value_type_t content_type
Definition: wasm.h:96
char field_str[RZ_BIN_WASM_STRING_LENGTH]
Definition: wasm.h:113
struct rz_bin_wasm_memory_type_t type_m
Definition: wasm.h:119
struct rz_bin_wasm_global_type_t type_g
Definition: wasm.h:117
struct rz_bin_wasm_table_type_t type_t
Definition: wasm.h:118
char module_str[RZ_BIN_WASM_STRING_LENGTH]
Definition: wasm.h:111
struct rz_bin_wasm_resizable_limits_t limits
Definition: wasm.h:106
struct rz_bin_wasm_resizable_limits_t limits
Definition: wasm.h:102
rz_bin_wasm_value_type_t elem_type
Definition: wasm.h:101

References b, consume_limits_r(), consume_s7_r(), consume_str_r(), consume_u1_r(), consume_u32_r(), consume_u7_r(), rz_bin_wasm_global_type_t::content_type, rz_bin_wasm_table_type_t::elem_type, rz_bin_wasm_import_t::field_len, rz_bin_wasm_import_t::field_str, free(), rz_bin_wasm_import_t::kind, rz_bin_wasm_table_type_t::limits, rz_bin_wasm_memory_type_t::limits, max, rz_bin_wasm_import_t::module_len, rz_bin_wasm_import_t::module_str, rz_bin_wasm_global_type_t::mutability, NULL, RZ_NEW0, st8, rz_bin_wasm_import_t::type_f, rz_bin_wasm_import_t::type_g, rz_bin_wasm_import_t::type_m, and rz_bin_wasm_import_t::type_t.

Referenced by rz_bin_wasm_get_import_entries().

◆ parse_memory_entry()

static void* parse_memory_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 589 of file wasm.c.

589  {
591  if (!ptr) {
592  return NULL;
593  }
594  if (!(consume_limits_r(b, max, &ptr->limits))) {
595  goto beach;
596  }
597  return ptr;
598 
599 beach:
600  free(ptr);
601  return NULL;
602 }
struct rz_bin_wasm_resizable_limits_t limits
Definition: wasm.h:134

References b, consume_limits_r(), free(), rz_bin_wasm_memory_t::limits, max, NULL, and RZ_NEW0.

Referenced by rz_bin_wasm_get_memory_entries().

◆ parse_namemap()

static bool parse_namemap ( RzBuffer b,
ut64  max,
RzIDStorage map,
ut32 count 
)
static

Definition at line 447 of file wasm.c.

447  {
448  size_t i;
449  if (!(consume_u32_r(b, max, count))) {
450  return false;
451  }
452 
453  for (i = 0; i < *count; i++) {
455  if (!name) {
456  return false;
457  }
458 
459  ut32 idx;
460  if (!(consume_u32_r(b, max, &idx))) {
461  RZ_FREE(name);
462  return false;
463  }
464 
465  if (!(consume_u32_r(b, max, &name->len))) {
466  RZ_FREE(name);
467  return false;
468  }
469 
470  if (!(consume_str_r(b, max, name->len, (char *)name->name))) {
471  RZ_FREE(name);
472  return false;
473  }
474  name->name[name->len] = 0;
475 
476  if (!rz_id_storage_add(map, name, &idx)) {
477  RZ_FREE(name);
478  return false;
479  };
480  }
481 
482  return true;
483 }
size_t map(int syms, int left, int len)
Definition: enough.c:237
int idx
Definition: setup.py:197
RZ_API bool rz_id_storage_add(RzIDStorage *storage, void *data, ut32 *id)
Definition: idpool.c:155
Definition: z80asm.h:102
char name[1]
Definition: z80asm.h:104

References b, consume_str_r(), consume_u32_r(), count, i, setup::idx, map(), max, name::name, RZ_FREE, rz_id_storage_add(), and RZ_NEW0.

Referenced by parse_custom_name_entry().

◆ parse_table_entry()

static void* parse_table_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 604 of file wasm.c.

604  {
606  if (!ptr) {
607  return NULL;
608  }
609  if (!(consume_s7_r(b, max, (st8 *)&ptr->element_type))) {
610  goto beach;
611  }
612  if (!(consume_limits_r(b, max, &ptr->limits))) {
613  goto beach;
614  }
615  return ptr;
616 
617 beach:
618  free(ptr);
619  return NULL;
620 }
struct rz_bin_wasm_resizable_limits_t limits
Definition: wasm.h:130

References b, consume_limits_r(), consume_s7_r(), rz_bin_wasm_table_t::element_type, free(), rz_bin_wasm_table_t::limits, max, NULL, RZ_NEW0, and st8.

Referenced by rz_bin_wasm_get_table_entries().

◆ parse_type_entry()

static void* parse_type_entry ( RzBuffer b,
ut64  max 
)
static

Definition at line 268 of file wasm.c.

268  {
270  if (!ptr) {
271  return NULL;
272  }
273  if (!(consume_u7_r(b, max, &ptr->form))) {
274  goto beach;
275  }
276  // check valid type?
277  if (!(consume_u32_r(b, max, &ptr->param_count))) {
278  goto beach;
279  }
280  ut32 count = ptr->param_count;
281  if (!(rz_buf_tell(b) + count <= max)) {
282  goto beach;
283  }
284  if (count > 0) {
286  goto beach;
287  }
288  }
289  int j;
290  for (j = 0; j < count; j++) {
291  if (!(consume_s7_r(b, max, (st8 *)&ptr->param_types[j]))) {
292  goto beach;
293  }
294  }
295  if (!(consume_u1_r(b, max, (ut8 *)&ptr->return_count))) {
296  goto beach;
297  }
298  if (ptr->return_count > 1) {
299  goto beach;
300  }
301  if (ptr->return_count == 1) {
302  if (!(consume_s7_r(b, max, (st8 *)&ptr->return_type))) {
303  goto beach;
304  }
305  }
306  return ptr;
307 
308 beach:
310  return NULL;
311 }
static void rz_bin_wasm_free_types(RzBinWasmTypeEntry *ptr)
Definition: wasm.c:221
rz_bin_wasm_value_type_t
Definition: wasm.h:34
rz_bin_wasm_value_type_t return_type
Definition: wasm.h:90
rz_bin_wasm_value_type_t * param_types
Definition: wasm.h:88
st8 return_count
Definition: wasm.h:89
ut32 param_count
Definition: wasm.h:87

References b, consume_s7_r(), consume_u1_r(), consume_u32_r(), consume_u7_r(), count, rz_bin_wasm_type_t::form, max, NULL, rz_bin_wasm_type_t::param_count, rz_bin_wasm_type_t::param_types, rz_bin_wasm_type_t::return_count, rz_bin_wasm_type_t::return_type, rz_bin_wasm_free_types(), rz_buf_tell(), RZ_NEW0, RZ_NEWS0, and st8.

Referenced by rz_bin_wasm_get_type_entries().

◆ rz_bin_wasm_destroy()

void rz_bin_wasm_destroy ( RzBinFile bf)

Definition at line 789 of file wasm.c.

789  {
790  RzBinWasmObj *bin;
791 
792  if (!bf || !bf->o || !bf->o->bin_obj) {
793  return;
794  }
795 
796  bin = bf->o->bin_obj;
797  rz_buf_free(bin->buf);
798 
799  rz_list_free(bin->g_sections);
800  rz_list_free(bin->g_types);
801 
802  rz_list_free(bin->g_imports);
803  rz_list_free(bin->g_exports);
804  rz_list_free(bin->g_tables);
805  rz_list_free(bin->g_memories);
806  rz_list_free(bin->g_globals);
807  rz_list_free(bin->g_codes);
808  rz_list_free(bin->g_datas);
809 
810  RzListIter *iter;
812  rz_list_foreach (bin->g_names, iter, nam) {
813  switch (nam->type) {
815  if (nam->mod_name) {
816  free(nam->mod_name);
817  }
818  break;
820  if (nam->func) {
822  }
823  break;
825  if (nam->local && nam->local->locals) {
826  RzListIter *iter;
828  rz_list_foreach (nam->local->locals, iter, local) {
829  if (local->names) {
830  rz_id_storage_free(local->names);
831  }
832  }
833 
834  rz_list_free(nam->local->locals);
835  }
836  break;
837  }
838  }
839  rz_list_free(bin->g_names);
840 
841  free(bin->g_start);
842  free(bin);
843  bf->o->bin_obj = NULL;
844 }
#define local
Definition: blast.c:36
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RzBinObject * o
Definition: rz_bin.h:305
void * bin_obj
Definition: rz_bin.h:293

References rz_bin_object_t::bin_obj, bin::buf, free(), rz_bin_wasm_custom_name_entry_t::func, rz_bin_wasm_custom_name_entry_t::local, local, rz_bin_wasm_custom_name_local_names_t::locals, rz_bin_wasm_custom_name_entry_t::mod_name, rz_bin_wasm_custom_name_function_names_t::names, NULL, rz_bin_file_t::o, RZ_BIN_WASM_NAMETYPE_Function, RZ_BIN_WASM_NAMETYPE_Local, RZ_BIN_WASM_NAMETYPE_Module, rz_buf_free(), rz_id_storage_free(), rz_list_free(), and rz_bin_wasm_custom_name_entry_t::type.

Referenced by destroy().

◆ rz_bin_wasm_free_codes()

static void rz_bin_wasm_free_codes ( RzBinWasmCodeEntry ptr)
static

Definition at line 228 of file wasm.c.

228  {
229  if (ptr) {
230  free(ptr->locals);
231  }
232  free(ptr);
233 }
struct rz_bin_wasm_local_entry_t * locals
Definition: wasm.h:169

References free(), and rz_bin_wasm_code_t::locals.

Referenced by parse_code_entry(), and rz_bin_wasm_get_code_entries().

◆ rz_bin_wasm_free_types()

static void rz_bin_wasm_free_types ( RzBinWasmTypeEntry ptr)
static

Definition at line 221 of file wasm.c.

221  {
222  if (ptr) {
223  free(ptr->param_types);
224  }
225  free(ptr);
226 }

References free(), and rz_bin_wasm_type_t::param_types.

Referenced by parse_type_entry(), and rz_bin_wasm_get_type_entries().

◆ rz_bin_wasm_get_code_entries()

static RzList* rz_bin_wasm_get_code_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 683 of file wasm.c.

683  {
685 }
static RzList * get_entries_from_section(RzBinWasmObj *bin, RzBinWasmSection *sec, ParseEntryFcn parse_entry, RzListFree free_entry)
Definition: wasm.c:236
static void * parse_code_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:392
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11

References get_entries_from_section(), parse_code_entry(), and rz_bin_wasm_free_codes().

Referenced by rz_bin_wasm_get_codes().

◆ rz_bin_wasm_get_codes()

RzList* rz_bin_wasm_get_codes ( RzBinWasmObj bin)

Definition at line 1168 of file wasm.c.

1168  {
1170  ;
1171  RzList *codes = NULL;
1172 
1173  if (!bin || !bin->g_sections) {
1174  return NULL;
1175  }
1176  if (bin->g_codes) {
1177  return bin->g_codes;
1178  }
1180  return rz_list_new();
1181  }
1182  // support for multiple export sections against spec
1183  if (!(code = (RzBinWasmSection *)rz_list_first(codes))) {
1185  return rz_list_new();
1186  }
1189  return bin->g_codes;
1190 }
static RzList * rz_bin_wasm_get_code_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:683
static RzList * rz_bin_wasm_get_sections_by_id(RzList *sections, ut8 id)
Definition: wasm.c:162
#define RZ_BIN_WASM_SECTION_CODE
Definition: wasm.h:31
RZ_API RZ_BORROW void * rz_list_first(RZ_NONNULL const RzList *list)
Returns the first element of the list.
Definition: list.c:77
int codes(struct state *s, const struct huffman *lencode, const struct huffman *distcode)
Definition: puff.c:436
Definition: inftree9.h:24

References codes(), NULL, rz_bin_wasm_get_code_entries(), rz_bin_wasm_get_sections_by_id(), RZ_BIN_WASM_SECTION_CODE, rz_list_first(), rz_list_free(), and rz_list_new().

Referenced by entries(), rz_bin_wasm_init(), and symbols().

◆ rz_bin_wasm_get_custom_name_entries()

static RzList* rz_bin_wasm_get_custom_name_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 730 of file wasm.c.

730  {
731  RzList *ret = rz_list_new();
732 
733  RzBuffer *buf = bin->buf;
734 
736  ut64 max = sec->payload_data + sec->payload_len - 1;
737 
738  if (max > rz_buf_size(buf)) {
739  goto beach;
740  }
741 
742  while (rz_buf_tell(buf) < max) {
744 
745  if (!nam) {
746  goto beach;
747  }
748 
749  if (!rz_list_append(ret, nam)) {
750  goto beach;
751  }
752  }
753 
754  return ret;
755 beach:
756  rz_list_free(ret);
757  return NULL;
758 }
static void * parse_custom_name_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:485

References bin::buf, max, NULL, parse_custom_name_entry(), rz_bin_wasm_section_t::payload_data, rz_bin_wasm_section_t::payload_len, rz_buf_seek(), RZ_BUF_SET, rz_buf_size(), rz_buf_tell(), rz_list_append(), rz_list_free(), rz_list_new(), and ut64().

Referenced by rz_bin_wasm_get_custom_names().

◆ rz_bin_wasm_get_custom_names()

RzList* rz_bin_wasm_get_custom_names ( RzBinWasmObj bin)

Definition at line 1215 of file wasm.c.

1215  {
1216  RzBinWasmSection *cust = NULL;
1217  RzList *customs = NULL;
1218 
1219  rz_return_val_if_fail(bin && bin->g_sections, NULL);
1220 
1221  if (bin->g_names) {
1222  return bin->g_names;
1223  }
1224  if (!(customs = rz_bin_wasm_get_sections_by_id(bin->g_sections, RZ_BIN_WASM_SECTION_CUSTOM))) {
1225  return rz_list_new();
1226  }
1227  // support for multiple "name" sections against spec
1228  if (!(cust = (RzBinWasmSection *)rz_list_first(customs)) || strncmp(cust->name, "name", 5)) {
1229  rz_list_free(customs);
1230  return rz_list_new();
1231  }
1232  bin->g_names = rz_bin_wasm_get_custom_name_entries(bin, cust);
1233  rz_list_free(customs);
1234  return bin->g_names;
1235 }
static RzList * rz_bin_wasm_get_custom_name_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:730
#define RZ_BIN_WASM_SECTION_CUSTOM
Definition: wasm.h:21

References rz_bin_wasm_section_t::name, NULL, rz_bin_wasm_get_custom_name_entries(), rz_bin_wasm_get_sections_by_id(), RZ_BIN_WASM_SECTION_CUSTOM, rz_list_first(), rz_list_free(), rz_list_new(), and rz_return_val_if_fail.

Referenced by rz_bin_wasm_init().

◆ rz_bin_wasm_get_data_entries()

static RzList* rz_bin_wasm_get_data_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 687 of file wasm.c.

687  {
689 }
static void * parse_data_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:424

References free(), get_entries_from_section(), and parse_data_entry().

Referenced by rz_bin_wasm_get_datas().

◆ rz_bin_wasm_get_datas()

RzList* rz_bin_wasm_get_datas ( RzBinWasmObj bin)

Definition at line 1192 of file wasm.c.

1192  {
1193  RzBinWasmSection *data = NULL;
1194  RzList *datas = NULL;
1195 
1196  if (!bin || !bin->g_sections) {
1197  return NULL;
1198  }
1199  if (bin->g_datas) {
1200  return bin->g_datas;
1201  }
1202  if (!(datas = rz_bin_wasm_get_sections_by_id(bin->g_sections, RZ_BIN_WASM_SECTION_DATA))) {
1203  return rz_list_new();
1204  }
1205  // support for multiple export sections against spec
1206  if (!(data = (RzBinWasmSection *)rz_list_first(datas))) {
1207  rz_list_free(datas);
1208  return rz_list_new();
1209  }
1210  bin->g_datas = rz_bin_wasm_get_data_entries(bin, data);
1211  rz_list_free(datas);
1212  return bin->g_datas;
1213 }
static RzList * rz_bin_wasm_get_data_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:687
#define RZ_BIN_WASM_SECTION_DATA
Definition: wasm.h:32

References NULL, rz_bin_wasm_get_data_entries(), rz_bin_wasm_get_sections_by_id(), RZ_BIN_WASM_SECTION_DATA, rz_list_first(), rz_list_free(), and rz_list_new().

Referenced by rz_bin_wasm_init().

◆ rz_bin_wasm_get_element_entries()

static RzList* rz_bin_wasm_get_element_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 726 of file wasm.c.

726  {
728 }
static void * parse_element_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:643

References free(), get_entries_from_section(), and parse_element_entry().

Referenced by rz_bin_wasm_get_elements().

◆ rz_bin_wasm_get_elements()

RzList* rz_bin_wasm_get_elements ( RzBinWasmObj bin)

Definition at line 1145 of file wasm.c.

1145  {
1146  RzBinWasmSection *element = NULL;
1147  RzList *elements = NULL;
1148 
1149  if (!bin || !bin->g_sections) {
1150  return NULL;
1151  }
1152  if (bin->g_elements) {
1153  return bin->g_elements;
1154  }
1155  if (!(elements = rz_bin_wasm_get_sections_by_id(bin->g_sections, RZ_BIN_WASM_SECTION_ELEMENT))) {
1156  return rz_list_new();
1157  }
1158  // support for multiple export sections against spec
1159  if (!(element = (RzBinWasmSection *)rz_list_first(elements))) {
1160  rz_list_free(elements);
1161  return rz_list_new();
1162  }
1163  bin->g_elements = rz_bin_wasm_get_element_entries(bin, element);
1164  rz_list_free(elements);
1165  return bin->g_elements;
1166 }
static RzList * rz_bin_wasm_get_element_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:726
#define RZ_BIN_WASM_SECTION_ELEMENT
Definition: wasm.h:30

References NULL, rz_bin_wasm_get_element_entries(), rz_bin_wasm_get_sections_by_id(), RZ_BIN_WASM_SECTION_ELEMENT, rz_list_first(), rz_list_free(), and rz_list_new().

◆ rz_bin_wasm_get_entrypoint()

ut32 rz_bin_wasm_get_entrypoint ( RzBinWasmObj bin)

Definition at line 966 of file wasm.c.

966  {
967  RzList *secs = NULL;
969  RzBinWasmSection *sec = NULL;
970  RzBinWasmCodeEntry *func = NULL;
971 
972  if (!bin || !bin->g_sections) {
973  return 0;
974  }
975  if (bin->entrypoint) {
976  return bin->entrypoint;
977  }
978  if (bin->g_start) {
979  start = bin->g_start;
980  } else if (!(secs = rz_bin_wasm_get_sections_by_id(bin->g_sections, RZ_BIN_WASM_SECTION_START))) {
981  return 0;
982  } else if (!(sec = (RzBinWasmSection *)rz_list_first(secs))) {
983  rz_list_free(secs);
984  return 0;
985  } else {
987  bin->g_start = start;
988  }
989  if (!start) {
990  rz_list_free(secs);
991  return 0;
992  }
993  // FIX: entrypoint can be also an import
994  if (!bin->g_codes) {
995  rz_list_free(secs);
996  return 0;
997  }
998  func = rz_list_get_n(bin->g_codes, start->index);
999  rz_list_free(secs);
1000  return (ut32)(func ? func->code : 0);
1001 }
static RzBinWasmStartEntry * rz_bin_wasm_get_start(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:691
#define RZ_BIN_WASM_SECTION_START
Definition: wasm.h:29
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
RZ_API RZ_BORROW void * rz_list_get_n(RZ_NONNULL const RzList *list, ut32 n)
Returns the N-th element of the list.
Definition: list.c:574

References rz_bin_wasm_code_t::code, NULL, rz_bin_wasm_get_sections_by_id(), rz_bin_wasm_get_start(), RZ_BIN_WASM_SECTION_START, rz_list_first(), rz_list_free(), rz_list_get_n(), and start.

Referenced by entries(), and rz_bin_wasm_init().

◆ rz_bin_wasm_get_export_entries()

static RzList* rz_bin_wasm_get_export_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 679 of file wasm.c.

679  {
681 }
static void * parse_export_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:369

References free(), get_entries_from_section(), and parse_export_entry().

Referenced by rz_bin_wasm_get_exports().

◆ rz_bin_wasm_get_exports()

RzList* rz_bin_wasm_get_exports ( RzBinWasmObj bin)

Definition at line 1026 of file wasm.c.

1026  {
1027  RzBinWasmSection *export = NULL;
1028  RzList *exports = NULL;
1029 
1030  if (!bin || !bin->g_sections) {
1031  return NULL;
1032  }
1033  if (bin->g_exports) {
1034  return bin->g_exports;
1035  }
1036  if (!(exports = rz_bin_wasm_get_sections_by_id(bin->g_sections, RZ_BIN_WASM_SECTION_EXPORT))) {
1037  return rz_list_new();
1038  }
1039  // support for multiple export sections against spec
1040  if (!(export = (RzBinWasmSection *)rz_list_first(exports))) {
1041  rz_list_free(exports);
1042  return rz_list_new();
1043  }
1044  bin->g_exports = rz_bin_wasm_get_export_entries(bin, export);
1045  rz_list_free(exports);
1046  return bin->g_exports;
1047 }
static RzList * rz_bin_wasm_get_export_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:679
#define RZ_BIN_WASM_SECTION_EXPORT
Definition: wasm.h:28

References NULL, rz_bin_wasm_get_export_entries(), rz_bin_wasm_get_sections_by_id(), RZ_BIN_WASM_SECTION_EXPORT, rz_list_first(), rz_list_free(), and rz_list_new().

Referenced by rz_bin_wasm_init(), and symbols().

◆ rz_bin_wasm_get_function_name()

const char* rz_bin_wasm_get_function_name ( RzBinWasmObj bin,
ut32  idx 
)

Definition at line 1237 of file wasm.c.

1237  {
1238  if (!(bin && bin->g_names)) {
1239  return NULL;
1240  };
1241 
1242  RzListIter *iter;
1244  rz_list_foreach (bin->g_names, iter, nam) {
1245  if (nam->type == RZ_BIN_WASM_NAMETYPE_Function) {
1246  struct rz_bin_wasm_name_t *n = NULL;
1247 
1248  if ((n = rz_id_storage_get(nam->func->names, idx))) {
1249  return (const char *)n->name;
1250  }
1251  }
1252  }
1253 
1254  return NULL;
1255 }
RZ_API void * rz_id_storage_get(RzIDStorage *storage, ut32 id)
Definition: idpool.c:162

References rz_bin_wasm_custom_name_entry_t::func, setup::idx, n, rz_bin_wasm_custom_name_function_names_t::names, NULL, RZ_BIN_WASM_NAMETYPE_Function, rz_id_storage_get(), and rz_bin_wasm_custom_name_entry_t::type.

Referenced by symbols().

◆ rz_bin_wasm_get_global_entries()

static RzList* rz_bin_wasm_get_global_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 722 of file wasm.c.

722  {
724 }
static void * parse_global_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:622

References free(), get_entries_from_section(), and parse_global_entry().

Referenced by rz_bin_wasm_get_globals().

◆ rz_bin_wasm_get_globals()

RzList* rz_bin_wasm_get_globals ( RzBinWasmObj bin)

Definition at line 1122 of file wasm.c.

1122  {
1123  RzBinWasmSection *global = NULL;
1124  RzList *globals = NULL;
1125 
1126  if (!bin || !bin->g_sections) {
1127  return NULL;
1128  }
1129  if (bin->g_globals) {
1130  return bin->g_globals;
1131  }
1132  if (!(globals = rz_bin_wasm_get_sections_by_id(bin->g_sections, RZ_BIN_WASM_SECTION_GLOBAL))) {
1133  return rz_list_new();
1134  }
1135  // support for multiple export sections against spec
1136  if (!(global = (RzBinWasmSection *)rz_list_first(globals))) {
1137  rz_list_free(globals);
1138  return rz_list_new();
1139  }
1140  bin->g_globals = rz_bin_wasm_get_global_entries(bin, global);
1141  rz_list_free(globals);
1142  return bin->g_globals;
1143 }
static RzList * rz_bin_wasm_get_global_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:722
#define RZ_BIN_WASM_SECTION_GLOBAL
Definition: wasm.h:27

References NULL, rz_bin_wasm_get_global_entries(), rz_bin_wasm_get_sections_by_id(), RZ_BIN_WASM_SECTION_GLOBAL, rz_list_first(), rz_list_free(), and rz_list_new().

Referenced by rz_bin_wasm_init().

◆ rz_bin_wasm_get_import_entries()

static RzList* rz_bin_wasm_get_import_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 675 of file wasm.c.

675  {
677 }
static void * parse_import_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:312

References free(), get_entries_from_section(), and parse_import_entry().

Referenced by rz_bin_wasm_get_imports().

◆ rz_bin_wasm_get_imports()

RzList* rz_bin_wasm_get_imports ( RzBinWasmObj bin)

Definition at line 1003 of file wasm.c.

1003  {
1004  RzBinWasmSection *import = NULL;
1005  RzList *imports = NULL;
1006 
1007  if (!bin || !bin->g_sections) {
1008  return NULL;
1009  }
1010  if (bin->g_imports) {
1011  return bin->g_imports;
1012  }
1014  return rz_list_new();
1015  }
1016  // support for multiple import sections against spec
1017  if (!(import = (RzBinWasmSection *)rz_list_first(imports))) {
1019  return rz_list_new();
1020  }
1021  bin->g_imports = rz_bin_wasm_get_import_entries(bin, import);
1023  return bin->g_imports;
1024 }
static RzList * rz_bin_wasm_get_import_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:675
#define RZ_BIN_WASM_SECTION_IMPORT
Definition: wasm.h:23
RzList * imports(RzBinFile *bf)
Definition: bin_ne.c:106

References imports(), NULL, rz_bin_wasm_get_import_entries(), rz_bin_wasm_get_sections_by_id(), RZ_BIN_WASM_SECTION_IMPORT, rz_list_first(), rz_list_free(), and rz_list_new().

Referenced by imports(), rz_bin_wasm_init(), and symbols().

◆ rz_bin_wasm_get_memories()

RzList* rz_bin_wasm_get_memories ( RzBinWasmObj bin)

Definition at line 1095 of file wasm.c.

1095  {
1096  RzBinWasmSection *memory;
1097  RzList *memories;
1098 
1099  if (!bin || !bin->g_sections) {
1100  return NULL;
1101  }
1102 
1103  if (bin->g_memories) {
1104  return bin->g_memories;
1105  }
1106 
1107  if (!(memories = rz_bin_wasm_get_sections_by_id(bin->g_sections, RZ_BIN_WASM_SECTION_MEMORY))) {
1108  return rz_list_new();
1109  }
1110 
1111  // support for multiple export sections against spec
1112  if (!(memory = (RzBinWasmSection *)rz_list_first(memories))) {
1113  rz_list_free(memories);
1114  return rz_list_new();
1115  }
1116 
1117  bin->g_memories = rz_bin_wasm_get_memory_entries(bin, memory);
1118  rz_list_free(memories);
1119  return bin->g_memories;
1120 }
static RzList * rz_bin_wasm_get_memory_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:714
#define RZ_BIN_WASM_SECTION_MEMORY
Definition: wasm.h:26

References NULL, rz_bin_wasm_get_memory_entries(), rz_bin_wasm_get_sections_by_id(), RZ_BIN_WASM_SECTION_MEMORY, rz_list_first(), rz_list_free(), and rz_list_new().

Referenced by rz_bin_wasm_init().

◆ rz_bin_wasm_get_memory_entries()

static RzList* rz_bin_wasm_get_memory_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 714 of file wasm.c.

714  {
716 }
static void * parse_memory_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:589

References free(), get_entries_from_section(), and parse_memory_entry().

Referenced by rz_bin_wasm_get_memories().

◆ rz_bin_wasm_get_sections()

RzList* rz_bin_wasm_get_sections ( RzBinWasmObj bin)

Definition at line 846 of file wasm.c.

846  {
847  RzList *ret = NULL;
848  RzBinWasmSection *ptr = NULL;
849 
850  if (!bin) {
851  return NULL;
852  }
853  if (bin->g_sections) {
854  return bin->g_sections;
855  }
856  if (!(ret = rz_list_newf((RzListFree)free))) {
857  return NULL;
858  }
859  RzBuffer *b = bin->buf;
860  ut64 max = rz_buf_size(b) - 1;
861  rz_buf_seek(b, 8, RZ_BUF_SET);
862  while (rz_buf_tell(b) <= max) {
863  if (!(ptr = RZ_NEW0(RzBinWasmSection))) {
864  return ret;
865  }
866  if (!(consume_u7_r(b, max, &ptr->id))) {
867  goto beach;
868  }
869  if (!(consume_u32_r(b, max, &ptr->size))) {
870  goto beach;
871  }
872  // against spec. TODO: choose criteria for parsing
873  if (ptr->size < 1) {
874  goto beach;
875  // free (ptr);
876  // continue;
877  }
878  if (!(rz_buf_tell(b) + (ut64)ptr->size - 1 <= max)) {
879  goto beach;
880  }
881  ptr->count = 0;
882  ptr->offset = rz_buf_tell(b);
883  switch (ptr->id) {
885  if (!(consume_u32_r(b, max, &ptr->name_len))) {
886  goto beach;
887  }
888  if (consume_str_r(b, max, ptr->name_len, (char *)ptr->name) < ptr->name_len) {
889  goto beach;
890  }
891  break;
893  strcpy(ptr->name, "type");
894  ptr->name_len = 4;
895  break;
897  strcpy(ptr->name, "import");
898  ptr->name_len = 6;
899  break;
901  strcpy(ptr->name, "function");
902  ptr->name_len = 8;
903  break;
905  strcpy(ptr->name, "table");
906  ptr->name_len = 5;
907  break;
909  strcpy(ptr->name, "memory");
910  ptr->name_len = 6;
911  break;
913  strcpy(ptr->name, "global");
914  ptr->name_len = 6;
915  break;
917  strcpy(ptr->name, "export");
918  ptr->name_len = 6;
919  break;
921  strcpy(ptr->name, "start");
922  ptr->name_len = 5;
923  break;
925  strcpy(ptr->name, "element");
926  ptr->name_len = 7;
927  break;
929  strcpy(ptr->name, "code");
930  ptr->name_len = 4;
931  break;
933  strcpy(ptr->name, "data");
934  ptr->name_len = 4;
935  break;
936  default:
937  RZ_LOG_ERROR("wasm: unkown section id: %d\n", ptr->id);
938  rz_buf_seek(b, ptr->size - 1, RZ_BUF_CUR);
939  continue;
940  }
942  if (!(consume_u32_r(b, max, &ptr->count))) {
943  goto beach;
944  }
945  }
946  ptr->payload_data = rz_buf_tell(b);
947  ptr->payload_len = ptr->size - (ptr->payload_data - ptr->offset);
948  if (ptr->payload_len > ptr->size) {
949  goto beach;
950  }
952  if (!rz_list_append(ret, ptr)) {
953  free(ptr);
954  // should it jump to beach?
955  }
956  ptr = NULL;
957  }
958  bin->g_sections = ret;
959  return ret;
960 beach:
961  RZ_LOG_ERROR("wasm: failed to read sections\n");
962  free(ptr);
963  return ret;
964 }
#define RZ_BIN_WASM_SECTION_TABLE
Definition: wasm.h:25
#define RZ_BIN_WASM_SECTION_FUNCTION
Definition: wasm.h:24
#define RZ_BIN_WASM_SECTION_TYPE
Definition: wasm.h:22

References b, bin::buf, consume_str_r(), consume_u32_r(), consume_u7_r(), rz_bin_wasm_section_t::count, free(), rz_bin_wasm_section_t::id, max, rz_bin_wasm_section_t::name, rz_bin_wasm_section_t::name_len, NULL, rz_bin_wasm_section_t::offset, rz_bin_wasm_section_t::payload_data, rz_bin_wasm_section_t::payload_len, RZ_BIN_WASM_SECTION_CODE, RZ_BIN_WASM_SECTION_CUSTOM, RZ_BIN_WASM_SECTION_DATA, RZ_BIN_WASM_SECTION_ELEMENT, RZ_BIN_WASM_SECTION_EXPORT, RZ_BIN_WASM_SECTION_FUNCTION, RZ_BIN_WASM_SECTION_GLOBAL, RZ_BIN_WASM_SECTION_IMPORT, RZ_BIN_WASM_SECTION_MEMORY, RZ_BIN_WASM_SECTION_START, RZ_BIN_WASM_SECTION_TABLE, RZ_BIN_WASM_SECTION_TYPE, RZ_BUF_CUR, rz_buf_seek(), RZ_BUF_SET, rz_buf_size(), rz_buf_tell(), rz_list_append(), rz_list_newf(), RZ_LOG_ERROR, RZ_NEW0, rz_bin_wasm_section_t::size, and ut64().

Referenced by rz_bin_wasm_init(), and sections().

◆ rz_bin_wasm_get_sections_by_id()

static RzList* rz_bin_wasm_get_sections_by_id ( RzList sections,
ut8  id 
)
static

Definition at line 162 of file wasm.c.

162  {
163  RzBinWasmSection *sec = NULL;
164  RzList *ret = rz_list_newf(NULL);
165  if (!ret) {
166  return NULL;
167  }
168  RzListIter *iter;
169  rz_list_foreach (sections, iter, sec) {
170  if (sec->id == id) {
171  rz_list_append(ret, sec);
172  }
173  }
174  return ret;
175 }
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110

References rz_bin_wasm_section_t::id, NULL, rz_list_append(), rz_list_newf(), and sections().

Referenced by rz_bin_wasm_get_codes(), rz_bin_wasm_get_custom_names(), rz_bin_wasm_get_datas(), rz_bin_wasm_get_elements(), rz_bin_wasm_get_entrypoint(), rz_bin_wasm_get_exports(), rz_bin_wasm_get_globals(), rz_bin_wasm_get_imports(), rz_bin_wasm_get_memories(), rz_bin_wasm_get_tables(), and rz_bin_wasm_get_types().

◆ rz_bin_wasm_get_start()

static RzBinWasmStartEntry* rz_bin_wasm_get_start ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 691 of file wasm.c.

691  {
692  RzBinWasmStartEntry *ptr;
693 
694  if (!(ptr = RZ_NEW0(RzBinWasmStartEntry))) {
695  return NULL;
696  }
697 
698  RzBuffer *b = bin->buf;
700  ut64 max = rz_buf_tell(b) + sec->payload_len - 1;
701  if (!(max < rz_buf_size(b))) {
702  goto beach;
703  }
704  if (!(consume_u32_r(b, max, &ptr->index))) {
705  goto beach;
706  }
707  return ptr;
708 beach:
709  RZ_LOG_ERROR("wasm: failed to read start payload\n");
710  free(ptr);
711  return NULL;
712 }

References b, bin::buf, consume_u32_r(), free(), rz_bin_wasm_start_t::index, max, NULL, rz_bin_wasm_section_t::payload_data, rz_bin_wasm_section_t::payload_len, rz_buf_seek(), RZ_BUF_SET, rz_buf_size(), rz_buf_tell(), RZ_LOG_ERROR, RZ_NEW0, and ut64().

Referenced by rz_bin_wasm_get_entrypoint().

◆ rz_bin_wasm_get_table_entries()

static RzList* rz_bin_wasm_get_table_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 718 of file wasm.c.

718  {
720 }
static void * parse_table_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:604

References free(), get_entries_from_section(), and parse_table_entry().

Referenced by rz_bin_wasm_get_tables().

◆ rz_bin_wasm_get_tables()

RzList* rz_bin_wasm_get_tables ( RzBinWasmObj bin)

Definition at line 1072 of file wasm.c.

1072  {
1073  RzBinWasmSection *table = NULL;
1074  RzList *tables = NULL;
1075 
1076  if (!bin || !bin->g_sections) {
1077  return NULL;
1078  }
1079  if (bin->g_tables) {
1080  return bin->g_tables;
1081  }
1082  if (!(tables = rz_bin_wasm_get_sections_by_id(bin->g_sections, RZ_BIN_WASM_SECTION_TABLE))) {
1083  return rz_list_new();
1084  }
1085  // support for multiple export sections against spec
1086  if (!(table = (RzBinWasmSection *)rz_list_first(tables))) {
1087  rz_list_free(tables);
1088  return rz_list_new();
1089  }
1090  bin->g_tables = rz_bin_wasm_get_table_entries(bin, table);
1091  rz_list_free(tables);
1092  return bin->g_tables;
1093 }
static RzList * rz_bin_wasm_get_table_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:718

References NULL, rz_bin_wasm_get_sections_by_id(), rz_bin_wasm_get_table_entries(), RZ_BIN_WASM_SECTION_TABLE, rz_list_first(), rz_list_free(), and rz_list_new().

Referenced by rz_bin_wasm_init().

◆ rz_bin_wasm_get_type_entries()

static RzList* rz_bin_wasm_get_type_entries ( RzBinWasmObj bin,
RzBinWasmSection sec 
)
static

Definition at line 671 of file wasm.c.

671  {
673 }
static void * parse_type_entry(RzBuffer *b, ut64 max)
Definition: wasm.c:268

References get_entries_from_section(), parse_type_entry(), and rz_bin_wasm_free_types().

Referenced by rz_bin_wasm_get_types().

◆ rz_bin_wasm_get_types()

RzList* rz_bin_wasm_get_types ( RzBinWasmObj bin)

Definition at line 1049 of file wasm.c.

1049  {
1051  RzList *types = NULL;
1052 
1053  if (!bin || !bin->g_sections) {
1054  return NULL;
1055  }
1056  if (bin->g_types) {
1057  return bin->g_types;
1058  }
1060  return rz_list_new();
1061  }
1062  // support for multiple export sections against spec
1063  if (!(type = (RzBinWasmSection *)rz_list_first(types))) {
1065  return rz_list_new();
1066  }
1069  return bin->g_types;
1070 }
static RzList * rz_bin_wasm_get_type_entries(RzBinWasmObj *bin, RzBinWasmSection *sec)
Definition: wasm.c:671
int type
Definition: mipsasm.c:17
insn_type_descr_t types[]
Definition: or1k_disas.c:7

References NULL, rz_bin_wasm_get_sections_by_id(), rz_bin_wasm_get_type_entries(), RZ_BIN_WASM_SECTION_TYPE, rz_list_first(), rz_list_free(), rz_list_new(), type, and types.

Referenced by rz_bin_wasm_init().

◆ rz_bin_wasm_init()

RzBinWasmObj* rz_bin_wasm_init ( RzBinFile bf,
RzBuffer buf 
)

Definition at line 761 of file wasm.c.

761  {
763  if (!bin) {
764  return NULL;
765  }
766  bin->buf = rz_buf_ref(buf);
767  bin->size = (ut32)rz_buf_size(bf->buf);
768  bin->g_sections = rz_bin_wasm_get_sections(bin);
769  // TODO: recursive invocation more natural with streamed parsing
770  // but dependency problems when sections are disordered (against spec)
771 
772  bin->g_types = rz_bin_wasm_get_types(bin);
773  bin->g_imports = rz_bin_wasm_get_imports(bin);
774  bin->g_exports = rz_bin_wasm_get_exports(bin);
775  bin->g_tables = rz_bin_wasm_get_tables(bin);
776  bin->g_memories = rz_bin_wasm_get_memories(bin);
777  bin->g_globals = rz_bin_wasm_get_globals(bin);
778  bin->g_codes = rz_bin_wasm_get_codes(bin);
779  bin->g_datas = rz_bin_wasm_get_datas(bin);
780 
782 
783  // entrypoint from Start section
784  bin->entrypoint = rz_bin_wasm_get_entrypoint(bin);
785 
786  return bin;
787 }
RzList * rz_bin_wasm_get_memories(RzBinWasmObj *bin)
Definition: wasm.c:1095
RzList * rz_bin_wasm_get_sections(RzBinWasmObj *bin)
Definition: wasm.c:846
RzList * rz_bin_wasm_get_custom_names(RzBinWasmObj *bin)
Definition: wasm.c:1215
RzList * rz_bin_wasm_get_datas(RzBinWasmObj *bin)
Definition: wasm.c:1192
ut32 rz_bin_wasm_get_entrypoint(RzBinWasmObj *bin)
Definition: wasm.c:966
RzList * rz_bin_wasm_get_imports(RzBinWasmObj *bin)
Definition: wasm.c:1003
RzList * rz_bin_wasm_get_codes(RzBinWasmObj *bin)
Definition: wasm.c:1168
RzList * rz_bin_wasm_get_exports(RzBinWasmObj *bin)
Definition: wasm.c:1026
RzList * rz_bin_wasm_get_globals(RzBinWasmObj *bin)
Definition: wasm.c:1122
RzList * rz_bin_wasm_get_types(RzBinWasmObj *bin)
Definition: wasm.c:1049
RzList * rz_bin_wasm_get_tables(RzBinWasmObj *bin)
Definition: wasm.c:1072
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
RzBuffer * buf
Definition: rz_bin.h:303

References rz_bin_file_t::buf, bin::buf, NULL, rz_bin_wasm_get_codes(), rz_bin_wasm_get_custom_names(), rz_bin_wasm_get_datas(), rz_bin_wasm_get_entrypoint(), rz_bin_wasm_get_exports(), rz_bin_wasm_get_globals(), rz_bin_wasm_get_imports(), rz_bin_wasm_get_memories(), rz_bin_wasm_get_sections(), rz_bin_wasm_get_tables(), rz_bin_wasm_get_types(), rz_buf_ref(), rz_buf_size(), and RZ_NEW0.

Referenced by load_buffer().