aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-02-18 17:02:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-02-18 17:02:32 -0300
commitcd38fe8cf3b0f54dcc1d4a21a7a9cb585c46a43e (patch)
tree3906d64958ec82fe82ccb1eb25fd10ea77dabf0d
parentfa1382b5cd504bdfc5fc3f5c447ed09a4c9804fd (diff)
downloadlua-cd38fe8cf3b0f54dcc1d4a21a7a9cb585c46a43e.tar.gz
lua-cd38fe8cf3b0f54dcc1d4a21a7a9cb585c46a43e.tar.bz2
lua-cd38fe8cf3b0f54dcc1d4a21a7a9cb585c46a43e.zip
Added macro LUAI_STRICT_ADDRESS
By default, the code assumes it is safe to use a dealocated pointer as long as the code does not access it.
-rw-r--r--ldo.c28
-rw-r--r--ltests.h4
2 files changed, 22 insertions, 10 deletions
diff --git a/ldo.c b/ldo.c
index 65252e07..4705b26c 100644
--- a/ldo.c
+++ b/ldo.c
@@ -192,14 +192,19 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
192 192
193/* 193/*
194** In ISO C, any pointer use after the pointer has been deallocated is 194** In ISO C, any pointer use after the pointer has been deallocated is
195** undefined behavior. So, before a stack reallocation, all pointers are 195** undefined behavior. So, before a stack reallocation, all pointers
196** changed to offsets, and after the reallocation they are changed back 196** should be changed to offsets, and after the reallocation they should
197** to pointers. As during the reallocation the pointers are invalid, the 197** be changed back to pointers. As during the reallocation the pointers
198** reallocation cannot run emergency collections. 198** are invalid, the reallocation cannot run emergency collections.
199** 199** Alternatively, we can use the old address after the deallocation.
200** That is not strict ISO C, but seems to work fine everywhere.
201** The following macro chooses how strict is the code.
200*/ 202*/
203#if !defined(LUAI_STRICT_ADDRESS)
204#define LUAI_STRICT_ADDRESS 0
205#endif
201 206
202#if 1 207#if LUAI_STRICT_ADDRESS
203/* 208/*
204** Change all pointers to the stack into offsets. 209** Change all pointers to the stack into offsets.
205*/ 210*/
@@ -238,12 +243,16 @@ static void correctstack (lua_State *L, StkId oldstack) {
238 243
239#else 244#else
240/* 245/*
241** Alternatively, we can use the old address after the deallocation. 246** Assume that it is fine to use an address after its deallocation,
242** That is not strict ISO C, but seems to work fine everywhere. 247** as long as we do not dereference it.
243*/ 248*/
244 249
245static void relstack (lua_State *L) { UNUSED(L); } 250static void relstack (lua_State *L) { UNUSED(L); } /* do nothing */
246 251
252
253/*
254** Correct pointers into 'oldstack' to point into 'L->stack'.
255*/
247static void correctstack (lua_State *L, StkId oldstack) { 256static void correctstack (lua_State *L, StkId oldstack) {
248 CallInfo *ci; 257 CallInfo *ci;
249 UpVal *up; 258 UpVal *up;
@@ -261,7 +270,6 @@ static void correctstack (lua_State *L, StkId oldstack) {
261 ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ 270 ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */
262 } 271 }
263} 272}
264
265#endif 273#endif
266 274
267 275
diff --git a/ltests.h b/ltests.h
index 543b0d55..df72307a 100644
--- a/ltests.h
+++ b/ltests.h
@@ -44,6 +44,10 @@
44#define LUA_RAND32 44#define LUA_RAND32
45 45
46 46
47/* test stack reallocation with strict address use */
48#define LUAI_STRICT_ADDRESS 1
49
50
47/* memory-allocator control variables */ 51/* memory-allocator control variables */
48typedef struct Memcontrol { 52typedef struct Memcontrol {
49 int failnext; 53 int failnext;