Skip to content

Commit 9597771

Browse files
committed
py, emitters: Fix dummy_data size for bytecode and thumb.
Thumb uses a bit less RAM, bytecode uses a tiny bit more, to avoid overflow of the dummy buffer in certain cases. Addresses issue adafruit#599.
1 parent 7db57bf commit 9597771

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

py/asmthumb.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct _asm_thumb_t {
4646
uint code_offset;
4747
uint code_size;
4848
byte *code_base;
49-
byte dummy_data[8];
49+
byte dummy_data[4];
5050

5151
uint max_num_labels;
5252
int *label_offsets;
@@ -113,6 +113,7 @@ void asm_thumb_end_pass(asm_thumb_t *as) {
113113
}
114114

115115
// all functions must go through this one to emit bytes
116+
// if as->pass < ASM_THUMB_PASS_EMIT, then this function only returns a buffer of 4 bytes length
116117
STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
117118
//printf("emit %d\n", num_bytes_to_write);
118119
if (as->pass < ASM_THUMB_PASS_EMIT) {
@@ -251,10 +252,13 @@ void asm_thumb_align(asm_thumb_t* as, uint align) {
251252

252253
void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
253254
byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
254-
// little endian
255-
for (uint i = 0; i < bytesize; i++) {
256-
*c++ = val;
257-
val >>= 8;
255+
// only write to the buffer in the emit pass (otherwise we overflow dummy_data)
256+
if (as->pass == ASM_THUMB_PASS_EMIT) {
257+
// little endian
258+
for (uint i = 0; i < bytesize; i++) {
259+
*c++ = val;
260+
val >>= 8;
261+
}
258262
}
259263
}
260264

py/emitbc.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444

4545
#if !MICROPY_EMIT_CPYTHON
4646

47+
#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
48+
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
49+
4750
struct _emit_t {
4851
pass_kind_t pass;
4952
int stack_size;
@@ -62,7 +65,7 @@ struct _emit_t {
6265
uint bytecode_offset;
6366
uint bytecode_size;
6467
byte *code_base; // stores both byte code and code info
65-
byte dummy_data[8];
68+
byte dummy_data[DUMMY_DATA_SIZE];
6669
};
6770

6871
STATIC void emit_bc_rot_two(emit_t *emit);
@@ -152,7 +155,7 @@ STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, uint b2) {
152155

153156
STATIC void emit_write_bytecode_uint(emit_t* emit, uint num) {
154157
// We store each 7 bits in a separate byte, and that's how many bytes needed
155-
byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
158+
byte buf[BYTES_FOR_INT];
156159
byte *p = buf + sizeof(buf);
157160
// We encode in little-ending order, but store in big-endian, to help decoding
158161
do {
@@ -171,7 +174,7 @@ STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, machine_int_t nu
171174
emit_write_bytecode_byte(emit, b1);
172175

173176
// We store each 7 bits in a separate byte, and that's how many bytes needed
174-
byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
177+
byte buf[BYTES_FOR_INT];
175178
byte *p = buf + sizeof(buf);
176179
// We encode in little-ending order, but store in big-endian, to help decoding
177180
do {

0 commit comments

Comments
 (0)