/src/testdir/tests/capi/luaL_loadbufferx_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 <stdint.h> |
8 | | #include <stdlib.h> /* malloc, free */ |
9 | | #include <string.h> /* memcpy */ |
10 | | |
11 | | #include "lua.h" |
12 | | #include "lualib.h" |
13 | | #include "lauxlib.h" |
14 | | |
15 | | /* |
16 | | * The main purpose of the test is testing Lua frontend (lexer, parser). |
17 | | * The test doesn't execute a loaded chunk to be quite fast. |
18 | | */ |
19 | | |
20 | | int |
21 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
22 | 12.9k | { |
23 | 12.9k | lua_State *L = luaL_newstate(); |
24 | 12.9k | if (L == NULL) |
25 | 0 | return 0; |
26 | | |
27 | | /* |
28 | | * The string "mode" controls whether the chunk can be text or binary |
29 | | * (that is, a precompiled chunk). It may be the string "b" (only binary |
30 | | * chunks), "t" (only text chunks), or "bt" (both binary and text). The |
31 | | * default is "bt". |
32 | | * Lua runtime (at least PUC Rio Lua and LuaJIT) has bytecode and Lua |
33 | | * parsers. It is desired to test both parsers, however, in LuaJIT |
34 | | * bytecode parser failed with assertion: |
35 | | * |
36 | | * LuaJIT ASSERT lj_bcread.c:123: bcread_byte: buffer read overflow |
37 | | * |
38 | | * so in LuaJIT only text mode is used and therefore only text parser is |
39 | | * tested. |
40 | | */ |
41 | | #ifdef LUAJIT |
42 | | const char *mode = "t"; |
43 | | #else |
44 | 12.9k | const char *mode = "bt"; |
45 | 12.9k | #endif /* LUAJIT */ |
46 | 12.9k | luaL_loadbufferx(L, (const char *)data, size, "fuzz", mode); |
47 | | |
48 | 12.9k | lua_settop(L, 0); |
49 | 12.9k | lua_close(L); |
50 | | |
51 | 12.9k | return 0; |
52 | 12.9k | } |