关于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要放到后面执行。

相关推荐
极客先躯1 天前
老版本Postman请求脚本编写指南:从Async/Await到回调函数的完整迁移方案
测试工具·lua·postman
oMcLin1 天前
如何在 Debian 11 上配置并调优 Nginx 与 Lua 脚本,提升高流量 API 网关的性能与安全性?
nginx·debian·lua
悟能不能悟4 天前
怎么使用postman批量的给api做测试
测试工具·lua·postman
最贪吃的虎5 天前
Redis其实并不是线程安全的
java·开发语言·数据库·redis·后端·缓存·lua
_200_6 天前
Lua 流程控制
开发语言·junit·lua
Kang.Charles6 天前
Lua UI系统框架逻辑详解
ui·lua
oMcLin6 天前
如何在 Rocky Linux 8.6 上配置并调优 Nginx 与 Lua 脚本,提升 API 网关的性能与并发处理能力
linux·nginx·lua
姓蔡小朋友8 天前
LUA脚本
开发语言·junit·lua
金融新世界8 天前
推动产业升级:倒逼转型创新与重构产业链格局
lua
oMcLin8 天前
如何在 Debian 11 上配置并优化 Nginx 与 Lua 脚本,提升高并发网站的动态请求处理能力?
nginx·debian·lua