Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

native runtime: a few endian and alignment fixes #782

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions runtimes/native/src/runtime.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "runtime.h"

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -240,30 +241,30 @@ void w4_runtimeTracef (const uint8_t* str, const void* stack) {
break;
case 'c':
bounds_check(argPtr, 4);
putc(*(char*)argPtr, stdout);
putc((char)w4_read32LE(argPtr), stdout);
argPtr += 4;
break;
case 'd':
bounds_check(argPtr, 4);
printf("%d", *(int32_t*)argPtr);
printf("%" PRId32, w4_read32LE(argPtr));
argPtr += 4;
break;
case 'x':
bounds_check(argPtr, 4);
printf("%x", *(uint32_t*)argPtr);
printf("%" PRIx32, w4_read32LE(argPtr));
argPtr += 4;
break;
case 's':
bounds_check(argPtr, 4);
strPtr = *(uint32_t*)argPtr;
strPtr = w4_read32LE(argPtr);
argPtr += 4;
const char *strPtr_host = (const char *)memory + strPtr;
bounds_check_cstr(strPtr_host);
printf("%s", strPtr_host);
break;
case 'f':
bounds_check(argPtr, 8);
printf("%lg", *(double*)argPtr);
printf("%lg", w4_readf64LE(argPtr));
argPtr += 8;
break;
default:
Expand Down
46 changes: 34 additions & 12 deletions runtimes/native/src/util.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define W4_BIG_ENDIAN
Expand All @@ -21,6 +22,7 @@ void* xrealloc(void* ptr, size_t size) {
fputs("Allocation failed.\n", stderr);
abort();
}
return ptr;
}

uint16_t bswap16(uint16_t x) {
Expand All @@ -34,34 +36,54 @@ uint32_t bswap32(uint32_t x) {
(( x & 0x000000ffu ) << 24 ));
}

uint16_t w4_read16LE (const uint16_t* ptr) {
uint16_t w4_read16LE (const void* ptr) {
uint16_t le;
memcpy(&le, ptr, sizeof(le));
#ifdef W4_BIG_ENDIAN
return bswap16(*ptr);
return bswap16(le);
#else
return *ptr;
return le;
#endif
}

uint32_t w4_read32LE (const uint32_t* ptr) {
uint32_t w4_read32LE (const void* ptr) {
uint32_t le;
memcpy(&le, ptr, sizeof(le));
#ifdef W4_BIG_ENDIAN
return bswap32(*ptr);
return bswap32(le);
#else
return *ptr;
return le;
#endif
}

double w4_readf64LE (const void* ptr) {
union {
uint64_t u;
double d;
} u;
memcpy(&u.d, ptr, sizeof(u.d));
#ifdef W4_BIG_ENDIAN
u.u = bswap32(u.u);
#endif
return u.d;
}

void w4_write16LE (uint16_t* ptr, uint16_t value) {
void w4_write16LE (void* ptr, uint16_t value) {
uint16_t le;
#ifdef W4_BIG_ENDIAN
*ptr = bswap16(value);
le = bswap16(value);
#else
*ptr = value;
le = value;
#endif
memcpy(ptr, &le, sizeof(le));
}

void w4_write32LE (uint32_t* ptr, uint32_t value) {
void w4_write32LE (void* ptr, uint32_t value) {
uint32_t le;
#ifdef W4_BIG_ENDIAN
*ptr = bswap32(value);
le = bswap32(value);
#else
*ptr = value;
le = value;
#endif
memcpy(ptr, &le, sizeof(le));
}
9 changes: 5 additions & 4 deletions runtimes/native/src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
void* xmalloc(size_t size);
void* xrealloc(void* ptr, size_t size);

uint16_t w4_read16LE (const uint16_t* ptr);
uint32_t w4_read32LE (const uint32_t* ptr);
uint16_t w4_read16LE (const void* ptr);
uint32_t w4_read32LE (const void* ptr);
double w4_readf64LE (const void* ptr);

void w4_write16LE (uint16_t* ptr, uint16_t value);
void w4_write32LE (uint32_t* ptr, uint32_t value);
void w4_write16LE (void* ptr, uint16_t value);
void w4_write32LE (void* ptr, uint32_t value);
Loading