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

Go to the source code of this file.

Classes

struct  rz_bin_wasm_init_expr_t
 
struct  rz_bin_wasm_resizable_limits_t
 
struct  rz_bin_wasm_name_t
 
struct  rz_bin_wasm_section_t
 
struct  rz_bin_wasm_type_t
 
struct  rz_bin_wasm_global_type_t
 
struct  rz_bin_wasm_table_type_t
 
struct  rz_bin_wasm_memory_type_t
 
struct  rz_bin_wasm_import_t
 
struct  rz_bin_wasm_function_t
 
struct  rz_bin_wasm_table_t
 
struct  rz_bin_wasm_memory_t
 
struct  rz_bin_wasm_global_t
 
struct  rz_bin_wasm_export_t
 
struct  rz_bin_wasm_start_t
 
struct  rz_bin_wasm_local_entry_t
 
struct  rz_bin_wasm_element_t
 
struct  rz_bin_wasm_code_t
 
struct  rz_bin_wasm_data_t
 
struct  rz_bin_wasm_custom_name_function_names_t
 
struct  rz_bin_wasm_custom_name_local_name_t
 
struct  rz_bin_wasm_custom_name_local_names_t
 
struct  rz_bin_wasm_custom_name_entry_t
 
struct  rz_bin_wasm_obj_t
 

Macros

#define RZ_BIN_WASM_MAGIC_BYTES
 
#define RZ_BIN_WASM_VERSION   0x1
 
#define RZ_BIN_WASM_STRING_LENGTH   256
 
#define RZ_BIN_WASM_END_OF_CODE   0xb
 
#define RZ_BIN_WASM_SECTION_CUSTOM   0x0
 
#define RZ_BIN_WASM_SECTION_TYPE   0x1
 
#define RZ_BIN_WASM_SECTION_IMPORT   0x2
 
#define RZ_BIN_WASM_SECTION_FUNCTION   0x3
 
#define RZ_BIN_WASM_SECTION_TABLE   0x4
 
#define RZ_BIN_WASM_SECTION_MEMORY   0x5
 
#define RZ_BIN_WASM_SECTION_GLOBAL   0x6
 
#define RZ_BIN_WASM_SECTION_EXPORT   0x7
 
#define RZ_BIN_WASM_SECTION_START   0x8
 
#define RZ_BIN_WASM_SECTION_ELEMENT   0x9
 
#define RZ_BIN_WASM_SECTION_CODE   0xa
 
#define RZ_BIN_WASM_SECTION_DATA   0xb
 

Typedefs

typedef struct rz_bin_wasm_section_t RzBinWasmSection
 
typedef struct rz_bin_wasm_type_t RzBinWasmTypeEntry
 
typedef struct rz_bin_wasm_import_t RzBinWasmImportEntry
 
typedef struct rz_bin_wasm_function_t RzBinWasmFunctionEntry
 
typedef struct rz_bin_wasm_table_t RzBinWasmTableEntry
 
typedef struct rz_bin_wasm_memory_t RzBinWasmMemoryEntry
 
typedef struct rz_bin_wasm_global_t RzBinWasmGlobalEntry
 
typedef struct rz_bin_wasm_export_t RzBinWasmExportEntry
 
typedef struct rz_bin_wasm_start_t RzBinWasmStartEntry
 
typedef struct rz_bin_wasm_element_t RzBinWasmElementEntry
 
typedef struct rz_bin_wasm_code_t RzBinWasmCodeEntry
 
typedef struct rz_bin_wasm_data_t RzBinWasmDataEntry
 
typedef struct rz_bin_wasm_custom_name_function_names_t RzBinWasmCustomNameFunctionNames
 
typedef struct rz_bin_wasm_custom_name_local_name_t RzBinWasmCustomNameLocalName
 
typedef struct rz_bin_wasm_custom_name_local_names_t RzBinWasmCustomNameLocalNames
 
typedef struct rz_bin_wasm_custom_name_entry_t RzBinWasmCustomNameEntry
 
typedef struct rz_bin_wasm_obj_t RzBinWasmObj
 

Enumerations

