/src/testdir/tests/capi/lua_dump_test.c
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 <stdint.h> |
9 | | #include <stdlib.h> |
10 | | #include <string.h> |
11 | | |
12 | | #include "lua.h" |
13 | | #include "lauxlib.h" |
14 | | |
15 | | static int |
16 | | Writer(struct lua_State *L, const void *p, size_t size, void *ud) |
17 | 5.76M | { |
18 | | /** |
19 | | * We are not interested in produced parts of the binary chunk. |
20 | | * Test does not execute a binary chunk because it is focused |
21 | | * on a Lua runtime frontend. Thereby, high speed of fuzzing is achieved. |
22 | | */ |
23 | | |
24 | 5.76M | (void)L; |
25 | 5.76M | (void)p; |
26 | 5.76M | (void)ud; |
27 | | |
28 | | /** |
29 | | * The writer returns an error code: 0 means no errors; any other value |
30 | | * means an error and stops lua_dump from calling the writer again. |
31 | | */ |
32 | 5.76M | return 0; |
33 | 5.76M | } |
34 | | |
35 | | int |
36 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
37 | 11.9k | { |
38 | 11.9k | lua_State *L = luaL_newstate(); |
39 | 11.9k | assert(L != NULL); |
40 | | |
41 | 11.9k | char *str = malloc(size + 1); |
42 | 11.9k | if (str == NULL) |
43 | 0 | return 0; |
44 | 11.9k | memcpy(str, data, size); |
45 | 11.9k | str[size] = '\0'; |
46 | | |
47 | 11.9k | if (luaL_loadstring(L, str) != LUA_OK) { |
48 | 9.93k | goto end; |
49 | 9.93k | } |
50 | | |
51 | | #if LUA_VERSION_NUM < 503 |
52 | | lua_dump(L, Writer, NULL); |
53 | | #else /* Lua 5.3+ */ |
54 | 2.02k | lua_dump(L, Writer, NULL, 0); |
55 | 2.02k | #endif /* LUA_VERSION_NUM */ |
56 | | |
57 | 11.9k | end: |
58 | 11.9k | free(str); |
59 | 11.9k | lua_settop(L, 0); |
60 | 11.9k | lua_close(L); |
61 | | |
62 | 11.9k | return 0; |
63 | 2.02k | } |