Rizin
unix-like reverse engineering framework and cli tools
wasm.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017-2018 cgvwzq
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 #include <rz_lib.h>
7 #include <rz_bin.h>
8 
9 #ifndef _INCLUDE_WASM_H_
10 #define _INCLUDE_WASM_H_
11 
12 // version 0x1 (WIP)
13 // https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
14 
15 #define RZ_BIN_WASM_MAGIC_BYTES "\x00" \
16  "asm"
17 #define RZ_BIN_WASM_VERSION 0x1
18 #define RZ_BIN_WASM_STRING_LENGTH 256
19 #define RZ_BIN_WASM_END_OF_CODE 0xb
20 
21 #define RZ_BIN_WASM_SECTION_CUSTOM 0x0
22 #define RZ_BIN_WASM_SECTION_TYPE 0x1
23 #define RZ_BIN_WASM_SECTION_IMPORT 0x2
24 #define RZ_BIN_WASM_SECTION_FUNCTION 0x3
25 #define RZ_BIN_WASM_SECTION_TABLE 0x4
26 #define RZ_BIN_WASM_SECTION_MEMORY 0x5
27 #define RZ_BIN_WASM_SECTION_GLOBAL 0x6
28 #define RZ_BIN_WASM_SECTION_EXPORT 0x7
29 #define RZ_BIN_WASM_SECTION_START 0x8
30 #define RZ_BIN_WASM_SECTION_ELEMENT 0x9
31 #define RZ_BIN_WASM_SECTION_CODE 0xa
32 #define RZ_BIN_WASM_SECTION_DATA 0xb
33 
34 typedef enum {
44 
45 typedef enum {
51 
52 typedef enum {
57 
59  // bytecode terminated in 0xb
60  size_t len;
61 };
62 
64  ut8 flags; // 1 if max field is present, 0 otherwise
67 };
68 
72 };
73 
74 typedef struct rz_bin_wasm_section_t {
84 
85 typedef struct rz_bin_wasm_type_t {
89  st8 return_count; // MVP = 1
93 
94 // Other Types
98 };
99 
103 };
104 
107 };
108 
109 typedef struct rz_bin_wasm_import_t {
115  union {
120  };
121 
123 
124 typedef struct rz_bin_wasm_function_t {
125  ut32 type_index; // index to Type entries
127 
128 typedef struct rz_bin_wasm_table_t {
129  ut8 element_type; // only anyfunc
132 
133 typedef struct rz_bin_wasm_memory_t {
136 
137 typedef struct rz_bin_wasm_global_t {
139  ut8 mutability; // 0 if immutable, 1 if mutable
142 
143 typedef struct rz_bin_wasm_export_t {
149 
150 typedef struct rz_bin_wasm_start_t {
153 
157 };
158 
159 typedef struct rz_bin_wasm_element_t {
165 
166 typedef struct rz_bin_wasm_code_t {
168  ut32 local_count; // numer of local entries
170  ut32 code; // offset
171  ut32 len; // real bytecode length
172  ut8 byte; // 0xb, indicating end of the body
174  char *signature;
176 
177 typedef struct rz_bin_wasm_data_t {
178  ut32 index; // linear memory index (0 in MVP)
179  struct rz_bin_wasm_init_expr_t offset; // bytecode evaluated at runtime
181  ut32 data; // offset
183 
184 // TODO: custom sections
185 
190 
192  ut32 index; // function index
193 
195  RzIDStorage *names; // local names
197 
200  RzList *locals; // RzBinWasmCustomNameLocalName
202 
203 // "name" section entry
207 
209  union {
213  };
215 
216 typedef struct rz_bin_wasm_obj_t {
217 
219  size_t size;
220 
222 
223  // cache purposes
235 
237  // etc...
238 
240 
257 
258 #endif
RzList * rz_bin_wasm_get_memories(RzBinWasmObj *bin)
Definition: wasm.c:1095
struct rz_bin_wasm_custom_name_local_names_t RzBinWasmCustomNameLocalNames
RzList * rz_bin_wasm_get_sections(RzBinWasmObj *bin)
Definition: wasm.c:846
struct rz_bin_wasm_custom_name_entry_t RzBinWasmCustomNameEntry
struct rz_bin_wasm_custom_name_local_name_t RzBinWasmCustomNameLocalName
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
struct rz_bin_wasm_import_t RzBinWasmImportEntry
RzList * rz_bin_wasm_get_custom_names(RzBinWasmObj *bin)
Definition: wasm.c:1215
struct rz_bin_wasm_export_t RzBinWasmExportEntry
RzList * rz_bin_wasm_get_datas(RzBinWasmObj *bin)
Definition: wasm.c:1192
ut32 rz_bin_wasm_get_entrypoint(RzBinWasmObj *bin)
Definition: wasm.c:966
struct rz_bin_wasm_data_t RzBinWasmDataEntry
struct rz_bin_wasm_memory_t RzBinWasmMemoryEntry
const char * rz_bin_wasm_get_function_name(RzBinWasmObj *bin, ut32 idx)
Definition: wasm.c:1237
struct rz_bin_wasm_start_t RzBinWasmStartEntry
struct rz_bin_wasm_custom_name_function_names_t RzBinWasmCustomNameFunctionNames
struct rz_bin_wasm_obj_t RzBinWasmObj
RzList * rz_bin_wasm_get_elements(RzBinWasmObj *bin)
Definition: wasm.c:1145
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
RzList * rz_bin_wasm_get_imports(RzBinWasmObj *bin)
Definition: wasm.c:1003
RzList * rz_bin_wasm_get_codes(RzBinWasmObj *bin)
Definition: wasm.c:1168
const char * rz_bin_wasm_valuetype_to_string(rz_bin_wasm_value_type_t type)
struct rz_bin_wasm_global_t RzBinWasmGlobalEntry
RzList * rz_bin_wasm_get_exports(RzBinWasmObj *bin)
Definition: wasm.c:1026
struct rz_bin_wasm_type_t RzBinWasmTypeEntry
struct rz_bin_wasm_code_t RzBinWasmCodeEntry
RzList * rz_bin_wasm_get_globals(RzBinWasmObj *bin)
Definition: wasm.c:1122
struct rz_bin_wasm_table_t RzBinWasmTableEntry
#define RZ_BIN_WASM_STRING_LENGTH
Definition: wasm.h:18
RzList * rz_bin_wasm_get_types(RzBinWasmObj *bin)
Definition: wasm.c:1049
RzBinWasmObj * rz_bin_wasm_init(RzBinFile *bf, RzBuffer *buf)
Definition: wasm.c:761
struct rz_bin_wasm_function_t RzBinWasmFunctionEntry
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
struct rz_bin_wasm_element_t RzBinWasmElementEntry
struct rz_bin_wasm_section_t RzBinWasmSection
void rz_bin_wasm_destroy(RzBinFile *bf)
Definition: wasm.c:789
RzList * rz_bin_wasm_get_tables(RzBinWasmObj *bin)
Definition: wasm.c:1072
uint32_t ut32
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
int type
Definition: mipsasm.c:17
int idx
Definition: setup.py:197
#define st8
Definition: rz_types_base.h:16
Definition: malloc.c:26
Definition: z80asm.h:102
XX curplugin == o->plugin.
Definition: rz_bin.h:298
ut32 body_size
Definition: wasm.h:167
struct rz_bin_wasm_local_entry_t * locals
Definition: wasm.h:169
ut32 local_count
Definition: wasm.h:168
char * signature
Definition: wasm.h:174
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
ut8 payload_data
Definition: wasm.h:208
RzBinWasmCustomNameLocalNames * local
Definition: wasm.h:212
ut32 size
Definition: wasm.h:206
struct rz_bin_wasm_init_expr_t init
Definition: wasm.h:161
char field_str[RZ_BIN_WASM_STRING_LENGTH]
Definition: wasm.h:145
rz_bin_wasm_value_type_t content_type
Definition: wasm.h:138
struct rz_bin_wasm_init_expr_t init
Definition: wasm.h:140
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
Definition: wasm.h:154
ut32 count
Definition: wasm.h:155
rz_bin_wasm_value_type_t type
Definition: wasm.h:156
struct rz_bin_wasm_resizable_limits_t limits
Definition: wasm.h:134
struct rz_bin_wasm_resizable_limits_t limits
Definition: wasm.h:106
RzList * g_memories
Definition: wasm.h:229
RzList * g_names
Definition: wasm.h:236
RzBuffer * buf
Definition: wasm.h:218
RzList * g_imports
Definition: wasm.h:226
RzList * g_exports
Definition: wasm.h:227
size_t size
Definition: wasm.h:219
RzList * g_types
Definition: wasm.h:225
ut32 entrypoint
Definition: wasm.h:221
RzList * g_globals
Definition: wasm.h:230
RzList * g_datas
Definition: wasm.h:233
RzList * g_codes
Definition: wasm.h:232
RzList * g_sections
Definition: wasm.h:224
RzList * g_tables
Definition: wasm.h:228
RzBinWasmStartEntry * g_start
Definition: wasm.h:234
RzList * g_elements
Definition: wasm.h:231
struct rz_bin_wasm_resizable_limits_t limits
Definition: wasm.h:130
struct rz_bin_wasm_resizable_limits_t limits
Definition: wasm.h:102
rz_bin_wasm_value_type_t elem_type
Definition: wasm.h:101
rz_bin_wasm_value_type_t return_type
Definition: wasm.h:90
rz_bin_wasm_value_type_t * param_types
Definition: wasm.h:88
char to_str[RZ_BIN_WASM_STRING_LENGTH]
Definition: wasm.h:91
st8 return_count
Definition: wasm.h:89
ut32 param_count
Definition: wasm.h:87