Fix code indent issue and 64-bit compiler error in libc wrapper src

This commit is contained in:
wenyongh 2019-05-14 21:17:12 +08:00
parent dc6441c55d
commit a595ec79ab
2 changed files with 605 additions and 510 deletions

View File

@ -21,12 +21,16 @@
void
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
uint32
wasm_runtime_get_temp_ret(wasm_module_inst_t module);
void
wasm_runtime_set_temp_ret(wasm_module_inst_t module, uint32 temp_ret);
uint32
wasm_runtime_get_llvm_stack(wasm_module_inst_t module);
void
wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
@ -51,22 +55,34 @@ wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
typedef int (*out_func_t)(int c, void *ctx);
enum pad_type {
PAD_NONE, PAD_ZERO_BEFORE, PAD_SPACE_BEFORE, PAD_SPACE_AFTER,
PAD_NONE,
PAD_ZERO_BEFORE,
PAD_SPACE_BEFORE,
PAD_SPACE_AFTER,
};
typedef char *_va_list;
#define _INTSIZEOF(n) \
((sizeof(n) + 3) & ~3)
#define _va_arg(ap,t) \
(*(t*)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)))
/**
* @brief Output an unsigned long in hex format
* @brief Output an unsigned int in hex format
*
* Output an unsigned long on output installed by platform at init time. Should
* be able to handle an unsigned long of any size, 32 or 64 bit.
* Output an unsigned int on output installed by platform at init time. Should
* be able to handle an unsigned int of any size, 32 or 64 bit.
* @param num Number to output
*
* @return N/A
*/
static void _printf_hex_ulong(out_func_t out, void *ctx,
const unsigned long num, enum pad_type padding, int min_width)
static void
_printf_hex_uint(out_func_t out, void *ctx,
const uint64 num, bool is_u64,
enum pad_type padding,
int min_width)
{
int size = sizeof(num) * 2;
int size = sizeof(num) * (is_u64 ? 2 : 1);
int found_largest_digit = 0;
int remaining = 8; /* 8 digits max */
int digits = 0;
@ -100,19 +116,22 @@ static void _printf_hex_ulong(out_func_t out, void *ctx,
}
/**
* @brief Output an unsigned long (32-bit) in decimal format
* @brief Output an unsigned int in decimal format
*
* Output an unsigned long on output installed by platform at init time. Only
* Output an unsigned int on output installed by platform at init time. Only
* works with 32-bit values.
* @param num Number to output
*
* @return N/A
*/
static void _printf_dec_ulong(out_func_t out, void *ctx,
const unsigned long num, enum pad_type padding, int min_width)
static void
_printf_dec_uint(out_func_t out, void *ctx,
const uint32 num,
enum pad_type padding,
int min_width)
{
unsigned long pos = 999999999;
unsigned long remainder = num;
uint32 pos = 999999999;
uint32 remainder = num;
int found_largest_digit = 0;
int remaining = 10; /* 10 digits max */
int digits = 1;
@ -145,7 +164,16 @@ static void _printf_dec_ulong(out_func_t out, void *ctx,
}
}
static void _vprintf(out_func_t out, void *ctx, const char *fmt, va_list ap,
static void
print_err(out_func_t out, void *ctx)
{
out('E', ctx);
out('R', ctx);
out('R', ctx);
}
static void
_vprintf(out_func_t out, void *ctx, const char *fmt, _va_list ap,
wasm_module_inst_t module_inst)
{
int might_format = 0; /* 1 if encountered a '%' */
@ -159,13 +187,15 @@ static void _vprintf(out_func_t out, void *ctx, const char *fmt, va_list ap,
if (!might_format) {
if (*fmt != '%') {
out((int) *fmt, ctx);
} else {
}
else {
might_format = 1;
min_width = -1;
padding = PAD_NONE;
long_ctr = 0;
}
} else {
}
else {
switch (*fmt) {
case '-':
padding = PAD_SPACE_AFTER;
@ -199,11 +229,18 @@ static void _vprintf(out_func_t out, void *ctx, const char *fmt, va_list ap,
case 'd':
case 'i': {
long d;
int32 d;
if (long_ctr < 2) {
d = va_arg(ap, long);
} else {
d = (long)va_arg(ap, long long);
d = _va_arg(ap, int32);
}
else {
int64 lld = _va_arg(ap, int64);
if (lld > INT32_MAX || lld < INT32_MIN) {
print_err(out, ctx);
break;
}
d = (int32)lld;
}
if (d < 0) {
@ -211,23 +248,26 @@ static void _vprintf(out_func_t out, void *ctx, const char *fmt, va_list ap,
d = -d;
min_width--;
}
_printf_dec_ulong(out, ctx, d, padding, min_width);
_printf_dec_uint(out, ctx, d, padding, min_width);
break;
}
case 'u': {
unsigned long u;
uint32 u;
if (long_ctr < 2) {
u = va_arg(ap, unsigned long);
} else {
u = (unsigned long)va_arg(ap,
unsigned long long);
u = _va_arg(ap, uint32);
}
_printf_dec_ulong(out, ctx, u, padding, min_width);
else {
uint64 llu = _va_arg(ap, uint64);
if (llu > INT32_MAX) {
print_err(out, ctx);
break;
}
u = (uint32)llu;
}
_printf_dec_uint(out, ctx, u, padding, min_width);
break;
}
case 'p':
out('0', ctx);
out('x', ctx);
@ -237,22 +277,22 @@ min_width = 8;
/* Fall through */
case 'x':
case 'X': {
unsigned long x;
uint64 x;
bool is_ptr = (*fmt == 'p') ? true : false;
if (long_ctr < 2) {
x = va_arg(ap, unsigned long);
x = _va_arg(ap, uint32);
} else {
x = (unsigned long)va_arg(ap, unsigned long long);
x = _va_arg(ap, uint64);
}
_printf_hex_ulong(out, ctx, x, padding, min_width);
_printf_hex_uint(out, ctx, x, !is_ptr, padding, min_width);
break;
}
case 's': {
char *s;
char *start;
int32 s_offset = va_arg(ap, uint32);
int32 s_offset = _va_arg(ap, uint32);
if (!validate_app_addr(s_offset, 1)) {
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
@ -274,7 +314,7 @@ break;
}
case 'c': {
int c = va_arg(ap, int);
int c = _va_arg(ap, int);
out(c, ctx);
break;
}
@ -293,7 +333,8 @@ break;
might_format = 0;
}
still_might_format: ++fmt;
still_might_format:
++fmt;
}
}
@ -303,7 +344,8 @@ int max;
int count;
};
static int sprintf_out(int c, struct str_context *ctx)
static int
sprintf_out(int c, struct str_context *ctx)
{
if (!ctx->str || ctx->count >= ctx->max) {
ctx->count++;
@ -319,34 +361,38 @@ ctx->str[ctx->count++] = c;
return c;
}
static int printf_out(int c, struct str_context *ctx)
static int
printf_out(int c, struct str_context *ctx)
{
printf("%c", c);
ctx->count++;
return c;
}
static inline va_list get_va_list(uint32 *args)
static inline _va_list
get_va_list(uint32 *args)
{
union {
uint32 u;
va_list v;
_va_list v;
} u;
u.u = args[0];
return u.v;
}
static bool parse_printf_args(wasm_module_inst_t module_inst, int32 fmt_offset,
int32 va_list_offset, const char **p_fmt, va_list *p_va_args)
static bool
parse_printf_args(wasm_module_inst_t module_inst, int32 fmt_offset,
int32 va_list_offset, const char **p_fmt,
_va_list *p_va_args)
{
const char *fmt;
union {
uintptr_t u;
va_list v;
_va_list v;
} u;
if (!validate_app_addr(fmt_offset,
1) || !validate_app_addr(va_list_offset, sizeof(int32)))
if (!validate_app_addr(fmt_offset, 1)
|| !validate_app_addr(va_list_offset, sizeof(int32)))
return false;
fmt = (const char*) addr_app_to_native(fmt_offset);
@ -357,12 +403,13 @@ u.u = (uintptr_t) addr_app_to_native(va_list_offset);
return true;
}
static int _printf_wrapper(int32 fmt_offset, int32 va_list_offset)
static int
_printf_wrapper(int32 fmt_offset, int32 va_list_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
struct str_context ctx = { NULL, 0, 0 };
const char *fmt;
va_list va_args;
_va_list va_args;
if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
return 0;
@ -371,14 +418,14 @@ _vprintf((out_func_t) printf_out, &ctx, fmt, va_args, module_inst);
return ctx.count;
}
static int _sprintf_wrapper(int32 str_offset, int32 fmt_offset,
int32 va_list_offset)
static int
_sprintf_wrapper(int32 str_offset, int32 fmt_offset, int32 va_list_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
struct str_context ctx;
char *str;
const char *fmt;
va_list va_args;
_va_list va_args;
if (!validate_app_addr(str_offset, 1))
return 0;
@ -401,14 +448,15 @@ str[ctx.count] = '\0';
return ctx.count;
}
static int _snprintf_wrapper(int32 str_offset, int32 size, int32 fmt_offset,
static int
_snprintf_wrapper(int32 str_offset, int32 size, int32 fmt_offset,
int32 va_list_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
struct str_context ctx;
char *str;
const char *fmt;
va_list va_args;
_va_list va_args;
if (!validate_app_addr(str_offset, size))
return 0;
@ -431,7 +479,8 @@ str[ctx.count] = '\0';
return ctx.count;
}
static int _puts_wrapper(int32 str_offset)
static int
_puts_wrapper(int32 str_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
const char *str;
@ -443,13 +492,15 @@ str = addr_app_to_native(str_offset);
return printf("%s\n", str);
}
static int _putchar_wrapper(int c)
static int
_putchar_wrapper(int c)
{
printf("%c", c);
return 1;
}
static int32 _strdup_wrapper(int32 str_offset)
static int32
_strdup_wrapper(int32 str_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
char *str, *str_ret;
@ -474,12 +525,14 @@ memcpy(str_ret, str, len);
return str_ret_offset;
}
static int32 _memcmp_wrapper(int32 s1_offset, int32 s2_offset, int32 size)
static int32
_memcmp_wrapper(int32 s1_offset, int32 s2_offset, int32 size)
{
wasm_module_inst_t module_inst = get_module_inst();
void *s1, *s2;
if (!validate_app_addr(s1_offset, size) || !validate_app_addr(s2_offset, size))
if (!validate_app_addr(s1_offset, size)
|| !validate_app_addr(s2_offset, size))
return 0;
s1 = addr_app_to_native(s1_offset);
@ -487,7 +540,8 @@ s2 = addr_app_to_native(s2_offset);
return memcmp(s1, s2, size);
}
static int32 _memcpy_wrapper(int32 dst_offset, int32 src_offset, int32 size)
static int32
_memcpy_wrapper(int32 dst_offset, int32 src_offset, int32 size)
{
wasm_module_inst_t module_inst = get_module_inst();
void *dst, *src;
@ -495,7 +549,8 @@ void *dst, *src;
if (size == 0)
return dst_offset;
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
if (!validate_app_addr(dst_offset, size)
|| !validate_app_addr(src_offset, size))
return dst_offset;
dst = addr_app_to_native(dst_offset);
@ -504,12 +559,14 @@ memcpy(dst, src, size);
return dst_offset;
}
static int32 _memmove_wrapper(int32 dst_offset, int32 src_offset, int32 size)
static int32
_memmove_wrapper(int32 dst_offset, int32 src_offset, int32 size)
{
wasm_module_inst_t module_inst = get_module_inst();
void *dst, *src;
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
if (!validate_app_addr(dst_offset, size)
|| !validate_app_addr(src_offset, size))
return dst_offset;
dst = addr_app_to_native(dst_offset);
@ -518,7 +575,8 @@ memmove(dst, src, size);
return dst_offset;
}
static int32 _memset_wrapper(int32 s_offset, int32 c, int32 size)
static int32
_memset_wrapper(int32 s_offset, int32 c, int32 size)
{
wasm_module_inst_t module_inst = get_module_inst();
void *s;
@ -531,7 +589,8 @@ memset(s, c, size);
return s_offset;
}
static int32 _strchr_wrapper(int32 s_offset, int32 c)
static int32
_strchr_wrapper(int32 s_offset, int32 c)
{
wasm_module_inst_t module_inst = get_module_inst();
const char *s;
@ -545,12 +604,14 @@ ret = strchr(s, c);
return ret ? addr_native_to_app(ret) : 0;
}
static int32 _strcmp_wrapper(int32 s1_offset, int32 s2_offset)
static int32
_strcmp_wrapper(int32 s1_offset, int32 s2_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
void *s1, *s2;
if (!validate_app_addr(s1_offset, 1) || !validate_app_addr(s2_offset, 1))
if (!validate_app_addr(s1_offset, 1)
|| !validate_app_addr(s2_offset, 1))
return 0;
s1 = addr_app_to_native(s1_offset);
@ -558,12 +619,14 @@ s2 = addr_app_to_native(s2_offset);
return strcmp(s1, s2);
}
static int32 _strncmp_wrapper(int32 s1_offset, int32 s2_offset, uint32 size)
static int32
_strncmp_wrapper(int32 s1_offset, int32 s2_offset, uint32 size)
{
wasm_module_inst_t module_inst = get_module_inst();
void *s1, *s2;
if (!validate_app_addr(s1_offset, size) || !validate_app_addr(s2_offset, size))
if (!validate_app_addr(s1_offset, size)
|| !validate_app_addr(s2_offset, size))
return 0;
s1 = addr_app_to_native(s1_offset);
@ -571,12 +634,14 @@ s2 = addr_app_to_native(s2_offset);
return strncmp(s1, s2, size);
}
static int32 _strcpy_wrapper(int32 dst_offset, int32 src_offset)
static int32
_strcpy_wrapper(int32 dst_offset, int32 src_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
char *dst, *src;
if (!validate_app_addr(dst_offset, 1) || !validate_app_addr(src_offset, 1))
if (!validate_app_addr(dst_offset, 1)
|| !validate_app_addr(src_offset, 1))
return 0;
dst = addr_app_to_native(dst_offset);
@ -585,12 +650,14 @@ strcpy(dst, src);
return dst_offset;
}
static int32 _strncpy_wrapper(int32 dst_offset, int32 src_offset, uint32 size)
static int32
_strncpy_wrapper(int32 dst_offset, int32 src_offset, uint32 size)
{
wasm_module_inst_t module_inst = get_module_inst();
char *dst, *src;
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
if (!validate_app_addr(dst_offset, size)
|| !validate_app_addr(src_offset, size))
return 0;
dst = addr_app_to_native(dst_offset);
@ -599,7 +666,8 @@ strncpy(dst, src, size);
return dst_offset;
}
static uint32 _strlen_wrapper(int32 s_offset)
static uint32
_strlen_wrapper(int32 s_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
char *s;
@ -611,13 +679,15 @@ s = addr_app_to_native(s_offset);
return strlen(s);
}
static int32 _malloc_wrapper(uint32 size)
static int32
_malloc_wrapper(uint32 size)
{
wasm_module_inst_t module_inst = get_module_inst();
return module_malloc(size);
}
static int32 _calloc_wrapper(uint32 nmemb, uint32 size)
static int32
_calloc_wrapper(uint32 nmemb, uint32 size)
{
uint64 total_size = (uint64) nmemb * (uint64) size;
wasm_module_inst_t module_inst = get_module_inst();
@ -636,7 +706,8 @@ memset(ret_ptr, 0, (uint32) total_size);
return ret_offset;
}
static void _free_wrapper(int32 ptr_offset)
static void
_free_wrapper(int32 ptr_offset)
{
wasm_module_inst_t module_inst = get_module_inst();
@ -645,30 +716,39 @@ return;
return module_free(ptr_offset);
}
static void setTempRet0_wrapper(uint32 temp_ret)
static void
setTempRet0_wrapper(uint32 temp_ret)
{
wasm_module_inst_t module_inst = get_module_inst();
wasm_runtime_set_temp_ret(module_inst, temp_ret);
}
static uint32 getTempRet0_wrapper()
static uint32
getTempRet0_wrapper()
{
wasm_module_inst_t module_inst = get_module_inst();
return wasm_runtime_get_temp_ret(module_inst);
}
static uint32 _llvm_bswap_i16_wrapper(uint32 data)
static uint32
_llvm_bswap_i16_wrapper(uint32 data)
{
return (data & 0xFFFF0000) | ((data & 0xFF) << 8) | ((data & 0xFF00) >> 8);
return (data & 0xFFFF0000)
| ((data & 0xFF) << 8)
| ((data & 0xFF00) >> 8);
}
static uint32 _llvm_bswap_i32_wrapper(uint32 data)
static uint32
_llvm_bswap_i32_wrapper(uint32 data)
{
return ((data & 0xFF) << 24) | ((data & 0xFF00) << 8) | ((data & 0xFF0000) >> 8)
return ((data & 0xFF) << 24)
| ((data & 0xFF00) << 8)
| ((data & 0xFF0000) >> 8)
| ((data & 0xFF000000) >> 24);
}
static uint32 _bitshift64Lshr_wrapper(uint32 uint64_part0, uint32 uint64_part1,
static uint32
_bitshift64Lshr_wrapper(uint32 uint64_part0, uint32 uint64_part1,
uint32 bits)
{
wasm_module_inst_t module_inst = get_module_inst();
@ -686,7 +766,8 @@ wasm_runtime_set_temp_ret(module_inst, (uint32) (u.value >> 32));
return (uint32) u.value;
}
static uint32 _bitshift64Shl_wrapper(uint32 int64_part0, uint32 int64_part1,
static uint32
_bitshift64Shl_wrapper(uint32 int64_part0, uint32 int64_part1,
uint32 bits)
{
wasm_module_inst_t module_inst = get_module_inst();
@ -704,27 +785,31 @@ wasm_runtime_set_temp_ret(module_inst, (uint32) (u.value >> 32));
return (uint32) u.value;
}
static void _llvm_stackrestore_wrapper(uint32 llvm_stack)
static void
_llvm_stackrestore_wrapper(uint32 llvm_stack)
{
wasm_module_inst_t module_inst = get_module_inst();
printf("_llvm_stackrestore called!\n");
wasm_runtime_set_llvm_stack(module_inst, llvm_stack);
}
static uint32 _llvm_stacksave_wrapper()
static uint32
_llvm_stacksave_wrapper()
{
wasm_module_inst_t module_inst = get_module_inst();
printf("_llvm_stacksave called!\n");
return wasm_runtime_get_llvm_stack(module_inst);
}
static int32 _emscripten_memcpy_big_wrapper(int32 dst_offset, int32 src_offset,
static int32
_emscripten_memcpy_big_wrapper(int32 dst_offset, int32 src_offset,
uint32 size)
{
wasm_module_inst_t module_inst = get_module_inst();
void *dst, *src;
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
if (!validate_app_addr(dst_offset, size)
|| !validate_app_addr(src_offset, size))
return dst_offset;
dst = addr_app_to_native(dst_offset);
@ -734,7 +819,8 @@ memcpy(dst, src, size);
return dst_offset;
}
static void abort_wrapper(int32 code)
static void
abort_wrapper(int32 code)
{
wasm_module_inst_t module_inst = get_module_inst();
char buf[32];
@ -742,7 +828,8 @@ snprintf(buf, sizeof(buf), "env.abort(%i)", code);
wasm_runtime_set_exception(module_inst, buf);
}
static void abortStackOverflow_wrapper(int32 code)
static void
abortStackOverflow_wrapper(int32 code)
{
wasm_module_inst_t module_inst = get_module_inst();
char buf[32];
@ -750,7 +837,8 @@ snprintf(buf, sizeof(buf), "env.abortStackOverflow(%i)", code);
wasm_runtime_set_exception(module_inst, buf);
}
static void nullFunc_X_wrapper(int32 code)
static void
nullFunc_X_wrapper(int32 code)
{
wasm_module_inst_t module_inst = get_module_inst();
char buf[32];
@ -799,7 +887,8 @@ REG_NATIVE_FUNC(env, _llvm_stacksave),
REG_NATIVE_FUNC(env, _emscripten_memcpy_big),
REG_NATIVE_FUNC(env, abort),
REG_NATIVE_FUNC(env, abortStackOverflow),
REG_NATIVE_FUNC(env, nullFunc_X), };
REG_NATIVE_FUNC(env, nullFunc_X)
};
void*
wasm_native_func_lookup(const char *module_name, const char *func_name)
@ -835,17 +924,22 @@ const char *global_name;
WASMValue global_data;
} WASMNativeGlobalDef;
static WASMNativeGlobalDef native_global_defs[] = { { "env", "STACKTOP",
.global_data.u32 = 0 }, { "env", "STACK_MAX", .global_data.u32 = 0 }, { "env",
"ABORT", .global_data.u32 = 0 }, { "env", "memoryBase", .global_data.u32 = 0 },
{ "env", "__memory_base", .global_data.u32 = 0 }, { "env", "tableBase",
.global_data.u32 = 0 }, { "env", "__table_base", .global_data.u32 = 0 }, {
"env", "DYNAMICTOP_PTR", .global_data.addr = 0 }, { "env", "tempDoublePtr",
.global_data.addr = 0 }, { "global", "NaN", .global_data.u64 =
0x7FF8000000000000LL }, { "global", "Infinity", .global_data.u64 =
0x7FF0000000000000LL }, };
static WASMNativeGlobalDef native_global_defs[] = {
{ "env", "STACKTOP", .global_data.u32 = 0 },
{ "env", "STACK_MAX", .global_data.u32 = 0 },
{ "env", "ABORT", .global_data.u32 = 0 },
{ "env", "memoryBase", .global_data.u32 = 0 },
{ "env", "__memory_base", .global_data.u32 = 0 },
{ "env", "tableBase", .global_data.u32 = 0 },
{ "env", "__table_base", .global_data.u32 = 0 },
{ "env", "DYNAMICTOP_PTR", .global_data.addr = 0 },
{ "env", "tempDoublePtr", .global_data.addr = 0 },
{ "global", "NaN", .global_data.u64 = 0x7FF8000000000000LL },
{ "global", "Infinity", .global_data.u64 = 0x7FF0000000000000LL }
};
bool wasm_native_global_lookup(const char *module_name, const char *global_name,
bool
wasm_native_global_lookup(const char *module_name, const char *global_name,
WASMGlobalImport *global)
{
uint32 size = sizeof(native_global_defs) / sizeof(WASMNativeGlobalDef);
@ -885,7 +979,8 @@ return true;
return false;
}
bool wasm_native_init()
bool
wasm_native_init()
{
/* TODO: qsort the function defs and global defs. */
return true;

View File

@ -44,7 +44,7 @@ int gci_check_platform()
CHECK(1, sizeof(gc_int8));
CHECK(1, sizeof(gc_uint8));
CHECK(4, sizeof(gc_size_t));
CHECK(4, sizeof(void *));
/*CHECK(4, sizeof(void *));*/
return GC_SUCCESS;
}