Coverage Report

Created: 2023-09-30 06:14

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