enum  rz_bin_wasm_value_type_t {
  RZ_BIN_WASM_VALUETYPE_i32 = 0x1 , RZ_BIN_WASM_VALUETYPE_i64 = 0x2 , RZ_BIN_WASM_VALUETYPE_f32 = 0x3 , RZ_BIN_WASM_VALUETYPE_f64 = 0x4 ,
  RZ_BIN_WASM_VALUETYPE_v128 = 0x5 , RZ_BIN_WASM_VALUETYPE_ANYFUNC = 0x10 , RZ_BIN_WASM_VALUETYPE_FUNC = 0x20 , RZ_BIN_WASM_VALUETYPE_EMPTY = 0x40
}
 
enum  rz_bin_wasm_external_kind_t { RZ_BIN_WASM_EXTERNALKIND_Function = 0x0 , RZ_BIN_WASM_EXTERNALKIND_Table = 0x1 , RZ_BIN_WASM_EXTERNALKIND_Memory = 0x2 , RZ_BIN_WASM_EXTERNALKIND_Global = 0x3 }
 
enum  rz_bin_wasm_custom_name_type_t { RZ_BIN_WASM_NAMETYPE_Module = 0x0 , RZ_BIN_WASM_NAMETYPE_Function = 0x1 , RZ_BIN_WASM_NAMETYPE_Local = 0x2 }
 

Functions

RzBinWasmObjrz_bin_wasm_init (RzBinFile *bf, RzBuffer *buf)
 
void rz_bin_wasm_destroy (RzBinFile *bf)
 
