Skip to content

Commit ddcb579

Browse files
committed
Merged PR 147153: Fixing HeapFree AV
Fixing HeapFree AV We have allocated objectBeforeCollectCallbackList object on HeapAllocator but we freed this by ArenaAllocator::Free function. Fixed that by allocate that object onto ArenaAllocator.
2 parents d2a2dd2 + 5da225c commit ddcb579

File tree

3 files changed

+206
-24
lines changed

3 files changed

+206
-24
lines changed

bin/ch/WScriptJsrt.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,21 +299,18 @@ class SerializerDelegateData : public SerializerCallbackBase
299299

300300
virtual void ThrowDataCloneError(void *message)
301301
{
302-
302+
// TBD
303303
}
304304

305305
virtual bool WriteHostObject(JsValueRef data)
306306
{
307+
// Not implemented
307308
return true;
308309
}
309310

310-
virtual uint GetSharedArrayBufferId(JsValueRef shared_array_buffer)
311-
{
312-
return 0;
313-
}
314-
315-
virtual uint GetWasmModuleTransferId(JsValueRef module)
311+
virtual uint GetSharedArrayBufferId(JsValueRef sharedArrayBuffer)
316312
{
313+
// Not implemented
317314
return 0;
318315
}
319316

lib/Common/Memory/Recycler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8962,7 +8962,7 @@ void Recycler::SetObjectBeforeCollectCallback(void* object,
89628962
if (objectBeforeCollectCallbackList == nullptr)
89638963
{
89648964
if (callback == nullptr) return;
8965-
objectBeforeCollectCallbackList = HeapNew(ObjectBeforeCollectCallbackList, &this->objectBeforeCollectCallbackArena);
8965+
objectBeforeCollectCallbackList = Anew(&this->objectBeforeCollectCallbackArena, ObjectBeforeCollectCallbackList, &this->objectBeforeCollectCallbackArena);
89668966
}
89678967

89688968
// only allow 1 callback per object

lib/Jsrt/ChakraCore.h

Lines changed: 201 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,69 +1584,254 @@ CHAKRA_API
15841584
_In_ size_t sizeInBytes,
15851585
_Out_ JsRef * buffer
15861586
);
1587-
1588-
class SerializerCallbackBase
1587+
1588+
/// <summary>
1589+
/// A callback structure to facilitate during variable serialization work. The callback handles many stuff like
1590+
/// allocating buffer, de-allocate that buffer. This memory buffer is used to hold the serialization data.
1591+
/// </summary>
1592+
typedef struct SerializerCallbackBase
15891593
{
15901594
public:
1595+
/// <summary>
1596+
/// A callback function to ask host to re-allocated buffer to the new size when the current buffer is full
1597+
/// </summary>
1598+
/// <param name="oldBuffer">An old memory buffer, which may be null, to be reallocated</param>
1599+
/// <param name="allocatedSize">Request host to allocate buffer of this size</param>
1600+
/// <param name="arrayBuffer">Actual size of the new buffer</param>
1601+
/// <returns>
1602+
/// New buffer will be returned upon success, null otherwise.
1603+
/// </returns>
15911604
virtual byte *ReallocateBufferMemory(byte *oldBuffer, size_t newSize, size_t *allocatedSize) = 0;
1605+
1606+
/// <summary>
1607+
/// A callback to ask host to free the buffer which was allocated using ReallocateBufferMemory
1608+
/// </summary>
1609+
/// <param name="buffer">Buffer to be freed</param>
15921610
virtual void FreeBufferMemory(byte *buffer) = 0;
1611+
1612+
/// <summary>
1613+
/// A callback to ask host to throw an exception upon Structured Cloning Algorithm error.
1614+
/// </summary>
1615+
/// <param name="message">Error message to be populated with</param>
15931616
virtual void ThrowDataCloneError(void *message) = 0;
1594-
virtual bool WriteHostObject(void* data) = 0;
1595-
virtual unsigned int GetSharedArrayBufferId(void * shared_array_buffer) = 0;
1596-
virtual unsigned int GetWasmModuleTransferId(void * wasmModule) = 0;
1597-
};
15981617

