Skip to content

Commit 77fe5dc

Browse files
authored
JIT: Fix heap allocations (#120290)
Also override the global new operators to assert unconditionally, to catch this common pitfall proactively.
1 parent ca29ebb commit 77fe5dc

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

src/coreclr/jit/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ function(add_jit jitName)
620620
link_natvis_sources_for_target(${jitName} PRIVATE clrjit.natvis)
621621
endif()
622622

623+
target_compile_definitions(${jitName} PRIVATE JIT_STANDALONE_BUILD)
624+
623625
# add the install targets
624626
install_clr(TARGETS ${jitName} DESTINATIONS . COMPONENT alljits)
625627
endfunction()

src/coreclr/jit/alloc.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,53 @@ void ArenaAllocator::dumpMaxMemStats(FILE* file)
327327
s_maxStats.Print(file);
328328
}
329329
#endif // MEASURE_MEM_ALLOC
330+
331+
#ifdef JIT_STANDALONE_BUILD
332+
333+
void* __cdecl operator new(std::size_t size)
334+
{
335+
assert(!"Global new called; use HostAllocator if long-lived allocation was intended");
336+
337+
if (size == 0)
338+
{
339+
size++;
340+
}
341+
342+
void* result = malloc(size);
343+
if (result == nullptr)
344+
{
345+
throw std::bad_alloc{};
346+
}
347+
348+
return result;
349+
}
350+
351+
void* __cdecl operator new[](std::size_t size)
352+
{
353+
assert(!"Global new called; use HostAllocator if long-lived allocation was intended");
354+
355+
if (size == 0)
356+
{
357+
size++;
358+
}
359+
360+
void* result = malloc(size);
361+
if (result == nullptr)
362+
{
363+
throw std::bad_alloc{};
364+
}
365+
366+
return result;
367+
}
368+
369+
void __cdecl operator delete(void* ptr) noexcept
370+
{
371+
free(ptr);
372+
}
373+
374+
void __cdecl operator delete[](void* ptr) noexcept
375+
{
376+
free(ptr);
377+
}
378+
379+
#endif

src/coreclr/jit/lsra.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -874,15 +874,15 @@ LinearScan::LinearScan(Compiler* theCompiler)
874874
}
875875
else
876876
{
877-
regIndices =
878-
new regNumber[]{REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
879-
REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
880-
REG_XMM0, REG_XMM1, REG_XMM2, REG_XMM3, REG_XMM4, REG_XMM5, REG_XMM6, REG_XMM7,
881-
REG_XMM8, REG_XMM9, REG_XMM10, REG_XMM11, REG_XMM12, REG_XMM13, REG_XMM14, REG_XMM15,
882-
REG_XMM16, REG_XMM17, REG_XMM18, REG_XMM19, REG_XMM20, REG_XMM21, REG_XMM22, REG_XMM23,
883-
REG_XMM24, REG_XMM25, REG_XMM26, REG_XMM27, REG_XMM28, REG_XMM29, REG_XMM30, REG_XMM31,
884-
REG_K0, REG_K1, REG_K2, REG_K3, REG_K4, REG_K5, REG_K6, REG_K7,
885-
REG_COUNT};
877+
regIndices = new (theCompiler, CMK_LSRA)
878+
regNumber[]{REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
879+
REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
880+
REG_XMM0, REG_XMM1, REG_XMM2, REG_XMM3, REG_XMM4, REG_XMM5, REG_XMM6, REG_XMM7,
881+
REG_XMM8, REG_XMM9, REG_XMM10, REG_XMM11, REG_XMM12, REG_XMM13, REG_XMM14, REG_XMM15,
882+
REG_XMM16, REG_XMM17, REG_XMM18, REG_XMM19, REG_XMM20, REG_XMM21, REG_XMM22, REG_XMM23,
883+
REG_XMM24, REG_XMM25, REG_XMM26, REG_XMM27, REG_XMM28, REG_XMM29, REG_XMM30, REG_XMM31,
884+
REG_K0, REG_K1, REG_K2, REG_K3, REG_K4, REG_K5, REG_K6, REG_K7,
885+
REG_COUNT};
886886
}
887887
#endif // TARGET_AMD64
888888

@@ -12575,7 +12575,7 @@ LinearScan::RegisterSelection::RegisterSelection(LinearScan* linearScan)
1257512575
this->linearScan = linearScan;
1257612576

1257712577
#ifdef DEBUG
12578-
mappingTable = new ScoreMappingTable(linearScan->compiler->getAllocator(CMK_LSRA));
12578+
mappingTable = new (linearScan->compiler, CMK_LSRA) ScoreMappingTable(linearScan->compiler->getAllocator(CMK_LSRA));
1257912579

1258012580
#define REG_SEL_DEF(stat, value, shortname, orderSeqId) \
1258112581
mappingTable->Set(stat, &LinearScan::RegisterSelection::try_##stat);

0 commit comments

Comments
 (0)