Skip to content

Commit

Permalink
Fix data race in string fixup during inlining.
Browse files Browse the repository at this point in the history
Switches to using the fixed size allocator, so we can free the array
of strings at a safepoint, thus preventing reads of freed memory.
  • Loading branch information
jnthn committed Jan 11, 2017
1 parent 5389d60 commit ef4d6a7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/6model/reprs/MVMCompUnit.c
Expand Up @@ -94,7 +94,10 @@ static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
MVM_fixed_size_free(tc, tc->instance->fsa,
body->num_extops * sizeof(MVMExtOpRecord),
body->extops);
MVM_free(body->strings);
if (body->strings)
MVM_fixed_size_free(tc, tc->instance->fsa,
body->num_strings * sizeof(MVMString *),
body->strings);
MVM_free(body->scs);
MVM_free(body->scs_to_resolve);
MVM_free(body->sc_handle_idxs);
Expand Down
3 changes: 2 additions & 1 deletion src/core/bytecode.c
Expand Up @@ -854,7 +854,8 @@ void MVM_bytecode_unpack(MVMThreadContext *tc, MVMCompUnit *cu) {
rs = dissect_bytecode(tc, cu);

/* Allocate space for the strings heap; we deserialize it lazily. */
cu_body->strings = MVM_calloc(rs->expected_strings, sizeof(MVMString *));
cu_body->strings = MVM_fixed_size_alloc_zeroed(tc, tc->instance->fsa,
rs->expected_strings * sizeof(MVMString *));
cu_body->num_strings = rs->expected_strings;
cu_body->orig_strings = rs->expected_strings;
cu_body->string_heap_fast_table = MVM_calloc(
Expand Down
12 changes: 9 additions & 3 deletions src/core/compunit.c
Expand Up @@ -145,10 +145,16 @@ MVMuint32 MVM_cu_string_add(MVMThreadContext *tc, MVMCompUnit *cu, MVMString *st
}
if (!found) {
/* Not known; let's add it. */
size_t orig_size = cu->body.num_strings * sizeof(MVMString *);
size_t new_size = (cu->body.num_strings + 1) * sizeof(MVMString *);
MVMString **new_strings = MVM_fixed_size_alloc(tc, tc->instance->fsa, new_size);
memcpy(new_strings, cu->body.strings, orig_size);
idx = cu->body.num_strings;
cu->body.strings = MVM_realloc(cu->body.strings,
(idx + 1) * sizeof(MVMString *));
cu->body.strings[idx] = str;
new_strings[idx] = str;
if (cu->body.strings)
MVM_fixed_size_free_at_safepoint(tc, tc->instance->fsa, orig_size,
cu->body.strings);
cu->body.strings = new_strings;
cu->body.num_strings++;
}

Expand Down

0 comments on commit ef4d6a7

Please sign in to comment.