@@ -64,12 +64,12 @@ COOP_PINVOKE_HELPER(Object *, RhpNewFast, (EEType* pEEType))
6464
6565 size_t size = pEEType->get_BaseSize ();
6666
67- UInt8* result = acontext->alloc_ptr ;
68- UInt8* advance = result + size ;
69- if (advance <= acontext->alloc_limit )
67+ UInt8* alloc_ptr = acontext->alloc_ptr ;
68+ ASSERT (alloc_ptr <= acontext-> alloc_limit ) ;
69+ if (( size_t )( acontext->alloc_limit - alloc_ptr) >= size )
7070 {
71- acontext->alloc_ptr = advance ;
72- pObject = (Object *)result ;
71+ acontext->alloc_ptr = alloc_ptr + size ;
72+ pObject = (Object *)alloc_ptr ;
7373 pObject->set_EEType (pEEType);
7474 return pObject;
7575 }
@@ -147,12 +147,12 @@ COOP_PINVOKE_HELPER(Array *, RhpNewArray, (EEType * pArrayEEType, int numElement
147147 size = ALIGN_UP (size, sizeof (UIntNative));
148148 }
149149
150- UInt8* result = acontext->alloc_ptr ;
151- UInt8* advance = result + size ;
152- if (advance <= acontext->alloc_limit )
150+ UInt8* alloc_ptr = acontext->alloc_ptr ;
151+ ASSERT (alloc_ptr <= acontext-> alloc_limit ) ;
152+ if (( size_t )( acontext->alloc_limit - alloc_ptr) >= size )
153153 {
154- acontext->alloc_ptr = advance ;
155- pObject = (Array *)result ;
154+ acontext->alloc_ptr = alloc_ptr + size ;
155+ pObject = (Array *)alloc_ptr ;
156156 pObject->set_EEType (pArrayEEType);
157157 pObject->InitArrayLength ((UInt32)numElements);
158158 return pObject;
@@ -192,6 +192,7 @@ COOP_PINVOKE_HELPER(Object *, RhpNewFinalizableAlign8, (EEType* pEEType))
192192 return pObject;
193193}
194194
195+ #ifndef HOST_64BIT
195196COOP_PINVOKE_HELPER (Object *, RhpNewFastAlign8, (EEType* pEEType))
196197{
197198 ASSERT (pEEType->RequiresAlign8 ());
@@ -207,20 +208,29 @@ COOP_PINVOKE_HELPER(Object *, RhpNewFastAlign8, (EEType* pEEType))
207208 UInt8* result = acontext->alloc_ptr ;
208209
209210 int requiresPadding = ((uint32_t )result) & 7 ;
210- if (requiresPadding) size += 12 ;
211- UInt8* advance = result + size;
212- if (advance <= acontext->alloc_limit )
211+ size_t paddedSize = size;
212+ if (requiresPadding)
213213 {
214- acontext->alloc_ptr = advance;
214+ if (paddedSize > SIZE_MAX - 12 )
215+ {
216+ ASSERT_UNCONDITIONALLY (" NYI" ); // TODO: Throw overflow
217+ }
218+ paddedSize += 12 ;
219+ }
220+
221+ UInt8* alloc_ptr = acontext->alloc_ptr ;
222+ ASSERT (alloc_ptr <= acontext->alloc_limit );
223+ if ((size_t )(acontext->alloc_limit - alloc_ptr) >= paddedSize)
224+ {
225+ acontext->alloc_ptr = alloc_ptr + paddedSize;
215226 if (requiresPadding)
216227 {
217- Object* dummy = (Object*)result ;
228+ Object* dummy = (Object*)alloc_ptr ;
218229 dummy->set_EEType (g_pFreeObjectEEType);
219- result += 12 ;
230+ alloc_ptr += 12 ; // if result + paddedSize was ok, then cant overflow
220231 }
221- pObject = (Object*)result ;
232+ pObject = (Object *)alloc_ptr ;
222233 pObject->set_EEType (pEEType);
223-
224234 return pObject;
225235 }
226236
@@ -247,20 +257,28 @@ COOP_PINVOKE_HELPER(Object*, RhpNewFastMisalign, (EEType* pEEType))
247257 UInt8* result = acontext->alloc_ptr ;
248258
249259 int requiresPadding = (((uint32_t )result) & 7 ) != 4 ;
250- if (requiresPadding) size += 12 ;
251- UInt8* advance = result + size;
252- if (advance <= acontext->alloc_limit )
260+ size_t paddedSize = size;
261+ if (requiresPadding)
253262 {
254- acontext->alloc_ptr = advance;
263+ if (paddedSize > SIZE_MAX - 12 )
264+ {
265+ ASSERT_UNCONDITIONALLY (" NYI" ); // TODO: Throw overflow
266+ }
267+ paddedSize += 12 ;
268+ }
269+ UInt8* alloc_ptr = acontext->alloc_ptr ;
270+ ASSERT (alloc_ptr <= acontext->alloc_limit );
271+ if ((size_t )(acontext->alloc_limit - alloc_ptr) >= paddedSize)
272+ {
273+ acontext->alloc_ptr = alloc_ptr + paddedSize;
255274 if (requiresPadding)
256275 {
257- Object* dummy = (Object*)result ;
276+ Object* dummy = (Object*)alloc_ptr ;
258277 dummy->set_EEType (g_pFreeObjectEEType);
259- result += 12 ;
278+ alloc_ptr += 12 ; // if result + paddedSize was ok, then cant overflow
260279 }
261- pObject = (Object*)result ;
280+ pObject = (Object *)alloc_ptr ;
262281 pObject->set_EEType (pEEType);
263-
264282 return pObject;
265283 }
266284
@@ -293,7 +311,6 @@ COOP_PINVOKE_HELPER(Array *, RhpNewArrayAlign8, (EEType * pArrayEEType, int numE
293311 size_t size;
294312
295313 UInt32 baseSize = pArrayEEType->get_BaseSize ();
296- #ifndef HOST_64BIT
297314 // if the element count is <= 0x10000, no overflow is possible because the component size is
298315 // <= 0xffff, and thus the product is <= 0xffff0000, and the base size is only ~12 bytes
299316 if (numElements > 0x10000 )
@@ -309,26 +326,33 @@ COOP_PINVOKE_HELPER(Array *, RhpNewArrayAlign8, (EEType * pArrayEEType, int numE
309326 }
310327 }
311328 else
312- #endif // !HOST_64BIT
313329 {
314330 size = (size_t )baseSize + ((size_t )numElements * (size_t )pArrayEEType->get_ComponentSize ());
315331 size = ALIGN_UP (size, sizeof (UIntNative));
316332 }
317333 UInt8* result = acontext->alloc_ptr ;
318334 int requiresAlignObject = ((uint32_t )result) & 7 ;
319- if (requiresAlignObject) size += 12 ;
320-
321- UInt8* advance = result + size;
322- if (advance <= acontext->alloc_limit )
335+ size_t paddedSize = size;
336+ if (requiresAlignObject)
323337 {
324- acontext->alloc_ptr = advance;
338+ if (paddedSize > SIZE_MAX - 12 )
339+ {
340+ ASSERT_UNCONDITIONALLY (" NYI" ); // TODO: Throw overflow
341+ }
342+ paddedSize += 12 ;
343+ }
344+ UInt8* alloc_ptr = acontext->alloc_ptr ;
345+ ASSERT (alloc_ptr <= acontext->alloc_limit );
346+ if ((size_t )(acontext->alloc_limit - alloc_ptr) >= paddedSize)
347+ {
348+ acontext->alloc_ptr = alloc_ptr + paddedSize;
325349 if (requiresAlignObject)
326350 {
327- Object* dummy = (Object*)result ;
351+ Object* dummy = (Object*)alloc_ptr ;
328352 dummy->set_EEType (g_pFreeObjectEEType);
329- result += 12 ;
353+ alloc_ptr += 12 ; // if result + paddedSize was ok, then cant overflow
330354 }
331- pObject = (Array*)result ;
355+ pObject = (Array*)alloc_ptr ;
332356 pObject->set_EEType (pArrayEEType);
333357 pObject->InitArrayLength ((UInt32)numElements);
334358 return pObject;
@@ -347,6 +371,7 @@ COOP_PINVOKE_HELPER(Array *, RhpNewArrayAlign8, (EEType * pArrayEEType, int numE
347371
348372 return pObject;
349373}
374+ #endif // !HOST_64BIT
350375#endif // defined(HOST_ARM) || defined(HOST_WASM)
351376
352377COOP_PINVOKE_HELPER (void , RhpInitialDynamicInterfaceDispatch, ())
0 commit comments