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

相关推荐
小王不爱笑1322 天前
Postman 使用教程
测试工具·lua·postman
TracyCoder1233 天前
Redis 进阶之路:探秘事务、Lua 与特殊数据结构
数据结构·redis·lua
星空露珠4 天前
速算24点所有题库公式
开发语言·数据库·算法·游戏·lua
星空露珠4 天前
速算24点检测生成核心lua
开发语言·数据库·算法·游戏·lua
想做后端的前端6 天前
Lua的热更新
开发语言·lua
澄风7 天前
Redis ZSet+Lua脚本+SpringBoot实战:滑动窗口限流方案从原理到落地
spring boot·redis·lua
Geoking.7 天前
【Redis】Redis 中的 Pipeline 与 Lua 脚本:高性能与原子性的两种武器
redis·lua
剑之所向8 天前
嵌入式之lua脚本
开发语言·junit·lua
plmm烟酒僧8 天前
使用 Lua 进行汽车 UDS 诊断:轻量级脚本化诊断新思路
嵌入式·lua·汽车电子·uds诊断·汽车诊断·can通信·诊断协议
假女吖☌9 天前
Lua 脚本
开发语言·lua