关于lua源代码中的EXTRA_STACK宏

EXTRA_STACK是给栈额外预留的空间:

c 复制代码
/* extra stack space to handle TM calls and some other extras */
#define EXTRA_STACK 5

这样top之上的几个位置就可以先用,后面再增加栈空间:

c 复制代码
static void callTM (lua_State *L, const TValue *f, const TValue *p1,
                    const TValue *p2, const TValue *p3) {
  setobj2s(L, L->top, f);  /* push function */
  setobj2s(L, L->top+1, p1);  /* 1st argument */
  setobj2s(L, L->top+2, p2);  /* 2nd argument */
  setobj2s(L, L->top+3, p3);  /* 3th argument */
  luaD_checkstack(L, 4);
  L->top += 4;
  luaD_call(L, L->top - 4, 0);
}

我看到这里就想,干嘛不提前luaD_checkstack(L, 4);

c 复制代码
static void callTM (lua_State *L, const TValue *f, const TValue *p1,
                    const TValue *p2, const TValue *p3) {
  setobj2s(L, L->top, f); /* push function */
  setobj2s(L, L->top+1, p1); /* 1st argument */
  setobj2s(L, L->top+2, p2); /* 2nd argument */
  setobj2s(L, L->top+3, p3); /* 3th argument */
  luaD_checkstack(L, 4);
  L->top += 4;
  luaD_call(L, L->top - 4, 0);
}

但是用AddressSanitizer工具测试发现,在执行源码自带的trace-globals.lua时,setobj2s(L, L->top+3, p3);会报使用已free内存。

经过查看代码发现,p3可能是栈上的数据,luaD_checkstack中可能会调用luaD_reallocstack导致p3不可用。

所以luaD_checkstack要放到后面执行。

相关推荐
利来利往3 天前
【ai写代码】lua-判断表是否被修改
lua
陈天cjq3 天前
Redis 实用型限流与延时队列:从 Lua 固定/滑动窗口到 Streams 消费组(含脚本与压测)
redis·junit·lua
Warren983 天前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
柯南二号4 天前
MacOS 系统计算机专业好用工具安装
开发语言·lua
神洛华4 天前
Lua语言程序设计2:函数、输入输出、控制结构
开发语言·lua
测试界清流9 天前
Postman接口测试入门
开发语言·lua
Volunteer Technology9 天前
Lua基础+Lua数据类型
开发语言·junit·lua
Volunteer Technology9 天前
openresty-lua-redis案例
redis·lua·openresty
WaterRun12 天前
开源项目luaToEXE: 将.lua封装为自带解释器无lua环境依赖的可执行文件工具
lua
William一直在路上13 天前
LUA脚本语言
开发语言·lua