Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Bronnikov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Evgeniy Temirgaleev <e.temirgaleev@tarantool.org>,
	Sergey Bronnikov <estetus@gmail.com>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush
Date: Tue, 1 Dec 2026 18:33:43 +0300	[thread overview]
Message-ID: <32822726-e822-492e-a4eb-110fd8bf6d85@tarantool.org> (raw)
In-Reply-To: <a22710f2-8844-4594-bb42-ee70aae254d2@tarantool.org>

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 <sergeyb@tarantool.org>
>>
>> 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
>
> <snipped>
>

  reply	other threads:[~2026-12-01 15:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 13:11 [Tarantool-patches] [PATCH luajit 0/3] Fix sysprof crash due to insufficient free disk space Sergey Bronnikov via Tarantool-patches
2026-11-02 13:12 ` [Tarantool-patches] [PATCH luajit 1/3] test: add unit tests for the write buffer Sergey Bronnikov via Tarantool-patches
2026-11-05 18:39   ` Sergey Kaplun via Tarantool-patches
2026-12-01 15:11     ` Sergey Bronnikov via Tarantool-patches
2026-11-11 15:24   ` Evgeniy Temirgaleev via Tarantool-patches
2026-12-01 14:33     ` Sergey Bronnikov via Tarantool-patches
2026-11-02 13:12 ` [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush Sergey Bronnikov via Tarantool-patches
2026-11-06 10:59   ` Sergey Kaplun via Tarantool-patches
2026-12-01 14:41     ` Sergey Bronnikov via Tarantool-patches
2026-11-11 16:34   ` Evgeniy Temirgaleev via Tarantool-patches
2026-12-01 15:33     ` Sergey Bronnikov via Tarantool-patches [this message]
2026-11-15 15:12   ` Evgeniy Temirgaleev via Tarantool-patches
2026-12-01 16:09     ` Sergey Bronnikov via Tarantool-patches
2026-11-02 13:12 ` [Tarantool-patches] [PATCH luajit 3/3] sysprof: release resources on a halted profiler Sergey Bronnikov via Tarantool-patches
2026-11-06 11:06   ` Sergey Kaplun via Tarantool-patches
2026-11-11 23:29   ` Evgeniy Temirgaleev via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=32822726-e822-492e-a4eb-110fd8bf6d85@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=e.temirgaleev@tarantool.org \
    --cc=estetus@gmail.com \
    --cc=sergeyb@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox