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 C5A786E445; Tue, 1 Dec 2026 17:41:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C5A786E445 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1796136092; bh=CRt0jKN49fmMSLM4hReOqGHSTsxLOoOKkwKejSaa77E=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=LAhBAyQNcDV/KTDQvqM167MwnCFQ1vdx+aRJNNts2m4Gqy44uZBburqIdgw0EsAWA rDoFdxBI19NtQkm2JFrrmbvK1BUoDxfesaoiDN++aW27gyMwFvIlyghpoUA88uzt4h N2EOoLMTibY4U/SSyJjNt9dRHOzhAVSBPE6bWNMk= Received: from send217.i.mail.ru (send217.i.mail.ru [95.163.59.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 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 BAD2D6E445 for ; Tue, 1 Dec 2026 17:41:31 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BAD2D6E445 Received: by exim-smtp-canary-10-6c7678fc95-gfrt7 with esmtpa (envelope-from ) id 1xYP2w-0000000031D-1kZP; Tue, 01 Dec 2026 17:41:31 +0300 Message-ID: <67e12ee6-902d-4215-81dc-552db1a67db3@tarantool.org> Date: Tue, 1 Dec 2026 17:41:30 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: Sergey Kaplun , Sergey Bronnikov References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9221F1A12F53D9B5DADF74A7097A5D101DD5887D5D869C39E182A05F5380850404222F3BCD345C4BA3DE06ABAFEAF67050352ACE0CCA6D553DCF9AFBAE8D0C9DC4A9DBE0811142006 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE79145AB6E9E75F07EEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB55337566D0A544BCAD4EC9EC56B3D05A0F55329F594475F91242AD0B8B9BDC57E89D75C4389733CBF5DBD5E9D5E8D9A59859A8B636DA1BED736F9328CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8090A508E0FED62991661749BA6B9773586AD39DBD4CF2FFBCD04E86FAF290E2DB606B96278B59C421DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B60425ED1B70270490089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A56C1A9FF3F1A1C2705002B1117B3ED696CEF00EF7706C0B7D22DFD5397F446790823CB91A9FED034534781492E4B8EEAD003C2D46C52F18F2BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE1918E10F71CB4DF9F96AB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659255A9B72FC156F1D1A4431BB98A71541C1C550F5960807AACBAC08D006FC594A1F98D9364199BA3EB8341EE9D5BE9A0A1CD7AAF7B4596182B6D8267E041E721AC83D79245BBE66A8C7CEAA0681F5848F4C41F94D744909CE2512F26BEC029E55448553D2254B8D95CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXOcIETfglQORZ0zpDET4Zrk3igikrdHlWHD4lS/Og6RIsl0Q2O/NArY= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11BF57B4C7F620E2CDE3DE06ABAFEAF67050352ACE0CCA6D553B7CBEF92542CD7C8795FA72BAB74744FC77752E0C033A69EA16A481184E8BB1C9B38E6EA4F046BE03A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush 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 Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Sergey, thanks for review! Please see comments below. Sergey On 11/6/26 13:59, Sergey Kaplun wrote: > Sergey, > Thanks for the fixing this issue! > Generally LGTM, with a minor nits regarding the tests below. > > On 02.11.26, Sergey Bronnikov wrote: >> From: Sergey Bronnikov >> >> A writer may release the stream buffer and set its pointer to NULL >> on an unrecoverable error, e.g., when the file system with the >> profile is full. The write buffer stops the stream in this case, >> but a caller of `wbuf_reserve()` still writes data using the >> released buffer. This leads to a null pointer dereference inside >> `lj_utils_write_uleb128()` when the flush is triggered while >> streaming a sample. >> >> Now `wbuf_reserve()` reports whether the stream was stopped during >> the flush. All the write functions skip writing in this case, >> including the loop pumping a large payload through the buffer. >> >> Fixes tarantool/tarantool#11620 > Typo: s/Fixes/Resolves/ > It will be fixed after submodule bump in the Tarantool. Fixed. > >> --- >> src/lj_wbuf.c | 20 +- >> .../gh-11620-sysprof-enospc-crash.test.c | 180 ++++++++++++++++++ >> test/tarantool-c-tests/unit-wbuf.test.c | 102 ++++++++++ >> 3 files changed, 298 insertions(+), 4 deletions(-) >> create mode 100644 test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c >> >> diff --git a/src/lj_wbuf.c b/src/lj_wbuf.c >> index 0001a02e..0794b186 100644 >> --- a/src/lj_wbuf.c >> +++ b/src/lj_wbuf.c > > >> diff --git a/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c b/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c >> new file mode 100644 >> index 00000000..0f5ffe53 >> --- /dev/null >> +++ b/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c >> @@ -0,0 +1,180 @@ >> +/* >> +** Testing of the sysprof behaviour when a stream write fails. >> +** >> +** A full file system makes the sysprof writer return an error and >> +** release the stream buffer. The profiler must stop the stream >> +** gracefully, return an error from luaM_sysprof_stop() and >> +** release the resources. >> +*/ > Minor: It's better to use > | /* > | * > | */ > Style of comments in the tests to be consistent with other codebase. > The comment style in the is inconsistent by my > mistake. Fixed: --- a/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c +++ b/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c @@ -1,11 +1,11 @@  /* -** Testing of the sysprof behaviour when a stream write fails. -** -** A full file system makes the sysprof writer return an error and -** release the stream buffer. The profiler must stop the stream -** gracefully, return an error from luaM_sysprof_stop() and -** release the resources. -*/ + * Testing of the sysprof behaviour when a stream write fails. + * + * A full file system makes the sysprof writer return an error and + * release the stream buffer. The profiler must stop the stream + * gracefully, return an error from luaM_sysprof_stop() and + * release the resources. + */  #include "lauxlib.h"  #include "lua.h" > >> + >> +#include "lauxlib.h" >> +#include "lua.h" >> + >> +/* Need for skipcond for OS and ARCH. */ >> +#include "lj_arch.h" >> + >> +#include "test.h" >> + >> +#if LJ_HASSYSPROF && !defined(LUAJIT_USE_VALGRIND) >> + >> +#include "lmisclib.h" >> +#include "utils.h" >> + >> +#include >> +#include >> +#include >> +#include >> +#include >> + >> +/* >> + * The buffer size is enough for the maximal ULEB128 value. >> + * A small buffer makes the profiler flush it right during the >> + * prologue or the first sample. >> + */ >> +#define STREAM_BUFFER_SIZE 10 >> + >> +/* Do not let the timer interfere with the manually emitted signals. */ > Nit: Comment line length is more than 66 symbols. Fixed: @@ -33,7 +33,10 @@   */  #define STREAM_BUFFER_SIZE 10 -/* Do not let the timer interfere with the manually emitted signals. */ +/* + * Do not let the timer interfere with the manually emitted + * signals. + */  #define SYSPROF_TEST_INTERVAL 1000  struct sysprof_ctx { > > > >> +static int sysprof_enospc_during_sampling(void *test_state) >> +{ >> + lua_State *L = test_state; >> + static uint8_t buf[STREAM_BUFFER_SIZE]; >> + struct luam_Sysprof_Options opt; >> + struct luam_Sysprof_Counters counters = {}; >> + >> + sysprof_test_configure(&opt, buf); >> + assert_true(luaM_sysprof_start(L, &opt) == PROFILE_SUCCESS); >> + assert_int_equal(ctx.on_stop_calls, 0); >> + >> + /* Let the next stream flush fail as if the disk is full. */ > Nit: Comment line length is more than 66 symbols. Fixed: --- a/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c +++ b/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c @@ -115,7 +115,9 @@ static int sysprof_enospc_during_sampling(void *test_state)         assert_true(luaM_sysprof_start(L, &opt) == PROFILE_SUCCESS);         assert_int_equal(ctx.on_stop_calls, 0); -       /* Let the next stream flush fail as if the disk is full. */ +       /* +        * Let the next stream flush fail as if the disk is full. +        */         ctx.fail = 1;         assert_int_equal(kill(getpid(), SIGPROF), 0); > >> + ctx.fail = 1; >> + assert_int_equal(kill(getpid(), SIGPROF), 0); >> + >> + /* The stream is stopped gracefully, without a crash. */ >> + errno = 0; >> + assert_int_equal(luaM_sysprof_stop(L), PROFILE_ERRIO); >> + assert_int_equal(errno, ENOSPC); >> + >> + /* Counters are still available. */ >> + assert_true(luaM_sysprof_report(&counters) == PROFILE_SUCCESS); >> + assert_true(counters.samples >= 1); >> + >> + /* The profiler is stopped. */ >> + assert_int_equal(luaM_sysprof_stop(L), PROFILE_ERRRUN); >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> +static int sysprof_enospc_on_start(void *test_state) >> +{ >> + lua_State *L = test_state; >> + static uint8_t buf[STREAM_BUFFER_SIZE]; >> + struct luam_Sysprof_Options opt; >> + >> + sysprof_test_configure(&opt, buf); >> + /* The very first flush fails, so the prologue cannot be streamed. */ > Nit: Comment line length is more than 66 symbols. Fixed: @@ -141,7 +143,10 @@ static int sysprof_enospc_on_start(void *test_state)         struct luam_Sysprof_Options opt;         sysprof_test_configure(&opt, buf); -       /* The very first flush fails, so the prologue cannot be streamed. */ +       /* +        * The very first flush fails, so the prologue cannot be +        * streamed. +        */         ctx.fail = 1;         errno = 0;         assert_int_equal(luaM_sysprof_start(L, &opt), PROFILE_ERRIO); > >> + ctx.fail = 1; >> + errno = 0; >> + assert_int_equal(luaM_sysprof_start(L, &opt), PROFILE_ERRIO); >> + assert_int_equal(errno, ENOSPC); >> + /* The start error path releases the profiler resources. */ > Nit: Comment line length is more than 66 symbols. Fixed:         ctx.fail = 1;         errno = 0;         assert_int_equal(luaM_sysprof_start(L, &opt), PROFILE_ERRIO);         assert_int_equal(errno, ENOSPC); -       /* The start error path releases the profiler resources. */ +       /* +        * The start error path releases the profiler resources. +        */         assert_int_equal(ctx.on_stop_calls, 1);         /* The profiler is not running. */ > >> + assert_int_equal(ctx.on_stop_calls, 1); >> + >> + /* The profiler is not running. */ >> + assert_int_equal(luaM_sysprof_stop(L), PROFILE_ERRRUN); >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> +#endif /* LJ_HASSYSPROF && !defined(LUAJIT_USE_VALGRIND) */ >> + >> +int main(void) >> +{ >> +#if LUAJIT_USE_VALGRIND >> + return skip_all("Disabled due to #10803"); >> +#elif !LJ_HASSYSPROF >> + return skip_all("Sysprof is disabled"); >> +#else >> + if (LUAJIT_OS != LUAJIT_OS_LINUX) >> + return skip_all("Sysprof is implemented for Linux only"); >> + if (LUAJIT_TARGET != LUAJIT_ARCH_X86 >> + && LUAJIT_TARGET != LUAJIT_ARCH_X64) >> + return skip_all("Sysprof is implemented for x86_64 only"); >> + >> + lua_State *L = utils_lua_init(); >> + >> + const struct test_unit tgroup[] = { >> + test_unit_def(sysprof_enospc_during_sampling), >> + test_unit_def(sysprof_enospc_on_start) >> + }; >> + const int test_result = test_run_group(tgroup, L); >> + utils_lua_close(L); >> + return test_result; >> +#endif /* LUAJIT_USE_VALGRIND */ >> +} >> diff --git a/test/tarantool-c-tests/unit-wbuf.test.c b/test/tarantool-c-tests/unit-wbuf.test.c >> index 4bee6243..c1b71ff3 100644 >> --- a/test/tarantool-c-tests/unit-wbuf.test.c >> +++ b/test/tarantool-c-tests/unit-wbuf.test.c >> @@ -367,6 +367,104 @@ static int test_terminate(void *state) >> ** this case, so the write functions must not touch the released >> ** buffer. >> */ >> +static int test_addbyte_failed_flush(void *state) >> +{ >> + uint8_t mem[BUFFER_SIZE]; >> + struct lj_wbuf buf; >> + struct writer_ctx ctx; >> + size_t i; >> + >> + UNUSED(state); >> + >> + writer_ctx_init(&ctx, WMODE_FAIL); >> + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); >> + >> + /* Fill the buffer without flushes. */ >> + for (i = 0; i < sizeof(mem); ++i) >> + lj_wbuf_addbyte(&buf, (uint8_t)i); >> + assert_sizet_equal(ctx.flushes, 0); >> + assert_sizet_equal(buf_len(&buf), sizeof(mem)); >> + >> + /* The buffer is full, so the byte below triggers a failed flush. */ > Nit: Comment line length is more than 66 symbols. Fixed: --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -395,7 +395,10 @@ static int test_addbyte_failed_flush(void *state)         assert_sizet_equal(ctx.flushes, 0);         assert_sizet_equal(buf_len(&buf), sizeof(mem)); -       /* The buffer is full, so the byte below triggers a failed flush. */ +       /* +        * The buffer is full, so the byte below triggers a failed +        * flush. +        */         lj_wbuf_addbyte(&buf, 0xaa);         assert_stream_stopped_with_error(&buf);         assert_sizet_equal(ctx.flushes, 1); > >> + lj_wbuf_addbyte(&buf, 0xaa); >> + assert_stream_stopped_with_error(&buf); >> + assert_sizet_equal(ctx.flushes, 1); >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> +static int test_addu64_failed_flush(void *state) >> +{ >> + uint8_t mem[BUFFER_SIZE]; >> + struct lj_wbuf buf; >> + struct writer_ctx ctx; >> + >> + UNUSED(state); >> + >> + writer_ctx_init(&ctx, WMODE_FAIL); >> + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); >> + >> + /* The maximal value takes exactly BUFFER_SIZE bytes. */ >> + lj_wbuf_addu64(&buf, UINT64_MAX); >> + assert_sizet_equal(buf_len(&buf), sizeof(mem)); >> + >> + /* There is no space left, so the next write triggers a failed flush. */ > Nit: Comment line length is more than 66 symbols. Fixed. > >> + lj_wbuf_addu64(&buf, 0); >> + assert_stream_stopped_with_error(&buf); >> + assert_sizet_equal(ctx.flushes, 1); >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> +static int test_addn_failed_flush(void *state) >> +{ >> + uint8_t mem[BUFFER_SIZE]; >> + uint8_t payload[BUFFER_SIZE]; >> + struct lj_wbuf buf; >> + struct writer_ctx ctx; >> + >> + UNUSED(state); >> + >> + memset(payload, 'a', sizeof(payload)); >> + writer_ctx_init(&ctx, WMODE_FAIL); >> + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); >> + >> + /* Fill the buffer without flushes. */ >> + lj_wbuf_addn(&buf, payload, sizeof(payload)); >> + assert_sizet_equal(ctx.flushes, 0); >> + >> + /* There is no space left, so the next write triggers a failed flush. */ > Nit: Comment line length is more than 66 symbols. Fixed. > >> + lj_wbuf_addn(&buf, "b", 1); >> + assert_stream_stopped_with_error(&buf); >> + assert_sizet_equal(ctx.flushes, 1); >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> +static int test_addn_failed_flush_in_loop(void *state) >> +{ >> + uint8_t mem[BUFFER_SIZE]; >> + uint8_t payload[PAYLOAD_SIZE]; >> + struct lj_wbuf buf; >> + struct writer_ctx ctx; >> + >> + UNUSED(state); >> + >> + memset(payload, 'a', sizeof(payload)); >> + writer_ctx_init(&ctx, WMODE_FAIL); >> + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); >> + >> + /* >> + * The payload does not fit into the buffer, so it is pumped through >> + * it. The flush fails in the middle of the pump loop. >> + */ > Nit: Comment line length is more than 66 symbols. Fixed: @@ -465,8 +474,9 @@ static int test_addn_failed_flush_in_loop(void *state)         lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));         /* -        * The payload does not fit into the buffer, so it is pumped through -        * it. The flush fails in the middle of the pump loop. +        * The payload does not fit into the buffer, so it is +        * pumped through it. The flush fails in the middle of the +        * pump loop.          */         lj_wbuf_addn(&buf, payload, sizeof(payload));         assert_stream_stopped_with_error(&buf); > >> + lj_wbuf_addn(&buf, payload, sizeof(payload)); >> + assert_stream_stopped_with_error(&buf); >> + assert_sizet_equal(ctx.flushes, 1); >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> int main(void) >> { >> const struct test_unit tgroup[] = { >> @@ -381,6 +479,10 @@ int main(void) >> test_unit_def(test_flush_stopped_stream), >> test_unit_def(test_write_to_stopped_stream), >> test_unit_def(test_terminate), >> + test_unit_def(test_addbyte_failed_flush), >> + test_unit_def(test_addu64_failed_flush), >> + test_unit_def(test_addn_failed_flush), > I suppose this 3 tests may be placed in one test_group, since they are: > 1) Init buffer > 2) Fill it until maximum capacity. > 3) Write a value with 1 flush and ENOSPC error. > Hence, I suppose initialization of the test may be unified. init is already unified (WBUF_TEST_SETUP macro) > > Feel free to ignore, though. I would leave as is, without grouping. > >> + test_unit_def(test_addn_failed_flush_in_loop), >> }; >> const int test_result = test_run_group(tgroup, NULL); >> >> -- >> 2.56.0 >>