Coverage Report

Created: 2023-09-15 06:20

/src/testdir/tests/luaL_loadbufferx_test.c
Line
Count
Source (jump to first uncovered line)
1
#include <stdint.h>
2
#include <stdlib.h> /* malloc, free */
3
#include <string.h> /* memcpy */
4
5
#include <lua.h>
6
#include <lualib.h>
7
#include <lauxlib.h>
8
9
/*
10
 * The main purpose of the test is testing Lua frontend (lexer, parser).
11
 * The test doesn't execute a loaded chunk to be quite fast.
12
 */
13
14
int
15
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
16
9.57k
{
17
9.57k
  lua_State *L = luaL_newstate();
18
9.57k
  if (L == NULL)
19
0
    return 0;
20
21
  /*
22
   * The string "mode" controls whether the chunk can be text or binary
23
   * (that is, a precompiled chunk). It may be the string "b" (only binary
24
   * chunks), "t" (only text chunks), or "bt" (both binary and text). The
25
   * default is "bt".
26
   * Lua runtime (at least PUC Rio Lua and LuaJIT) has bytecode and Lua
27
   * parsers. It is desired to test both parsers, however, in LuaJIT
28
   * bytecode parser failed with assertion:
29
   *
30
   * LuaJIT ASSERT lj_bcread.c:123: bcread_byte: buffer read overflow
31
   *
32
   * so in LuaJIT only text mode is used and therefore only text parser is
33
   * tested.
34
   */
35
#ifdef LUAJIT
36
  const char *mode = "t";
37
#else
38
9.57k
  const char *mode = "bt";
39
9.57k
#endif /* LUAJIT */
40
9.57k
  luaL_loadbufferx(L, (const char *)data, size, "fuzz", mode);
41
42
9.57k
  lua_settop(L, 0);
43
9.57k
  lua_close(L);
44
45
9.57k
  return 0;
46
9.57k
}