From 4f04148f75abaf6a0efef1e0b0b6f4bcb8608dea Mon Sep 17 00:00:00 2001 From: liam Date: Tue, 14 Jun 2022 08:03:25 +0800 Subject: [PATCH] Add a sanitizer to check if there is a definition of vreg before its reference --- core/iwasm/fast-jit/jit_regalloc.c | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/core/iwasm/fast-jit/jit_regalloc.c b/core/iwasm/fast-jit/jit_regalloc.c index 098d2bc99..c6644e76f 100644 --- a/core/iwasm/fast-jit/jit_regalloc.c +++ b/core/iwasm/fast-jit/jit_regalloc.c @@ -6,6 +6,10 @@ #include "jit_utils.h" #include "jit_compiler.h" +#if BH_DEBUG != 0 +#define VREG_DEF_SANITIZER +#endif + /** * A uint16 stack for storing distances of occurrences of virtual * registers. @@ -350,6 +354,29 @@ is_alloc_candidate(JitCompContext *cc, JitReg reg) && (!jit_cc_is_hreg(cc, reg) || !jit_cc_is_hreg_fixed(cc, reg))); } +#ifdef VREG_DEF_SANITIZER +static void +check_vreg_definition(RegallocContext *rc, JitInsn *insn) +{ + unsigned j; + JitRegVec regvec = jit_insn_opnd_regs(insn); + unsigned first_use = jit_insn_opnd_first_use(insn); + JitReg *regp; + + /* check if there is the definition of an vr before its references */ + JIT_REG_VEC_FOREACH_USE(regvec, j, regp, first_use) + { + VirtualReg *vr = NULL; + + if (!is_alloc_candidate(rc->cc, *regp)) + continue; + + vr = rc_get_vr(rc, *regp); + bh_assert(vr->distances); + } +} +#endif + /** * Collect distances from the beginning of basic block of all occurrences of * each virtual register. @@ -371,6 +398,10 @@ collect_distances(RegallocContext *rc, JitBasicBlock *basic_block) unsigned i; JitReg *regp; +#ifdef VREG_DEF_SANITIZER + check_vreg_definition(rc, insn); +#endif + /* NOTE: the distance may be pushed more than once if the virtual register occurs multiple times in the instruction. */