Issue #472 - Avoid races with tracing GC when allocating large vectors

This commit is contained in:
Justin Ethier 2021-07-28 22:26:33 -04:00
parent 543ce4f4be
commit 14d4c27eac
2 changed files with 13 additions and 4 deletions

View file

@ -6,6 +6,10 @@ Features
- Initiate major garbage collections faster after allocating a huge object (more than 500K). This allows the system to reclaim the memory faster and keep overall memory usage low for certain workloads.
Bug Fixes
- When allocating a large vector we now guarantee all vector elements are initialized before the major collector can trace those elements. This avoids the potential for a race condition which could lead to a segmentation fault.
## 0.31.0 - July 27, 2021
### Bug Fixes

View file

@ -2972,8 +2972,8 @@ object Cyc_make_vector(void *data, object cont, int argc, object len, ...)
((vector) v)->hdr.mark = ((gc_thread_data *)data)->gc_alloc_color;
((vector) v)->hdr.grayed = 0;
((vector) v)->hdr.immutable = 0;
((vector) v)->tag = vector_tag;
((vector) v)->num_elements = ulen;
((vector) v)->tag = double_tag; // Avoid race conditions w/GC tracing
((vector) v)->num_elements = 0; // until array is filled
((vector) v)->elements = (object *)(((char *)v) + sizeof(vector_type));
// Use write barrier to ensure fill is moved to heap if it is on the stack
// Otherwise if next minor GC misses fill it could be catastrophic
@ -3000,6 +3000,9 @@ object Cyc_make_vector(void *data, object cont, int argc, object len, ...)
for (i = 0; i < ((vector) v)->num_elements; i++) {
((vector) v)->elements[i] = fill;
}
((vector) v)->tag = vector_tag;
((vector) v)->num_elements = ulen;
_return_closcall1(data, cont, v);
}
@ -3411,8 +3414,8 @@ object Cyc_list2vector(void *data, object cont, object l)
((vector) v)->hdr.mark = ((gc_thread_data *)data)->gc_alloc_color;
((vector) v)->hdr.grayed = 0;
((vector) v)->hdr.immutable = 0;
((vector) v)->tag = vector_tag;
((vector) v)->num_elements = len;
((vector) v)->tag = double_tag; // Avoid race with GC tracing until
((vector) v)->num_elements = 0; // array is initialized
((vector) v)->elements = (object *)(((char *)v) + sizeof(vector_type));
// TODO: do we need to worry about stack object in the list????
//// Use write barrier to ensure fill is moved to heap if it is on the stack
@ -3437,6 +3440,8 @@ object Cyc_list2vector(void *data, object cont, object l)
((vector) v)->elements[i++] = car(lst);
lst = cdr(lst);
}
((vector) v)->tag = vector_tag;
((vector) v)->num_elements = len;
_return_closcall1(data, cont, v);
}