Skip to content

Commit b8bede4

Browse files
Prevent compiler optimization
1 parent 5699702 commit b8bede4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/Jsrt/Jsrt.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,14 @@ CHAKRA_API JsGarbageCollectionClearStack()
479479
{
480480
// https://github.com/bdwgc/bdwgc/blob/e1042aa86d9403f433a2ab38ee2aab081984fca8/misc.c#L260-L285
481481

482-
const int SMALL_CLEAR_SIZE = 256;
482+
constexpr int SMALL_CLEAR_SIZE = 256;
483483
volatile void *dummy[SMALL_CLEAR_SIZE];
484-
memset((void *)dummy, 0, sizeof(dummy));
484+
485+
// https://news.ycombinator.com/item?id=4711605
486+
// Zero memory + prevent compiler optimizations
487+
volatile unsigned char *bp = (unsigned char *)dummy;
488+
while (bp < (unsigned char *)dummy + sizeof(dummy))
489+
*bp++ = 0;
485490
return JsNoError;
486491
}
487492

0 commit comments

Comments
 (0)