Coverage Report

Created: 2023-09-30 06:14

/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
12.8k
{
17
12.8k
  lua_State *L = luaL_newstate();
18
12.8k
  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
12.8k
  const char *mode = "bt";
39
12.8k
#endif /* LUAJIT */
40
12.8k
  luaL_loadbufferx(L, (const char *)data, size, "fuzz", mode);
41
42
12.8k
  lua_settop(L, 0);
43
12.8k
  lua_close(L);
44
45
12.8k
  return 0;
46
12.8k
}