From 8fe53a84cae9a25a4b59bbee70f06bec173e057d Mon Sep 17 00:00:00 2001 From: Bastian Mueller Date: Wed, 4 Oct 2023 22:15:10 -0700 Subject: [PATCH] simplify big-endian support --- w2c2/w2c2_base.h | 270 ++++++++++++++++++++++++++++++------------ wasi/wasi.c | 302 +---------------------------------------------- 2 files changed, 199 insertions(+), 373 deletions(-) diff --git a/w2c2/w2c2_base.h b/w2c2/w2c2_base.h index fed67609..97461ab1 100644 --- a/w2c2/w2c2_base.h +++ b/w2c2/w2c2_base.h @@ -96,12 +96,35 @@ typedef double F64; #endif /* WASM_ENDIAN */ #if WASM_ENDIAN == WASM_BIG_ENDIAN + +/* + * Use compiler byte-swapping intrinsics if they are available. + * 32-bit and 64-bit versions are available in Clang and GCC as of GCC 4.8.0. + */ +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5)) + +#define swap16(x) __builtin_bswap16(x) +#define swap32(x) __builtin_bswap32(x) +#define swap64(x) __builtin_bswap64(x) + +#elif defined(__APPLE__) + +#include + +#define swap16(x) OSSwapInt16(x) +#define swap32(x) OSSwapInt32(x) +#define swap64(x) OSSwapInt64(x) + +#else + /* * Mask, and then shift. * Mask, and then shift. * Mask, and then shift. * Mask, and then shift. */ +#define swap16(x) ((((x) & 0xFF00) >> 8) \ + | (((x) & 0x00FF) << 8)) #define swap32(x) ((((x) & 0xFF000000) >> 24) \ | (((x) & 0x00FF0000) >> 8 ) \ | (((x) & 0x0000FF00) << 8 ) \ @@ -114,6 +137,9 @@ typedef double F64; | (((x) & 0x0000000000ff0000ull) << 24) \ | (((x) & 0x000000000000ff00ull) << 40) \ | (((x) & 0x00000000000000ffull) << 56)) + +#endif + #elif WASM_ENDIAN == WASM_LITTLE_ENDIAN #define swap32(x) (x) #define swap64(x) (x) @@ -569,12 +595,7 @@ wasmMemoryGrow( return (U32) -1; } -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memset(newData + oldSize, 0, deltaSize); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - memmove(newData + newSize - oldSize, newData, oldSize); - memset(newData, 0, deltaSize); -#endif memory->pages = newPages; memory->size = newSize; memory->data = newData; @@ -593,20 +614,12 @@ wasmMemoryCopy( U32 sourceAddress, U32 count ) { -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memmove( destinationMemory->data + destinationAddress, sourceMemory->data + sourceAddress, count ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - memmove( - destinationMemory->data + destinationMemory->size - destinationAddress - count, - sourceMemory->data + sourceMemory->size - sourceAddress - count, - count - ); -#endif - } +} static W2C2_INLINE @@ -617,22 +630,13 @@ wasmMemoryFill( U32 value, U32 count ) { -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memset( memory->data + destinationAddress, (int) value, (size_t) count ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - memset( - memory->data + memory->size - destinationAddress - count, - (int) value, - (size_t) count - ); -#endif } -#if WASM_ENDIAN == WASM_BIG_ENDIAN static W2C2_INLINE void @@ -641,48 +645,44 @@ load_data( const void *src, size_t n ) { - size_t i = 0; - U8* destChars = dest; memcpy(dest, src, n); - for (; i < (n>>1); i++) { - U8 cursor = destChars[i]; - destChars[i] = destChars[n - i - 1]; - destChars[n - i - 1] = cursor; - } } -#define LOAD_DATA(m, o, i, s) \ - load_data(&((m).data[(m).size - (o) - (s)]), i, s) +#if WASM_ENDIAN == WASM_BIG_ENDIAN -#define DEFINE_LOAD(name, t1, t2, t3) \ - static W2C2_INLINE t3 name(wasmMemory* mem, U64 addr) { \ - t1 result; \ - memcpy(&result, &mem->data[mem->size - addr - sizeof(t1)], sizeof(t1)); \ - return (t3)(t2)result; \ - } +#if defined(__APPLE__) -#define DEFINE_STORE(name, t1, t2) \ - static W2C2_INLINE void name(wasmMemory* mem, U64 addr, t2 value) { \ - t1 wrapped = (t1)value; \ - memcpy(&mem->data[mem->size - addr - sizeof(t1)], &wrapped, sizeof(t1)); \ - } +#include -#elif WASM_ENDIAN == WASM_LITTLE_ENDIAN +#define readSwap16(base, offset) OSReadSwapInt16(base, offset) +#define readSwap32(base, offset) OSReadSwapInt32(base, offset) +#define readSwap64(base, offset) OSReadSwapInt64(base, offset) -static -W2C2_INLINE -void -load_data( - void *dest, - const void *src, - size_t n -) { - memcpy(dest, src, n); -} +#define writeSwap16(base, offset, value) OSWriteSwapInt16(base, offset, value) +#define writeSwap32(base, offset, value) OSWriteSwapInt32(base, offset, value) +#define writeSwap64(base, offset, value) OSWriteSwapInt64(base, offset, value) + +#else + +#define readSwap16(base, offset) swap16(*(U16*)((base) + (offset))) +#define readSwap32(base, offset) swap32(*(U32*)((base) + (offset))) +#define readSwap64(base, offset) swap64(*(U64*)((base) + (offset))) + +#define writeSwap16(base, offset, value) (*(U16*)((base) + (offset)) = swap16(value)) +#define writeSwap32(base, offset, value) (*(U32*)((base) + (offset)) = swap32(value)) +#define writeSwap64(base, offset, value) (*(U64*)((base) + (offset)) = swap64(value)) + +#endif + +#endif + +/* LOAD_DATA */ #define LOAD_DATA(m, o, i, s) \ load_data(&((m).data[o]), i, s) +/* DEFINE_LOAD */ + #define DEFINE_LOAD(name, t1, t2, t3) \ static W2C2_INLINE t3 name(wasmMemory* mem, U64 addr) { \ t1 result; \ @@ -690,37 +690,153 @@ load_data( return (t3)(t2)result; \ } +/* DEFINE_LOAD8 */ + +#define DEFINE_LOAD8(name, t1, t2, t3) DEFINE_LOAD(name, t1, t2, t3) + +/* DEFINE_LOAD16 */ + +#if WASM_ENDIAN == WASM_LITTLE_ENDIAN + +#define DEFINE_LOAD16(name, t1, t2, t3) DEFINE_LOAD(name, t1, t2, t3) + +#elif WASM_ENDIAN == WASM_BIG_ENDIAN + +#define DEFINE_LOAD16(name, t1, t2, t3) \ + static W2C2_INLINE t3 name(wasmMemory* mem, U64 addr) { \ + t1 result; \ + U16 v = readSwap16(mem->data, addr); \ + memcpy(&result, &v, sizeof(U16)); \ + return (t3)(t2)result; \ + } + +#endif + +/* DEFINE_LOAD32 */ + +#if WASM_ENDIAN == WASM_LITTLE_ENDIAN + +#define DEFINE_LOAD32(name, t1, t2, t3) DEFINE_LOAD(name, t1, t2, t3) + +#elif WASM_ENDIAN == WASM_BIG_ENDIAN + +#define DEFINE_LOAD32(name, t1, t2, t3) \ + static W2C2_INLINE t3 name(wasmMemory* mem, U64 addr) { \ + t1 result; \ + U32 v = readSwap32(mem->data, addr); \ + memcpy(&result, &v, sizeof(U32)); \ + return (t3)(t2)result; \ + } + +#endif + +/* DEFINE_LOAD64 */ + +#if WASM_ENDIAN == WASM_LITTLE_ENDIAN + +#define DEFINE_LOAD64(name, t1, t2, t3) DEFINE_LOAD(name, t1, t2, t3) + +#elif WASM_ENDIAN == WASM_BIG_ENDIAN + +#define DEFINE_LOAD64(name, t1, t2, t3) \ + static W2C2_INLINE t3 name(wasmMemory* mem, U64 addr) { \ + t1 result; \ + U64 v = readSwap64(mem->data, addr); \ + memcpy(&result, &v, sizeof(U64)); \ + return (t3)(t2)result; \ + } + +#endif + +/* DEFINE_STORE */ + #define DEFINE_STORE(name, t1, t2) \ static W2C2_INLINE void name(wasmMemory* mem, U64 addr, t2 value) { \ t1 wrapped = (t1)value; \ memcpy(&mem->data[addr], &wrapped, sizeof(t1)); \ } +/* DEFINE_STORE8 */ + +#define DEFINE_STORE8(name, t1, t2) DEFINE_STORE(name, t1, t2) + +/* DEFINE_STORE16 */ + +#if WASM_ENDIAN == WASM_LITTLE_ENDIAN + +#define DEFINE_STORE16(name, t1, t2) DEFINE_STORE(name, t1, t2) + +#elif WASM_ENDIAN == WASM_BIG_ENDIAN + +#define DEFINE_STORE16(name, t1, t2) \ + static W2C2_INLINE void name(wasmMemory* mem, U64 addr, t2 value) { \ + t1 wrapped = (t1)value; \ + U16 v; \ + memcpy(&v, &wrapped, sizeof(U16)); \ + writeSwap16(mem->data, addr, v); \ + } + +#endif + +/* DEFINE_STORE32 */ + +#if WASM_ENDIAN == WASM_LITTLE_ENDIAN + +#define DEFINE_STORE32(name, t1, t2) DEFINE_STORE(name, t1, t2) + +#elif WASM_ENDIAN == WASM_BIG_ENDIAN + +#define DEFINE_STORE32(name, t1, t2) \ + static W2C2_INLINE void name(wasmMemory* mem, U64 addr, t2 value) { \ + t1 wrapped = (t1)value; \ + U32 v; \ + memcpy(&v, &wrapped, sizeof(U32)); \ + writeSwap32(mem->data, addr, v); \ + } + +#endif + +/* DEFINE_STORE64 */ + +#if WASM_ENDIAN == WASM_LITTLE_ENDIAN + +#define DEFINE_STORE64(name, t1, t2) DEFINE_STORE(name, t1, t2) + +#elif WASM_ENDIAN == WASM_BIG_ENDIAN + +#define DEFINE_STORE64(name, t1, t2) \ + static W2C2_INLINE void name(wasmMemory* mem, U64 addr, t2 value) { \ + t1 wrapped = (t1)value; \ + U64 v; \ + memcpy(&v, &wrapped, sizeof(U64)); \ + writeSwap64(mem->data, addr, v); \ + } + #endif -DEFINE_LOAD(i32_load, U32, U32, U32) -DEFINE_LOAD(i64_load, U64, U64, U64) -DEFINE_LOAD(f32_load, F32, F32, F32) -DEFINE_LOAD(f64_load, F64, F64, F64) -DEFINE_LOAD(i32_load8_s, I8, I32, U32) -DEFINE_LOAD(i64_load8_s, I8, I64, U64) -DEFINE_LOAD(i32_load8_u, U8, U32, U32) -DEFINE_LOAD(i64_load8_u, U8, U64, U64) -DEFINE_LOAD(i32_load16_s, I16, I32, U32) -DEFINE_LOAD(i64_load16_s, I16, I64, U64) -DEFINE_LOAD(i32_load16_u, U16, U32, U32) -DEFINE_LOAD(i64_load16_u, U16, U64, U64) -DEFINE_LOAD(i64_load32_s, I32, I64, U64) -DEFINE_LOAD(i64_load32_u, U32, U64, U64) -DEFINE_STORE(i32_store, U32, U32) -DEFINE_STORE(i64_store, U64, U64) -DEFINE_STORE(f32_store, F32, F32) -DEFINE_STORE(f64_store, F64, F64) -DEFINE_STORE(i32_store8, U8, U32) -DEFINE_STORE(i32_store16, U16, U32) -DEFINE_STORE(i64_store8, U8, U64) -DEFINE_STORE(i64_store16, U16, U64) -DEFINE_STORE(i64_store32, U32, U64) +DEFINE_LOAD32(i32_load, U32, U32, U32) +DEFINE_LOAD64(i64_load, U64, U64, U64) +DEFINE_LOAD32(f32_load, F32, F32, F32) +DEFINE_LOAD64(f64_load, F64, F64, F64) +DEFINE_LOAD8(i32_load8_s, I8, I32, U32) +DEFINE_LOAD8(i64_load8_s, I8, I64, U64) +DEFINE_LOAD8(i32_load8_u, U8, U32, U32) +DEFINE_LOAD8(i64_load8_u, U8, U64, U64) +DEFINE_LOAD16(i32_load16_s, I16, I32, U32) +DEFINE_LOAD16(i64_load16_s, I16, I64, U64) +DEFINE_LOAD16(i32_load16_u, U16, U32, U32) +DEFINE_LOAD16(i64_load16_u, U16, U64, U64) +DEFINE_LOAD32(i64_load32_s, I32, I64, U64) +DEFINE_LOAD32(i64_load32_u, U32, U64, U64) +DEFINE_STORE32(i32_store, U32, U32) +DEFINE_STORE64(i64_store, U64, U64) +DEFINE_STORE32(f32_store, F32, F32) +DEFINE_STORE64(f64_store, F64, F64) +DEFINE_STORE8(i32_store8, U8, U32) +DEFINE_STORE16(i32_store16, U16, U32) +DEFINE_STORE8(i64_store8, U8, U64) +DEFINE_STORE16(i64_store16, U16, U64) +DEFINE_STORE32(i64_store32, U32, U64) typedef void (*wasmFunc)(void); diff --git a/wasi/wasi.c b/wasi/wasi.c index cb0400a1..67c48b6f 100644 --- a/wasi/wasi.c +++ b/wasi/wasi.c @@ -725,9 +725,6 @@ wasiFDWrite( struct iovec* iovecs = NULL; I64 total = 0; WasiFileDescriptor descriptor = emptyWasiFileDescriptor; -#if WASM_ENDIAN == WASM_BIG_ENDIAN - U8* temporaryBuffer = NULL; -#endif if (wasiFD > 2) { WASI_TRACE(( @@ -760,7 +757,6 @@ wasiFDWrite( return WASI_ERRNO_NOMEM; } -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN /* Convert WASI ciovecs to native iovecs */ { U32 ciovecIndex = 0; @@ -783,63 +779,12 @@ wasiFDWrite( iovecs[ciovecIndex].iov_len = length; } } -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - - /* Convert WASI ciovecs to native iovecs */ - { - U8* memoryStart = memory->data + memory->size - 1; - - U32 totalLength = 0; - U32 ciovecIndex = 0; - for (; ciovecIndex < ciovecsCount; ciovecIndex++) { - U64 ciovecPointer = ciovecsPointer + ciovecIndex * ciovecSize; - U32 length = i32_load(memory, ciovecPointer + 4); - iovecs[ciovecIndex].iov_len = length; - totalLength += length; - } - - temporaryBuffer = malloc(totalLength); - if (temporaryBuffer == NULL) { - return WASI_ERRNO_NOMEM; - } - - totalLength = 0; - ciovecIndex = 0; - for (; ciovecIndex < ciovecsCount; ciovecIndex++) { - U64 ciovecPointer = ciovecsPointer + ciovecIndex * ciovecSize; - U32 bufferPointer = i32_load(memory, ciovecPointer); - U32 length = iovecs[ciovecIndex].iov_len; - U8* bufferStart = memoryStart - bufferPointer; - U32 i = 0; - for (; i < length; i++) { - temporaryBuffer[totalLength + i] = bufferStart[-i]; - } - - iovecs[ciovecIndex].iov_base = temporaryBuffer + totalLength; - - totalLength += length; - if (wasiFD > 2) { - WASI_TRACE(( - "fd_write: " - "length=%d, " - "bufferPointer=0x%x", - length, - bufferPointer - )); - } - } - } -#endif /* Perform the writes */ total = writeFunc(descriptor.fd, iovecs, ciovecsCount, offset); free(iovecs); -#if WASM_ENDIAN == WASM_BIG_ENDIAN - free(temporaryBuffer); -#endif - if (total < 0) { WASI_TRACE(("fd_write: writev failed: %s", strerror(errno))); return wasiErrno(); @@ -998,20 +943,12 @@ wasiFDRead( /* Convert WASI iovecs to native iovecs */ { -#if WASM_ENDIAN == WASM_BIG_ENDIAN - U8* memoryStart = memory->data + memory->size; -#endif U32 iovecIndex = 0; for (; iovecIndex < iovecsCount; iovecIndex++) { U64 iovecPointer = iovecsPointer + iovecIndex * iovecSize; U32 bufferPointer = i32_load(memory, iovecPointer); U32 length = i32_load(memory, iovecPointer + 4); - -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN iovecs[iovecIndex].iov_base = memory->data + bufferPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - iovecs[iovecIndex].iov_base = memoryStart - bufferPointer - length; -#endif iovecs[iovecIndex].iov_len = length; } } @@ -1026,22 +963,6 @@ wasiFDRead( return wasiErrno(); } -#if WASM_ENDIAN == WASM_BIG_ENDIAN - { - U32 iovecIndex = 0; - for (; iovecIndex < iovecsCount; iovecIndex++) { - U8* base = iovecs[iovecIndex].iov_base; - U32 length = iovecs[iovecIndex].iov_len; - int i = 0; - for (; i < length / 2; i++) { - U8 value = base[i]; - base[i] = base[length - i - 1]; - base[length - i - 1] = value; - } - } - } -#endif - free(iovecs); /* Store the amount of read bytes at the result pointer */ @@ -1153,9 +1074,6 @@ wasiEnvironGet( wasmMemory* memory = wasiMemory(instance); U32 index = 0; -#if WASM_ENDIAN == WASM_BIG_ENDIAN - U8* memoryStart = memory->data + memory->size - 1; -#endif WASI_TRACE(( "environ_get(" @@ -1169,18 +1087,11 @@ wasiEnvironGet( for (; wasi.envp[index] != NULL; index++) { char* env = wasi.envp[index]; size_t length = strlen(env) + 1; -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memcpy( memory->data + envpBufPointer, env, length ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - U32 i = 0; - for (; i < length; i++) { - memoryStart[-envpBufPointer-i] = env[i]; - } -#endif i32_store( memory, envpPointer + index * sizeof(U32), @@ -1244,9 +1155,6 @@ wasiArgsGet( wasmMemory* memory = wasiMemory(instance); U32 index = 0; -#if WASM_ENDIAN == WASM_BIG_ENDIAN - U8* memoryStart = memory->data + memory->size - 1; -#endif WASI_TRACE(( "args_get(" @@ -1260,18 +1168,11 @@ wasiArgsGet( for (; index < wasi.argc; index++) { char* arg = wasi.argv[index]; size_t length = strlen(arg) + 1; -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memcpy( memory->data + argvBufPointer, arg, length ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - U32 i = 0; - for (; i < length; i++) { - memoryStart[-argvBufPointer-i] = arg[i]; - } -#endif i32_store( memory, argvPointer + index * sizeof(U32), @@ -1684,19 +1585,11 @@ wasiFDReaddir( break; } -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memset( memory->data + resultPointer, 0, WASI_DIRENT_SIZE ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - memset( - memory->data + memory->size - resultPointer - WASI_DIRENT_SIZE, - 0, - WASI_DIRENT_SIZE - ); -#endif i64_store(memory, resultPointer, next); i64_store(memory, resultPointer + 8, inode); @@ -1714,22 +1607,12 @@ wasiFDReaddir( ? bufferRemaining : nameLength; -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memcpy( memory->data + resultPointer, name, adjustedNameLength ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - { - U8* base = memory->data + memory->size - 1 - resultPointer; - U32 i = 0; - for (; i < adjustedNameLength; i++) { - base[-i] = name[i]; - } - } -#endif bufferUsed += adjustedNameLength; } @@ -2338,19 +2221,11 @@ wasiFdFdstatGet( } /* Store result */ -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memset( memory->data + resultPointer, 0, WASI_FDSTAT_SIZE ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - memset( - memory->data + memory->size - resultPointer - WASI_FDSTAT_SIZE, - 0, - WASI_FDSTAT_SIZE - ); -#endif WASI_TRACE(( "fd_fdstat_get: " @@ -2564,22 +2439,11 @@ wasiFdPrestatDirName( length = pathLength; } -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memcpy( memory->data + pathPointer, path, length ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - { - U8* base = memory->data + memory->size - 1 - pathPointer; - - U32 i = 0; - for (; i < length; i++) { - base[-i] = path[i]; - } - } -#endif return WASI_ERRNO_SUCCESS; } @@ -2598,29 +2462,6 @@ WASI_IMPORT(U32, fd_prestat_dir_name, ( ); }) -static -W2C2_INLINE -bool -getBigEndianPath( - wasmMemory* memory, - U32 pathPointer, - U32 pathLength, - char path[PATH_MAX] -) { - char* base = (char*) memory->data + memory->size - 1 - pathPointer; - U32 i = 0; - - MUST (pathLength <= PATH_MAX) - - for (; i < pathLength; i++) { - path[i] = base[-i]; - } - - path[pathLength] = '\0'; - - return true; -} - static W2C2_INLINE U32 @@ -2638,11 +2479,8 @@ wasiPathOpen( ) { wasmMemory* memory = wasiMemory(instance); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN char* path = (char*) memory->data + pathPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - char path[PATH_MAX]; -#endif + char resolvedPath[PATH_MAX]; char nativeResolvedPath[PATH_MAX]; int nativeFlags = 0; @@ -2683,13 +2521,6 @@ wasiPathOpen( fdPointer )); -#if WASM_ENDIAN == WASM_BIG_ENDIAN - if (!getBigEndianPath(memory, pathPointer, pathLength, path)) { - WASI_TRACE(("path_open: bad path")); - return WASI_ERRNO_INVAL; - } -#endif - WASI_TRACE(( "path_open: " "path=%.*s", @@ -2697,7 +2528,6 @@ wasiPathOpen( path )); - if (!wasiFileDescriptorGet(wasiDirFD, &preopenFileDescriptor)) { WASI_TRACE(("path_open: bad preopen FD")); return WASI_ERRNO_BADF; @@ -2925,19 +2755,11 @@ storePreview1Filestat( getStatTimes(st, &access, &modification, &creation); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memset( memory->data + statPointer, 0, wasiPreview1FilestatSize ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - memset( - memory->data + memory->size - statPointer - wasiPreview1FilestatSize, - 0, - wasiPreview1FilestatSize - ); -#endif { I64 dev = st->st_dev; @@ -3071,19 +2893,11 @@ storeUnstableFilestat( getStatTimes(st, &access, &modification, &creation); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN memset( memory->data + statPointer, 0, wasiUnstableFilestatSize ); -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - memset( - memory->data + memory->size - statPointer - wasiUnstableFilestatSize, - 0, - wasiUnstableFilestatSize - ); -#endif { I64 dev = st->st_dev; @@ -3167,11 +2981,7 @@ wasiPathFilestatGet( U32 pathLength, struct stat* st ) { -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN char* path = (char*) memory->data + pathPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - char path[PATH_MAX]; -#endif char resolvedPath[PATH_MAX]; char nativeResolvedPath[PATH_MAX]; int res = 0; @@ -3183,13 +2993,6 @@ wasiPathFilestatGet( return WASI_ERRNO_BADF; } -#if WASM_ENDIAN == WASM_BIG_ENDIAN - if (!getBigEndianPath(memory, pathPointer, pathLength, path)) { - WASI_TRACE(("path_filestat_get: bad path")); - return WASI_ERRNO_INVAL; - } -#endif - WASI_TRACE(( "path_filestat_get: " "path=%.*s", @@ -3365,13 +3168,8 @@ wasiPathRename( ) { wasmMemory* memory = wasiMemory(instance); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN char* oldPath = (char*) memory->data + oldPathPointer; char* newPath = (char*) memory->data + newPathPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - char oldPath[PATH_MAX]; - char newPath[PATH_MAX]; -#endif char oldResolvedPath[PATH_MAX]; char newResolvedPath[PATH_MAX]; char nativeOldResolvedPath[PATH_MAX]; @@ -3409,18 +3207,6 @@ wasiPathRename( return WASI_ERRNO_BADF; } -#if WASM_ENDIAN == WASM_BIG_ENDIAN - if (!getBigEndianPath(memory, oldPathPointer, oldPathLength, oldPath)) { - WASI_TRACE(("path_rename: bad old path")); - return WASI_ERRNO_INVAL; - } - - if (!getBigEndianPath(memory, newPathPointer, newPathLength, newPath)) { - WASI_TRACE(("path_rename: bad new path")); - return WASI_ERRNO_INVAL; - } -#endif - WASI_TRACE(( "path_rename: " "oldPath=%.*s, " @@ -3519,11 +3305,7 @@ wasiPathUnlinkFile( ) { wasmMemory* memory = wasiMemory(instance); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN char* path = (char*) memory->data + pathPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - char path[PATH_MAX]; -#endif char resolvedPath[PATH_MAX]; char nativeResolvedPath[PATH_MAX]; int res = -1; @@ -3546,13 +3328,6 @@ wasiPathUnlinkFile( return WASI_ERRNO_BADF; } -#if WASM_ENDIAN == WASM_BIG_ENDIAN - if (!getBigEndianPath(memory, pathPointer, pathLength, path)) { - WASI_TRACE(("path_unlink_file: bad path")); - return WASI_ERRNO_INVAL; - } -#endif - WASI_TRACE(( "path_unlink_file: " "path=%.*s", @@ -3623,11 +3398,7 @@ wasiPathRemoveDirectory( ) { wasmMemory* memory = wasiMemory(instance); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN char* path = (char*) memory->data + pathPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - char path[PATH_MAX]; -#endif char resolvedPath[PATH_MAX]; char nativeResolvedPath[PATH_MAX]; int res = -1; @@ -3650,13 +3421,6 @@ wasiPathRemoveDirectory( return WASI_ERRNO_BADF; } -#if WASM_ENDIAN == WASM_BIG_ENDIAN - if (!getBigEndianPath(memory, pathPointer, pathLength, path)) { - WASI_TRACE(("path_remove_directory: bad path")); - return WASI_ERRNO_INVAL; - } -#endif - WASI_TRACE(( "path_remove_directory: " "path=%.*s", @@ -3727,11 +3491,7 @@ wasiPathCreateDirectory( ) { wasmMemory* memory = wasiMemory(instance); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN char* path = (char*) memory->data + pathPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - char path[PATH_MAX]; -#endif char resolvedPath[PATH_MAX]; char nativeResolvedPath[PATH_MAX]; int res = -1; @@ -3754,13 +3514,6 @@ wasiPathCreateDirectory( return WASI_ERRNO_BADF; } -#if WASM_ENDIAN == WASM_BIG_ENDIAN - if (!getBigEndianPath(memory, pathPointer, pathLength, path)) { - WASI_TRACE(("path_create_directory: bad path")); - return WASI_ERRNO_INVAL; - } -#endif - WASI_TRACE(( "path_create_directory: " "path=%.*s", @@ -3832,11 +3585,7 @@ wasiPathSymlink( ) { wasmMemory* memory = wasiMemory(instance); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN char* newPath = (char*) memory->data + newPathPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - char newPath[PATH_MAX]; -#endif char oldResolvedPath[PATH_MAX]; char newResolvedPath[PATH_MAX]; char nativeOldResolvedPath[PATH_MAX]; @@ -3884,22 +3633,13 @@ wasiPathSymlink( return WASI_ERRNO_INVAL; } -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN - memcpy(oldResolvedPath, memory->data + oldPathPointer, oldPathLength); + memcpy( + oldResolvedPath, + memory->data + oldPathPointer, + oldPathLength + ); oldResolvedPath[oldPathLength] = '\0'; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - if (!getBigEndianPath(memory, oldPathPointer, oldPathLength, oldResolvedPath)) { - WASI_TRACE(("path_symlink: bad old path")); - return WASI_ERRNO_INVAL; - } - - if (!getBigEndianPath(memory, newPathPointer, newPathLength, newPath)) { - WASI_TRACE(("path_symlink: bad new path")); - return WASI_ERRNO_INVAL; - } -#endif - WASI_TRACE(( "path_symlink: " "oldResolvedPath=%s, " @@ -4001,11 +3741,7 @@ wasiPathReadlink( ) { wasmMemory* memory = wasiMemory(instance); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN char* path = (char*) memory->data + pathPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - char path[PATH_MAX]; -#endif char resolvedPath[PATH_MAX]; char nativeResolvedPath[PATH_MAX]; char* buffer = NULL; @@ -4048,13 +3784,6 @@ wasiPathReadlink( return WASI_ERRNO_BADF; } -#if WASM_ENDIAN == WASM_BIG_ENDIAN - if (!getBigEndianPath(memory, pathPointer, pathLength, path)) { - WASI_TRACE(("path_readlink: bad path")); - return WASI_ERRNO_INVAL; - } -#endif - WASI_TRACE(( "path_readlink: " "path=%.*s", @@ -4079,11 +3808,7 @@ wasiPathReadlink( resolvedPath )); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN buffer = (char*)memory->data + bufferPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - buffer = (char*)memory->data + memory->size - bufferPointer - bufferLength; -#endif strcpy(nativeResolvedPath, resolvedPath); #if HAS_NONPOSIXPATH @@ -4107,17 +3832,6 @@ wasiPathReadlink( return wasiErrno(); } -#if WASM_ENDIAN == WASM_BIG_ENDIAN - { - U32 i = 0; - for (; i < length / 2; i++) { - char value = buffer[i]; - buffer[i] = buffer[length - i - 1]; - buffer[length - i - 1] = value; - } - } -#endif - i32_store(memory, lengthPointer, length); return WASI_ERRNO_SUCCESS; @@ -4187,11 +3901,7 @@ wasiRandomGet( bufferLength )); -#if WASM_ENDIAN == WASM_LITTLE_ENDIAN bufferStart = memory->data + bufferPointer; -#elif WASM_ENDIAN == WASM_BIG_ENDIAN - bufferStart = memory->data + memory->size - bufferPointer - bufferLength; -#endif #ifdef _WIN32 #include