Skip to content

Commit 5699702

Browse files
Clear ghost-refs from old stack area
1 parent b8e02b3 commit 5699702

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

bin/NativeTests/JsRTApiTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3-
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
44
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
55
//-------------------------------------------------------------------------------------------------------
66
#include "stdafx.h"
@@ -128,6 +128,7 @@ namespace JsRTApiTest
128128
valueRef = JS_INVALID_REFERENCE;
129129
valueRefFromWeakRef = JS_INVALID_REFERENCE;
130130

131+
CHECK(JsGarbageCollectionClearStack() == JsNoError);
131132
CHECK(JsCollectGarbage(runtime) == JsNoError);
132133

133134
// JsGetWeakReferenceValue should return an invalid reference after the value was GC'd.

lib/Jsrt/ChakraCore.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
34
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45
//-------------------------------------------------------------------------------------------------------
56
/// \mainpage Chakra Hosting API Reference
@@ -2017,6 +2018,16 @@ JsGetEmbedderData(_In_ JsValueRef instance, _Out_ JsValueRef* embedderData);
20172018
CHAKRA_API
20182019
JsSetEmbedderData(_In_ JsValueRef instance, _In_ JsValueRef embedderData);
20192020

2021+
/// <summary>
2022+
/// Clear some of the inaccessible part of the stack.
2023+
/// </summary>
2024+
/// <returns>
2025+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code
2026+
/// otherwise.
2027+
/// </returns>
2028+
CHAKRA_API
2029+
JsGarbageCollectionClearStack();
2030+
20202031
#ifdef _WIN32
20212032
#include "ChakraCoreWindows.h"
20222033
#endif // _WIN32

lib/Jsrt/Jsrt.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
34
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45
//-------------------------------------------------------------------------------------------------------
56
#include "JsrtPch.h"
@@ -474,6 +475,16 @@ CHAKRA_API JsCollectGarbage(_In_ JsRuntimeHandle runtimeHandle)
474475
return JsCollectGarbageCommon<CollectNowExhaustive>(runtimeHandle);
475476
}
476477

478+
CHAKRA_API JsGarbageCollectionClearStack()
479+
{
480+
// https://github.com/bdwgc/bdwgc/blob/e1042aa86d9403f433a2ab38ee2aab081984fca8/misc.c#L260-L285
481+
482+
const int SMALL_CLEAR_SIZE = 256;
483+
volatile void *dummy[SMALL_CLEAR_SIZE];
484+
memset((void *)dummy, 0, sizeof(dummy));
485+
return JsNoError;
486+
}
487+
477488
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
478489
CHAKRA_API JsPrivateCollectGarbageSkipStack(_In_ JsRuntimeHandle runtimeHandle)
479490
{

lib/Jsrt/JsrtCommonExports.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
JsSetObjectBeforeCollectCallback
22
JsCreateRuntime
33
JsCollectGarbage
4+
JsGarbageCollectionClearStack
45
JsDisposeRuntime
56
JsAddRef
67
JsRelease

0 commit comments

Comments
 (0)