Mon Apr 17 20:33:12 2023 UTC ()
lua: apply ustream bugfix for "C-stack overflow with deep nesting of coroutine.close."


(nikita)
diff -r1.8 -r1.9 src/external/mit/lua/dist/src/lcorolib.c
diff -r1.10 -r1.11 src/external/mit/lua/dist/src/lstate.c
diff -r1.12 -r1.13 src/external/mit/lua/dist/src/lua.h

cvs diff -r1.8 -r1.9 src/external/mit/lua/dist/src/lcorolib.c (expand / switch to unified diff)

--- src/external/mit/lua/dist/src/lcorolib.c 2023/04/16 20:46:17 1.8
+++ src/external/mit/lua/dist/src/lcorolib.c 2023/04/17 20:33:12 1.9
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lcorolib.c,v 1.8 2023/04/16 20:46:17 nikita Exp $ */ 1/* $NetBSD: lcorolib.c,v 1.9 2023/04/17 20:33:12 nikita Exp $ */
2 2
3/* 3/*
4** Id: lcorolib.c  4** Id: lcorolib.c
5** Coroutine Library 5** Coroutine Library
6** See Copyright Notice in lua.h 6** See Copyright Notice in lua.h
7*/ 7*/
8 8
9#define lcorolib_c 9#define lcorolib_c
10#define LUA_LIB 10#define LUA_LIB
11 11
12#include "lprefix.h" 12#include "lprefix.h"
13 13
14 14
@@ -70,27 +70,27 @@ static int luaB_coresume (lua_State *L)  @@ -70,27 +70,27 @@ static int luaB_coresume (lua_State *L)
70 lua_pushboolean(L, 1); 70 lua_pushboolean(L, 1);
71 lua_insert(L, -(r + 1)); 71 lua_insert(L, -(r + 1));
72 return r + 1; /* return true + 'resume' returns */ 72 return r + 1; /* return true + 'resume' returns */
73 } 73 }
74} 74}
75 75
76 76
77static int luaB_auxwrap (lua_State *L) { 77static int luaB_auxwrap (lua_State *L) {
78 lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 78 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
79 int r = auxresume(L, co, lua_gettop(L)); 79 int r = auxresume(L, co, lua_gettop(L));
80 if (l_unlikely(r < 0)) { /* error? */ 80 if (l_unlikely(r < 0)) { /* error? */
81 int stat = lua_status(co); 81 int stat = lua_status(co);
82 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ 82 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
83 stat = lua_resetthread(co); /* close its tbc variables */ 83 stat = lua_resetthread(co, L); /* close its tbc variables */
84 lua_assert(stat != LUA_OK); 84 lua_assert(stat != LUA_OK);
85 lua_xmove(co, L, 1); /* move error message to the caller */ 85 lua_xmove(co, L, 1); /* move error message to the caller */
86 } 86 }
87 if (stat != LUA_ERRMEM && /* not a memory error and ... */ 87 if (stat != LUA_ERRMEM && /* not a memory error and ... */
88 lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ 88 lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
89 luaL_where(L, 1); /* add extra info, if available */ 89 luaL_where(L, 1); /* add extra info, if available */
90 lua_insert(L, -2); 90 lua_insert(L, -2);
91 lua_concat(L, 2); 91 lua_concat(L, 2);
92 } 92 }
93 return lua_error(L); /* propagate error */ 93 return lua_error(L); /* propagate error */
94 } 94 }
95 return r; 95 return r;
96} 96}
@@ -166,27 +166,27 @@ static int luaB_yieldable (lua_State *L) @@ -166,27 +166,27 @@ static int luaB_yieldable (lua_State *L)
166 166
167static int luaB_corunning (lua_State *L) { 167static int luaB_corunning (lua_State *L) {
168 int ismain = lua_pushthread(L); 168 int ismain = lua_pushthread(L);
169 lua_pushboolean(L, ismain); 169 lua_pushboolean(L, ismain);
170 return 2; 170 return 2;
171} 171}
172 172
173 173
174static int luaB_close (lua_State *L) { 174static int luaB_close (lua_State *L) {
175 lua_State *co = getco(L); 175 lua_State *co = getco(L);
176 int status = auxstatus(L, co); 176 int status = auxstatus(L, co);
177 switch (status) { 177 switch (status) {
178 case COS_DEAD: case COS_YIELD: { 178 case COS_DEAD: case COS_YIELD: {
179 status = lua_resetthread(co); 179 status = lua_resetthread(co, L);
180 if (status == LUA_OK) { 180 if (status == LUA_OK) {
181 lua_pushboolean(L, 1); 181 lua_pushboolean(L, 1);
182 return 1; 182 return 1;
183 } 183 }
184 else { 184 else {
185 lua_pushboolean(L, 0); 185 lua_pushboolean(L, 0);
186 lua_xmove(co, L, 1); /* move error message */ 186 lua_xmove(co, L, 1); /* move error message */
187 return 2; 187 return 2;
188 } 188 }
189 } 189 }
190 default: /* normal or running coroutine */ 190 default: /* normal or running coroutine */
191 return luaL_error(L, "cannot close a %s coroutine", statname[status]); 191 return luaL_error(L, "cannot close a %s coroutine", statname[status]);
192 } 192 }

cvs diff -r1.10 -r1.11 src/external/mit/lua/dist/src/lstate.c (expand / switch to unified diff)

--- src/external/mit/lua/dist/src/lstate.c 2023/04/16 20:46:17 1.10
+++ src/external/mit/lua/dist/src/lstate.c 2023/04/17 20:33:12 1.11
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lstate.c,v 1.10 2023/04/16 20:46:17 nikita Exp $ */ 1/* $NetBSD: lstate.c,v 1.11 2023/04/17 20:33:12 nikita Exp $ */
2 2
3/* 3/*
4** Id: lstate.c  4** Id: lstate.c
5** Global State 5** Global State
6** See Copyright Notice in lua.h 6** See Copyright Notice in lua.h
7*/ 7*/
8 8
9#define lstate_c 9#define lstate_c
10#define LUA_CORE 10#define LUA_CORE
11 11
12#include "lprefix.h" 12#include "lprefix.h"
13 13
14 14
@@ -337,29 +337,30 @@ int luaE_resetthread (lua_State *L, int  @@ -337,29 +337,30 @@ int luaE_resetthread (lua_State *L, int
337 status = LUA_OK; 337 status = LUA_OK;
338 L->status = LUA_OK; /* so it can run __close metamethods */ 338 L->status = LUA_OK; /* so it can run __close metamethods */
339 status = luaD_closeprotected(L, 1, status); 339 status = luaD_closeprotected(L, 1, status);
340 if (status != LUA_OK) /* errors? */ 340 if (status != LUA_OK) /* errors? */
341 luaD_seterrorobj(L, status, L->stack + 1); 341 luaD_seterrorobj(L, status, L->stack + 1);
342 else 342 else
343 L->top = L->stack + 1; 343 L->top = L->stack + 1;
344 ci->top = L->top + LUA_MINSTACK; 344 ci->top = L->top + LUA_MINSTACK;
345 luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); 345 luaD_reallocstack(L, cast_int(ci->top - L->stack), 0);
346 return status; 346 return status;
347} 347}
348 348
349 349
350LUA_API int lua_resetthread (lua_State *L) { 350LUA_API int lua_resetthread (lua_State *L, lua_State *from) {
351 int status; 351 int status;
352 lua_lock(L); 352 lua_lock(L);
 353 L->nCcalls = (from) ? getCcalls(from) : 0;
353 status = luaE_resetthread(L, L->status); 354 status = luaE_resetthread(L, L->status);
354 lua_unlock(L); 355 lua_unlock(L);
355 return status; 356 return status;
356} 357}
357 358
358 359
359LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 360LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
360 int i; 361 int i;
361 lua_State *L; 362 lua_State *L;
362 global_State *g; 363 global_State *g;
363 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); 364 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
364 if (l == NULL) return NULL; 365 if (l == NULL) return NULL;
365 L = &l->l.l; 366 L = &l->l.l;

