commit cd38fe8cf3b0f54dcc1d4a21a7a9cb585c46a43e
parent fa1382b5cd504bdfc5fc3f5c447ed09a4c9804fd
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 18 Feb 2025 17:02:05 -0300
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.
Diffstat:
2 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/ldo.c b/ldo.c
@@ -192,14 +192,19 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
/*
** In ISO C, any pointer use after the pointer has been deallocated is
-** undefined behavior. So, before a stack reallocation, all pointers are
-** changed to offsets, and after the reallocation they are changed back
-** to pointers. As during the reallocation the pointers are invalid, the
-** reallocation cannot run emergency collections.
-**
+** undefined behavior. So, before a stack reallocation, all pointers
+** should be changed to offsets, and after the reallocation they should
+** be changed back to pointers. As during the reallocation the pointers
+** are invalid, the reallocation cannot run emergency collections.
+** Alternatively, we can use the old address after the deallocation.
+** That is not strict ISO C, but seems to work fine everywhere.
+** The following macro chooses how strict is the code.
*/
+#if !defined(LUAI_STRICT_ADDRESS)
+#define LUAI_STRICT_ADDRESS 0
+#endif
-#if 1
+#if LUAI_STRICT_ADDRESS
/*
** Change all pointers to the stack into offsets.
*/
@@ -238,12 +243,16 @@ static void correctstack (lua_State *L, StkId oldstack) {
#else
/*
-** Alternatively, we can use the old address after the deallocation.
-** That is not strict ISO C, but seems to work fine everywhere.
+** Assume that it is fine to use an address after its deallocation,
+** as long as we do not dereference it.
*/
-static void relstack (lua_State *L) { UNUSED(L); }
+static void relstack (lua_State *L) { UNUSED(L); } /* do nothing */
+
+/*
+** Correct pointers into 'oldstack' to point into 'L->stack'.
+*/
static void correctstack (lua_State *L, StkId oldstack) {
CallInfo *ci;
UpVal *up;
@@ -261,7 +270,6 @@ static void correctstack (lua_State *L, StkId oldstack) {
ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */
}
}
-
#endif
diff --git a/ltests.h b/ltests.h
@@ -44,6 +44,10 @@
#define LUA_RAND32
+/* test stack reallocation with strict address use */
+#define LUAI_STRICT_ADDRESS 1
+
+
/* memory-allocator control variables */
typedef struct Memcontrol {
int failnext;