diff --git a/ChangeLog b/ChangeLog index ea59d291a..08e05bb46 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2014-07-28 Paulo Andrade + + * lib/jit_memory.c: Do not call free on NULL pointers. + + * include/lightning/jit_private.h, lib/jit_note.c, + lib/lightning.c: Add a wrapper to memcpy and memmove + to not actually call those functions with a zero size + argument, and likely also a null src or dst. + 2014-07-27 Paulo Andrade * include/lightning/jit_private.h, lib/jit_disasm.c, diff --git a/include/lightning/jit_private.h b/include/lightning/jit_private.h index 8de2c8b0e..1915ce256 100644 --- a/include/lightning/jit_private.h +++ b/include/lightning/jit_private.h @@ -592,6 +592,8 @@ extern void _jit_set_note(jit_state_t*, jit_note_t*, char*, int, jit_int32_t); #define jit_annotate() _jit_annotate(_jit) extern void _jit_annotate(jit_state_t*); +extern jit_pointer_t jit_memcpy(jit_pointer_t,jit_pointer_t,jit_word_t); +extern jit_pointer_t jit_memmove(jit_pointer_t,jit_pointer_t,jit_word_t); extern void jit_alloc(jit_pointer_t*, jit_word_t); extern void jit_realloc(jit_pointer_t*, jit_word_t, jit_word_t); void jit_free(jit_pointer_t*); diff --git a/lib/jit_arm.c b/lib/jit_arm.c index e71fedabc..62d8e8af7 100644 --- a/lib/jit_arm.c +++ b/lib/jit_arm.c @@ -1750,7 +1750,7 @@ _flush_consts(jit_state_t *_jit) _jitc->consts.size = _jitc->consts.length << 2; /* FIXME check will not overrun, otherwise, need to reallocate * code buffer and start over */ - memcpy(_jitc->consts.data, _jitc->consts.values, _jitc->consts.size); + jit_memcpy(_jitc->consts.data, _jitc->consts.values, _jitc->consts.size); _jit->pc.w += _jitc->consts.size; #if DISASSEMBLER diff --git a/lib/jit_memory.c b/lib/jit_memory.c index 39cc549f1..b4193e478 100644 --- a/lib/jit_memory.c +++ b/lib/jit_memory.c @@ -38,6 +38,22 @@ static jit_free_func_ptr jit_free_ptr = jit_default_free_func; /* * Implementation */ +jit_pointer_t +jit_memcpy(jit_pointer_t dst, jit_pointer_t src, jit_word_t size) +{ + if (size) + return (memcpy(dst, src, size)); + return (dst); +} + +jit_pointer_t +jit_memmove(jit_pointer_t dst, jit_pointer_t src , jit_word_t size) +{ + if (size) + return (memmove(dst, src, size)); + return (dst); +} + void jit_set_memory_functions(jit_alloc_func_ptr alloc_ptr, jit_realloc_func_ptr realloc_ptr, @@ -82,8 +98,10 @@ jit_realloc(jit_pointer_t *ptr, jit_word_t old_size, jit_word_t new_size) void jit_free(jit_pointer_t *ptr) { - (*jit_free_ptr)(*ptr); - *ptr = NULL; + if (*ptr) { + (*jit_free_ptr)(*ptr); + *ptr = NULL; + } } static void * diff --git a/lib/jit_note.c b/lib/jit_note.c index 412eba3de..f3d35fa49 100644 --- a/lib/jit_note.c +++ b/lib/jit_note.c @@ -126,16 +126,17 @@ _jit_annotate(jit_state_t *_jit) note->size = _jit->pc.uc - note->code; /* annotations may be very complex with conditions to extend - * or ignore redudant notes, as well as add entries to earlier + * or ignore redundant notes, as well as add entries to earlier * notes, so, relocate the information to the data buffer, * with likely over allocated reserved space */ /* relocate jit_line_t objects */ for (note_offset = 0; note_offset < _jit->note.length; note_offset++) { note = _jit->note.ptr + note_offset; - length = sizeof(jit_line_t) * note->length; + if ((length = sizeof(jit_line_t) * note->length) == 0) + continue; assert(_jitc->note.base + length < _jit->data.ptr + _jit->data.length); - memcpy(_jitc->note.base, note->lines, length); + jit_memcpy(_jitc->note.base, note->lines, length); jit_free((jit_pointer_t *)¬e->lines); note->lines = (jit_line_t *)_jitc->note.base; _jitc->note.base += length; @@ -149,13 +150,13 @@ _jit_annotate(jit_state_t *_jit) length = sizeof(jit_int32_t) * line->length; assert(_jitc->note.base + length < _jit->data.ptr + _jit->data.length); - memcpy(_jitc->note.base, line->linenos, length); + jit_memcpy(_jitc->note.base, line->linenos, length); jit_free((jit_pointer_t *)&line->linenos); line->linenos = (jit_int32_t *)_jitc->note.base; _jitc->note.base += length; assert(_jitc->note.base + length < _jit->data.ptr + _jit->data.length); - memcpy(_jitc->note.base, line->offsets, length); + jit_memcpy(_jitc->note.base, line->offsets, length); jit_free((jit_pointer_t *)&line->offsets); line->offsets = (jit_int32_t *)_jitc->note.base; _jitc->note.base += length; @@ -200,10 +201,10 @@ _jit_set_note(jit_state_t *_jit, jit_note_t *note, (line->length + 17) * sizeof(jit_int32_t)); } if (index < note->length) { - memmove(line->linenos + index + 1, line->linenos + index, - sizeof(jit_int32_t) * (line->length - index)); - memmove(line->offsets + index + 1, line->offsets + index, - sizeof(jit_int32_t) * (line->length - index)); + jit_memmove(line->linenos + index + 1, line->linenos + index, + sizeof(jit_int32_t) * (line->length - index)); + jit_memmove(line->offsets + index + 1, line->offsets + index, + sizeof(jit_int32_t) * (line->length - index)); } line->linenos[index] = lineno; line->offsets[index] = offset; @@ -280,8 +281,8 @@ new_line(jit_int32_t index, jit_note_t *note, (note->length + 17) * sizeof(jit_line_t)); if (index < note->length) - memmove(note->lines + index + 1, note->lines + index, - sizeof(jit_line_t) * (note->length - index)); + jit_memmove(note->lines + index + 1, note->lines + index, + sizeof(jit_line_t) * (note->length - index)); line = note->lines + index; ++note->length; diff --git a/lib/lightning.c b/lib/lightning.c index 9a6d16f75..ba18396b2 100644 --- a/lib/lightning.c +++ b/lib/lightning.c @@ -610,7 +610,7 @@ _jit_data(jit_state_t *_jit, jit_pointer_t data, } node->u.w = _jitc->data.offset; node->v.w = length; - memcpy(_jitc->data.ptr + _jitc->data.offset, data, length); + jit_memcpy(_jitc->data.ptr + _jitc->data.offset, data, length); _jitc->data.offset += length; node->next = _jitc->data.table[key]; @@ -1722,9 +1722,12 @@ _jit_dataset(jit_state_t *_jit) } if (!_jitc->no_data) - memcpy(_jit->data.ptr, _jitc->data.ptr, _jitc->data.offset); + jit_memcpy(_jit->data.ptr, _jitc->data.ptr, _jitc->data.offset); if (_jitc->no_note) { + /* Space for one note is always allocated, so revert it here + * if after jit_new_state was called, it is also requested to + * not generate annotation information */ _jit->note.length = 0; _jitc->note.size = 0; } @@ -2581,7 +2584,7 @@ _simplify_movr(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node, return (1); } if (_jitc->values[right].kind == jit_kind_word) - memcpy(value, _jitc->values + right, sizeof(jit_value_t)); + jit_memcpy(value, _jitc->values + right, sizeof(jit_value_t)); else { value->kind = jit_kind_register; value->base.q.l = right; @@ -2630,14 +2633,14 @@ _simplify_movi(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node, else node->code = jit_code_movr_d; node->v.w = offset; - memcpy(value, _jitc->values + offset, sizeof(jit_value_t)); + jit_memcpy(value, _jitc->values + offset, sizeof(jit_value_t)); ++_jitc->gen[regno]; return (0); } } } value->kind = kind; - memcpy(&value->base.w, &node->v.w, size); + jit_memcpy(&value->base.w, &node->v.w, size); ++_jitc->gen[regno]; return (0);