Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent * Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default * Add a new extension library: connection * Fix bug of reading magic number and version in big endian platform * Re-org platform APIs: move most platform APIs from iwasm to shared-lib * Enhance wasm loader to fix some security issues * Fix issue about illegal load of EXC_RETURN into PC on stm32 board * Updates that let a restricted version of the interpreter run in SGX * Enable native/app address validation and conversion for wasm app * Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused * Refine binary size and fix several minor issues Optimize interpreter LOAD/STORE opcodes to decrease the binary size Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper Add macros of global heap size, stack size, heap size for Zephyr main.c Clear compile warning of wasm_application.c * Add more strict security checks for libc wrapper API's * Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files * Enhance security of libc strcpy/sprintf wrapper function * Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions * Remove get_module_inst() and fix issue of call native * Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate * Refine interpreter call native process, refine memory boudary check * Fix issues of invokeNative function of arm/mips/general version * Add a switch to build simple sample without gui support * Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code * Re-org shared lib header files, remove unused info; fix compile issues of vxworks * Add build target general * Remove unused files * Update license header * test push * Restore file * Sync up with internal/feature * Sync up with internal/feature * Rename build_wamr_app to build_wasm_app * Fix small issues of README * Enhance malformed wasm file checking Fix issue of print hex int and implement utf8 string check Fix wasi file read/write right issue Fix minor issue of build wasm app doc * Sync up with internal/feature * Sync up with internal/feature: fix interpreter arm issue, fix read leb issue * Sync up with internal/feature * Fix bug of config.h and rename wasi config.h to ssp_config.h * Sync up with internal/feature * Import wamr aot * update document * update document * Update document, disable WASI in 32bit * update document * remove files * update document * Update document * update document * update document * update samples * Sync up with internal repo
This commit is contained in:
69
core/iwasm/common/arch/invokeNative_arm.s
Normal file
69
core/iwasm/common/arch/invokeNative_arm.s
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
.text
|
||||
.align 2
|
||||
.global invokeNative
|
||||
.type invokeNative,function
|
||||
|
||||
/*
|
||||
* Arguments passed in:
|
||||
*
|
||||
* r0 function ptr
|
||||
* r1 argv
|
||||
* r2 argc
|
||||
*/
|
||||
|
||||
invokeNative:
|
||||
stmfd sp!, {r4, r5, r6, r7, lr}
|
||||
mov ip, r0 /* ip = function ptr */
|
||||
mov r4, r1 /* r4 = argv */
|
||||
mov r5, r2 /* r5 = argc */
|
||||
|
||||
cmp r5, #1 /* at least one argument required: exec_env */
|
||||
blt return
|
||||
|
||||
mov r6, #0 /* increased stack size */
|
||||
|
||||
ldr r0, [r4], #4 /* r0 = argv[0] = exec_env */
|
||||
cmp r5, #1
|
||||
beq call_func
|
||||
|
||||
ldr r1, [r4], #4 /* r1 = argv[1] */
|
||||
cmp r5, #2
|
||||
beq call_func
|
||||
|
||||
ldr r2, [r4], #4 /* r2 = argv[2] */
|
||||
cmp r5, #3
|
||||
beq call_func
|
||||
|
||||
ldr r3, [r4], #4 /* r3 = argv[3] */
|
||||
cmp r5, #4
|
||||
beq call_func
|
||||
|
||||
sub r5, r5, #4 /* argc -= 4, now we have r0 ~ r3 */
|
||||
|
||||
/* Ensure address is 8 byte aligned */
|
||||
mov r6, r5, lsl#2 /* r6 = argc * 4 */
|
||||
add r6, r6, #7 /* r6 = (r6 + 7) & ~7 */
|
||||
bic r6, r6, #7
|
||||
add r6, r6, #4 /* +4 because odd(5) registers are in stack */
|
||||
sub sp, sp, r6 /* reserved stack space for left arguments */
|
||||
mov r7, sp
|
||||
|
||||
loop_args: /* copy left arguments to stack */
|
||||
cmp r5, #0
|
||||
beq call_func
|
||||
ldr lr, [r4], #4
|
||||
str lr, [r7], #4
|
||||
sub r5, r5, #1
|
||||
b loop_args
|
||||
|
||||
call_func:
|
||||
blx ip
|
||||
add sp, sp, r6 /* restore sp */
|
||||
|
||||
return:
|
||||
ldmfd sp!, {r4, r5, r6, r7, lr}
|
||||
bx lr
|
||||
79
core/iwasm/common/arch/invokeNative_arm_vfp.s
Normal file
79
core/iwasm/common/arch/invokeNative_arm_vfp.s
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
.text
|
||||
.align 2
|
||||
.global invokeNative
|
||||
.type invokeNative,function
|
||||
|
||||
/*
|
||||
* Arguments passed in:
|
||||
*
|
||||
* r0 function ptr
|
||||
* r1 argv
|
||||
* r2 nstacks
|
||||
*/
|
||||
|
||||
invokeNative:
|
||||
stmfd sp!, {r4, r5, r6, r7, lr}
|
||||
mov ip, r0 /* ip = function ptr */
|
||||
mov r4, r1 /* r4 = argv */
|
||||
mov r5, r2 /* r5 = nstacks */
|
||||
mov r6, sp
|
||||
|
||||
/* Fill all int args */
|
||||
ldr r0, [r4], #4 /* r0 = *(int*)&argv[0] = exec_env */
|
||||
ldr r1, [r4], #4 /* r1 = *(int*)&argv[1] */
|
||||
ldr r2, [r4], #4 /* r2 = *(int*)&argv[2] */
|
||||
ldr r3, [r4], #4 /* r3 = *(int*)&argv[3] */
|
||||
|
||||
/* Fill all float/double args to 16 single-precision registers, s0-s15, */
|
||||
/* which may also be accessed as 8 double-precision registers, d0-d7 (with */
|
||||
/* d0 overlapping s0, s1; d1 overlapping s2, s3; etc). */
|
||||
vldr s0, [r4, #0] /* s0 = *(float*)&argv[4] */
|
||||
vldr s1, [r4, #4]
|
||||
vldr s2, [r4, #8]
|
||||
vldr s3, [r4, #12]
|
||||
vldr s4, [r4, #16]
|
||||
vldr s5, [r4, #20]
|
||||
vldr s6, [r4, #24]
|
||||
vldr s7, [r4, #28]
|
||||
vldr s8, [r4, #32]
|
||||
vldr s9, [r4, #36]
|
||||
vldr s10, [r4, #40]
|
||||
vldr s11, [r4, #44]
|
||||
vldr s12, [r4, #48]
|
||||
vldr s13, [r4, #52]
|
||||
vldr s14, [r4, #56]
|
||||
vldr s15, [r4, #60]
|
||||
/* Directly call the fucntion if no args in stack */
|
||||
cmp r5, #0
|
||||
beq call_func
|
||||
|
||||
|
||||
/* Fill all stack args: reserve stack space and fill ony by one */
|
||||
add r4, r4, #64 /* r4 points to stack args */
|
||||
bic sp, sp, #7 /* Ensure stack is 8 byte aligned */
|
||||
mov r7, r5, lsl#2 /* r7 = nstacks * 4 */
|
||||
add r7, r7, #7 /* r7 = (r7 + 7) & ~7 */
|
||||
bic r7, r7, #7
|
||||
sub sp, sp, r7 /* reserved stack space for stack arguments */
|
||||
mov r7, sp
|
||||
|
||||
loop_stack_args: /* copy stack arguments to stack */
|
||||
cmp r5, #0
|
||||
beq call_func
|
||||
ldr lr, [r4], #4 /* Note: caller should insure int64 and */
|
||||
str lr, [r7], #4 /* double are placed in 8 bytes aligned address */
|
||||
sub r5, r5, #1
|
||||
b loop_stack_args
|
||||
|
||||
call_func:
|
||||
blx ip
|
||||
mov sp, r6 /* restore sp */
|
||||
|
||||
return:
|
||||
ldmfd sp!, {r4, r5, r6, r7, lr}
|
||||
bx lr
|
||||
|
||||
64
core/iwasm/common/arch/invokeNative_em64.s
Normal file
64
core/iwasm/common/arch/invokeNative_em64.s
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
.text
|
||||
.align 2
|
||||
#ifndef BH_PLATFORM_DARWIN
|
||||
.globl invokeNative
|
||||
.type invokeNative, @function
|
||||
invokeNative:
|
||||
#else
|
||||
.globl _invokeNative
|
||||
_invokeNative:
|
||||
#endif /* end of BH_PLATFORM_DARWIN */
|
||||
/* rdi - function ptr */
|
||||
/* rsi - argv */
|
||||
/* rdx - n_stacks */
|
||||
|
||||
push %rbp
|
||||
mov %rsp, %rbp
|
||||
|
||||
mov %rdx, %r10
|
||||
mov %rsp, %r11 /* Check that stack is aligned on */
|
||||
and $8, %r11 /* 16 bytes. This code may be removed */
|
||||
je check_stack_succ /* when we are sure that compiler always */
|
||||
int3 /* calls us with aligned stack */
|
||||
check_stack_succ:
|
||||
mov %r10, %r11 /* Align stack on 16 bytes before pushing */
|
||||
and $1, %r11 /* stack arguments in case we have an odd */
|
||||
shl $3, %r11 /* number of stack arguments */
|
||||
sub %r11, %rsp
|
||||
/* store memory args */
|
||||
movq %rdi, %r11 /* func ptr */
|
||||
movq %r10, %rcx /* counter */
|
||||
lea 64+48-8(%rsi,%rcx,8), %r10
|
||||
sub %rsp, %r10
|
||||
cmpq $0, %rcx
|
||||
je push_args_end
|
||||
push_args:
|
||||
push 0(%rsp,%r10)
|
||||
loop push_args
|
||||
push_args_end:
|
||||
/* fill all fp args */
|
||||
movq 0x00(%rsi), %xmm0
|
||||
movq 0x08(%rsi), %xmm1
|
||||
movq 0x10(%rsi), %xmm2
|
||||
movq 0x18(%rsi), %xmm3
|
||||
movq 0x20(%rsi), %xmm4
|
||||
movq 0x28(%rsi), %xmm5
|
||||
movq 0x30(%rsi), %xmm6
|
||||
movq 0x38(%rsi), %xmm7
|
||||
|
||||
/* fill all int args */
|
||||
movq 0x40(%rsi), %rdi
|
||||
movq 0x50(%rsi), %rdx
|
||||
movq 0x58(%rsi), %rcx
|
||||
movq 0x60(%rsi), %r8
|
||||
movq 0x68(%rsi), %r9
|
||||
movq 0x48(%rsi), %rsi
|
||||
|
||||
call *%r11
|
||||
leave
|
||||
ret
|
||||
|
||||
86
core/iwasm/common/arch/invokeNative_general.c
Normal file
86
core/iwasm/common/arch/invokeNative_general.c
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "../wasm_runtime_common.h"
|
||||
#include "../wasm_exec_env.h"
|
||||
|
||||
void invokeNative(void (*native_code)(), uint32 argv[], uint32 argc)
|
||||
{
|
||||
bh_assert(argc >= sizeof(WASMExecEnv*)/sizeof(uint32));
|
||||
|
||||
switch(argc) {
|
||||
case 0:
|
||||
native_code();
|
||||
break;
|
||||
case 1:
|
||||
native_code(argv[0]);
|
||||
break;
|
||||
case 2:
|
||||
native_code(argv[0], argv[1]);
|
||||
break;
|
||||
case 3:
|
||||
native_code(argv[0], argv[1], argv[2]);
|
||||
break;
|
||||
case 4:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3]);
|
||||
break;
|
||||
case 5:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4]);
|
||||
break;
|
||||
case 6:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
|
||||
break;
|
||||
case 7:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
|
||||
break;
|
||||
case 8:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
|
||||
break;
|
||||
case 9:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
|
||||
break;
|
||||
case 10:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
|
||||
break;
|
||||
case 11:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]);
|
||||
break;
|
||||
case 12:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]);
|
||||
break;
|
||||
case 13:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12]);
|
||||
break;
|
||||
case 14:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13]);
|
||||
break;
|
||||
case 15:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14]);
|
||||
break;
|
||||
case 16:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15]);
|
||||
break;
|
||||
case 17:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16]);
|
||||
break;
|
||||
case 18:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17]);
|
||||
break;
|
||||
case 19:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18]);
|
||||
break;
|
||||
case 20:
|
||||
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19]);
|
||||
break;
|
||||
default:
|
||||
{
|
||||
/* FIXME: If this happen, add more cases. */
|
||||
WASMExecEnv *exec_env = *(WASMExecEnv**)argv;
|
||||
WASMModuleInstanceCommon *module_inst = exec_env->module_inst;
|
||||
wasm_runtime_set_exception(module_inst, "the argument number of native function exceeds maximum");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
core/iwasm/common/arch/invokeNative_ia32.s
Normal file
32
core/iwasm/common/arch/invokeNative_ia32.s
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
.text
|
||||
.align 2
|
||||
#ifndef BH_PLATFORM_DARWIN
|
||||
.globl invokeNative
|
||||
.type invokeNative, @function
|
||||
invokeNative:
|
||||
#else
|
||||
.globl _invokeNative
|
||||
_invokeNative:
|
||||
#endif /* end of BH_PLATFORM_DARWIN */
|
||||
push %ebp
|
||||
movl %esp, %ebp
|
||||
movl 16(%ebp), %ecx /* ecx = argc */
|
||||
movl 12(%ebp), %edx /* edx = argv */
|
||||
test %ecx, %ecx
|
||||
jz skip_push_args /* if ecx == 0, skip pushing arguments */
|
||||
leal -4(%edx,%ecx,4), %edx /* edx = edx + ecx * 4 - 4 */
|
||||
subl %esp, %edx /* edx = edx - esp */
|
||||
1:
|
||||
push 0(%esp,%edx)
|
||||
loop 1b /* loop ecx counts */
|
||||
skip_push_args:
|
||||
movl 8(%ebp), %edx /* edx = func_ptr */
|
||||
call *%edx
|
||||
leave
|
||||
ret
|
||||
|
||||
74
core/iwasm/common/arch/invokeNative_mips.s
Normal file
74
core/iwasm/common/arch/invokeNative_mips.s
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
.text
|
||||
.align 2
|
||||
.globl invokeNative
|
||||
.ent invokeNative
|
||||
.type invokeNative, @function
|
||||
|
||||
/**
|
||||
* On function entry parameters:
|
||||
* $4 = func_ptr
|
||||
* $5 = args
|
||||
* $6 = arg_num
|
||||
*/
|
||||
|
||||
invokeNative:
|
||||
.frame $fp, 8, $0
|
||||
.mask 0x00000000, 0
|
||||
.fmask 0x00000000, 0
|
||||
|
||||
/* Fixed part of frame */
|
||||
subu $sp, 8
|
||||
|
||||
/* save registers */
|
||||
sw $31, 4($sp)
|
||||
sw $fp, 0($sp)
|
||||
|
||||
/* set frame pointer to bottom of fixed frame */
|
||||
move $fp, $sp
|
||||
|
||||
/* allocate enough stack space */
|
||||
sll $11, $6, 2 /* $11 == arg_num * 4 */
|
||||
subu $sp, $11
|
||||
|
||||
/* make 8-byte aligned */
|
||||
and $sp, ~7
|
||||
|
||||
move $9, $sp
|
||||
move $25, $4 /* $25 = func_ptr */
|
||||
|
||||
push_args:
|
||||
beq $6, 0, done /* arg_num == 0 ? */
|
||||
lw $8, 0($5) /* $8 = *args */
|
||||
sw $8, 0($9) /* store $8 to stack */
|
||||
addu $5, 4 /* args++ */
|
||||
addu $9, 4 /* sp++ */
|
||||
subu $6, 1 /* arg_num-- */
|
||||
j push_args
|
||||
|
||||
done:
|
||||
lw $4, 0($sp) /* Load $4..$7 from stack */
|
||||
lw $5, 4($sp)
|
||||
lw $6, 8($sp)
|
||||
lw $7, 12($sp)
|
||||
ldc1 $f12, 0($sp) /* Load $f12, $f13, $f14, $f15 */
|
||||
ldc1 $f14, 8($sp)
|
||||
|
||||
jalr $25 /* call function */
|
||||
|
||||
nop
|
||||
|
||||
/* restore saved registers */
|
||||
move $sp, $fp
|
||||
lw $31, 4($sp)
|
||||
lw $fp, 0($sp)
|
||||
|
||||
/* pop frame */
|
||||
addu $sp, $sp, 8
|
||||
|
||||
j $31
|
||||
.end invokeNative
|
||||
84
core/iwasm/common/arch/invokeNative_thumb.s
Normal file
84
core/iwasm/common/arch/invokeNative_thumb.s
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
.text
|
||||
.align 2
|
||||
.global invokeNative
|
||||
.type invokeNative,function
|
||||
|
||||
/*
|
||||
* Arguments passed in:
|
||||
*
|
||||
* r0 function ptr
|
||||
* r1 argv
|
||||
* r2 argc
|
||||
*/
|
||||
|
||||
invokeNative:
|
||||
push {r4, r5, r6, r7}
|
||||
push {lr}
|
||||
mov ip, r0 /* ip = function ptr */
|
||||
mov r4, r1 /* r4 = argv */
|
||||
mov r5, r2 /* r5 = argc */
|
||||
|
||||
cmp r5, #1 /* at least one argument required: exec_env */
|
||||
blt return
|
||||
|
||||
mov r6, #0 /* increased stack size */
|
||||
|
||||
ldr r0, [r4] /* r0 = argv[0] = exec_env */
|
||||
add r4, r4, #4 /* r4 += 4 */
|
||||
cmp r5, #1
|
||||
beq call_func
|
||||
|
||||
ldr r1, [r4] /* r1 = argv[1] */
|
||||
add r4, r4, #4
|
||||
cmp r5, #2
|
||||
beq call_func
|
||||
|
||||
ldr r2, [r4] /* r2 = argv[2] */
|
||||
add r4, r4, #4
|
||||
cmp r5, #3
|
||||
beq call_func
|
||||
|
||||
ldr r3, [r4] /* r3 = argv[3] */
|
||||
add r4, r4, #4
|
||||
cmp r5, #4
|
||||
beq call_func
|
||||
|
||||
sub r5, r5, #4 /* argc -= 4, now we have r0 ~ r3 */
|
||||
|
||||
/* Ensure address is 8 byte aligned */
|
||||
lsl r6, r5, #2 /* r6 = argc * 4 */
|
||||
mov r7, #7
|
||||
add r6, r6, r7 /* r6 = (r6 + 7) & ~7 */
|
||||
bic r6, r6, r7
|
||||
add r6, r6, #4 /* +4 because odd(5) registers are in stack */
|
||||
mov r7, sp
|
||||
sub r7, r7, r6 /* reserved stack space for left arguments */
|
||||
mov sp, r7
|
||||
|
||||
mov lr, r2 /* save r2 */
|
||||
loop_args: /* copy left arguments to stack */
|
||||
cmp r5, #0
|
||||
beq call_func1
|
||||
ldr r2, [r4]
|
||||
add r4, r4, #4
|
||||
str r2, [r7]
|
||||
add r7, r7, #4
|
||||
sub r5, r5, #1
|
||||
b loop_args
|
||||
|
||||
call_func1:
|
||||
mov r2, lr /* restore r2 */
|
||||
|
||||
call_func:
|
||||
blx ip
|
||||
add sp, sp, r6 /* restore sp */
|
||||
|
||||
return:
|
||||
pop {r3}
|
||||
pop {r4, r5, r6, r7}
|
||||
mov lr, r3
|
||||
bx lr
|
||||
93
core/iwasm/common/arch/invokeNative_thumb_vfp.s
Normal file
93
core/iwasm/common/arch/invokeNative_thumb_vfp.s
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
.text
|
||||
.align 2
|
||||
.global invokeNative
|
||||
.type invokeNative,function
|
||||
|
||||
/*
|
||||
* Arguments passed in:
|
||||
*
|
||||
* r0 function ptr
|
||||
* r1 argv
|
||||
* r2 nstacks
|
||||
*/
|
||||
|
||||
invokeNative:
|
||||
push {r4, r5, r6, r7}
|
||||
push {lr}
|
||||
mov ip, r0 /* ip = function ptr */
|
||||
mov r4, r1 /* r4 = argv */
|
||||
mov r5, r2 /* r5 = nstacks */
|
||||
mov r7, sp
|
||||
|
||||
/* Fill all int args */
|
||||
ldr r0, [r4, #0] /* r0 = *(int*)&argv[0] = exec_env */
|
||||
ldr r1, [r4, #4] /* r1 = *(int*)&argv[1] */
|
||||
ldr r2, [r4, #8] /* r2 = *(int*)&argv[2] */
|
||||
ldr r3, [r4, #12] /* r3 = *(int*)&argv[3] */
|
||||
add r4, r4, #16 /* r4 points to float args */
|
||||
|
||||
/* Fill all float/double args to 16 single-precision registers, s0-s15, */
|
||||
/* which may also be accessed as 8 double-precision registers, d0-d7 (with */
|
||||
/* d0 overlapping s0, s1; d1 overlapping s2, s3; etc). */
|
||||
vldr s0, [r4, #0] /* s0 = *(float*)&argv[4] */
|
||||
vldr s1, [r4, #4]
|
||||
vldr s2, [r4, #8]
|
||||
vldr s3, [r4, #12]
|
||||
vldr s4, [r4, #16]
|
||||
vldr s5, [r4, #20]
|
||||
vldr s6, [r4, #24]
|
||||
vldr s7, [r4, #28]
|
||||
vldr s8, [r4, #32]
|
||||
vldr s9, [r4, #36]
|
||||
vldr s10, [r4, #40]
|
||||
vldr s11, [r4, #44]
|
||||
vldr s12, [r4, #48]
|
||||
vldr s13, [r4, #52]
|
||||
vldr s14, [r4, #56]
|
||||
vldr s15, [r4, #60]
|
||||
/* Directly call the fucntion if no args in stack */
|
||||
cmp r5, #0
|
||||
beq call_func
|
||||
|
||||
mov lr, r2 /* save r2 */
|
||||
|
||||
/* Fill all stack args: reserve stack space and fill ony by one */
|
||||
add r4, r4, #64 /* r4 points to stack args */
|
||||
mov r6, sp
|
||||
mov r7, #7
|
||||
bic r6, r6, r7 /* Ensure stack is 8 byte aligned */
|
||||
lsl r2, r5, #2 /* r2 = nstacks * 4 */
|
||||
add r2, r2, #7 /* r2 = (r2 + 7) & ~7 */
|
||||
bic r2, r2, r7
|
||||
sub r6, r6, r2 /* reserved stack space for stack arguments */
|
||||
mov r7, sp
|
||||
mov sp, r6
|
||||
|
||||
loop_stack_args: /* copy stack arguments to stack */
|
||||
cmp r5, #0
|
||||
beq call_func1
|
||||
ldr r2, [r4] /* Note: caller should insure int64 and */
|
||||
add r4, r4, #4 /* double are placed in 8 bytes aligned address */
|
||||
str r2, [r6]
|
||||
add r6, r6, #4
|
||||
|
||||
sub r5, r5, #1
|
||||
b loop_stack_args
|
||||
|
||||
call_func1:
|
||||
mov r2, lr /* restore r2 */
|
||||
|
||||
call_func:
|
||||
blx ip
|
||||
mov sp, r7 /* restore sp */
|
||||
|
||||
return:
|
||||
pop {r3}
|
||||
pop {r4, r5, r6, r7}
|
||||
mov lr, r3
|
||||
bx lr
|
||||
|
||||
74
core/iwasm/common/arch/invokeNative_xtensa.s
Normal file
74
core/iwasm/common/arch/invokeNative_xtensa.s
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
.text
|
||||
.align 2
|
||||
.global invokeNative
|
||||
.type invokeNative,function
|
||||
|
||||
/*
|
||||
* Arguments passed in:
|
||||
*
|
||||
* a2 function pntr
|
||||
* a3 argv
|
||||
* a4 argc
|
||||
*/
|
||||
|
||||
invokeNative:
|
||||
entry a1, 256
|
||||
|
||||
blti a4, 1, return /* at least one argument required: exec_env */
|
||||
|
||||
/* register a10 ~ a15 are used to pass first 6 arguments */
|
||||
|
||||
l32i.n a10, a3, 0
|
||||
beqi a4, 1, call_func
|
||||
|
||||
l32i.n a11, a3, 4
|
||||
beqi a4, 2, call_func
|
||||
|
||||
l32i.n a12, a3, 8
|
||||
beqi a4, 3, call_func
|
||||
|
||||
l32i.n a13, a3, 12
|
||||
beqi a4, 4, call_func
|
||||
|
||||
l32i.n a14, a3, 16
|
||||
beqi a4, 5, call_func
|
||||
|
||||
l32i.n a15, a3, 20
|
||||
beqi a4, 6, call_func
|
||||
|
||||
/* left arguments are passed through stack */
|
||||
|
||||
addi a4, a4, -6
|
||||
addi a3, a3, 24 /* move argv pointer */
|
||||
mov.n a6, a1 /* store stack pointer */
|
||||
addi a7, a1, 256 /* stack boundary */
|
||||
|
||||
loop_args:
|
||||
beqi a4, 0, call_func
|
||||
bge a6, a7, call_func /* reach stack boundary */
|
||||
|
||||
l32i.n a5, a3, 0 /* load argument to a5 */
|
||||
s32i.n a5, a6, 0 /* push data to stack */
|
||||
|
||||
addi a4, a4, -1 /* decrease argc */
|
||||
addi a3, a3, 4 /* move argv pointer */
|
||||
addi a6, a6, 4 /* move stack pointer */
|
||||
|
||||
j loop_args
|
||||
|
||||
call_func:
|
||||
mov.n a8, a2
|
||||
callx8 a8
|
||||
|
||||
/* the result returned from callee is stored in a2
|
||||
mov the result to a10 so the caller of this function
|
||||
can receive the value */
|
||||
mov.n a2, a10
|
||||
mov.n a3, a11
|
||||
|
||||
return:
|
||||
retw.n
|
||||
43
core/iwasm/common/iwasm_common.cmake
Normal file
43
core/iwasm/common/iwasm_common.cmake
Normal file
@ -0,0 +1,43 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
set (IWASM_COMMON_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories (${IWASM_COMMON_DIR})
|
||||
|
||||
file (GLOB c_source_all ${IWASM_COMMON_DIR}/*.c)
|
||||
|
||||
if (${WAMR_BUILD_TARGET} STREQUAL "X86_64" OR ${WAMR_BUILD_TARGET} STREQUAL "AMD_64")
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_em64.s)
|
||||
elseif (${WAMR_BUILD_TARGET} STREQUAL "X86_32")
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_ia32.s)
|
||||
elseif (${WAMR_BUILD_TARGET} MATCHES "ARM.*")
|
||||
if (${WAMR_BUILD_TARGET} MATCHES "ARM.*_VFP")
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_arm_vfp.s)
|
||||
else ()
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_arm.s)
|
||||
endif ()
|
||||
elseif (${WAMR_BUILD_TARGET} MATCHES "THUMB.*")
|
||||
if (${WAMR_BUILD_TARGET} MATCHES "THUMB.*_VFP")
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_thumb_vfp.s)
|
||||
else ()
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_thumb.s)
|
||||
endif ()
|
||||
elseif (${WAMR_BUILD_TARGET} STREQUAL "MIPS")
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_mips.s)
|
||||
elseif (${WAMR_BUILD_TARGET} STREQUAL "XTENSA")
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_xtensa.s)
|
||||
elseif (${WAMR_BUILD_TARGET} STREQUAL "GENERAL")
|
||||
# Use invokeNative_general.c instead of assembly code,
|
||||
# but the maximum number of native arguments is limited to 20,
|
||||
# and there are possible issues when passing arguments to
|
||||
# native function for some cpus, e.g. int64 and double arguments
|
||||
# in arm and mips need to be 8-bytes aligned, and some arguments
|
||||
# of x86_64 are passed by registers but not stack
|
||||
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_general.c)
|
||||
else ()
|
||||
message (FATAL_ERROR "Build target isn't set")
|
||||
endif ()
|
||||
|
||||
set (IWASM_COMMON_SOURCE ${source_all})
|
||||
|
||||
42
core/iwasm/common/wasm_exec_env.c
Normal file
42
core/iwasm/common/wasm_exec_env.c
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "wasm_exec_env.h"
|
||||
#include "bh_memory.h"
|
||||
#include "wasm_runtime_common.h"
|
||||
|
||||
WASMExecEnv *
|
||||
wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
|
||||
uint32 stack_size)
|
||||
{
|
||||
uint64 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
|
||||
+ (uint64)stack_size;
|
||||
WASMExecEnv *exec_env;
|
||||
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(exec_env = wasm_malloc((uint32)total_size)))
|
||||
return NULL;
|
||||
|
||||
memset(exec_env, 0, (uint32)total_size);
|
||||
exec_env->module_inst = module_inst;
|
||||
exec_env->wasm_stack_size = stack_size;
|
||||
exec_env->wasm_stack.s.top_boundary =
|
||||
exec_env->wasm_stack.s.bottom + stack_size;
|
||||
exec_env->wasm_stack.s.top = exec_env->wasm_stack.s.bottom;
|
||||
return exec_env;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_exec_env_destroy(WASMExecEnv *exec_env)
|
||||
{
|
||||
wasm_free(exec_env);
|
||||
}
|
||||
|
||||
WASMModuleInstanceCommon *
|
||||
wasm_exec_env_get_module_inst(WASMExecEnv *exec_env)
|
||||
{
|
||||
return exec_env->module_inst;
|
||||
}
|
||||
|
||||
151
core/iwasm/common/wasm_exec_env.h
Normal file
151
core/iwasm/common/wasm_exec_env.h
Normal file
@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _WASM_EXEC_ENV_H
|
||||
#define _WASM_EXEC_ENV_H
|
||||
|
||||
#include "bh_thread.h"
|
||||
#include "bh_assert.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct WASMModuleInstanceCommon;
|
||||
struct WASMInterpFrame;
|
||||
|
||||
/* Execution environment */
|
||||
typedef struct WASMExecEnv {
|
||||
/* Next thread's exec env of a WASM module instance. */
|
||||
struct WASMExecEnv *next;
|
||||
|
||||
/* Previous thread's exec env of a WASM module instance. */
|
||||
struct WASMExecEnv *prev;
|
||||
|
||||
/* The WASM module instance of current thread */
|
||||
struct WASMModuleInstanceCommon *module_inst;
|
||||
|
||||
/* Current interpreter frame of current thread */
|
||||
struct WASMInterpFrame *cur_frame;
|
||||
|
||||
/* The native thread handle of current thread */
|
||||
korp_tid handle;
|
||||
|
||||
/* The boundary of native stack. When interpreter detects that native
|
||||
frame may overrun this boundary, it throws a stack overflow
|
||||
exception. */
|
||||
void *native_stack_boundary;
|
||||
|
||||
/* The WASM stack size */
|
||||
uint32 wasm_stack_size;
|
||||
|
||||
/* The WASM stack of current thread */
|
||||
union {
|
||||
uint64 __make_it_8_byte_aligned_;
|
||||
|
||||
struct {
|
||||
/* The top boundary of the stack. */
|
||||
uint8 *top_boundary;
|
||||
|
||||
/* Top cell index which is free. */
|
||||
uint8 *top;
|
||||
|
||||
/* The Java stack. */
|
||||
uint8 bottom[1];
|
||||
} s;
|
||||
} wasm_stack;
|
||||
} WASMExecEnv;
|
||||
|
||||
WASMExecEnv *
|
||||
wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
|
||||
uint32 stack_size);
|
||||
|
||||
void
|
||||
wasm_exec_env_destroy(WASMExecEnv *exec_env);
|
||||
|
||||
/**
|
||||
* Allocate a WASM frame from the WASM stack.
|
||||
*
|
||||
* @param exec_env the current execution environment
|
||||
* @param size size of the WASM frame, it must be a multiple of 4
|
||||
*
|
||||
* @return the WASM frame if there is enough space in the stack area
|
||||
* with a protection area, NULL otherwise
|
||||
*/
|
||||
static inline void *
|
||||
wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size)
|
||||
{
|
||||
uint8 *addr = exec_env->wasm_stack.s.top;
|
||||
|
||||
bh_assert(!(size & 3));
|
||||
|
||||
/* The outs area size cannot be larger than the frame size, so
|
||||
multiplying by 2 is enough. */
|
||||
if (addr + size * 2 > exec_env->wasm_stack.s.top_boundary) {
|
||||
/* WASM stack overflow. */
|
||||
/* When throwing SOE, the preserved space must be enough. */
|
||||
/* bh_assert(!exec_env->throwing_soe);*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
exec_env->wasm_stack.s.top += size;
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
static inline void
|
||||
wasm_exec_env_free_wasm_frame(WASMExecEnv *exec_env, void *prev_top)
|
||||
{
|
||||
bh_assert((uint8 *)prev_top >= exec_env->wasm_stack.s.bottom);
|
||||
exec_env->wasm_stack.s.top = (uint8 *)prev_top;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current WASM stack top pointer.
|
||||
*
|
||||
* @param exec_env the current execution environment
|
||||
*
|
||||
* @return the current WASM stack top pointer
|
||||
*/
|
||||
static inline void*
|
||||
wasm_exec_env_wasm_stack_top(WASMExecEnv *exec_env)
|
||||
{
|
||||
return exec_env->wasm_stack.s.top;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current frame pointer.
|
||||
*
|
||||
* @param exec_env the current execution environment
|
||||
* @param frame the WASM frame to be set for the current exec env
|
||||
*/
|
||||
static inline void
|
||||
wasm_exec_env_set_cur_frame(WASMExecEnv *exec_env,
|
||||
struct WASMInterpFrame *frame)
|
||||
{
|
||||
exec_env->cur_frame = frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current frame pointer.
|
||||
*
|
||||
* @param exec_env the current execution environment
|
||||
*
|
||||
* @return the current frame pointer
|
||||
*/
|
||||
static inline struct WASMInterpFrame*
|
||||
wasm_exec_env_get_cur_frame(WASMExecEnv *exec_env)
|
||||
{
|
||||
return exec_env->cur_frame;
|
||||
}
|
||||
|
||||
struct WASMModuleInstanceCommon *
|
||||
wasm_exec_env_get_module_inst(WASMExecEnv *exec_env);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _WASM_EXEC_ENV_H */
|
||||
125
core/iwasm/common/wasm_native.c
Normal file
125
core/iwasm/common/wasm_native.c
Normal file
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "wasm_native.h"
|
||||
|
||||
|
||||
typedef struct NativeSymbol {
|
||||
const char *symbol;
|
||||
void *func_ptr;
|
||||
} NativeSymbol;
|
||||
|
||||
static bool
|
||||
sort_symbol_ptr(NativeSymbol *ptr, int len)
|
||||
{
|
||||
int i, j;
|
||||
NativeSymbol temp;
|
||||
|
||||
for (i = 0; i < len - 1; ++i) {
|
||||
for (j = i + 1; j < len; ++j) {
|
||||
if (strcmp((ptr+i)->symbol, (ptr+j)->symbol) > 0) {
|
||||
temp = ptr[i];
|
||||
ptr[i] = ptr[j];
|
||||
ptr[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void *
|
||||
lookup_symbol(NativeSymbol *ptr, int len, const char *symbol)
|
||||
{
|
||||
int low = 0, mid, ret;
|
||||
int high = len - 1;
|
||||
|
||||
while (low <= high) {
|
||||
mid = (low + high) / 2;
|
||||
ret = strcmp(symbol, ptr[mid].symbol);
|
||||
|
||||
if (ret == 0)
|
||||
return ptr[mid].func_ptr;
|
||||
else if (ret < 0)
|
||||
high = mid - 1;
|
||||
else
|
||||
low = mid + 1;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_BASE_LIB != 0
|
||||
static bool is_base_lib_sorted = false;
|
||||
static NativeSymbol *base_native_symbol_defs;
|
||||
static int base_native_symbol_len;
|
||||
|
||||
int
|
||||
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
|
||||
|
||||
void *
|
||||
wasm_native_lookup_base_lib_func(const char *module_name,
|
||||
const char *func_name)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
if (strcmp(module_name, "env"))
|
||||
return NULL;
|
||||
|
||||
if (!is_base_lib_sorted) {
|
||||
base_native_symbol_len = get_base_lib_export_apis(&base_native_symbol_defs);
|
||||
|
||||
if (base_native_symbol_len > 0)
|
||||
sort_symbol_ptr(base_native_symbol_defs, base_native_symbol_len);
|
||||
|
||||
is_base_lib_sorted = true;
|
||||
}
|
||||
|
||||
if ((ret = lookup_symbol(base_native_symbol_defs, base_native_symbol_len,
|
||||
func_name))
|
||||
|| (func_name[0] == '_'
|
||||
&& (ret = lookup_symbol(base_native_symbol_defs, base_native_symbol_len,
|
||||
func_name + 1))))
|
||||
return ret;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_BASE_LIB */
|
||||
|
||||
static bool is_ext_lib_sorted = false;
|
||||
static NativeSymbol *ext_native_symbol_defs;
|
||||
static int ext_native_symbol_len;
|
||||
|
||||
int
|
||||
get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
|
||||
|
||||
void *
|
||||
wasm_native_lookup_extension_lib_func(const char *module_name,
|
||||
const char *func_name)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
if (strcmp(module_name, "env"))
|
||||
return NULL;
|
||||
|
||||
if (!is_ext_lib_sorted) {
|
||||
ext_native_symbol_len = get_ext_lib_export_apis(&ext_native_symbol_defs);
|
||||
|
||||
if (ext_native_symbol_len > 0)
|
||||
sort_symbol_ptr(ext_native_symbol_defs, ext_native_symbol_len);
|
||||
|
||||
is_ext_lib_sorted = true;
|
||||
}
|
||||
|
||||
if ((ret = lookup_symbol(ext_native_symbol_defs, ext_native_symbol_len,
|
||||
func_name))
|
||||
|| (func_name[0] == '_'
|
||||
&& (ret = lookup_symbol(ext_native_symbol_defs, ext_native_symbol_len,
|
||||
func_name + 1))))
|
||||
return ret;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
92
core/iwasm/common/wasm_native.h
Normal file
92
core/iwasm/common/wasm_native.h
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _WASM_NATIVE_H
|
||||
#define _WASM_NATIVE_H
|
||||
|
||||
#include "bh_common.h"
|
||||
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
|
||||
#include "../interpreter/wasm.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Lookup native function implementation of a given import function
|
||||
* in libc builtin API's
|
||||
*
|
||||
* @param module_name the module name of the import function
|
||||
* @param func_name the function name of the import function
|
||||
*
|
||||
* @return return the native function pointer if success, NULL otherwise
|
||||
*/
|
||||
void *
|
||||
wasm_native_lookup_libc_builtin_func(const char *module_name,
|
||||
const char *func_name);
|
||||
|
||||
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
|
||||
/**
|
||||
* Lookup global variable of a given import global
|
||||
* in libc builtin globals
|
||||
*
|
||||
* @param module_name the module name of the import global
|
||||
* @param global_name the global name of the import global
|
||||
* @param global return the global data
|
||||
*
|
||||
* @param return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
wasm_native_lookup_libc_builtin_global(const char *module_name,
|
||||
const char *global_name,
|
||||
WASMGlobalImport *global);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Lookup native function implementation of a given import function
|
||||
* in libc wasi API's
|
||||
*
|
||||
* @param module_name the module name of the import function
|
||||
* @param func_name the function name of the import function
|
||||
*
|
||||
* @return return the native function pointer if success, NULL otherwise
|
||||
*/
|
||||
void *
|
||||
wasm_native_lookup_libc_wasi_func(const char *module_name,
|
||||
const char *func_name);
|
||||
|
||||
/**
|
||||
* Lookup native function implementation of a given import function
|
||||
* in base lib API's
|
||||
*
|
||||
* @param module_name the module name of the import function
|
||||
* @param func_name the function name of the import function
|
||||
*
|
||||
* @return return the native function pointer if success, NULL otherwise
|
||||
*/
|
||||
void *
|
||||
wasm_native_lookup_base_lib_func(const char *module_name,
|
||||
const char *func_name);
|
||||
|
||||
/**
|
||||
* Lookup native function implementation of a given import function
|
||||
* in extension lib API's
|
||||
*
|
||||
* @param module_name the module name of the import function
|
||||
* @param func_name the function name of the import function
|
||||
*
|
||||
* @return return the native function pointer if success, NULL otherwise
|
||||
*/
|
||||
void *
|
||||
wasm_native_lookup_extension_lib_func(const char *module_name,
|
||||
const char *func_name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _WASM_NATIVE_H */
|
||||
|
||||
1820
core/iwasm/common/wasm_runtime_common.c
Normal file
1820
core/iwasm/common/wasm_runtime_common.c
Normal file
File diff suppressed because it is too large
Load Diff
296
core/iwasm/common/wasm_runtime_common.h
Normal file
296
core/iwasm/common/wasm_runtime_common.h
Normal file
@ -0,0 +1,296 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _WASM_COMMON_H
|
||||
#define _WASM_COMMON_H
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_common.h"
|
||||
#include "bh_thread.h"
|
||||
#include "wasm_exec_env.h"
|
||||
#include "../interpreter/wasm.h"
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
#include "wasmtime_ssp.h"
|
||||
#include "posix.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define wasm_malloc bh_malloc
|
||||
#define wasm_free bh_free
|
||||
|
||||
/* Package Type */
|
||||
typedef enum {
|
||||
Wasm_Module_Bytecode = 0,
|
||||
Wasm_Module_AoT,
|
||||
Package_Type_Unknown = 0xFFFF
|
||||
} PackageType;
|
||||
|
||||
typedef struct WASMModuleCommon {
|
||||
/* Module type, for module loaded from WASM bytecode binary,
|
||||
this field is Wasm_Module_Bytecode, and this structure should
|
||||
be treated as WASMModule structure;
|
||||
for module loaded from AOT binary, this field is
|
||||
Wasm_Module_AoT, and this structure should be treated as
|
||||
AOTModule structure. */
|
||||
uint32 module_type;
|
||||
uint8 module_data[1];
|
||||
} WASMModuleCommon;
|
||||
|
||||
typedef struct WASMModuleInstanceCommon {
|
||||
/* Module instance type, for module instance loaded from WASM
|
||||
bytecode binary, this field is Wasm_Module_Bytecode, and this
|
||||
structure should be treated as WASMModuleInstance structure;
|
||||
for module instance loaded from AOT binary, this field is
|
||||
Wasm_Module_AoT, and this structure should be treated as
|
||||
AOTModuleInstance structure. */
|
||||
uint32 module_type;
|
||||
uint8 module_inst_data[1];
|
||||
} WASMModuleInstanceCommon;
|
||||
|
||||
typedef void WASMFunctionInstanceCommon;
|
||||
|
||||
/* WASM section */
|
||||
typedef struct WASMSection {
|
||||
struct WASMSection *next;
|
||||
/* section type */
|
||||
int section_type;
|
||||
/* section body, not include type and size */
|
||||
const uint8 *section_body;
|
||||
/* section body size */
|
||||
uint32 section_body_size;
|
||||
} WASMSection, AOTSection;
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
typedef struct WASIContext {
|
||||
struct fd_table *curfds;
|
||||
struct fd_prestats *prestats;
|
||||
struct argv_environ_values *argv_environ;
|
||||
} WASIContext;
|
||||
#endif
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_runtime_init();
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_destroy();
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
PackageType
|
||||
get_package_type(const uint8 *buf, uint32 size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASMModuleCommon *
|
||||
wasm_runtime_load(const uint8 *buf, uint32 size,
|
||||
char *error_buf, uint32 error_buf_size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASMModuleCommon *
|
||||
wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
|
||||
char *error_buf, uint32_t error_buf_size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_unload(WASMModuleCommon *module);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASMModuleInstanceCommon *
|
||||
wasm_runtime_instantiate(WASMModuleCommon *module,
|
||||
uint32 stack_size, uint32 heap_size,
|
||||
char *error_buf, uint32 error_buf_size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASMFunctionInstanceCommon *
|
||||
wasm_runtime_lookup_function(const WASMModuleInstanceCommon *module_inst,
|
||||
const char *name, const char *signature);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASMExecEnv *
|
||||
wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
|
||||
uint32 stack_size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASMModuleInstanceCommon *
|
||||
wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_runtime_call_wasm(WASMExecEnv *exec_env,
|
||||
WASMFunctionInstanceCommon *function,
|
||||
unsigned argc, uint32 argv[]);
|
||||
|
||||
bool
|
||||
wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
|
||||
WASMFunctionInstanceCommon *function,
|
||||
unsigned argc, uint32 argv[]);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
|
||||
int argc, char *argv[]);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
|
||||
const char *name, int argc, char *argv[]);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
|
||||
const char *exception);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
const char *
|
||||
wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
|
||||
void *custom_data);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void *
|
||||
wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
int32
|
||||
wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, int32 ptr);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
int32
|
||||
wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
|
||||
const char *src, uint32 size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
|
||||
int32 app_offset, uint32 size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
|
||||
int32 app_str_offset);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
|
||||
void *native_ptr, uint32 size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
void *
|
||||
wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
|
||||
int32 app_offset);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
int32
|
||||
wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
|
||||
void *native_ptr);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
|
||||
int32 app_offset,
|
||||
int32 *p_app_start_offset,
|
||||
int32 *p_app_end_offset);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
|
||||
uint8 *native_ptr,
|
||||
uint8 **p_native_start_addr,
|
||||
uint8 **p_native_end_addr);
|
||||
|
||||
uint32
|
||||
wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
void
|
||||
wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
|
||||
uint32 temp_ret);
|
||||
|
||||
uint32
|
||||
wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
void
|
||||
wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
|
||||
uint32 llvm_stack);
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
/* See wasm_export.h for description */
|
||||
void
|
||||
wasm_runtime_set_wasi_args(WASMModuleCommon *module,
|
||||
const char *dir_list[], uint32 dir_count,
|
||||
const char *map_dir_list[], uint32 map_dir_count,
|
||||
const char *env_list[], uint32 env_count,
|
||||
const char *argv[], uint32 argc);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
bool
|
||||
wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASMFunctionInstanceCommon *
|
||||
wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
bool
|
||||
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
const char *dir_list[], uint32 dir_count,
|
||||
const char *map_dir_list[], uint32 map_dir_count,
|
||||
const char *env[], uint32 env_count,
|
||||
const char *argv[], uint32 argc,
|
||||
char *error_buf, uint32 error_buf_size);
|
||||
|
||||
void
|
||||
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
void
|
||||
wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
|
||||
WASIContext *wasi_ctx);
|
||||
|
||||
WASIContext *
|
||||
wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
|
||||
#endif /* end of WASM_ENABLE_LIBC_WASI */
|
||||
|
||||
/**
|
||||
* Enlarge wasm memory data space.
|
||||
*
|
||||
* @param module the wasm module instance
|
||||
* @param inc_page_count denote the page number to increase
|
||||
* @return return true if enlarge successfully, false otherwise
|
||||
*/
|
||||
bool
|
||||
wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count);
|
||||
|
||||
bool
|
||||
wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
|
||||
WASMExecEnv *exec_env,
|
||||
uint32 *argv, uint32 argc, uint32 *ret);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _WASM_COMMON_H */
|
||||
|
||||
Reference in New Issue
Block a user