|
| 1 | +local tap = require('tap') |
| 2 | +-- Test file to demonstrate the infinite loop in LuaJIT during the |
| 3 | +-- use-def analysis for upvalues. |
| 4 | +-- See details in https://github.com/LuaJIT/LuaJIT/issues/736. |
| 5 | +local test = tap.test('lj-736-BC_UCLO-triggers-infinite-loop'):skipcond({ |
| 6 | + ['Test requires JIT enabled'] = not jit.status(), |
| 7 | +}) |
| 8 | +test:plan(2) |
| 9 | + |
| 10 | +-- Before the patch, the code flow like in the `testcase()` below |
| 11 | +-- may cause the problem -- use-def analysis for the 0019 UCLO |
| 12 | +-- creates an infinite loop in 0014 - 0019: |
| 13 | +-- | 0008 FORI base: 4 jump: => 0013 |
| 14 | +-- | 0009 ISNEN var: 7 num: 0 ; number 2 |
| 15 | +-- | 0010 JMP rbase: 8 jump: => 0012 |
| 16 | +-- | 0011 UCLO rbase: 2 jump: => 0014 |
| 17 | +-- | 0012 FORL base: 4 jump: => 0009 |
| 18 | +-- | 0013 UCLO rbase: 2 jump: => 0014 |
| 19 | +-- | 0014 KPRI dst: 2 pri: 0 ; Start of `assert()` line. |
| 20 | +-- | ... |
| 21 | +-- | 0019 UCLO rbase: 2 jump: => 0014 |
| 22 | + |
| 23 | +jit.opt.start('hotloop=1') |
| 24 | + |
| 25 | +local assert_msg = 'Infinite loop is not reproduced.' |
| 26 | +local assert = assert |
| 27 | + |
| 28 | +local function testcase() |
| 29 | + -- The code in the first scope `do`/`end` is a prerequisite. |
| 30 | + -- It contains the UCLO instruction for the `uv1`. The use-def |
| 31 | + -- analysis for it escapes this `do`/`end` scope. |
| 32 | + do |
| 33 | + local uv1 -- luacheck: no unused |
| 34 | + local _ = function() return uv1 end |
| 35 | + |
| 36 | + -- Records the trace for which use-def analysis is applied. |
| 37 | + for i = 1, 2 do |
| 38 | + -- This condition triggers snapshoting and use-def analysis. |
| 39 | + -- Before the patch this triggers the infinite loop in the |
| 40 | + -- `snap_usedef()`, so the `goto` is never taken. |
| 41 | + if i == 2 then |
| 42 | + goto x |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | +::x:: |
| 48 | + do |
| 49 | + local uv2 -- luacheck: no unused |
| 50 | + |
| 51 | + -- Create a tight loop for the one more upvalue (`uv2`). |
| 52 | + -- Before the patch, use-def analysis gets stuck in this code |
| 53 | + -- flow. |
| 54 | + assert(nil, assert_msg) |
| 55 | + goto x |
| 56 | + -- This code is unreachable by design. |
| 57 | + local _ = function() return uv2 end -- luacheck: ignore |
| 58 | + end |
| 59 | +end |
| 60 | + |
| 61 | +local ok, err = pcall(testcase) |
| 62 | + |
| 63 | +test:is(ok, false, 'assertion is triggered in a function with testcase') |
| 64 | +test:ok(err:match(assert_msg), 'BC_UCLO does not trigger an infinite loop') |
| 65 | + |
| 66 | +test:done(true) |
0 commit comments