|
| 1 | +--[[ |
| 2 | +SPDX-License-Identifier: ISC |
| 3 | +Copyright (c) 2023-2025, Sergey Bronnikov. |
| 4 | +
|
| 5 | +2.6 – Coroutines |
| 6 | +https://www.lua.org/manual/5.3/manual.html#2.6 |
| 7 | +
|
| 8 | +Computation of stack limit when entering a coroutine is wrong, |
| 9 | +https://github.com/lua/lua/commit/e1d8770f12542d34a3e32b825c95b93f8a341ee1 |
| 10 | +
|
| 11 | +C-stack overflow with deep nesting of coroutine.close, |
| 12 | +https://www.lua.org/bugs.html#5.4.4-9 |
| 13 | +
|
| 14 | +C stack overflow (again), |
| 15 | +https://github.com/lua/lua/commit/34affe7a63fc5d842580a9f23616d057e17dfe27 |
| 16 | +
|
| 17 | +When a coroutine tries to resume a non-suspended coroutine, |
| 18 | +it can do some mess (and break C assertions) before detecting the error, |
| 19 | +https://www.lua.org/bugs.html#5.3.3-4 |
| 20 | +
|
| 21 | +Suspended __le metamethod can give wrong result, |
| 22 | +https://www.lua.org/bugs.html#5.3.0-3 |
| 23 | +
|
| 24 | +Resuming the running coroutine makes it unyieldable, |
| 25 | +https://www.lua.org/bugs.html#5.2.2-8 |
| 26 | +
|
| 27 | +pcall may not restore previous error function when inside coroutines, |
| 28 | +https://www.lua.org/bugs.html#5.2.1-2 |
| 29 | +
|
| 30 | +Wrong handling of nCcalls in coroutines, |
| 31 | +https://www.lua.org/bugs.html#5.2.0-4 |
| 32 | +
|
| 33 | +coroutine.resume pushes element without ensuring stack size, |
| 34 | +https://www.lua.org/bugs.html#5.1.3-2 |
| 35 | +
|
| 36 | +Recursive coroutines may overflow C stack, |
| 37 | +https://www.lua.org/bugs.html#5.1.2-4 |
| 38 | +
|
| 39 | +Stand-alone interpreter shows incorrect error message when the |
| 40 | +"message" is a coroutine, |
| 41 | +https://www.lua.org/bugs.html#5.1.2-12 |
| 42 | +
|
| 43 | +Debug hooks may get wrong when mixed with coroutines, |
| 44 | +https://www.lua.org/bugs.html#5.1-7 |
| 45 | +
|
| 46 | +Values held in open upvalues of suspended threads may be |
| 47 | +incorrectly collected, |
| 48 | +https://www.lua.org/bugs.html#5.0.2-3 |
| 49 | +
|
| 50 | +Attempt to resume a running coroutine crashes Lua, |
| 51 | +https://www.lua.org/bugs.html#5.0-2 |
| 52 | +
|
| 53 | +Synopsis: |
| 54 | +
|
| 55 | +coroutine.close(co) |
| 56 | +coroutine.create(f) |
| 57 | +coroutine.isyieldable([co]) |
| 58 | +coroutine.resume(co [, val1, ...]) |
| 59 | +coroutine.running() |
| 60 | +coroutine.status(co) |
| 61 | +coroutine.wrap(f) |
| 62 | +coroutine.yield(...) |
| 63 | +]] |
| 64 | + |
| 65 | +local luzer = require("luzer") |
| 66 | + |
| 67 | +local C = {} |
| 68 | + |
| 69 | +local STATUS_CREATE = "CREATE" |
| 70 | +local STATUS_CLOSE = "CLOSE" |
| 71 | +local STATUS_YIELD = "YIELD" |
| 72 | +local STATUS_RESUME = "RESUME" |
| 73 | +local STATUS_DEAD = "DEAD" |
| 74 | + |
| 75 | +local CORO_MAX_NUMBER = 10^53 |
| 76 | + |
| 77 | +local CORO_STATE = { |
| 78 | + STATUS_CLOSE, |
| 79 | + STATUS_CREATE, |
| 80 | + STATUS_RESUME, |
| 81 | + STATUS_YIELD, |
| 82 | +} |
| 83 | + |
| 84 | +-- Forward declaration. |
| 85 | +local coro_loop |
| 86 | + |
| 87 | +local function random_coro(fdp) |
| 88 | + local coro, coro_n = fdp:one_of(C) |
| 89 | + local coro_status = coroutine.status(coro) |
| 90 | + if coro_status == STATUS_DEAD then |
| 91 | + table.remove(C, coro_n) |
| 92 | + coro = coroutine.create(coro_loop) |
| 93 | + table.insert(C, coro) |
| 94 | + coroutine.resume(coro, 1) |
| 95 | + end |
| 96 | + return fdp:one_of(C) |
| 97 | +end |
| 98 | + |
| 99 | +local function coro_step(fdp) |
| 100 | + local state = fdp:one_of(CORO_STATE) |
| 101 | + if state == STATUS_CREATE then |
| 102 | + local co = coroutine.create(coro_loop) |
| 103 | + table.insert(C, co) |
| 104 | + coroutine.resume(co, 1) |
| 105 | + return |
| 106 | + end |
| 107 | + |
| 108 | + state = fdp:one_of(CORO_STATE) |
| 109 | + local coro = random_coro(fdp) |
| 110 | + local status = coroutine.status(coro) |
| 111 | + if status == STATUS_DEAD then |
| 112 | + return |
| 113 | + end |
| 114 | + io.write(("[%0.6s] STATE: %s -> %s\n"):format(#C, status, state)) |
| 115 | + if state == STATUS_CLOSE then |
| 116 | + coroutine.close(coro) |
| 117 | + elseif state == STATUS_YIELD and coroutine.isyieldable(coro) then |
| 118 | + coroutine.yield(coro) |
| 119 | + elseif state == STATUS_RESUME then |
| 120 | + coroutine.resume(coro) |
| 121 | + end |
| 122 | + |
| 123 | + return |
| 124 | +end |
| 125 | + |
| 126 | +coro_loop = function(fdp, max_n) |
| 127 | + local n = fdp:consume_integer(1, max_n) |
| 128 | + for _ = 1, n do |
| 129 | + coro_step(fdp) |
| 130 | + end |
| 131 | +end |
| 132 | + |
| 133 | +local function TestOneInput(buf, _size) |
| 134 | + local fdp = luzer.FuzzedDataProvider(buf) |
| 135 | + local co = coroutine.create(coro_loop) |
| 136 | + table.insert(C, co) |
| 137 | + -- The function `coroutine.resume` starts the execution of |
| 138 | + -- a coroutine, changing its state from suspended to running. |
| 139 | + coroutine.resume(co, fdp, CORO_MAX_NUMBER) |
| 140 | +end |
| 141 | + |
| 142 | +local args = { |
| 143 | + artifact_prefix = "coroutine_create_", |
| 144 | +} |
| 145 | +luzer.Fuzz(TestOneInput, nil, args) |
0 commit comments