Skip to content

Commit d978d2e

Browse files
committed
api: claim lua_isyieldable reads
1 parent dc1e656 commit d978d2e

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎notes/api-debug-claim-cleanup.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Permanent shape:
5353
allocation, then resume-claim to publish the stack slot. `lua_newuserdata()`
5454
and `lua_newthread()` snapshot the target current environment before dropping
5555
the preclaim.
56+
- `lua_isyieldable()` now follows the same read-only claim shape as
57+
`lua_status()`/`lua_gettop()` before reading `L->cframe`.
5658
- `lua_pushvfstring()` prechecks target ownership, drops the preclaim while
5759
formatting/interning through the current error state, then resume-claims the
5860
target for one-slot growth and string publication. `lua_pushfstring()`

‎notes/m5.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,3 +2918,9 @@ M5 C wrapper callback publication:
29182918
external C function entry.
29192919
- Added `m5_wrapcf_func_publish` to reject raw C-side `->wrapf` access and pin
29202920
the `wrapf_store()` before `BC_FUNCCW` publication order.
2921+
2922+
M5 read-only C API owner claims:
2923+
- `lua_isyieldable()` now claims the target state before reading `L->cframe`,
2924+
matching the already-claimed `lua_status()` and `lua_gettop()` public
2925+
read-only APIs. `m5_api_debug_claim_cleanup` now rejects regressions in that
2926+
claim/read/drop boundary.

‎src/lj_api.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,13 @@ LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
21982198

21992199
LUA_API int lua_isyieldable(lua_State *L)
22002200
{
2201-
return cframe_canyield(L->cframe);
2201+
LJStateClaim claim;
2202+
int ok;
2203+
if (!lj_state_tryclaim(L, lj_thr_current_id(G(L)), &claim))
2204+
lj_err_callermsg(api_errstate(L), "thread busy");
2205+
ok = cframe_canyield(L->cframe);
2206+
lj_state_dropclaim(&claim);
2207+
return ok;
22022208
}
22032209

22042210
LUA_API int lua_yield(lua_State *L, int nresults)

‎tests/suites/m5_publication.lua‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,21 @@ END {
515515
exit 1
516516
}
517517
}
518+
]=], "src/lj_api.c")
519+
520+
awk([=[
521+
BEGIN { infn = 0; claim = 0; read = 0; drop = 0 }
522+
/^LUA_API int lua_isyieldable\(lua_State \*L\)/ { infn = 1; next }
523+
infn && /^}/ { infn = 0; next }
524+
infn && /lj_state_tryclaim/ { claim = NR }
525+
infn && /cframe_canyield\(L->cframe\)/ { read = NR }
526+
infn && /lj_state_dropclaim\(&claim\)/ { drop = NR }
527+
END {
528+
if (!claim || !read || !drop || claim > read || read > drop) {
529+
print "lua_isyieldable owner-claim boundary missing"
530+
exit 1
531+
}
532+
}
518533
]=], "src/lj_api.c")
519534

520535
awk([=[

0 commit comments

Comments
 (0)