Skip to content

Commit 1c81ad6

Browse files
authored
Enhance wasm loader and update build app document (#147)
1 parent ab15747 commit 1c81ad6

File tree

7 files changed

+276
-129
lines changed

7 files changed

+276
-129
lines changed

core/iwasm/lib/native/libc/libc_builtin_wrapper.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ _printf_hex_uint(out_func_t out, void *ctx,
8080
enum pad_type padding,
8181
int min_width)
8282
{
83-
int size = sizeof(num) * (is_u64 ? 2 : 1);
83+
int shift = sizeof(num) * 8;
8484
int found_largest_digit = 0;
85-
int remaining = 8; /* 8 digits max */
85+
int remaining = 16; /* 16 digits max */
8686
int digits = 0;
87+
char nibble;
8788

88-
for (; size; size--) {
89-
char nibble = (num >> ((size - 1) << 2) & 0xf);
89+
while (shift >= 4) {
90+
shift -= 4;
91+
nibble = (num >> shift) & 0xf;
9092

91-
if (nibble || found_largest_digit || size == 1) {
93+
if (nibble || found_largest_digit || shift == 0) {
9294
found_largest_digit = 1;
9395
nibble = (char)(nibble + (nibble > 9 ? 87 : 48));
9496
out((int) nibble, ctx);
@@ -1153,17 +1155,13 @@ __cxa_throw_wrapper(wasm_module_inst_t module_inst,
11531155
wasm_runtime_set_exception(module_inst, buf);
11541156
}
11551157

1156-
/*#define ENABLE_SPEC_TEST 1*/
1157-
1158-
#ifdef ENABLE_SPEC_TEST
1159-
static void
1160-
print_i32_wrapper(wasm_module_inst_t module_inst, int i32)
1161-
{
1162-
bh_printf("%d\n", i32);
1163-
}
1158+
#ifndef ENABLE_SPEC_TEST
1159+
#define ENABLE_SPEC_TEST 0
1160+
#endif
11641161

1162+
#if ENABLE_SPEC_TEST != 0
11651163
static void
1166-
print_wrapper(wasm_module_inst_t module_inst, int i32)
1164+
print_i32_wrapper(wasm_module_inst_t module_inst, int32 i32)
11671165
{
11681166
bh_printf("%d\n", i32);
11691167
}
@@ -1180,9 +1178,8 @@ typedef struct WASMNativeFuncDef {
11801178
} WASMNativeFuncDef;
11811179

11821180
static WASMNativeFuncDef native_func_defs[] = {
1183-
#ifdef ENABLE_SPEC_TEST
1181+
#if ENABLE_SPEC_TEST != 0
11841182
REG_NATIVE_FUNC(spectest, print_i32),
1185-
REG_NATIVE_FUNC(spectest, print),
11861183
#endif
11871184
REG_NATIVE_FUNC(env, _printf),
11881185
REG_NATIVE_FUNC(env, _sprintf),
@@ -1284,8 +1281,12 @@ typedef struct WASMNativeGlobalDef {
12841281
} WASMNativeGlobalDef;
12851282

12861283
static WASMNativeGlobalDef native_global_defs[] = {
1287-
#ifdef ENABLE_SPEC_TEST
1288-
{ "spectest", "global_i32", .global_data.u32 = 0 },
1284+
#if ENABLE_SPEC_TEST != 0
1285+
{ "spectest", "global_i32", .global_data.i32 = 666 },
1286+
{ "spectest", "global_f32", .global_data.f32 = 0 },
1287+
{ "spectest", "global_f64", .global_data.f64 = 0 },
1288+
{ "test", "global-i32", .global_data.i32 = 0 },
1289+
{ "test", "global-f32", .global_data.f32 = 0 },
12891290
#endif
12901291
{ "env", "STACKTOP", .global_data.u32 = 0 },
12911292
{ "env", "STACK_MAX", .global_data.u32 = 0 },

core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ __wasi_errno_t wasmtime_ssp_fd_pread(
846846

847847
struct fd_object *fo;
848848
__wasi_errno_t error = fd_object_get(curfds,
849-
&fo, fd, __WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_SEEK, 0);
849+
&fo, fd, __WASI_RIGHT_FD_READ, 0);
850850
if (error != 0)
851851
return error;
852852

@@ -918,7 +918,7 @@ __wasi_errno_t wasmtime_ssp_fd_pwrite(
918918

919919
struct fd_object *fo;
920920
__wasi_errno_t error = fd_object_get(curfds,
921-
&fo, fd, __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_SEEK, 0);
921+
&fo, fd, __WASI_RIGHT_FD_WRITE, 0);
922922
if (error != 0)
923923
return error;
924924

core/iwasm/runtime/vmcore-wasm/wasm.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ extern "C" {
6868
#define BLOCK_TYPE_IF 2
6969
#define BLOCK_TYPE_FUNCTION 3
7070

71-
#define CALL_TYPE_WRAPPER 0
72-
#define CALL_TYPE_C_INTRINSIC 1
73-
7471
typedef union WASMValue {
7572
int32 i32;
7673
uint32 u32;
@@ -140,8 +137,6 @@ typedef struct WASMFunctionImport {
140137
char *field_name;
141138
/* function type */
142139
WASMType *func_type;
143-
/* c intrinsic function or wrapper function */
144-
uint32 call_type;
145140
/* function pointer after linked */
146141
void *func_ptr_linked;
147142
} WASMFunctionImport;

core/iwasm/runtime/vmcore-wasm/wasm_interp.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,16 @@ wasm_interp_call_func_native(WASMThread *self,
612612

613613
wasm_thread_set_cur_frame (self, frame);
614614

615+
if (!cur_func->u.func_import->func_ptr_linked) {
616+
char buf[128];
617+
snprintf(buf,
618+
sizeof(buf), "fail to call unlinked import function (%s, %s)",
619+
cur_func->u.func_import->module_name,
620+
cur_func->u.func_import->field_name);
621+
wasm_runtime_set_exception(self->module_inst, buf);
622+
return;
623+
}
624+
615625
ret = wasm_runtime_invoke_native(cur_func->u.func_import->func_ptr_linked,
616626
cur_func->u.func_import->func_type,
617627
self->module_inst,
@@ -840,7 +850,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
840850
HANDLE_OP (WASM_OP_CALL_INDIRECT):
841851
{
842852
WASMType *cur_type, *cur_func_type;
843-
/* TODO: test */
853+
844854
read_leb_uint32(frame_ip, frame_ip_end, tidx);
845855
if (tidx >= module->module->type_count) {
846856
wasm_runtime_set_exception(module, "type index is overflow");
@@ -858,8 +868,11 @@ wasm_interp_call_func_bytecode(WASMThread *self,
858868
}
859869

860870
fidx = ((uint32*)table->base_addr)[val];
861-
/* Skip function index check, it has been checked
862-
in wasm module instantiate */
871+
if (fidx == (uint32)-1) {
872+
wasm_runtime_set_exception(module, "uninitialized element");
873+
goto got_exception;
874+
}
875+
863876
cur_func = module->functions + fidx;
864877

865878
if (cur_func->is_import_func)

0 commit comments

Comments
 (0)