From 8360fe94bec2308d46d8dd5d22a6fd526fdccce9 Mon Sep 17 00:00:00 2001 From: Basilisk-Dev Date: Sat, 25 Apr 2026 15:10:38 -0400 Subject: [PATCH] Issue #3049 - loongarch64: flush icache after JIT runtime patching --- .../jit/loongarch64/Assembler-loongarch64.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/js/src/jit/loongarch64/Assembler-loongarch64.cpp b/js/src/jit/loongarch64/Assembler-loongarch64.cpp index 77e7c734ce..b8d33f2ef1 100644 --- a/js/src/jit/loongarch64/Assembler-loongarch64.cpp +++ b/js/src/jit/loongarch64/Assembler-loongarch64.cpp @@ -18,6 +18,21 @@ using mozilla::DebugOnly; using namespace js; using namespace js::jit; +static size_t Load64PatchSize(Instruction* inst0) { + InstImm* i0 = (InstImm*)inst0; + InstImm* i1 = (InstImm*)i0->next(); + InstImm* i2 = (InstImm*)i1->next(); + InstImm* i3 = (InstImm*)i2->next(); + + MOZ_ASSERT((i0->extractBitField(31, 25)) == ((uint32_t)op_lu12i_w >> 25)); + MOZ_ASSERT((i1->extractBitField(31, 22)) == ((uint32_t)op_ori >> 22)); + MOZ_ASSERT((i2->extractBitField(31, 25)) == ((uint32_t)op_lu32i_d >> 25)); + + return ((i3->extractBitField(31, 22)) == ((uint32_t)op_lu52i_d >> 22)) + ? 4 * sizeof(uint32_t) + : 3 * sizeof(uint32_t); +} + void js::jit::PatchJump(CodeLocationJump& jump_, CodeLocationLabel label, ReprotectCode reprotect) { Instruction* inst = reinterpret_cast(jump_.raw()); @@ -2302,6 +2317,7 @@ static void TraceOneDataRelocation(JSTracer* trc, awjc.emplace(code); } Assembler::UpdateLoad64Value(inst, uint64_t(ptr)); + AutoFlushICache::flush(uintptr_t(inst), Load64PatchSize(inst)); } } @@ -2428,6 +2444,8 @@ void Assembler::PatchWrite_NearCall(CodeLocationLabel start, // Short jump wouldn't be more efficient. Assembler::WriteLoad64Instructions(inst, ScratchRegister, (uint64_t)dest); inst[3] = InstImm(op_jirl, BOffImm16(0), ScratchRegister, ra); + + AutoFlushICache::flush(uintptr_t(inst), PatchWrite_NearCallSize()); } uint64_t Assembler::ExtractLoad64Value(Instruction* inst0) { @@ -2527,6 +2545,7 @@ void Assembler::PatchDataWithValueCheck(CodeLocationLabel label, // Replace with new value Assembler::UpdateLoad64Value(inst, uint64_t(newValue.value)); + AutoFlushICache::flush(uintptr_t(inst), Load64PatchSize(inst)); } void Assembler::PatchInstructionImmediate(uint8_t* code, PatchedImmPtr imm) { @@ -2557,4 +2576,6 @@ void Assembler::ToggleCall(CodeLocationLabel inst_, bool enabled) { InstNOP nop; *i3 = nop; } + + AutoFlushICache::flush(uintptr_t(i3), sizeof(uint32_t)); }