Skip to content

Commit 3ec91a4

Browse files
committed
fix: Fix makeGray misuse in GC (#2178)
1 parent 618460c commit 3ec91a4

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

std/assembly/rt/itcms.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ function initLazy(space: Object): Object {
148148
this.unlink();
149149
this.linkTo(toSpace, this.isPointerfree ? i32(!white) : gray);
150150
}
151+
152+
/** Marks this object need to be scanned */
153+
needScan(): void {
154+
if (state == STATE_MARK) {
155+
this.makeGray();
156+
} else {
157+
this.unlink();
158+
this.linkTo(fromSpace, white);
159+
}
160+
}
151161
}
152162

153163
/** Visits all objects considered to be program roots. */
@@ -301,10 +311,10 @@ export function __link(parentPtr: usize, childPtr: usize, expectMultiple: bool):
301311
if (expectMultiple) {
302312
// Move the barrier "backward". Suitable for containers receiving multiple stores.
303313
// Avoids a barrier for subsequent objects stored into the same container.
304-
parent.makeGray();
314+
parent.needScan();
305315
} else {
306316
// Move the barrier "forward". Suitable for objects receiving isolated stores.
307-
child.makeGray();
317+
child.needScan();
308318
}
309319
} else if (parentColor == transparent && state == STATE_MARK) {
310320
// Pinned objects are considered 'black' during the mark phase.
@@ -350,15 +360,7 @@ export function __unpin(ptr: usize): void {
350360
if (obj.color != transparent) {
351361
throw new Error(E_NOT_PINNED);
352362
}
353-
if (state == STATE_MARK) {
354-
// We may be right at the point after marking roots for the second time and
355-
// entering the sweep phase, in which case the object would be missed if it
356-
// is not only pinned but also a root. Make sure it isn't missed.
357-
obj.makeGray();
358-
} else {
359-
obj.unlink();
360-
obj.linkTo(fromSpace, white);
361-
}
363+
obj.needScan();
362364
}
363365

364366
// @ts-ignore: decorator

0 commit comments

Comments
 (0)