Coverage Report

Created: 2025-08-09 06:54

/src/testdir/tests/capi/lua_load_test.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright 2023, Sergey Bronnikov.
5
 */
6
7
#include <assert.h>
8
#include <string.h>
9
10
#if defined(__cplusplus)
11
extern "C" {
12
#endif /* defined(__cplusplus) */
13
14
#include "lua.h"
15
#include "lualib.h"
16
#include "lauxlib.h"
17
18
#if defined(__cplusplus)
19
} /* extern "C" */
20
#endif /* defined(__cplusplus) */
21
22
#include <fuzzer/FuzzedDataProvider.h>
23
24
typedef struct {
25
  FuzzedDataProvider *fdp;
26
} dt;
27
28
static const char *
29
Reader(lua_State *L, void *data, size_t *size)
30
975k
{
31
975k
  dt *test_data = (dt *)data;
32
975k
  static char *buf = NULL;
33
34
975k
  FuzzedDataProvider *fdp = test_data->fdp;
35
975k
  uint8_t max_str_size = fdp->ConsumeIntegral<uint8_t>();
36
975k
  if (fdp->remaining_bytes() < max_str_size)
37
1.21k
    return NULL;
38
974k
  auto str = fdp->ConsumeRandomLengthString(max_str_size);
39
974k
  *size = str.size();
40
41
974k
  free(buf);
42
974k
  buf = (char *)malloc(*size);
43
974k
  assert(buf);
44
974k
  memcpy(buf, str.c_str(), *size);
45
46
974k
  return buf;
47
974k
}
48
49
extern "C" int
50
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
51
3.66k
{
52
3.66k
  lua_State *L = luaL_newstate();
53
3.66k
  if (L == NULL)
54
0
    return 0;
55
56
3.66k
  luaL_openlibs(L);
57
58
3.66k
  FuzzedDataProvider fdp(data, size);
59
3.66k
  dt test_data;
60
3.66k
  test_data.fdp = &fdp;
61
62
#if LUA_VERSION_NUM == 501
63
  int res = lua_load(L, Reader, &test_data, "libFuzzer");
64
#else /* Lua 5.3+ */
65
3.66k
  const char *mode = "t";
66
3.66k
  int res = lua_load(L, Reader, &test_data, "libFuzzer", mode);
67
3.66k
#endif /* LUA_VERSION_NUM */
68
3.66k
  if (res == LUA_OK) {
69
2.68k
    lua_pcall(L, 0, 0, 0);
70
2.68k
  }
71
72
3.66k
  lua_settop(L, 0);
73
3.66k
  lua_close(L);
74
75
3.66k
  return 0;
76
3.66k
}