From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 75E1654CE65; Tue, 25 Jul 2023 19:37:10 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 75E1654CE65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1690303030; bh=i/VgdheGZHs3W9Gcd0aaw3ChQcBTiTclmi7zULedhX4=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=tv+BaYX9kFp30dHpQjsLZFQmcbAMMnamgBTp+TnjNtBzxTthEUmzFwxVWhxhX20Hy jk44JSMl5FIsMmZUtJYCS5PJFdUXldTPaEoNmLmS9bmrxDiesJlZK1S2W6zl6D38eU P8UlyKaRpqKeonkWEh3ygqB2P5YjjnhKgX012HeY= Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 407A954C63D for ; Tue, 25 Jul 2023 19:37:09 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 407A954C63D Received: by mail-lj1-f180.google.com with SMTP id 38308e7fff4ca-2b962c226ceso83365851fa.3 for ; Tue, 25 Jul 2023 09:37:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690303028; x=1690907828; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+0KKVquhM3pqzkGCjrxHjH9FHsTc5F1dnh9NU1xnw6E=; b=JOhz2S5M11qWg3uQxrrxPCYXB/qoW7h/Y9rbp2ngmWNrIPcWeAk3Mws/HUvTzhBj6f vWVrUdDCYjAMqx4nYT5jW3D5IBDMMOL84rU617fA8jLH61fGo/DxIfYDnp8vY17xVgqq ighYeV6MwfZRZnZ4vOuVrp1m5pM5tXAuvdIKNvKHULt86LtcB3Eldk3JseKPy62h312q ekRWn7lIoyPmxnwv8R8PoOIarCMJtCoeVmWBz+A4V30Adx7WjKl/OMba6APAT07RXsfX njhMYlVQ7hDDQLnX6YWncCIBtkSazWlsaIWHZa1hUY6O/jXWw1KYIyl5lAxZ+8XMJQHg Mqjw== X-Gm-Message-State: ABy/qLbi2wz78CcRHyVOXPmgspPK1IBOf6JriuowKbK5SjVRZpmc/L8y VCpQUHyd8xC/b/TC0HqK0H9RlC9ykoE= X-Google-Smtp-Source: APBJJlHBkg9ZRn678EMCBYlEVN0k6xS4p9uA0o4fIpsQorMsafi1YuESU+KcqN9YjXLNQj7QkDQTNQ== X-Received: by 2002:a2e:9dd4:0:b0:2b7:a72:103b with SMTP id x20-20020a2e9dd4000000b002b70a72103bmr8671728ljj.53.1690303028287; Tue, 25 Jul 2023 09:37:08 -0700 (PDT) Received: from pony.mail.msk ([2a00:1148:b0ba:16:37e9:bf80:d77e:ff11]) by smtp.gmail.com with ESMTPSA id e18-20020a2e9852000000b002b6d7682050sm3612656ljj.89.2023.07.25.09.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 09:37:07 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, Sergey Kaplun , max.kokryashkin@gmail.com Date: Tue, 25 Jul 2023 19:37:01 +0300 Message-Id: <738d30841b4fb39c094e3fe53317c8f7915e268a.1690300762.git.sergeyb@tarantool.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 2/2] Followup fix for embedded bytecode loader. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: sergeyb@tarantool.org (cherry-picked from commit e49863eda13d095b1a78fd4ca0fd3a6a9a17d782) Sergey Bronnikov: * added the partial test for this and a previous patch --- src/lj_lex.c | 1 + test/tarantool-c-tests/lj-549-lua_load.test.c | 146 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 test/tarantool-c-tests/lj-549-lua_load.test.c diff --git a/src/lj_lex.c b/src/lj_lex.c index 82e4ba6f..161d862e 100644 --- a/src/lj_lex.c +++ b/src/lj_lex.c @@ -51,6 +51,7 @@ static LJ_NOINLINE LexChar lex_more(LexState *ls) if (sz >= LJ_MAX_BUF) { if (sz != ~(size_t)0) lj_err_mem(ls->L); sz = ~(uintptr_t)0 - (uintptr_t)p; + if (sz >= LJ_MAX_BUF) sz = LJ_MAX_BUF-1; ls->endmark = 1; } ls->pe = p + sz; diff --git a/test/tarantool-c-tests/lj-549-lua_load.test.c b/test/tarantool-c-tests/lj-549-lua_load.test.c new file mode 100644 index 00000000..4fb144cf --- /dev/null +++ b/test/tarantool-c-tests/lj-549-lua_load.test.c @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "test.h" +#include "utils.h" + +/* Need for skipcond. */ +#include "lj_arch.h" + +/* Defined in lj_def.h. */ +#define LJ_MAX_MEM32 0x7fffff00 /* Max. 32 bit memory allocation. */ +#define LJ_MAX_BUF LJ_MAX_MEM32 /* Max. buffer length. */ + +#define UNUSED(x) ((void)(x)) + +/** + * Array with bytecode was generated using commands below: + * + * cat << EOF > a.lua + * local a = 1 + * EOF + * luajit -b a.lua a.h + */ +#define luaJIT_BC_sample_SIZE 22 +static const unsigned char luaJIT_BC_sample[] = { +27,76,74,2,2,15,2,0,1,0,0,0,2,41,0,1,0,75,0,1,0,0 +}; + +/** + * Function generates a huge chunk with "bytecode" with a size bigger than + * LJ_MAX_BUF. Generated chunk must enable endmark in a Lex state. + */ +static const char * +bc_reader_with_endmark(lua_State *L, void *data, size_t *size) +{ + UNUSED(data); + static char *bc_chunk = NULL; + free(bc_chunk); + + int bc_chunk_size = (size_t)0; + bc_chunk = malloc(bc_chunk_size); + assert(bc_chunk != NULL); + + /** + * Put a chunk with a valid bytecode to the beginning of allocated region. + * 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 first char. + * Strictly speaking it is enough to put LUA_SIGNATURE[0] as a first + * symbol in produced chunk. + */ + memcpy(bc_chunk, luaJIT_BC_sample, luaJIT_BC_sample_SIZE); + + *size = bc_chunk_size; + + return bc_chunk; +} + +static int bc_loader_with_endmark(void *test_state) +{ + lua_State *L = test_state; + void *ud = NULL; + int res = lua_load(L, bc_reader_with_endmark, ud, "endmark"); + + /* Make sure we passed condition with lj_err_mem in a function lex_more. */ + assert_true(res != LUA_ERRMEM); + + return TEST_EXIT_SUCCESS; +} + +enum bc_emission_state { + EMIT_BC, + EMIT_EOF, +}; + +typedef struct { + enum bc_emission_state state; +} dt; + +/** + * Function returns bytecode chunk on the first call and NULL and size equals + * to zero on the second call. Triggers END_OF_STREAM flag in a function + * lex_more. + */ +static const char * +bc_reader_with_eof(lua_State *L, void *data, size_t *size) +{ + UNUSED(data); + UNUSED(L); + dt *test_data = (dt *)data; + if (test_data->state == EMIT_EOF) { + *size = 0; + return NULL; + } + + char *bc_chunk = malloc(luaJIT_BC_sample_SIZE); + memcpy(bc_chunk, luaJIT_BC_sample, luaJIT_BC_sample_SIZE); + *size = luaJIT_BC_sample_SIZE; + + 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"); + assert_true(res = LUA_ERRSYNTAX); + if (res == LUA_OK) { + lua_pcall(L, 0, 0, 0); + } + + return TEST_EXIT_SUCCESS; +} + +int main(void) +{ + if (LJ_GC64 || !LUAJIT_ARCH_X64 || !LJ_TARGET_LINUX) + /** + * lua_load source code is common on all platforms, + * when bytecode is not portable. + * So test runs on Linux/x86_64 only and skipped on other + * platforms. + */ + return skip_all("Enabled on Linux/x86_64 with disabled GC64"); + + lua_State *L = utils_lua_init(); + const struct test_unit tgroup[] = { + test_unit_def(bc_loader_with_endmark), + test_unit_def(bc_loader_with_eof) + }; + + const int test_result = test_run_group(tgroup, L); + utils_lua_close(L); + return test_result; +} -- 2.34.1