RzListrz_bin_wasm_get_sections (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_types (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_imports (RzBinWasmObj *bin)
 
RzListrz_bin_wasm_get_exports (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)
 
ut32 rz_bin_wasm_get_entrypoint (RzBinWasmObj *bin)
 
const char * rz_bin_wasm_get_function_name (RzBinWasmObj *bin, ut32 idx)
 
const char * rz_bin_wasm_valuetype_to_string (rz_bin_wasm_value_type_t type)
 

Macro Definition Documentation

◆ RZ_BIN_WASM_END_OF_CODE

#define RZ_BIN_WASM_END_OF_CODE   0xb

Definition at line 19 of file wasm.h.

◆ RZ_BIN_WASM_MAGIC_BYTES

#define RZ_BIN_WASM_MAGIC_BYTES
Value:
"\x00" \
"asm"

Definition at line 15 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_CODE

#define RZ_BIN_WASM_SECTION_CODE   0xa

Definition at line 31 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_CUSTOM

#define RZ_BIN_WASM_SECTION_CUSTOM   0x0

Definition at line 21 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_DATA

#define RZ_BIN_WASM_SECTION_DATA   0xb

Definition at line 32 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_ELEMENT

#define RZ_BIN_WASM_SECTION_ELEMENT   0x9

Definition at line 30 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_EXPORT

#define RZ_BIN_WASM_SECTION_EXPORT   0x7

Definition at line 28 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_FUNCTION

#define RZ_BIN_WASM_SECTION_FUNCTION   0x3

Definition at line 24 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_GLOBAL

#define RZ_BIN_WASM_SECTION_GLOBAL   0x6

Definition at line 27 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_IMPORT

#define RZ_BIN_WASM_SECTION_IMPORT   0x2

Definition at line 23 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_MEMORY

#define RZ_BIN_WASM_SECTION_MEMORY   0x5

Definition at line 26 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_START

#define RZ_BIN_WASM_SECTION_START   0x8

Definition at line 29 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_TABLE

#define RZ_BIN_WASM_SECTION_TABLE   0x4

Definition at line 25 of file wasm.h.

◆ RZ_BIN_WASM_SECTION_TYPE

#define RZ_BIN_WASM_SECTION_TYPE   0x1

Definition at line 22 of file wasm.h.

◆ RZ_BIN_WASM_STRING_LENGTH

#define RZ_BIN_WASM_STRING_LENGTH   256

Definition at line 18 of file wasm.h.

◆ RZ_BIN_WASM_VERSION

#define RZ_BIN_WASM_VERSION   0x1

Definition at line 17 of file wasm.h.

Typedef Documentation

◆ RzBinWasmCodeEntry

◆ RzBinWasmCustomNameEntry

◆ RzBinWasmCustomNameFunctionNames

◆ RzBinWasmCustomNameLocalName

◆ RzBinWasmCustomNameLocalNames

◆ RzBinWasmDataEntry

◆ RzBinWasmElementEntry

◆ RzBinWasmExportEntry

◆ RzBinWasmFunctionEntry

◆ RzBinWasmGlobalEntry

◆ RzBinWasmImportEntry

◆ RzBinWasmMemoryEntry

◆ RzBinWasmObj

◆ RzBinWasmSection

◆ RzBinWasmStartEntry

◆ RzBinWasmTableEntry

◆ RzBinWasmTypeEntry

Enumeration Type Documentation

◆ rz_bin_wasm_custom_name_type_t

Enumerator
RZ_BIN_WASM_NAMETYPE_Module 
RZ_BIN_WASM_NAMETYPE_Function 
RZ_BIN_WASM_NAMETYPE_Local 

Definition at line 52 of file wasm.h.

52  {
rz_bin_wasm_custom_name_type_t
Definition: wasm.h:52
@ 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_bin_wasm_external_kind_t

Enumerator
RZ_BIN_WASM_EXTERNALKIND_Function 
RZ_BIN_WASM_EXTERNALKIND_Table 
RZ_BIN_WASM_EXTERNALKIND_Memory 
RZ_BIN_WASM_EXTERNALKIND_Global 

Definition at line 45 of file wasm.h.

45  {
rz_bin_wasm_external_kind_t
Definition: wasm.h:45
@ RZ_BIN_WASM_EXTERNALKIND_Memory
Definition: wasm.h:48
@ RZ_BIN_WASM_EXTERNALKIND_Global
Definition: wasm.h:49
@ RZ_BIN_WASM_EXTERNALKIND_Table
Definition: wasm.h:47
@ RZ_BIN_WASM_EXTERNALKIND_Function
Definition: wasm.h:46

◆ rz_bin_wasm_value_type_t

Enumerator
RZ_BIN_WASM_VALUETYPE_i32 
RZ_BIN_WASM_VALUETYPE_i64 
RZ_BIN_WASM_VALUETYPE_f32 
RZ_BIN_WASM_VALUETYPE_f64 
RZ_BIN_WASM_VALUETYPE_v128 
RZ_BIN_WASM_VALUETYPE_ANYFUNC 
RZ_BIN_WASM_VALUETYPE_FUNC 
RZ_BIN_WASM_VALUETYPE_EMPTY 

Definition at line 34 of file wasm.h.

34  {
rz_bin_wasm_value_type_t
Definition: wasm.h:34
@ RZ_BIN_WASM_VALUETYPE_v128
Definition: wasm.h:39
@ RZ_BIN_WASM_VALUETYPE_i32
Definition: wasm.h:35
@ RZ_BIN_WASM_VALUETYPE_f64
Definition: wasm.h:38
@ RZ_BIN_WASM_VALUETYPE_f32
Definition: wasm.h:37
@ RZ_BIN_WASM_VALUETYPE_i64
Definition: wasm.h:36
@ RZ_BIN_WASM_VALUETYPE_EMPTY
Definition: wasm.h:42
@ RZ_BIN_WASM_VALUETYPE_ANYFUNC
Definition: wasm.h:40
@ RZ_BIN_WASM_VALUETYPE_FUNC
Definition: wasm.h:41

Function Documentation

◆ 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
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API void rz_id_storage_free(RzIDStorage *storage)
Definition: idpool.c:270
Definition: malloc.c:26
unsigned char * buf
Definition: gzjoin.c:83
RzBinObject * o
Definition: rz_bin.h:305
void * bin_obj
Definition: rz_bin.h:293
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

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_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_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
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_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
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
char name[RZ_BIN_WASM_STRING_LENGTH]
Definition: wasm.h:78

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_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_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
uint32_t ut32
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_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 }
int n
Definition: mipsasm.c:19
int idx
Definition: setup.py:197
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_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_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_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 }
static size_t consume_str_r(RzBuffer *b, ut64 max, size_t sz, char *out)
Definition: wasm.c:77
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
#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
int max
Definition: enough.c:225
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
RZ_API ut64 rz_buf_tell(RZ_NONNULL RzBuffer *b)
Return the current cursor position.
Definition: buf.c:1238
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_CUR
Definition: rz_buf.h:15
#define RZ_BUF_SET
Definition: rz_buf.h:14
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define b(i)
Definition: sha256.c:42
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

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_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_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
voidpf void * buf
Definition: ioapi.h:138
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().

◆ rz_bin_wasm_valuetype_to_string()

const char* rz_bin_wasm_valuetype_to_string ( rz_bin_wasm_value_type_t  type)