-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
222 lines (187 loc) · 7.48 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "fixes.h"
#define __USE_ISOC99 1
#include <math.h>
#undef __USE_ISOC99
/* Call table/trapping table lookups/execution*/
#include <signal.h>
#include <unistd.h>
#include "platform.h"
#include "thunk.h"
#include "util.h"
#include "wa.h"
/* WASM test files */
unsigned char arith_wasm[] = {
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01,
0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x05, 0x04, 0x00, 0x00,
0x00, 0x00, 0x07, 0x19, 0x04, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00,
0x03, 0x73, 0x75, 0x62, 0x00, 0x01, 0x03, 0x6d, 0x75, 0x6c, 0x00,
0x02, 0x03, 0x64, 0x69, 0x76, 0x00, 0x03, 0x0a, 0x21, 0x04, 0x07,
0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b, 0x07, 0x00, 0x20, 0x00,
0x20, 0x01, 0x6b, 0x0b, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6c,
0x0b, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6e, 0x0b};
unsigned int arith_wasm_len = 86;
const char* arith_wasm_funcs[] = {"add",
"sub"
"mul",
"div"};
unsigned char wasi_test_wasm[] = {
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60,
0x00, 0x00, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x04, 0x03, 0x00, 0x01,
0x01, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00,
0x02, 0x06, 0x15, 0x03, 0x7f, 0x01, 0x41, 0x80, 0x88, 0x04, 0x0b, 0x7f,
0x00, 0x41, 0x80, 0x88, 0x04, 0x0b, 0x7f, 0x00, 0x41, 0x80, 0x08, 0x0b,
0x07, 0x34, 0x05, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00,
0x0b, 0x5f, 0x5f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x62, 0x61, 0x73, 0x65,
0x03, 0x01, 0x0a, 0x5f, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x65, 0x6e,
0x64, 0x03, 0x02, 0x03, 0x66, 0x69, 0x62, 0x00, 0x01, 0x06, 0x66, 0x69,
0x62, 0x52, 0x65, 0x63, 0x00, 0x02, 0x0a, 0x5c, 0x03, 0x02, 0x00, 0x0b,
0x2e, 0x01, 0x03, 0x7f, 0x41, 0x01, 0x21, 0x01, 0x20, 0x00, 0x41, 0x01,
0x6a, 0x21, 0x00, 0x41, 0x00, 0x21, 0x02, 0x03, 0x40, 0x20, 0x01, 0x22,
0x03, 0x20, 0x02, 0x6a, 0x21, 0x01, 0x20, 0x03, 0x21, 0x02, 0x20, 0x00,
0x41, 0x7f, 0x6a, 0x22, 0x00, 0x0d, 0x00, 0x0b, 0x20, 0x01, 0x0b, 0x28,
0x00, 0x02, 0x40, 0x02, 0x40, 0x20, 0x00, 0x45, 0x0d, 0x00, 0x20, 0x00,
0x41, 0x01, 0x47, 0x0d, 0x01, 0x41, 0x01, 0x0f, 0x0b, 0x41, 0x00, 0x0f,
0x0b, 0x20, 0x00, 0x41, 0x7f, 0x6a, 0x10, 0x82, 0x80, 0x80, 0x80, 0x00,
0x20, 0x00, 0x6a, 0x0b, 0x00, 0x28, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x01,
0x21, 0x03, 0x00, 0x11, 0x5f, 0x5f, 0x77, 0x61, 0x73, 0x6d, 0x5f, 0x63,
0x61, 0x6c, 0x6c, 0x5f, 0x63, 0x74, 0x6f, 0x72, 0x73, 0x01, 0x03, 0x66,
0x69, 0x62, 0x02, 0x06, 0x66, 0x69, 0x62, 0x52, 0x65, 0x63};
unsigned int wasi_test_wasm_len = 250;
const char* wasi_test_wasm_funcs[] = {"fib", "fibRec"};
/* //////////////////////////////////////////////////////// */
/* memory layout */
#ifdef LOW_MEMORY_CONFIG
#define PAGE_COUNT 1 // 64K each
#define TOTAL_PAGES 1
#define TABLE_COUNT 2
#else
#define PAGE_COUNT 1 // 64K each
#define TOTAL_PAGES 0x100000 / PAGE_SIZE // use 1MByte of memory
#define TABLE_COUNT 20
#endif
Memory _env__memory_ = {PAGE_COUNT, // initial size (64K pages)
TOTAL_PAGES, // max size (64K pages)
PAGE_COUNT, // current size (64K pages)
NULL}; // memory base
uint8_t* _env__memoryBase_;
Table _env__table_ = {ANYFUNC, // on;y allowed value in WASM MVP
TABLE_COUNT, // initial
TABLE_COUNT, // max
TABLE_COUNT, // current
0};
// uint32_t *_env__table_ = 0;
uint32_t* _env__tableBase_;
/*double _global__NaN_ = NAN;*/
/*double _global__Infinity_ = INFINITY;*/
uint32_t** _env__DYNAMICTOP_PTR_;
uint32_t* _env__tempDoublePtr_;
// Initialize memory globals
void init_wac_eps(void) {
_env__memoryBase_ = calloc(PAGE_COUNT, PAGE_SIZE);
_env__tempDoublePtr_ = (uint32_t*)_env__memoryBase_;
_env__DYNAMICTOP_PTR_ = (uint32_t**)(_env__memoryBase_ + 16);
*_env__DYNAMICTOP_PTR_ =
(uint32_t*)(_env__memoryBase_ + PAGE_COUNT * PAGE_SIZE);
// This arrangement correlates to the module mangle_table_offset option
// if (posix_memalign((void **)&_env__table_.entries,
// sysconf(_SC_PAGESIZE),
// TABLE_COUNT*sizeof(uint32_t))) {
// perror("posix_memalign");
_env__table_.entries = malloc(TABLE_COUNT * sizeof(uint32_t));
if (_env__table_.entries == NULL) {
//
perror("init_wac_eps: malloc(tablecount....)");
exit(1);
}
_env__tableBase_ = _env__table_.entries;
wa_info("init_mem results:\n");
wa_info(" _env__memory_.bytes: %p\n", _env__memory_.bytes);
wa_info(" _env__memoryBase_: %p\n", _env__memoryBase_);
wa_info(" _env__DYNAMIC_TOP_PTR_: %p\n", _env__DYNAMICTOP_PTR_);
wa_info(" *_env__DYNAMIC_TOP_PTR_: %p\n", *_env__DYNAMICTOP_PTR_);
wa_info(" _env__table_.entries: %p\n", _env__table_.entries);
wa_info(" _env__tableBase_: %p\n", _env__tableBase_);
}
/////////////////////////////////////////////////////////
// Command line
int main(int argc, char** argv) {
char* mod_path = NULL;
int fidx = 0;
result_t res;
uint8_t* bytes = NULL;
int byte_count;
Options opts;
Module* m;
FILE* f;
opts.disable_memory_bounds = false;
opts.dlsym_trim_underscore = false;
opts.mangle_table_index = false;
// init_wac_eps();
// Load the module
// bytes = arith_wasm;
// byte_count = arith_wasm_len;
// bytes = wasi_test_wasm;
// byte_count = wasi_test_wasm_len;
f = fopen("/home/cub3d/wabt/nop.0.wasm", "rb");
if (f) {
fseek(f, 0, SEEK_END);
byte_count = ftell(f);
fseek(f, 0, SEEK_SET);
bytes = malloc(byte_count);
if (bytes) {
(void)fread(bytes, 1, byte_count, f);
}
fclose(f);
}
if (bytes == NULL) {
fprintf(stderr, "Could not load %s", mod_path);
return 2;
}
m = load_module(bytes, byte_count, opts);
m->path = "arith,wasm";
init_thunk_in(m);
// setup argc/argv
m->stack[++m->sp].value_type = I32;
m->stack[m->sp].value.uint32 = 3;
// m->stack[m->sp].value.uint32 = 361; // mac result that fits to two
// bytes
// Invoke main/_main function and exit
fidx = get_export_fidx(m, "fibRec", strlen("fibRec") + 1);
if (fidx == -1) {
fidx = get_export_fidx(m, "fib", strlen("fib") + 1);
if (fidx == -1) {
FATAL("no exported function named 'fib' or 'finbRec'\n");
}
}
res = invoke(m, fidx);
if (res_err(res)) {
wa_error("Exception: %s\n", res_err_msg(res));
return 1;
}
if (m->sp >= 0) {
StackValue* result = &m->stack[m->sp--];
switch (result->value_type) {
case I32:
printf("I32 return value: 0x%x:i32", result->value.uint32);
break;
case I64:
printf("I64 return value: 0x%llx:i64", result->value.uint64);
break;
case F32:
printf("F32 return value: %.7g:f32", result->value.f32);
break;
case F64:
printf("F64 return value: %.7g:f64", result->value.f64);
break;
}
// value_repr(&m->stack[m->sp--]);
} else {
printf("No result.\n");
}
printf("\n\n ------ :-) DONE ------\n\n");
return 0;
}