【问题标题】:Lua Crash When Creating a New State创建新状态时 Lua 崩溃
【发布时间】:2019-01-25 09:42:23
【问题描述】:

我正在开发一个带有 256 kB RAM 和 1 MB 闪存的 Cortex-M4F 嵌入式系统。我的应用程序是用 C++ 编写的,但我使用 GCC 将 Lua 构建并集成为 C 库。我只需要将 l_signalT 定义为无符号 32 位整数。我的测试代码如下:

#include "lua.hpp"

int main(void)
{
    while (true)
    {
      lua_State * L = luaL_newstate(); 
      luaL_dostring(L, "a = 10 + 5"); 
      lua_getglobal(L, "a"); 
      int i = lua_tointeger(L, -1); 
      printf("%d\n", i); 
      lua_close(L); 
    }
}

我正在取消引用空指针。增加堆和栈并没有解决问题。我还在调用 realloc 的分配器中添加了一个 null 测试。 setjmp/longjump 代码中似乎发生了一些事情。我想知道是否有关于如何进一步调试的想法。我添加了一些带有<----- 的cmets,可能需要一些滚动。这实际上是我的堆栈转储。

static void setnodevector (lua_State *L, Table *t, unsigned int size) {
  if (size == 0) {  /* no elements to hash part? */
    t->node = cast(Node *, dummynode);  /* use common 'dummynode' */
    t->lsizenode = 0;                                                      <---- t is zero. Write 
                                                                                 does evil.
                                                                                 Hardfault.

static void auxsetnode (lua_State *L, void *ud) {
  AuxsetnodeT *asn = cast(AuxsetnodeT *, ud);
  setnodevector(L, asn->t, asn->nhsize);           <--- Last two values pointed to are zero.
}

<setjmp>
    466A        mov r2, sp
    E8A05FF4    stm r0!, {r2, r4-r12, lr}
    EC808B10    vstmia r0, {d8-d15}          <------ Part of LUAI_TRY called below. Memory location
    2000        movs r0, #0                          of ud is cleared here.
    4770        bx lr

int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
  unsigned short oldnCcalls = L->nCcalls;
  struct lua_longjmp lj;
  lj.status = LUA_OK;
  lj.previous = L->errorJmp;  /* chain new error handler */
  L->errorJmp = &lj;
  LUAI_TRY(L, &lj,                                            <---- Value pointed to by ud is nonzero
    (*f)(L, ud);                                                    but cleared in setjmp.
  );
  L->errorJmp = lj.previous;  /* restore old error handler */
  L->nCcalls = oldnCcalls;
  return lj.status;
}

【问题讨论】:

    标签: lua cortex-m


    【解决方案1】:

    我没有为 VFP 定义导致 jmp_buf 大小错误的宏:

    #if defined(__ARM_ARCH_VFP__) || defined(__ARM_ARCH_VFP3_D16__) || 
    defined(__ARM_ARCH_VFP3_D32__) || defined(__ARM_ARCH_VFPV4_D16__) || 
    defined(__ARM_ARCH_FPV4_SP_D16__) || defined(__ARM_ARCH_FPV5_SP_D16__) || 
    defined(__ARM_ARCH_FPV5_D16__)
    typedef unsigned long long jmp_buf[14];  // R4-R14, D8-D15
    #else
    typedef unsigned long jmp_buf[11];  // R4-R14
    #endif
    

    Lua 现在可以正确计算和打印10 + 5

    【讨论】:

      猜你喜欢
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多