openresty / openresty/luajit2

unexpected index -1 comes up when copy a table with only one element

Open
#188 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
1.4k
Forks
252
Avg merge
7d 11h
Merged PRs (30d)
3

Description

For me, this bug happens in my company computer resty command (Intel(R) Celeron(R) J4125 8GB ram win10x64 + wsl2 ubuntu 20.04+openresty 1.21.4 install from official apt)

It should be a luajit related bug because when I turn jit off for resty, there's no bug:

for ((i=1; i<=500; i++)); do resty -joff test.lua; done

BUG machine info:

设备名称 DESKTOP-S3UK088
处理器 Intel(R) Celeron(R) J4125 CPU @ 2.00GHz 2.00 GHz
机带 RAM 8.00 GB (7.85 GB 可用)

test.lua

local function jcopy(o)
  local function copy(v)
    if type(v) == "table" then
      local cv = {}
      for key, value in pairs(v) do
        if key == -1 then
          error(tostring(key))
        end
        cv[key] = copy(value)
      end
      return cv
    else
      return v
    end
  end

  return copy(o)
end

local function main()
  local n = 0
  for i = 1, 100, 1 do
    local c = jcopy { cond = { { "you shouldn't see this via index -1" } } }
    if c.cond[-1] then
      if n == 0 then
        -- print for the first time
        print(c.cond[-1][1])
      end
      n = n + 1
    end
  end
  if n > 0 then
    print("-1 index happends:" .. n)
  end
end

main()

then run test.lua 500 times

for ((i=1; i<=500; i++)); do resty test.lua; done

you will get output like:

root@DESKTOP-S3UK088:~/rsks# for ((i=1; i<=50; i++))
> do
>   resty test.lua
> done
you shouldn't see this via index -1
-1 index happends:77
you shouldn't see this via index -1
-1 index happends:29
you shouldn't see this via index -1
-1 index happends:8
you shouldn't see this via index -1
-1 index happends:6
you shouldn't see this via index -1
-1 index happends:38

But you shouldn't see any output if the copy work as expected.
this is the minimal repo: https://github.com/xiangnanscu/luajit-bug

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with test.lua using resty repeatedly, then compare normal JIT runs with the -joff command shown. Use the minimal repository linked in the issue as the starting point for investigation. Done means repeated JIT-enabled runs no longer produce the unexpected -1 entries or output, matching the -joff behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.