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 298496E238; Tue, 1 Dec 2026 18:33:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 298496E238 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1796139226; bh=+YlDRdSiezM0vaINgg5Kp4S152+OrraRpB57nPWt5rk=; 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=L7Ipc43cUTidW+aQ0JAZKFUgpwNgg5vBvo2NLssMkMS4iAK7yWp0ZzYGclMBexN17 1KpqMGKMKZPddRFZo4HcYk39QvAKSMrXeompsbUayK/94EYgN3uM9hbeBLOIF4rP8i h6XxUuVUCwbU7WXQvrlJdWfyZ1vsxT8iD4n8SA9Q= Received: from send149.i.mail.ru (send149.i.mail.ru [89.221.237.244]) (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 08CA26E238 for ; Tue, 1 Dec 2026 18:33:44 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 08CA26E238 Received: by exim-smtp-7cdd59bc4f-d4gk2 with esmtpa (envelope-from ) id 1xYPrT-000000001n8-1wcn; Tue, 01 Dec 2026 18:33:43 +0300 Message-ID: <32822726-e822-492e-a4eb-110fd8bf6d85@tarantool.org> Date: Tue, 1 Dec 2026 18:33:43 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: Evgeniy Temirgaleev , 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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9D183B3A831A946F46FE0521334C312F485DBADF385021586182A05F5380850406C1B96BDF7B067BF3DE06ABAFEAF6705DB0674100498EBB15869FD136C287ECBB6BD4AB1FA1FD2A3 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7D9C4478D0B876341EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB55337566D0A544BCAD4EC9EC7F2ECF3277849D2F45AD5286E272271E5BBF19AB5913B1D9389733CBF5DBD5E9D5E8D9A59859A8B65FF0BFC5AEE34BE6CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8090A508E0FED6299176DF2183F8FC7C0905A4887E4E532A7CD04E86FAF290E2DB606B96278B59C421DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B642416645EBD45DC2089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A580BCBB851D9F31365002B1117B3ED69670C370334798398954BB1175C6E7DD94823CB91A9FED034534781492E4B8EEAD47A3109F1ACFD409BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34CAB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D6594E2554D750253B0E928F0E6B0DFEB48EFF6DA3E861F9E999FB67F33E705F2DF8FE51AA920BD2B052B8341EE9D5BE9A0A014180AB7D0ACBBCA71DFE91EDB916CB26E38983E2FF4F97C7CEAA0681F5848F4C41F94D744909CE2512F26BEC029E55448553D2254B8D95CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXOcIETfglQORZ0zpDET4Zrk3igikrdHlWFmmZSEiMEUq3KofJT2Itw4= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11B2016E1EE16F36A263DE06ABAFEAF6705DB0674100498EBB1B7CBEF92542CD7C8795FA72BAB74744FC77752E0C033A69EA16A481184E8BB1C9B38E6EA4F046BE03A5DB60FBEB33A8A0DA7A0AF5A3A8387 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, Thanks for review! Please see comments below. Sergey On 11/11/26 19:34, Evgeniy Temirgaleev wrote: > Sergey! At the end of the letter for commit 1/3 I write a note on > lj_wbuf_addn() implementation. Actually, this note and question is > closer to this commit. > > Please, see. > > On 11/2/26 4:12 PM, 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 >> --- >>   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 >> @@ -51,11 +51,18 @@ void LJ_FASTCALL lj_wbuf_terminate(struct lj_wbuf >> *buf) >>     lj_wbuf_init(buf, NULL, NULL, NULL, 0); >>   } >>   -static LJ_AINLINE void wbuf_reserve(struct lj_wbuf *buf, size_t n) >> +/* >> +** Reserve n bytes in the buffer. Returns non-zero if the space is >> reserved >> +** and zero if the stream was stopped during the flush. In the >> latter case >> +** the writer may release the buffer (buf->buf == NULL), so it must >> not be >> +** used by the caller. >> +*/ >> +static LJ_AINLINE int wbuf_reserve(struct lj_wbuf *buf, size_t n) >>   { >>     lj_assertX(n <= buf->size, "wbuf overflow"); >>     if (LJ_UNLIKELY(wbuf_left(buf) < n)) >>       lj_wbuf_flush(buf); >> +  return !lj_wbuf_test_flag(buf, STREAM_STOP); >>   } >>     /* Writes a byte to the output buffer. */ >> @@ -63,7 +70,8 @@ void LJ_FASTCALL lj_wbuf_addbyte(struct lj_wbuf >> *buf, uint8_t b) >>   { >>     if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP))) >>       return; >> -  wbuf_reserve(buf, sizeof(b)); >> +  if (LJ_UNLIKELY(!wbuf_reserve(buf, sizeof(b)))) >> +    return; >>     *buf->pos++ = b; >>   } >>   @@ -72,7 +80,8 @@ void LJ_FASTCALL lj_wbuf_addu64(struct lj_wbuf >> *buf, uint64_t n) >>   { >>     if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP))) >>       return; >> -  wbuf_reserve(buf, LEB128_U64_MAXSIZE); >> +  if (LJ_UNLIKELY(!wbuf_reserve(buf, LEB128_U64_MAXSIZE))) >> +    return; > Is it OK, if the writer report an error only, the buffer content will > be discarded (not written), and we will success in 'pushing more data > to the stream buffer after a hole'? Updated, stops writing in case of IO error. Added as a separate commit, because it is not a part of fix, but followup. diff --git a/src/lj_memprof.c b/src/lj_memprof.c index 3df8e8fb..b4804eb3 100644 --- a/src/lj_memprof.c +++ b/src/lj_memprof.c @@ -241,7 +241,7 @@ static void *memprof_allocf(void *ud, void *ptr, size_t osize, size_t nsize)    }    /* Deinstrument memprof if required. */ -  if (LJ_UNLIKELY(lj_wbuf_test_flag(out, STREAM_STOP))) +  if (LJ_UNLIKELY(lj_wbuf_test_flag(out, STREAM_ERRIO | STREAM_STOP)))      lj_memprof_stop(mainthread(mp->g));    return nptr; diff --git a/src/lj_wbuf.c b/src/lj_wbuf.c index 0794b186..23a60bfe 100644 --- a/src/lj_wbuf.c +++ b/src/lj_wbuf.c @@ -53,22 +53,22 @@ void LJ_FASTCALL lj_wbuf_terminate(struct lj_wbuf *buf)  /*  ** Reserve n bytes in the buffer. Returns non-zero if the space is reserved -** and zero if the stream was stopped during the flush. In the latter case -** the writer may release the buffer (buf->buf == NULL), so it must not be -** used by the caller. +** and zero if the stream was stopped or an I/O error occurred during the +** flush. In the stopped case the writer may release the buffer +** (buf->buf == NULL), so it must not be used by the caller.  */  static LJ_AINLINE int wbuf_reserve(struct lj_wbuf *buf, size_t n)  {    lj_assertX(n <= buf->size, "wbuf overflow");    if (LJ_UNLIKELY(wbuf_left(buf) < n))      lj_wbuf_flush(buf); -  return !lj_wbuf_test_flag(buf, STREAM_STOP); +  return !lj_wbuf_test_flag(buf, STREAM_ERRIO | STREAM_STOP);  }  /* Writes a byte to the output buffer. */  void LJ_FASTCALL lj_wbuf_addbyte(struct lj_wbuf *buf, uint8_t b)  { -  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP))) +  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_ERRIO | STREAM_STOP)))      return;    if (LJ_UNLIKELY(!wbuf_reserve(buf, sizeof(b))))      return; @@ -78,7 +78,7 @@ void LJ_FASTCALL lj_wbuf_addbyte(struct lj_wbuf *buf, uint8_t b)  /* Writes an unsigned integer which is at most 64 bits long to the output. */  void LJ_FASTCALL lj_wbuf_addu64(struct lj_wbuf *buf, uint64_t n)  { -  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP))) +  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_ERRIO | STREAM_STOP)))      return;    if (LJ_UNLIKELY(!wbuf_reserve(buf, LEB128_U64_MAXSIZE)))      return; @@ -88,7 +88,7 @@ void LJ_FASTCALL lj_wbuf_addu64(struct lj_wbuf *buf, uint64_t n)  /* Writes n bytes from an arbitrary buffer src to the buffer. */  void lj_wbuf_addn(struct lj_wbuf *buf, const void *src, size_t n)  { -  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP))) +  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_ERRIO | STREAM_STOP)))      return;    /*    ** Very unlikely: We are told to write a large buffer at once. @@ -100,7 +100,7 @@ void lj_wbuf_addn(struct lj_wbuf *buf, const void *src, size_t n)      memcpy(buf->pos, src, left);      buf->pos += (ptrdiff_t)left;      lj_wbuf_flush(buf); -    if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP))) +    if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_ERRIO | STREAM_STOP)))        return;      src = (uint8_t *)src + (ptrdiff_t)left;      n -= left; @@ -127,7 +127,7 @@ void LJ_FASTCALL lj_wbuf_flush(struct lj_wbuf *buf)    const size_t len = wbuf_len(buf);    size_t written; -  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP))) +  if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_ERRIO | STREAM_STOP)))      return;    written = buf->writer((const void **)&buf->buf, len, buf->ctx); diff --git a/test/tarantool-c-tests/unit-wbuf.test.c b/test/tarantool-c-tests/unit-wbuf.test.c index 6f1b2a97..b3c93856 100644 --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -228,25 +228,21 @@ static int test_addn_pumped_through_buffer_partial(void *state)      UNUSED(state);      memset(payload, 'a', sizeof(payload)); -    const int writes = PAYLOAD_SIZE / BUFFER_SIZE; -    const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE;      /* -     * The writer reports only a half of each flushed buffer as -     * written, so the pumping loop loses the unwritten halves, -     * but it does not stop on STREAM_ERRIO and keeps pumping -     * the rest of the payload. +     * The writer reports only a half of the flushed buffer as +     * written, so the pumping loop stops on STREAM_ERRIO and +     * does not buffer the rest of the payload.       */      lj_wbuf_addn(&buf, payload, sizeof(payload)); -    assert_sizet_equal(ctx.flushes, writes); -    assert_sizet_equal(ctx.written, writes * (BUFFER_SIZE / 2)); +    assert_sizet_equal(ctx.flushes, 1); +    assert_sizet_equal(ctx.written, BUFFER_SIZE / 2);      /* Not all the data has been written. */      assert_true(ctx.written < sizeof(payload));      assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO));      assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));      assert_int_equal(lj_wbuf_errno(&buf), EIO); -    /* The tail that did not fit into a full buffer is buffered. */ -    assert_sizet_equal(wbuf_len(&buf), remains); -    assert_true(memcmp(mem, payload + writes * BUFFER_SIZE, remains) == 0); +    /* The stream is aborted, so nothing is left in the buffer. */ +    assert_sizet_equal(wbuf_len(&buf), 0);      return TEST_EXIT_SUCCESS;  } @@ -405,6 +401,34 @@ static int test_write_to_stopped_stream(void *state)      return TEST_EXIT_SUCCESS;  } +static int test_write_to_errio_stream(void *state) +{ +    WBUF_TEST_SETUP(WMODE_PARTIAL); + +    UNUSED(state); + +    lj_wbuf_addn(&buf, "xyz", 3); +    lj_wbuf_flush(&buf); +    /* A partial write sets STREAM_ERRIO but keeps the buffer. */ +    assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO)); +    assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP)); +    assert_sizet_equal(ctx.flushes, 1); +    assert_sizet_equal(ctx.written, 1); + +    /* Writes to a failed stream are ignored. */ +    lj_wbuf_addbyte(&buf, 0x7f); +    lj_wbuf_addu64(&buf, 128); +    lj_wbuf_addn(&buf, "xyz", 3); +    lj_wbuf_addstring(&buf, "xyz"); +    assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO)); +    assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP)); +    assert_sizet_equal(ctx.flushes, 1); +    assert_sizet_equal(ctx.written, 1); +    assert_sizet_equal(wbuf_len(&buf), 0); + +    return TEST_EXIT_SUCCESS; +} +  static int test_terminate(void *state)  {      WBUF_TEST_SETUP(WMODE_OK); @@ -534,6 +558,7 @@ int main(void)          test_unit_def(test_flush_partial_write),          test_unit_def(test_flush_stopped_stream),          test_unit_def(test_write_to_stopped_stream), +        test_unit_def(test_write_to_errio_stream),          test_unit_def(test_terminate),          test_unit_def(test_addbyte_failed_flush),          test_unit_def(test_addu64_failed_flush), > >>     buf->pos += (ptrdiff_t)lj_utils_write_uleb128(buf->pos, n); >>   } >>   @@ -91,11 +100,14 @@ void lj_wbuf_addn(struct lj_wbuf *buf, const >> void *src, size_t n) >>       memcpy(buf->pos, src, left); >>       buf->pos += (ptrdiff_t)left; >>       lj_wbuf_flush(buf); >> +    if (LJ_UNLIKELY(lj_wbuf_test_flag(buf, STREAM_STOP))) >> +      return; > What would happen if writer() reports an error, but doesn't stop a > stream? STREAM_ERRIO is now handled like `STREAM_STOP` for write admission: - wbuf_reserve() returns false on either flag; - lj_wbuf_addbyte(), lj_wbuf_addu64(), lj_wbuf_addn() (entry and the   pumping loop) and lj_wbuf_flush() bail out on STREAM_ERRIO | STREAM_STOP - lj_memprof.c deinstruments on STREAM_ERRIO | STREAM_STOP as well. So once a short write happens, no further bytes are pushed into the stream and no buffer tail is left behind. >>       src = (uint8_t *)src + (ptrdiff_t)left; >>       n -= left; >>     } >>   -  wbuf_reserve(buf, n); >> +  if (LJ_UNLIKELY(!wbuf_reserve(buf, n))) >> +    return; >>     memcpy(buf->pos, src, n); >>     buf->pos += (ptrdiff_t)n; >>   } >> 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 > > >