1599-
class SerializerHandleBase
1618+
/// <summary>
1619+
/// A callback to ask host write current Host object to the serialization buffer.
1620+
/// </summary>
1621+
/// <param name="hostObject">Host object to be serialized</param>
1622+
/// <returns>
1623+
/// A Boolean true is returned upon success, false otherwise.
1624+
/// </returns>
1625+
virtual bool WriteHostObject(void* hostObject) = 0;
1626+
1627+
/// <summary>
1628+
/// A callback to ask host to record current SharedArrayBuffer and pass an unique ID
1629+
/// </summary>
1630+
/// <param name="sharedArrayBuffer">SharedArrayBuffer object</param>
1631+
/// <returns>
1632+
/// An unique ID representing the SharedArrayBuffer is returned. Upon failures the exception is thrown
1633+
/// </returns>
1634+
virtual unsigned int GetSharedArrayBufferId(void * sharedArrayBuffer) = 0;
1635+
1636+
} SerializerCallbackBase;
1637+
1638+
/// <summary>
1639+
/// The object of SerializerHandleBase will be passed to the caller when JsVarSerializer is called.
1640+
/// This object will provide functionality to write data to serialization buffer.
1641+
/// </summary>
1642+
typedef struct SerializerHandleBase
16001643
{
16011644
public:
1645+
/// <summary>
1646+
/// Write raw bytes to the buffer.
1647+
/// </summary>
1648+
/// <param name="source">Source byte buffer</param>
1649+
/// <param name="length">Length of bytes to write from source raw byte buffer</param>
16021650
virtual void WriteRawBytes(const void* source, size_t length) = 0;
1603-
virtual bool WriteValue(JsValueRef root) = 0;
1651+
1652+
/// <summary>
1653+
/// A method to serialize given Javascript object to the serialization buffer
1654+
/// </summary>
1655+
/// <param name="rootObject">A Javascript object to be serialized</param>
1656+
/// <returns>
1657+
/// A Boolean value true is returned upon success, false otherwise.
1658+
/// </returns>
1659+
virtual bool WriteValue(JsValueRef rootObject) = 0;
1660+
1661+
/// <summary>
1662+
/// A method to pass on the current serialized buffer (this buffer was allocated using ReallocateBufferMemory) to host.
1663+
/// </summary>
1664+
/// <param name="data">A buffer which holds current serialized data</param>
1665+
/// <param name="dataLength">Length of the buffer</param>
1666+
/// <returns>
1667+
/// A Boolean value true is returned upon success, false otherwise.
1668+
/// </returns>
16041669
virtual bool ReleaseData(byte** data, size_t *dataLength) = 0;
1670+
1671+
/// <summary>
1672+
/// Detach all array buffers which were passed using SetTransferableVars.
1673+
/// </summary>
1674+
/// <returns>
1675+
/// A Boolean value true is returned upon success, false otherwise.
1676+
/// </returns>
16051677
virtual bool DetachArrayBuffer() = 0;
1678+
1679+
/// <summary>
1680+
/// Host provides all the objects which has transferable semantics (Such as ArrayBuffers).
1681+
/// </summary>
1682+
/// <param name="transferableVars">An array of transferable objects</param>
1683+
/// <param name="transferableVarsCount">Length of transferableVars array </param>
1684+
/// <returns>
1685+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
1686+
/// </returns>
16061687
virtual JsErrorCode SetTransferableVars(JsValueRef *transferableVars, size_t transferableVarsCount) = 0;
1688+
1689+
/// <summary>
1690+
/// Free current object (which was created upon JsVarSerializer) when the serialization is done. SerializerHandleBase object should not be used further after FreeSelf call.
1691+
/// </summary>
16071692
virtual void FreeSelf() = 0;
1608-
};
1693+
} SerializerHandleBase;
16091694

1610-
class DeserializerCallbackBase
1695+
/// <summary>
1696+
/// A callback structure to facilitate de-serialization work.
1697+
/// </summary>
1698+
typedef struct DeserializerCallbackBase
16111699
{
16121700
public:
1701+
1702+
/// <summary>
1703+
/// A callback to ask host to read the current data from the serialization buffer as a Host object.
1704+
/// </summary>
1705+
/// <returns>
1706+
/// A valid host object is returned upon success, an exception is thrown otherwise.
1707+
/// </returns>
16131708
virtual JsValueRef ReadHostObject() = 0;
1709+
1710+
/// <summary>
1711+
/// A callback to ask host to retrieve SharedArrayBuffer object from given ID.
1712+
/// </summary>
1713+
/// <param name="id">An ID, which was provided by SerializerCallbackBase::GetSharedArrayBufferId method</param>
1714+
/// <returns>
1715+
/// A valid SharedArrayBuffer is returned upon success, an exception is thrown otherwise.
1716+
/// </returns>
16141717
virtual JsValueRef GetSharedArrayBufferFromId(uint32_t id) = 0;
1615-
virtual JsValueRef GetWasmModuleFromId(uint32_t transfer_id) = 0;
1616-
};
16171718

