Coverage Report

Created: 2023-09-15 06:18

/src/testdir/tests/lua_load_test.c
Line
Count
Source (jump to first uncovered line)
1
#include <stdint.h>
2
#include <stddef.h>
3
#include <string.h>
4
#include <stdlib.h>
5
#include <stdio.h>
6
7
#include <lua.h>
8
#include <lualib.h>
9
#include <lauxlib.h>
10
11
typedef struct {
12
  FILE *fd;
13
  size_t sz;
14
} dt;
15
16
static const char *
17
Reader(lua_State *L, void *data, size_t *size)
18
141M
{
19
141M
  dt *test_data = (dt *)data;
20
141M
  static char *buf = NULL;
21
22
141M
  free(buf);
23
24
141M
  buf = malloc(test_data->sz);
25
141M
  *size = fread(buf, test_data->sz, 1, test_data->fd);
26
27
141M
  return buf;
28
141M
}
29
30
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
31
2.25k
{
32
2.25k
  lua_State *L = luaL_newstate();
33
2.25k
  if (L == NULL)
34
0
    return 0;
35
36
2.25k
  luaL_openlibs(L);
37
38
2.25k
  FILE *fd = fmemopen((void *)data, size, "r");
39
2.25k
  if (fd == NULL)
40
0
    return 0;
41
42
2.25k
  dt test_data;
43
2.25k
  test_data.fd = fd;
44
2.25k
  test_data.sz = 1;
45
46
2.25k
  const char *mode = "t";
47
2.25k
  int res = lua_load(L, Reader, &test_data, "libFuzzer", mode);
48
2.25k
  if (res == LUA_OK) {
49
1.77k
    lua_pcall(L, 0, 0, 0);
50
1.77k
  }
51
52
2.25k
  if (test_data.fd != NULL)
53
2.25k
    fclose(test_data.fd);
54
2.25k
  lua_settop(L, 0);
55
2.25k
  lua_close(L);
56
57
2.25k
  return 0;
58
2.25k
}