Skip to content

Commit 5f22a9a

Browse files
committed
[1.7>master] [MERGE #3768 @MSLaguana] Adding error checking to JsCreateWeakReference
Merge pull request #3768 from MSLaguana:jsrtWeakRefObjectsOnly If a tagged value or a non-RecyclableObject is passed in, we will now report an error rather than crashing non-deterministically in a future GC.
2 parents c0c146f + a42c41d commit 5f22a9a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/Jsrt/ChakraCommon.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ typedef unsigned short uint16_t;
223223
/// </summary>
224224
JsErrorModuleParsed,
225225
/// <summary>
226+
/// Argument passed to JsCreateWeakReference is a primitive that is not managed by the GC.
227+
/// No weak reference is required, the value will never be collected.
228+
/// </summary>
229+
JsNoWeakRefRequired,
230+
/// <summary>
226231
/// Category of errors that relates to errors occurring within the engine itself.
227232
/// </summary>
228233
JsErrorCategoryEngine = 0x20000,

lib/Jsrt/Jsrt.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4618,6 +4618,11 @@ CHAKRA_API JsCreateWeakReference(
46184618
PARAM_NOT_NULL(weakRef);
46194619
*weakRef = nullptr;
46204620

4621+
if (Js::TaggedNumber::Is(value))
4622+
{
4623+
return JsNoWeakRefRequired;
4624+
}
4625+
46214626
return GlobalAPIWrapper_NoRecord([&]() -> JsErrorCode {
46224627
ThreadContext* threadContext = ThreadContext::GetContextForCurrentThread();
46234628
if (threadContext == nullptr)
@@ -4631,6 +4636,13 @@ CHAKRA_API JsCreateWeakReference(
46314636
return JsErrorInObjectBeforeCollectCallback;
46324637
}
46334638

4639+
RecyclerHeapObjectInfo dummyObjectInfo;
4640+
if (!recycler->FindHeapObject(value, Memory::FindHeapObjectFlags::FindHeapObjectFlags_NoFlags, dummyObjectInfo))
4641+
{
4642+
// value is not recyler-allocated
4643+
return JsErrorInvalidArgument;
4644+
}
4645+
46344646
recycler->FindOrCreateWeakReferenceHandle<char>(
46354647
reinterpret_cast<char*>(value),
46364648
reinterpret_cast<Memory::RecyclerWeakReference<char>**>(weakRef));

0 commit comments

Comments
 (0)