From 52b8d2ccb170374813d26f5c41651b7a9e3d52c4 Mon Sep 17 00:00:00 2001 From: Katelyn Gadd Date: Thu, 11 Sep 2025 10:06:11 -0700 Subject: [PATCH] Detect relocations pointing to invalid targets Call badcode instead of asserting for certain types of invalid branches --- src/coreclr/interpreter/compiler.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/coreclr/interpreter/compiler.cpp b/src/coreclr/interpreter/compiler.cpp index 7e6bfa7e4d45db..7c3f56008e73a7 100644 --- a/src/coreclr/interpreter/compiler.cpp +++ b/src/coreclr/interpreter/compiler.cpp @@ -865,6 +865,9 @@ void InterpCompiler::PatchRelocations(TArray *relocs) for (int32_t i = 0; i < size; i++) { Reloc *reloc = relocs->Get(i); + if (reloc->pTargetBB->nativeOffset < 0) + BADCODE("jump with invalid offset"); + int32_t offset = reloc->pTargetBB->nativeOffset - reloc->offset; int32_t *pSlot = NULL; @@ -2111,14 +2114,15 @@ void InterpCompiler::EmitBranch(InterpOpcode opcode, int32_t ilOffset) { int32_t target = (int32_t)(m_ip - m_pILCode) + ilOffset; if (target < 0 || target >= m_ILCodeSize) - assert(0); + BADCODE("code jumps to outer space"); // Backwards branch, emit safepoint if (ilOffset < 0) AddIns(INTOP_SAFEPOINT); InterpBasicBlock *pTargetBB = m_ppOffsetToBB[target]; - assert(pTargetBB != NULL); + if (pTargetBB == NULL) + BADCODE("code jumps to invalid offset"); EmitBranchToBB(opcode, pTargetBB); }