summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-04-10 16:02:56 +1000
committerDamien George <damien.p.george@gmail.com>2017-04-10 16:02:56 +1000
commitee86de1f1a22c45548bc39916fcd2b72dd26ebca (patch)
tree716766c5ab42e6079058262c9d5b36a2b7b1ea6e /py
parentb87432b8fb8332548be11b63c9139065ce565f91 (diff)
downloadmicropython-ee86de1f1a22c45548bc39916fcd2b72dd26ebca.tar.gz
micropython-ee86de1f1a22c45548bc39916fcd2b72dd26ebca.zip
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).
Diffstat (limited to 'py')
-rw-r--r--py/mpstate.h4
-rw-r--r--py/objexcept.c6
2 files changed, 5 insertions, 5 deletions
diff --git a/py/mpstate.h b/py/mpstate.h
index 0134dd8e88..2b8f29a6ae 100644
--- a/py/mpstate.h
+++ b/py/mpstate.h
@@ -120,8 +120,8 @@ typedef struct _mp_state_vm_t {
// memory for exception arguments if we can't allocate RAM
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
#if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
- // statically allocated buf
- byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE];
+ // statically allocated buf (needs to be aligned to mp_obj_t)
+ mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
#else
// dynamically allocated buf
byte *mp_emergency_exception_buf;
diff --git a/py/objexcept.c b/py/objexcept.c
index 1f1009ff78..dcc7800dca 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -348,7 +348,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
tuple->items[0] = MP_OBJ_FROM_PTR(str);
byte *str_data = (byte *)&str[1];
- uint max_len = MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size
+ size_t max_len = (byte*)MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size
- str_data;
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
o->args = tuple;
- uint offset = &str_data[str->len] - MP_STATE_VM(mp_emergency_exception_buf);
+ size_t offset = &str_data[str->len] - (byte*)MP_STATE_VM(mp_emergency_exception_buf);
offset += sizeof(void *) - 1;
offset &= ~(sizeof(void *) - 1);
if ((mp_emergency_exception_buf_size - offset) > (sizeof(o->traceback_data[0]) * 3)) {
// We have room to store some traceback.
o->traceback_data = (size_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset);
- o->traceback_alloc = (MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]);
+ 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]);
o->traceback_len = 0;
}
}