Skip to content

Commit ee86de1

Browse files
committed
py: Make sure that static emg-exc-buffer is aligned to size of mp_obj_t.
This buffer is used to allocate objects temporarily, and such objects require that their underlying memory be correctly aligned for their data type. Aligning for mp_obj_t should be sufficient for emergency exceptions, but in general the memory buffer should aligned to the maximum alignment of the machine (eg on a 32-bit machine with mp_obj_t being 4 bytes, a double may not be correctly aligned). This patch fixes a bug for certain nan-boxing builds, where mp_obj_t is 8 bytes and must be aligned to 8 bytes (even though the machine is 32 bit).
1 parent b87432b commit ee86de1

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

py/mpstate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ typedef struct _mp_state_vm_t {
120120
// memory for exception arguments if we can't allocate RAM
121121
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
122122
#if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
123-
// statically allocated buf
124-
byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE];
123+
// statically allocated buf (needs to be aligned to mp_obj_t)
124+
mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
125125
#else
126126
// dynamically allocated buf
127127
byte *mp_emergency_exception_buf;

py/objexcept.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
348348
tuple->items[0] = MP_OBJ_FROM_PTR(str);
349349

350350
byte *str_data = (byte *)&str[1];
351-
uint max_len = MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size
351+
size_t max_len = (byte*)MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size
352352
- str_data;
353353

354354
vstr_t vstr;
@@ -366,14 +366,14 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
366366

367367
o->args = tuple;
368368

369-
uint offset = &str_data[str->len] - MP_STATE_VM(mp_emergency_exception_buf);
369+
size_t offset = &str_data[str->len] - (byte*)MP_STATE_VM(mp_emergency_exception_buf);
370370
offset += sizeof(void *) - 1;
371371
offset &= ~(sizeof(void *) - 1);
372372

373373
if ((mp_emergency_exception_buf_size - offset) > (sizeof(o->traceback_data[0]) * 3)) {
374374
// We have room to store some traceback.
375375
o->traceback_data = (size_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset);
376-
o->traceback_alloc = (MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]);
376+
o->traceback_alloc = ((byte*)MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]);
377377
o->traceback_len = 0;
378378
}
379379
}

0 commit comments

Comments
 (0)