1618-
class DeserializerHandleBase
1719+
} DeserializerCallbackBase;
1720+
1721+
/// <summary>
1722+
/// This object will be passed from Engine to Host when JsVarDeserializer is called.
1723+
/// This object handles functionalities related reading current buffer and create Javascript objects
1724+
/// </summary>
1725+
typedef struct DeserializerHandleBase
16191726
{
16201727
public:
1728+
/// <summary>
1729+
/// A method to read bytes from the serialized buffer. Caller should not allocate the data buffer.
1730+
/// </summary>
1731+
/// <param name="length">Advance current buffer's position by length</param>
1732+
/// <param name="data">The data will be pointing to the raw serialized buffer</param>
1733+
/// <returns>
1734+
/// A Boolean value true is returned upon success, false otherwise.
1735+
/// </returns>
16211736
virtual bool ReadRawBytes(size_t length, void **data) = 0;
1737+
1738+
/// <summary>
1739+
/// A method to read bytes from the serialized buffer. Caller must allocate data buffer by length.
1740+
/// </summary>
1741+
/// <param name="length">Length of data buffer</param>
1742+
/// <param name="data">data buffer to be populated from the serialized buffer till the given length</param>
1743+
/// <returns>
1744+
/// A Boolean value true is returned upon success, false otherwise.
1745+
/// </returns>
16221746
virtual bool ReadBytes(size_t length, void **data) = 0;
1747+
1748+
/// <summary>
1749+
/// Deserialized current buffer and pass the root object.
1750+
/// </summary>
1751+
/// <returns>
1752+
/// A valid Javascript object is returned upon success, an exception is thrown otherwise.
1753+
/// </returns>
16231754
virtual JsValueRef ReadValue() = 0;
1755+
1756+
/// <summary>
1757+
/// Host provides all the objects which has transferable semantics (Such as ArrayBuffers).
1758+
/// </summary>
1759+
/// <param name="transferableVars">An array of transferable objects</param>
1760+
/// <param name="transferableVarsCount">Length of transferableVars array </param>
1761+
/// <returns>
1762+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
1763+
/// </returns>
16241764
virtual JsErrorCode SetTransferableVars(JsValueRef *transferableVars, size_t transferableVarsCount) = 0;
1765+
1766+
/// <summary>
1767+
/// Free current object (which was created upon JsVarDeserializer) when the deserialization is done. DeserializerHandleBase object should not be used further after FreeSelf call.
1768+
/// </summary>
16251769
virtual void FreeSelf() = 0;
1626-
};
1770+
} DeserializerHandleBase;
16271771

1772+
/// <summary>
1773+
/// Initialize Serialization of the object.
1774+
/// </summary>
1775+
/// <param name="serializerCallback">A callback object to interact with host during serialization.</param>
1776+
/// <param name="serializerHandle">A handle which provides various functionalities to serailize objects</param>
1777+
/// <returns>
1778+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
1779+
/// </returns>
16281780
CHAKRA_API
16291781
JsVarSerializer(
1630-
_In_ SerializerCallbackBase *delegate,
1782+
_In_ SerializerCallbackBase *serializerCallback,
16311783
_Out_ SerializerHandleBase **serializerHandle);
16321784

1785+
/// <summary>
1786+
/// Initiate Deserialization of the memory buffer to a Javascript object.
1787+
/// </summary>
1788+
/// <param name="data">A memory buffer which holds the serialized data</param>
1789+
/// <param name="size">Length of the passed data in bytes</param>
1790+
/// <param name="deserializerCallback">A callback object to interact with host during deserialization</param>
1791+
/// <param name="deserializerHandle">A handle which provides various functionalities to deserailize a buffer to an object</param>
1792+
/// <returns>
1793+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
1794+
/// </returns>
16331795
CHAKRA_API
16341796
JsVarDeserializer(
16351797
_In_ void *data,
16361798
_In_ size_t size,
1637-
_In_ DeserializerCallbackBase *delegate,
1799+
_In_ DeserializerCallbackBase *deserializerCallback,
16381800
_Out_ DeserializerHandleBase **deserializerHandle);
16391801

1802+
/// <summary>
1803+
/// Extract extra info stored from an ArrayBuffer object
1804+
/// </summary>
1805+
/// <param name="arrayBuffer">An ArrayBuffer from which the extrainfor needed to extracted</param>
1806+
/// <param name="extraInfo">The host information (some flags such as object externalized, detached) stored in the object</param>
1807+
/// <returns>
1808+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
1809+
/// </returns>
16401810
CHAKRA_API
16411811
JsGetArrayBufferExtraInfo(
16421812
_In_ JsValueRef arrayBuffer,
16431813
_Out_ char *extraInfo);
16441814

1815+
/// <summary>
1816+
/// Set Extra info (host data) to an ArrayBuffer object.
1817+
/// </summary>
1818+
/// <param name="arrayBuffer">An ArrayBuffer on which the host information will be stored</param>
1819+
/// <param name="extraInfo">The host data</param>
1820+
/// <returns>
1821+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
1822+
/// </returns>
16451823
CHAKRA_API
16461824
JsSetArrayBufferExtraInfo(
16471825
_In_ JsValueRef arrayBuffer,
16481826
_In_ char extraInfo);
16491827

1828+
/// <summary>
1829+
/// Neuter current ArrayBuffer
1830+
/// </summary>
1831+
/// <param name="arrayBuffer">An ArrayBuffer which will be neutered </param>
1832+
/// <returns>
1833+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
1834+
/// </returns>
16501835
CHAKRA_API
16511836
JsDetachArrayBuffer(
16521837
_In_ JsValueRef arrayBuffer);

0 commit comments

Comments
 (0)