【问题标题】:libc stat function and LuaJITlibc stat 函数和 LuaJIT
【发布时间】:2020-05-27 06:04:18
【问题描述】:

我整天都在研究 LuaJIT 的一个神秘的不当行为。 libcstat 函数在其stat 缓冲区中返回错误值。

LuaJIT 脚本:

-- definitions for sys/types.h
 typedef uint32_t      mode_t;
 typedef uint64_t      dev_t;
 typedef uint64_t      ino_t;
 typedef unsigned int  nlink_t;
 typedef int           pid_t;
 typedef unsigned int  id_t;
 typedef unsigned int  uid_t;
 typedef unsigned int  gid_t;
 typedef int64_t       off_t;
 typedef long          blksize_t;
 typedef int64_t       blkcnt_t;
 typedef uint64_t      fsblkcnt_t;
 typedef uint64_t      fsfilcnt_t;

-- for sys/stat.h
  struct stat {
   dev_t      st_dev;         /* Device */
   ino_t      st_ino; /* File serial number. */
   nlink_t    st_nlink;     /* Link count.  */
   mode_t     st_mode; /* File mode.  */
   uid_t      st_uid; /* User ID of the file's owner. */
   gid_t      st_gid; /* Group ID of the file's group.*/
   int        __pad0;
   dev_t      st_rdev; /* Device number, if device.  */
   off_t      st_size;     /* Size of file, in bytes.  */
   blksize_t  st_blksize; /* Optimal block size for I/O.  */
   blkcnt_t   st_blocks; /* Number 512-byte blocks allocated. */
   /* __USE_XOPEN2K8 */
   struct timespec st_atim; /* Time of last access.  */
   struct timespec st_mtim; /* Time of last modification.  */
   struct timespec st_ctim; /* Time of last status change.  */
   long   __unused[3];
  };
  /* luajit calls this */
  int __xstat(int ver, const char *path, struct stat *buf); 

-- lua stat function part
stat = function(path, buf) return ffi.C.__xstat(_STAT_VER, path, buf) end;

以上内容取自我的系统 C 头文件。现在 LuaJIT 调用是:

local buf = ffi.new("struct stat[1]")
local res = stat('main.c', buf)
ffi.cdef [[
 int printf(const char *fmt, ...);
]]
ffi.C.printf("size: %lu, ino: %lu, mode: %d\n", buf[0].st_size, buf[0].st_ino, buf[0].st_mode);

ffi.new 中的 struct stat[1] 是由 luajit 邮件列表推荐的。

更新

想法是调用linux __xtat。添加了声明。

__xstat 方法取自 https://github.com/Wiladams/LJIT2libc。否则C 标题中的定义对我来说太多了。

输出正常,直到出现st_mode 字段。该字段为零。我用C 语言进行了测试,一切顺利。所以问题在于 LuaJIT stat 给了我错误的结果。请告知该怎么做。花了一整天的时间在那个东西上。

【问题讨论】:

  • 我认为你应该在 LuaJIT 问题跟踪器(在 Github github.com/LuaJIT/LuaJIT)上开一张票。
  • 请说明你是如何声明stat函数的。
  • @EgorSkriptunoff 我错过了。进行了编辑。

标签: c lua luajit


【解决方案1】:

我在ffi.cdef 类型声明中犯了一些错别字。在luajit 邮件列表的用户的帮助下,问题现在解决了。快速简历:

  1. 获取在包含#include <sys/stat.h>C 源文件上调用的clang -E <some_c_file>.c 命令的输出。
  2. 做出正确的struct stat 定义。
  3. 我的系统stat 函数有太多级别的宏垃圾。最后,stat 函数调用了__xstat。我调用stat 函数的唯一明智方法是创建syscall
  4. 请记住 Lua print 函数不知道 cdata 类型。而printf 不知道Lua 类型。但是luajit 对如何来回转换常见数据类型有一个很好且简单的解释。

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2022-01-13
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2011-10-30
    相关资源
    最近更新 更多