Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for #9853 #10217

Merged
merged 5 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -69,6 +69,9 @@ Working version
to the debugger via a socket.
(Antonin Décimo, review by Xavier Leroy)

- #xxxxx: Fix a segfault in a corner case of compaction (reported in #9853)
(Damien Doligez, report by Sadiq Jaffer, review by xxxx)

### Code generation and optimizations:

- #9876: do not cache the young_limit GC variable in a processor register.
Expand Down
15 changes: 7 additions & 8 deletions runtime/compact.c
Expand Up @@ -39,7 +39,8 @@ extern void caml_shrink_heap (char *); /* memory.c */

We use the GC's color bits in the following way:

- White words are headers of live blocks.
- White words are headers of live blocks except for 0, which is a
fragment.
- Blue words are headers of free blocks.
- Black words are headers of out-of-heap "blocks".
- Gray words are the encoding of pointers in inverted lists.
Expand Down Expand Up @@ -122,11 +123,9 @@ static char *compact_allocate (mlsize_t size)
{
char *chunk, *adr;

while (Chunk_size (compact_fl) - Chunk_alloc (compact_fl) <= Bhsize_wosize (3)
&& Chunk_size (Chunk_next (compact_fl))
- Chunk_alloc (Chunk_next (compact_fl))
<= Bhsize_wosize (3)){
while (Chunk_size(compact_fl) - Chunk_alloc(compact_fl) < Bhsize_wosize(1)){
compact_fl = Chunk_next (compact_fl);
CAMLassert (compact_fl != NULL);
}
chunk = compact_fl;
while (Chunk_size (chunk) - Chunk_alloc (chunk) < size){
Expand Down Expand Up @@ -242,7 +241,7 @@ static void do_compaction (intnat new_allocation_policy)

CAMLassert (!Is_black_hd (h));
CAMLassert (!Is_gray_hd (h));
if (Is_white_hd (h)){
if (h != 0 && Is_white_hd (h)){
word q;
tag_t t;
char *newadr;
Expand Down Expand Up @@ -304,13 +303,13 @@ static void do_compaction (intnat new_allocation_policy)
chend = ch + Chunk_size (ch);
while ((char *) p < chend){
word q = *p;
if (Color_hd (q) == Caml_white){
if (q != 0 && Is_white_hd (q)){
size_t sz = Bhsize_hd (q);
char *newadr = compact_allocate (sz);
memmove (newadr, p, sz);
p += Wsize_bsize (sz);
}else{
CAMLassert (Color_hd (q) == Caml_blue);
CAMLassert (q == 0 || Is_blue_hd (q));
p += Whsize_hd (q);
}
}
Expand Down
10 changes: 10 additions & 0 deletions testsuite/tests/regression/pr9853/compaction_corner_case.ml
@@ -0,0 +1,10 @@
(* TEST *)

(* Compaction crash when there is only one heap chunk and it is fully used. *)
let c = ref []

let () =
for i = 0 to 25000 do
c := 0 :: !c;
Gc.compact ()
done