Skip to content

Commit

Permalink
fixup! Followup fix for embedded bytecode loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
igormunkin committed Feb 8, 2024
1 parent 0021bee commit bf239af
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions test/tarantool-c-tests/lj-549-lua-load.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
static const char *
bc_reader_with_endmark(lua_State *L, void *data, size_t *size)
{
UNUSED(L);
UNUSED(data);
*size = ~(size_t)0;

Expand Down Expand Up @@ -41,10 +42,6 @@ enum bc_emission_state {
EMIT_EOF,
};

typedef struct {
enum bc_emission_state state;
} dt;

/*
* Function returns the bytecode chunk on the first call and NULL
* and size equal to zero on the second call. Triggers the flag
Expand All @@ -54,43 +51,39 @@ static const char *
bc_reader_with_eof(lua_State *L, void *data, size_t *size)
{
UNUSED(L);
dt *test_data = (dt *)data;
if (test_data->state == EMIT_EOF) {
enum bc_emission_state *state = (enum bc_emission_state *)data;
if (*state == EMIT_EOF) {
*size = 0;
return NULL;
}

static char *bc_chunk = NULL;

/*
* Minimal size of a buffer with bytecode:
* signature (1 byte) and a bytecode itself (1 byte).
*/
size_t sz = 2;
free(bc_chunk);
bc_chunk = malloc(sz);
/*
* `lua_load` automatically detects whether the chunk is text
* or binary and loads it accordingly. We need a trace for
* *bytecode* input, so it is necessary to deceive a check in
* `lj_lex_setup`, that makes a sanity check and detects
* whether input is bytecode or text by the first char.
* Put `LUA_SIGNATURE[0]` at the beginning of the allocated
* region.
* nul-terminated region.
*/
bc_chunk[0] = LUA_SIGNATURE[0];
*size = sz;
test_data->state = EMIT_EOF;
static const char bc_chunk[] = {LUA_SIGNATURE[0], 0};
*size = sizeof(bc_chunk);
*state = EMIT_EOF;

return bc_chunk;
}

static int bc_loader_with_eof(void *test_state)
{
lua_State *L = test_state;
dt test_data = {0};
test_data.state = EMIT_BC;
int res = lua_load(L, bc_reader_with_eof, &test_data, "eof");
enum bc_emission_state state = EMIT_BC;
int res = lua_load(L, bc_reader_with_eof, &state, "eof");

/*
* Attempt of loading LuaJIT bytecode via Lua source
* loader function failed: `lj_lex_setup` routine throws
* LUA_ERRSYNTAX error with LJ_ERR_BCBAD payload.
*/
assert_true(res == LUA_ERRSYNTAX);
lua_settop(L, 0);

Expand Down

0 comments on commit bf239af

Please sign in to comment.