From 5043268cb187b668fff575baa50a8c48a282eb46 Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Sun, 24 Jan 2021 11:28:26 +0100 Subject: [PATCH] Fix unaligned access arm (openbsd) libtcc.c: Fix unaligned load/store from magic3 tccgen.c: For packed struct allways use single byte code If field does not start at at align check it Align stack correctly with vla --- libtcc.c | 18 +++++++++++++----- tccgen.c | 10 +++++++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/libtcc.c b/libtcc.c index 84392be7..9d2cbaf9 100644 --- a/libtcc.c +++ b/libtcc.c @@ -273,12 +273,20 @@ PUB_FUNC char *tcc_strdup(const char *str) #define MEM_DEBUG_MAGIC2 0xFEEDDEB2 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3 #define MEM_DEBUG_FILE_LEN 40 +#define MEM_DEBUG_OFF3 offsetof(mem_debug_header_t, magic3) #define MEM_DEBUG_CHECK3(header) \ - ((mem_debug_header_t*)((char*)header + header->size))->magic3 + ({unsigned magic3; \ + memcpy(&magic3, (char*)header + MEM_DEBUG_OFF3 + header->size, \ + sizeof (magic3)); \ + magic3;}) +#define MEM_DEBUG_SET3(header) \ + {unsigned magic3 = MEM_DEBUG_MAGIC3; \ + memcpy((char*)header + MEM_DEBUG_OFF3 + header->size, &magic3, \ + sizeof (magic3));} #define MEM_USER_PTR(header) \ - ((char *)header + offsetof(mem_debug_header_t, magic3)) + ((char *)header + MEM_DEBUG_OFF3) #define MEM_HEADER_PTR(ptr) \ - (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3)) + (mem_debug_header_t *)((char*)ptr - MEM_DEBUG_OFF3) struct mem_debug_header { unsigned magic1; @@ -325,7 +333,7 @@ PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line) header->magic1 = MEM_DEBUG_MAGIC1; header->magic2 = MEM_DEBUG_MAGIC2; header->size = size; - MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3; + MEM_DEBUG_SET3(header); header->line_num = line; ofs = strlen(file) - MEM_DEBUG_FILE_LEN; strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN); @@ -382,7 +390,7 @@ PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file if (!header) _tcc_error("memory full (realloc)"); header->size = size; - MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3; + MEM_DEBUG_SET3(header); if (header->next) header->next->prev = header; if (header->prev) diff --git a/tccgen.c b/tccgen.c index 29e21669..2728af45 100644 --- a/tccgen.c +++ b/tccgen.c @@ -4727,7 +4727,10 @@ static void struct_layout(CType *type, AttributeDef *ad) continue; bit_pos = BIT_POS(f->type.t); size = type_size(&f->type, &align); - if (bit_pos + bit_size <= size * 8 && f->c + size <= c) + if (c & 3) /* packed struct */ + goto single_byte; + if (bit_pos + bit_size <= size * 8 && f->c + size <= c && + (f->c & (align - 1)) == 0) continue; /* try to access the field using a different type */ @@ -4768,6 +4771,7 @@ static void struct_layout(CType *type, AttributeDef *ad) cx, s, align, px, bit_size); #endif } else { +single_byte: /* fall back to load/store single-byte wise */ f->auxtype = VT_STRUCT; #ifdef BF_DEBUG @@ -8269,7 +8273,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, #ifdef CONFIG_TCC_BCHECK if (bcheck && v) { /* add padding between stack variables for bound checking */ - loc--; + loc -= align; } #endif loc = (loc - size) & -align; @@ -8278,7 +8282,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, #ifdef CONFIG_TCC_BCHECK if (bcheck && v) { /* add padding between stack variables for bound checking */ - loc--; + loc -= align; } #endif if (v) {