cvs diff -r1.12 -r1.13 src/external/mit/lua/dist/src/lua.h (expand / switch to unified diff)

--- src/external/mit/lua/dist/src/lua.h 2023/04/16 20:46:17 1.12
+++ src/external/mit/lua/dist/src/lua.h 2023/04/17 20:33:12 1.13
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lua.h,v 1.12 2023/04/16 20:46:17 nikita Exp $ */ 1/* $NetBSD: lua.h,v 1.13 2023/04/17 20:33:12 nikita Exp $ */
2 2
3/* 3/*
4** Id: lua.h  4** Id: lua.h
5** Lua - A Scripting Language 5** Lua - A Scripting Language
6** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 6** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
7** See Copyright Notice at the end of this file 7** See Copyright Notice at the end of this file
8*/ 8*/
9 9
10 10
11#ifndef lua_h 11#ifndef lua_h
12#define lua_h 12#define lua_h
13 13
14#include <stdarg.h> 14#include <stdarg.h>
@@ -147,27 +147,27 @@ typedef void (*lua_WarnFunction) (void * @@ -147,27 +147,27 @@ typedef void (*lua_WarnFunction) (void *
147 147
148/* 148/*
149** RCS ident string 149** RCS ident string
150*/ 150*/
151extern const char lua_ident[]; 151extern const char lua_ident[];
152 152
153 153
154/* 154/*
155** state manipulation 155** state manipulation
156*/ 156*/
157LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); 157LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
158LUA_API void (lua_close) (lua_State *L); 158LUA_API void (lua_close) (lua_State *L);
159LUA_API lua_State *(lua_newthread) (lua_State *L); 159LUA_API lua_State *(lua_newthread) (lua_State *L);
160LUA_API int (lua_resetthread) (lua_State *L); 160LUA_API int (lua_resetthread) (lua_State *L, lua_State *from);
161 161
162LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); 162LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
163 163
164 164
165LUA_API lua_Number (lua_version) (lua_State *L); 165LUA_API lua_Number (lua_version) (lua_State *L);
166 166
167 167
168/* 168/*
169** basic stack manipulation 169** basic stack manipulation
170*/ 170*/
171LUA_API int (lua_absindex) (lua_State *L, int idx); 171LUA_API int (lua_absindex) (lua_State *L, int idx);
172LUA_API int (lua_gettop) (lua_State *L); 172LUA_API int (lua_gettop) (lua_State *L);
173LUA_API void (lua_settop) (lua_State *L, int idx); 173LUA_API void (lua_settop) (lua_State *L, int idx);