Coverage Report

Created: 2023-08-27 06:20

/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
35.1M
{
19
35.1M
  dt *test_data = (dt *)data;
20
35.1M
  static char *buf = NULL;
21
22
35.1M
  free(buf);
23
24
35.1M
  buf = malloc(test_data->sz);
25
35.1M
  *size = fread(buf, test_data->sz, 1, test_data->fd);
26
27
35.1M
  return buf;
28
35.1M
}
29
30
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
31
47
{
32
47
  lua_State *L = luaL_newstate();
33
47
  if (L == NULL)
34
0
    return 0;
35
36
47
  luaL_openlibs(L);
37
38
47
  FILE *fd = fmemopen((void *)data, size, "r");
39
47
  if (fd == NULL)
40
0
    return 0;
41
42
47
  dt test_data;
43
47
  test_data.fd = fd;
44
47
  test_data.sz = 1;
45
46
47
  const char *mode = "t";
47
47
  int res = lua_load(L, Reader, &test_data, "libFuzzer", mode);
48
47
  if (res == LUA_OK) {
49
35
    lua_pcall(L, 0, 0, 0);
50
35
  }
51
52
47
  if (test_data.fd != NULL)
53
47
    fclose(test_data.fd);
54
47
  lua_settop(L, 0);
55
47
  lua_close(L);
56
57
47
  return 0;
58
47
}