Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit 0/3] Fix sysprof crash due to insufficient free disk space
@ 2026-11-02 13:11 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
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-11-02 13:11 UTC (permalink / raw)
  To: tarantool-patches, Sergey Kaplun, e.temirgaleev

The patch series adds a unit test for lj_wbuf.c file, a patch
with fix for sysprof crash with regression test and a fix for
release resources on a halted profiler.

Unfortunately, we don't have a unit test for lj_wbuf, so before
applying the fix for sysprof, I decided to add a test that
provides not just line code coverage, but also branch coverage.

The LuaJIT code coverage before (master):
https://github.com/tarantool/luajit/actions/runs/38898752231/job/116965510306

lines: 93.0% (21919 out of 23560)
functions: 95.0% (1448 out of 1524)
branches: 80.9% (12757 out of 15761)

The LuaJIT code coverage after (gh-11620-insufficient-free-disk-space-sysprof):

lines: 93.2% (21951 out of 23564)
functions: 95.1% (1449 out of 1524)
branches: 80.9% (12768 out of 15777)

CI: https://github.com/tarantool/luajit/tree/ligurio/gh-11620-insufficient-free-disk-space-sysprof

Related links:
- https://github.com/tarantool/tarantool/issues/11620
- https://coveralls.io/github/tarantool/luajit?branch=ligurio/gh-11620-insufficient-free-disk-space-sysprof

Sergey Bronnikov (3):
  test: add unit tests for the write buffer
  sysprof: fix crash on a failed stream flush
  sysprof: release resources on a halted profiler

 src/lj_sysprof.c                              |   4 +-
 src/lj_wbuf.c                                 |  20 +-
 .../gh-11620-sysprof-enospc-crash.test.c      | 182 +++++++
 test/tarantool-c-tests/unit-wbuf.test.c       | 490 ++++++++++++++++++
 4 files changed, 691 insertions(+), 5 deletions(-)
 create mode 100644 test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c
 create mode 100644 test/tarantool-c-tests/unit-wbuf.test.c

-- 
2.56.0


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [Tarantool-patches] [PATCH luajit 1/3] test: add unit tests for the write buffer
  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 ` Sergey Bronnikov via Tarantool-patches
  2026-11-05 18:39   ` Sergey Kaplun via Tarantool-patches
  2026-11-11 15:24   ` Evgeniy Temirgaleev 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-02 13:12 ` [Tarantool-patches] [PATCH luajit 3/3] sysprof: release resources on a halted profiler Sergey Bronnikov via Tarantool-patches
  2 siblings, 2 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-11-02 13:12 UTC (permalink / raw)
  To: tarantool-patches, Sergey Kaplun, e.temirgaleev

From: Sergey Bronnikov <sergeyb@tarantool.org>

The write buffer accumulates the profilers' output and passes it
to the writer on flush. It has no unit tests, so cover writing of
the data of all kinds, flushing of a full buffer, the explicit
flush and the error reporting.

Part of tarantool/tarantool#11620
---
 test/tarantool-c-tests/unit-wbuf.test.c | 388 ++++++++++++++++++++++++
 1 file changed, 388 insertions(+)
 create mode 100644 test/tarantool-c-tests/unit-wbuf.test.c

diff --git a/test/tarantool-c-tests/unit-wbuf.test.c b/test/tarantool-c-tests/unit-wbuf.test.c
new file mode 100644
index 00000000..4bee6243
--- /dev/null
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -0,0 +1,388 @@
+/*
+** Testing of the write buffer.
+**
+** The write buffer accumulates the profilers' output and passes
+** it to the writer on flush. The tests below cover writing of the
+** data of all kinds, flushing on overflow, the explicit flush and
+** the error reporting.
+*/
+
+#include <errno.h>
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "lj_wbuf.h"
+#include "test.h"
+
+/* Size of the write buffer used in the tests. */
+#define BUFFER_SIZE 10
+
+/* Size of the payload that does not fit into the write buffer. */
+#define PAYLOAD_SIZE (2 * BUFFER_SIZE + 5)
+
+/* What the writer does on flush. */
+enum writer_mode {
+	/* Report all the data as written. */
+	WMODE_OK,
+	/* Report a half of the data as written and keep the buffer. */
+	WMODE_PARTIAL,
+	/* Imitate an unrecoverable write error, e.g. ENOSPC. */
+	WMODE_FAIL,
+};
+
+/* Writer context for simulating different write outcomes. */
+struct writer_ctx {
+	/* What the writer does on flush. */
+	enum writer_mode mode;
+	/* Number of performed flushes. */
+	size_t flushes;
+	/* Number of bytes reported as written. */
+	size_t written;
+	/* Everything reported as written, in order of writing. */
+	uint8_t out[2 * PAYLOAD_SIZE];
+};
+
+static size_t test_writer(const void **buf_addr, size_t len, void *opt)
+{
+	struct writer_ctx *ctx = opt;
+	size_t n = len;
+
+	ctx->flushes++;
+	if (ctx->mode == WMODE_FAIL) {
+		errno = ENOSPC;
+		*buf_addr = NULL;
+		return 0;
+	}
+	if (ctx->mode == WMODE_PARTIAL) {
+		errno = EIO;
+		n = len / 2;
+	}
+	memcpy(ctx->out + ctx->written, *buf_addr, n);
+	ctx->written += n;
+	return n;
+}
+
+static void writer_ctx_init(struct writer_ctx *ctx, enum writer_mode mode)
+{
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->mode = mode;
+}
+
+static size_t buf_len(const struct lj_wbuf *buf)
+{
+	return (size_t)(buf->pos - buf->buf);
+}
+
+static void assert_stream_stopped_with_error(const struct lj_wbuf *buf)
+{
+	assert_true(lj_wbuf_test_flag(buf, STREAM_STOP));
+	assert_true(lj_wbuf_test_flag(buf, STREAM_ERRIO));
+	assert_int_equal(lj_wbuf_errno(buf), ENOSPC);
+	assert_ptr_equal(buf->buf, NULL);
+	assert_ptr_equal(buf->pos, NULL);
+}
+
+static int test_addbyte(void *state)
+{
+	uint8_t mem[BUFFER_SIZE];
+	struct lj_wbuf buf;
+	struct writer_ctx ctx;
+
+	UNUSED(state);
+
+	writer_ctx_init(&ctx, WMODE_OK);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	lj_wbuf_addbyte(&buf, 0x7f);
+	assert_sizet_equal(buf_len(&buf), 1);
+	assert_int_equal(mem[0], 0x7f);
+	assert_sizet_equal(ctx.flushes, 0);
+	assert_int_equal(lj_wbuf_errno(&buf), 0);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_addu64(void *state)
+{
+	uint8_t mem[BUFFER_SIZE];
+	struct lj_wbuf buf;
+	struct writer_ctx ctx;
+
+	UNUSED(state);
+
+	writer_ctx_init(&ctx, WMODE_OK);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	/* 128 is encoded as two bytes. */
+	lj_wbuf_addu64(&buf, 128);
+	assert_sizet_equal(buf_len(&buf), 2);
+	assert_int_equal(mem[0], 0x80);
+	assert_int_equal(mem[1], 0x01);
+	assert_sizet_equal(ctx.flushes, 0);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_addn(void *state)
+{
+	uint8_t mem[BUFFER_SIZE];
+	struct lj_wbuf buf;
+	struct writer_ctx ctx;
+
+	UNUSED(state);
+
+	writer_ctx_init(&ctx, WMODE_OK);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	lj_wbuf_addn(&buf, "xyz", 3);
+	assert_sizet_equal(buf_len(&buf), 3);
+	assert_true(memcmp(mem, "xyz", 3) == 0);
+	assert_sizet_equal(ctx.flushes, 0);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_addstring(void *state)
+{
+	uint8_t mem[BUFFER_SIZE];
+	struct lj_wbuf buf;
+	struct writer_ctx ctx;
+
+	UNUSED(state);
+
+	writer_ctx_init(&ctx, WMODE_OK);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	/* A string is a ULEB128 length followed by the payload. */
+	lj_wbuf_addstring(&buf, "xyz");
+	assert_sizet_equal(buf_len(&buf), 4);
+	assert_int_equal(mem[0], 3);
+	assert_true(memcmp(mem + 1, "xyz", 3) == 0);
+	assert_sizet_equal(ctx.flushes, 0);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_addn_pumped_through_buffer(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_OK);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	/*
+	 * The payload does not fit into the buffer, so it is
+	 * pumped through it. Two full buffers are flushed, the
+	 * rest stays in the buffer.
+	 */
+	lj_wbuf_addn(&buf, payload, sizeof(payload));
+	assert_sizet_equal(ctx.flushes, 2);
+	assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE);
+	assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0);
+	assert_sizet_equal(buf_len(&buf), PAYLOAD_SIZE - 2 * BUFFER_SIZE);
+	assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE,
+			   PAYLOAD_SIZE - 2 * BUFFER_SIZE) == 0);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_flush_on_overflow(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_OK);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	/* A byte written to a full buffer is preceded by a flush. */
+	lj_wbuf_addn(&buf, payload, sizeof(payload));
+	assert_sizet_equal(buf_len(&buf), sizeof(mem));
+	lj_wbuf_addbyte(&buf, 0xaa);
+	assert_sizet_equal(ctx.flushes, 1);
+	assert_sizet_equal(ctx.written, sizeof(mem));
+	assert_sizet_equal(buf_len(&buf), 1);
+	assert_int_equal(mem[0], 0xaa);
+
+	/* The same for an ULEB128 value written to a full buffer. */
+	lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
+	assert_sizet_equal(buf_len(&buf), sizeof(mem));
+	lj_wbuf_addu64(&buf, 0);
+	assert_sizet_equal(ctx.flushes, 2);
+	assert_sizet_equal(ctx.written, 2 * sizeof(mem));
+	assert_sizet_equal(buf_len(&buf), 1);
+
+	/* And for a data block written to a full buffer. */
+	lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
+	assert_sizet_equal(buf_len(&buf), sizeof(mem));
+	lj_wbuf_addn(&buf, payload, 1);
+	assert_sizet_equal(ctx.flushes, 3);
+	assert_sizet_equal(ctx.written, 3 * sizeof(mem));
+	assert_sizet_equal(buf_len(&buf), 1);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_flush(void *state)
+{
+	uint8_t mem[BUFFER_SIZE];
+	struct lj_wbuf buf;
+	struct writer_ctx ctx;
+
+	UNUSED(state);
+
+	writer_ctx_init(&ctx, WMODE_OK);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	lj_wbuf_addn(&buf, "xyz", 3);
+	lj_wbuf_flush(&buf);
+	assert_sizet_equal(ctx.flushes, 1);
+	assert_sizet_equal(ctx.written, 3);
+	assert_true(memcmp(ctx.out, "xyz", 3) == 0);
+	/* The buffer is empty after a successful flush. */
+	assert_sizet_equal(buf_len(&buf), 0);
+	assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));
+
+	/* The buffer is usable after the flush. */
+	lj_wbuf_addbyte(&buf, 0x7f);
+	assert_sizet_equal(buf_len(&buf), 1);
+	assert_int_equal(mem[0], 0x7f);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_flush_partial_write(void *state)
+{
+	uint8_t mem[BUFFER_SIZE];
+	struct lj_wbuf buf;
+	struct writer_ctx ctx;
+
+	UNUSED(state);
+
+	writer_ctx_init(&ctx, WMODE_PARTIAL);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	lj_wbuf_addn(&buf, "xyz", 3);
+	lj_wbuf_flush(&buf);
+	/* A partial write is reported as an error. */
+	assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO));
+	assert_int_equal(lj_wbuf_errno(&buf), EIO);
+	/* The writer keeps the buffer, so the stream is not stopped. */
+	assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));
+	assert_ptr_equal(buf.buf, mem);
+	/* The unwritten data is dropped. */
+	assert_sizet_equal(buf_len(&buf), 0);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_flush_stopped_stream(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));
+
+	lj_wbuf_addn(&buf, "xyz", 3);
+	lj_wbuf_flush(&buf);
+	assert_stream_stopped_with_error(&buf);
+	assert_sizet_equal(ctx.flushes, 1);
+
+	/* A stopped stream is not flushed again. */
+	lj_wbuf_flush(&buf);
+	assert_sizet_equal(ctx.flushes, 1);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_write_to_stopped_stream(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));
+
+	lj_wbuf_addn(&buf, "xyz", 3);
+	lj_wbuf_flush(&buf);
+	assert_stream_stopped_with_error(&buf);
+
+	/* Writes to a stopped 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_stream_stopped_with_error(&buf);
+	assert_sizet_equal(ctx.flushes, 1);
+	assert_sizet_equal(ctx.written, 0);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_terminate(void *state)
+{
+	uint8_t mem[BUFFER_SIZE];
+	struct lj_wbuf buf;
+	struct writer_ctx ctx;
+
+	UNUSED(state);
+
+	writer_ctx_init(&ctx, WMODE_OK);
+	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
+
+	lj_wbuf_terminate(&buf);
+	assert_true(buf.writer == NULL);
+	assert_ptr_equal(buf.ctx, NULL);
+	assert_ptr_equal(buf.buf, NULL);
+	assert_ptr_equal(buf.pos, NULL);
+	assert_sizet_equal(buf.size, 0);
+	assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));
+	assert_int_equal(lj_wbuf_errno(&buf), 0);
+
+	return TEST_EXIT_SUCCESS;
+}
+
+/*
+** A writer may release the buffer and set *data to NULL on an
+** unrecoverable error (e.g. ENOSPC). The stream is stopped in
+** this case, so the write functions must not touch the released
+** buffer.
+*/
+int main(void)
+{
+	const struct test_unit tgroup[] = {
+		test_unit_def(test_addbyte),
+		test_unit_def(test_addu64),
+		test_unit_def(test_addn),
+		test_unit_def(test_addstring),
+		test_unit_def(test_addn_pumped_through_buffer),
+		test_unit_def(test_flush_on_overflow),
+		test_unit_def(test_flush),
+		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_terminate),
+	};
+	const int test_result = test_run_group(tgroup, NULL);
+
+	return test_result;
+}
-- 
2.56.0


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush
  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-02 13:12 ` Sergey Bronnikov via Tarantool-patches
  2026-11-06 10:59   ` Sergey Kaplun via Tarantool-patches
                     ` (2 more replies)
  2026-11-02 13:12 ` [Tarantool-patches] [PATCH luajit 3/3] sysprof: release resources on a halted profiler Sergey Bronnikov via Tarantool-patches
  2 siblings, 3 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-11-02 13:12 UTC (permalink / raw)
  To: tarantool-patches, Sergey Kaplun, e.temirgaleev

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;
   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;
     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
@@ -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.
+*/
+
+#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 <errno.h>
+#include <signal.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+/*
+ * 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. */
+#define SYSPROF_TEST_INTERVAL 1000
+
+struct sysprof_ctx {
+	/* Simulate write errors. */
+	int fail;
+	/* Number of on_stop callback calls. */
+	int on_stop_calls;
+};
+
+static struct sysprof_ctx ctx;
+
+/*
+ * The profiler skips the frames up to
+ * SYSPROF_HANDLER_STACK_DEPTH (6) while streaming the host stack,
+ * so provide the frames after that boundary.
+ */
+#define SYSPROF_TEST_FRAME_START 7
+#define SYSPROF_TEST_FRAME_COUNT 4
+
+static size_t failing_writer(const void **buf_addr, size_t len, void *opt)
+{
+	struct sysprof_ctx *ctx = opt;
+
+	if (ctx->fail) {
+		errno = ENOSPC;
+		*buf_addr = NULL;
+		return 0;
+	}
+
+	return len;
+}
+
+static int on_stop(void *opt, uint8_t *buf)
+{
+	struct sysprof_ctx *ctx = opt;
+
+	UNUSED(buf);
+	ctx->on_stop_calls++;
+	return 0;
+}
+
+static void backtracer(void *(*frame_writer)(int, void *))
+{
+	int frame_no;
+
+	for (frame_no = SYSPROF_TEST_FRAME_START;
+	     frame_no < SYSPROF_TEST_FRAME_START + SYSPROF_TEST_FRAME_COUNT;
+	     ++frame_no)
+		frame_writer(frame_no, (void *)(uintptr_t)frame_no);
+}
+
+static void sysprof_test_configure(struct luam_Sysprof_Options *opt,
+				   uint8_t *buf)
+{
+	memset(&ctx, 0, sizeof(ctx));
+	memset(opt, 0, sizeof(*opt));
+	opt->mode = LUAM_SYSPROF_CALLGRAPH;
+	opt->interval = SYSPROF_TEST_INTERVAL;
+	opt->buf = buf;
+	opt->len = STREAM_BUFFER_SIZE;
+	opt->ctx = &ctx;
+
+	assert_true(luaM_sysprof_set_writer(failing_writer) == PROFILE_SUCCESS);
+	assert_true(luaM_sysprof_set_on_stop(on_stop) == PROFILE_SUCCESS);
+	assert_true(luaM_sysprof_set_backtracer(backtracer) == PROFILE_SUCCESS);
+}
+
+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. */
+	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. */
+	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. */
+	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. */
+	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. */
+	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. */
+	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.
+	 */
+	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),
+		test_unit_def(test_addn_failed_flush_in_loop),
 	};
 	const int test_result = test_run_group(tgroup, NULL);
 
-- 
2.56.0


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [Tarantool-patches] [PATCH luajit 3/3] sysprof: release resources on a halted profiler
  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-02 13:12 ` [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush Sergey Bronnikov via Tarantool-patches
@ 2026-11-02 13:12 ` 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
  2 siblings, 2 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-11-02 13:12 UTC (permalink / raw)
  To: tarantool-patches, Sergey Kaplun, e.temirgaleev

From: Sergey Bronnikov <sergeyb@tarantool.org>

If the stream fails (e.g., because of ENOSPC), the profiler moves
to the SPS_HALT state. `lj_sysprof_stop()` returns PROFILE_ERRIO
but does not call the `on_stop` callback, so the profile context
and the output file descriptor are leaked.

Call `on_stop` when the halted profiler is stopped, as it is
already done when starting fails or when the stream is stopped in
a regular way. Preserve the saved errno because `on_stop` may
change it.

Follows up tarantool/tarantool#11620
---
 src/lj_sysprof.c                                            | 4 +++-
 test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/lj_sysprof.c b/src/lj_sysprof.c
index 99bcc088..667c93a8 100644
--- a/src/lj_sysprof.c
+++ b/src/lj_sysprof.c
@@ -509,9 +509,11 @@ int lj_sysprof_stop(lua_State *L)
   lj_profile_timer_stop(&sp->timer);
 
   if (SPS_HALT == sp->state) {
-    errno = sp->saved_errno;
+    const int saved_errno = sp->saved_errno;
     sp->state = SPS_IDLE;
     /* wbuf was terminated when error occurred. */
+    sp->on_stop(sp->opt.ctx, sp->out.buf);
+    errno = saved_errno;
     return PROFILE_ERRIO;
   }
 
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
index 0f5ffe53..3c9ac05d 100644
--- a/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c
+++ b/test/tarantool-c-tests/gh-11620-sysprof-enospc-crash.test.c
@@ -120,6 +120,8 @@ static int sysprof_enospc_during_sampling(void *test_state)
 	errno = 0;
 	assert_int_equal(luaM_sysprof_stop(L), PROFILE_ERRIO);
 	assert_int_equal(errno, ENOSPC);
+	/* The error path releases the profiler resources. */
+	assert_int_equal(ctx.on_stop_calls, 1);
 
 	/* Counters are still available. */
 	assert_true(luaM_sysprof_report(&counters) == PROFILE_SUCCESS);
-- 
2.56.0


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 1/3] test: add unit tests for the write buffer
  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
  1 sibling, 1 reply; 16+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-05 18:39 UTC (permalink / raw)
  To: Sergey Bronnikov; +Cc: tarantool-patches

Hi, Sergey!
Thanks for the patch and painstaking work with coverage of wbuf module!
Generally LGTM with minor suggestions below.

On 02.11.26, Sergey Bronnikov wrote:
> From: Sergey Bronnikov <sergeyb@tarantool.org>
> 
> The write buffer accumulates the profilers' output and passes it
> to the writer on flush. It has no unit tests, so cover writing of
> the data of all kinds, flushing of a full buffer, the explicit
> flush and the error reporting.
> 
> Part of tarantool/tarantool#11620

Minor: I'm not sure that this is the part of this issue. Maybe
| Relates to
is more appropriate.

> ---
>  test/tarantool-c-tests/unit-wbuf.test.c | 388 ++++++++++++++++++++++++
>  1 file changed, 388 insertions(+)
>  create mode 100644 test/tarantool-c-tests/unit-wbuf.test.c
> 
> diff --git a/test/tarantool-c-tests/unit-wbuf.test.c b/test/tarantool-c-tests/unit-wbuf.test.c
> new file mode 100644
> index 00000000..4bee6243
> --- /dev/null
> +++ b/test/tarantool-c-tests/unit-wbuf.test.c
> @@ -0,0 +1,388 @@
> +/*
> +** Testing of the write buffer.
> +**
> +** The write buffer accumulates the profilers' output and passes
> +** it to the writer on flush. The tests below cover writing of the
> +** data of all kinds, flushing on overflow, the explicit flush and
> +** the error reporting.

Minor: It is better to use
| /*
|  *
|  */

Comments here as in the most C/C++ code in the tests.
The inconsistent comment style in the <unit-leb128.test.c> is my fault
and should be fixed.

> +*/
> +
> +#include <errno.h>
> +
> +#include <stddef.h>
> +#include <stdint.h>
> +#include <string.h>
> +
> +#include "lj_wbuf.h"
> +#include "test.h"
> +
> +/* Size of the write buffer used in the tests. */
> +#define BUFFER_SIZE 10
> +
> +/* Size of the payload that does not fit into the write buffer. */
> +#define PAYLOAD_SIZE (2 * BUFFER_SIZE + 5)
> +
> +/* What the writer does on flush. */
> +enum writer_mode {
> +	/* Report all the data as written. */
> +	WMODE_OK,
> +	/* Report a half of the data as written and keep the buffer. */

Nit: Comment length is more than 66 symbols.
Here and in:
* `test_flush_partial_write()`
* `test_addbyte_failed_flush()`
* `test_addu64_failed_flush()`
* `test_addn_failed_flush()`
* `test_addn_failed_flush_in_loop()`

> +	WMODE_PARTIAL,
> +	/* Imitate an unrecoverable write error, e.g. ENOSPC. */
> +	WMODE_FAIL,
> +};
> +
> +/* Writer context for simulating different write outcomes. */
> +struct writer_ctx {
> +	/* What the writer does on flush. */
> +	enum writer_mode mode;
> +	/* Number of performed flushes. */
> +	size_t flushes;
> +	/* Number of bytes reported as written. */
> +	size_t written;
> +	/* Everything reported as written, in order of writing. */
> +	uint8_t out[2 * PAYLOAD_SIZE];

Why do we need 2x size from the PAYLOAD here? Please add the comment.

> +};
> +
> +static size_t test_writer(const void **buf_addr, size_t len, void *opt)
> +{
> +	struct writer_ctx *ctx = opt;
> +	size_t n = len;
> +
> +	ctx->flushes++;

Minor: Please add the comment that writer function is called only by
`lj_wbuf_flush()` before this increment.

> +	if (ctx->mode == WMODE_FAIL) {
> +		errno = ENOSPC;
> +		*buf_addr = NULL;
> +		return 0;
> +	}
> +	if (ctx->mode == WMODE_PARTIAL) {

Minor: s/if/else if/

> +		errno = EIO;
> +		n = len / 2;
> +	}
> +	memcpy(ctx->out + ctx->written, *buf_addr, n);
> +	ctx->written += n;
> +	return n;
> +}
> +

<snipped>

> +
> +static void assert_stream_stopped_with_error(const struct lj_wbuf *buf)

I suggest using macro instead for such helpers to preserve the line of
original assertion check:

So instead of:
| not ok 9 - test_flush_stopped_stream
|   ---
|   location:     test/tarantool-c-tests/unit-wbuf.test.c:80
|   condition_failed:     'lj_wbuf_test_flag(buf, STREAM_STOP)'

We have:
| not ok 9 - test_flush_stopped_stream
|   ---
|   location:     test/tarantool-c-tests/unit-wbuf.test.c:304
|   condition_failed:     'lj_wbuf_test_flag((&buf), STREAM_STOP)'

> +{
> +	assert_true(lj_wbuf_test_flag(buf, STREAM_STOP));
> +	assert_true(lj_wbuf_test_flag(buf, STREAM_ERRIO));
> +	assert_int_equal(lj_wbuf_errno(buf), ENOSPC);

Since this precise assertion, I suggest naming the helper function/macro
something like `assert_wbuf_stopped_enospc()`.


> +	assert_ptr_equal(buf->buf, NULL);
> +	assert_ptr_equal(buf->pos, NULL);
> +}
> +
> +static int test_addbyte(void *state)

<snipped>


> +static int test_addstring(void *state)
> +{

<snipped>

> +}
> +
> +static int test_addn_pumped_through_buffer(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_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	/*
> +	 * The payload does not fit into the buffer, so it is
> +	 * pumped through it. Two full buffers are flushed, the
> +	 * rest stays in the buffer.
> +	 */
> +	lj_wbuf_addn(&buf, payload, sizeof(payload));
> +	assert_sizet_equal(ctx.flushes, 2);

This `2` may be not very obvious.
We may declare constant in this function before the comment above:

| const int writes = PAYLOAD_SIZE / BUFFER_SIZE;

> +	assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE);
> +	assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0);
> +	assert_sizet_equal(buf_len(&buf), PAYLOAD_SIZE - 2 * BUFFER_SIZE);

It is better to add the variable:

| const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE;

> +	assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE,
> +			   PAYLOAD_SIZE - 2 * BUFFER_SIZE) == 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_flush_on_overflow(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_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	/* A byte written to a full buffer is preceded by a flush. */
> +	lj_wbuf_addn(&buf, payload, sizeof(payload));
> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
> +	lj_wbuf_addbyte(&buf, 0xaa);
> +	assert_sizet_equal(ctx.flushes, 1);
> +	assert_sizet_equal(ctx.written, sizeof(mem));
> +	assert_sizet_equal(buf_len(&buf), 1);
> +	assert_int_equal(mem[0], 0xaa);

I suggest splitting this 1 test into 3 subtests, since they have the
similar logic:
1) Init buffer.
2) Write something without flushes.
3) Write something with a flush.

It helps to avoid the specific `ctx.out` size and makes checks for
`flushes`, `written` and `buf_len()` similar.

> +
> +	/* The same for an ULEB128 value written to a full buffer. */
> +	lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
> +	lj_wbuf_addu64(&buf, 0);
> +	assert_sizet_equal(ctx.flushes, 2);
> +	assert_sizet_equal(ctx.written, 2 * sizeof(mem));
> +	assert_sizet_equal(buf_len(&buf), 1);

Looks like there should be a check for the content as for the 0xaa
byte.

> +
> +	/* And for a data block written to a full buffer. */
> +	lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
> +	lj_wbuf_addn(&buf, payload, 1);
> +	assert_sizet_equal(ctx.flushes, 3);
> +	assert_sizet_equal(ctx.written, 3 * sizeof(mem));
> +	assert_sizet_equal(buf_len(&buf), 1);

Looks like there should be a check for the content as for the 0xaa
byte.

> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_flush(void *state)

I suggest renaming this as `test_flush_and_write()` or something similar.
Otherwise, the difference between this test and the test above is
unclear.

> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	lj_wbuf_addn(&buf, "xyz", 3);
> +	lj_wbuf_flush(&buf);
> +	assert_sizet_equal(ctx.flushes, 1);
> +	assert_sizet_equal(ctx.written, 3);
> +	assert_true(memcmp(ctx.out, "xyz", 3) == 0);
> +	/* The buffer is empty after a successful flush. */
> +	assert_sizet_equal(buf_len(&buf), 0);
> +	assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));
> +
> +	/* The buffer is usable after the flush. */
> +	lj_wbuf_addbyte(&buf, 0x7f);
> +	assert_sizet_equal(buf_len(&buf), 1);
> +	assert_int_equal(mem[0], 0x7f);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_flush_partial_write(void *state)
> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_PARTIAL);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	lj_wbuf_addn(&buf, "xyz", 3);
> +	lj_wbuf_flush(&buf);
> +	/* A partial write is reported as an error. */
> +	assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO));
> +	assert_int_equal(lj_wbuf_errno(&buf), EIO);
> +	/* The writer keeps the buffer, so the stream is not stopped. */
> +	assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));
> +	assert_ptr_equal(buf.buf, mem);
> +	/* The unwritten data is dropped. */
> +	assert_sizet_equal(buf_len(&buf), 0);

I'm not sure that we should fixate this policy. Generally there is no
guarantee what has happened with data. Except that it is not written
successfully.
Feel free to ignore.

> +
> +	return TEST_EXIT_SUCCESS;
> +}

<snipped>

> +/*
> +** A writer may release the buffer and set *data to NULL on an
> +** unrecoverable error (e.g. ENOSPC). The stream is stopped in
> +** this case, so the write functions must not touch the released
> +** buffer.
> +*/

Minor: It is better to use
| /*
|  *
|  */

Comments here as in the most C/C++ code in the tests.
The inconsistent comment style in the <unit-leb128.test.c> is my fault
and should be fixed.

> +int main(void)
> +{
> +	const struct test_unit tgroup[] = {
> +		test_unit_def(test_addbyte),
> +		test_unit_def(test_addu64),
> +		test_unit_def(test_addn),
> +		test_unit_def(test_addstring),
> +		test_unit_def(test_addn_pumped_through_buffer),
> +		test_unit_def(test_flush_on_overflow),
> +		test_unit_def(test_flush),
> +		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_terminate),
> +	};
> +	const int test_result = test_run_group(tgroup, NULL);

Minor: Why do we need this additional variable?

> +
> +	return test_result;
> +}
> -- 
> 2.56.0
> 

-- 
Best regards,
Sergey Kaplun

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush
  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-11-15 15:12   ` Evgeniy Temirgaleev via Tarantool-patches
  2 siblings, 1 reply; 16+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-06 10:59 UTC (permalink / raw)
  To: Sergey Bronnikov; +Cc: tarantool-patches

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 <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

Typo: s/Fixes/Resolves/
It will be fixed after submodule bump in the Tarantool.

> ---
>  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

<snipped>

> 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 <unit-leb128.test.c> is inconsistent by my
mistake.


> +
> +#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 <errno.h>
> +#include <signal.h>
> +#include <stdint.h>
> +#include <string.h>
> +#include <unistd.h>
> +
> +/*
> + * 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.

<snipped>

> +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.

> +	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.

> +	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.

> +	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.

> +	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.

> +	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.

> +	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.

> +	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.

Feel free to ignore, though.

> +		test_unit_def(test_addn_failed_flush_in_loop),
>  	};
>  	const int test_result = test_run_group(tgroup, NULL);
>  
> -- 
> 2.56.0
> 

-- 
Best regards,
Sergey Kaplun

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 3/3] sysprof: release resources on a halted profiler
  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
  1 sibling, 0 replies; 16+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-06 11:06 UTC (permalink / raw)
  To: Sergey Bronnikov; +Cc: tarantool-patches

Sergey,
Thanks for the patch!
LGTM.

-- 
Best regards,
Sergey Kaplun

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 1/3] test: add unit tests for the write buffer
  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-11-11 15:24   ` Evgeniy Temirgaleev via Tarantool-patches
  2026-12-01 14:33     ` Sergey Bronnikov via Tarantool-patches
  1 sibling, 1 reply; 16+ messages in thread
From: Evgeniy Temirgaleev via Tarantool-patches @ 2026-11-11 15:24 UTC (permalink / raw)
  To: Sergey Bronnikov; +Cc: tarantool-patches

Hi, Sergey! Thanks for the patch!

LGTM with some comments.

On 11/2/26 4:12 PM, Sergey Bronnikov wrote:
> From: Sergey Bronnikov <sergeyb@tarantool.org>
> 
> The write buffer accumulates the profilers' output and passes it
> to the writer on flush. It has no unit tests, so cover writing of
> the data of all kinds, flushing of a full buffer, the explicit
> flush and the error reporting.
> 
> Part of tarantool/tarantool#11620
> ---
>   test/tarantool-c-tests/unit-wbuf.test.c | 388 ++++++++++++++++++++++++
>   1 file changed, 388 insertions(+)
>   create mode 100644 test/tarantool-c-tests/unit-wbuf.test.c
> 
> diff --git a/test/tarantool-c-tests/unit-wbuf.test.c b/test/tarantool-c-tests/unit-wbuf.test.c
> new file mode 100644
> index 00000000..4bee6243
> --- /dev/null
> +++ b/test/tarantool-c-tests/unit-wbuf.test.c
> @@ -0,0 +1,388 @@
> +/*
> +** Testing of the write buffer.
> +**
> +** The write buffer accumulates the profilers' output and passes
> +** it to the writer on flush. The tests below cover writing of the
> +** data of all kinds, flushing on overflow, the explicit flush and
> +** the error reporting.
> +*/
> +
> +#include <errno.h>
> +
> +#include <stddef.h>
> +#include <stdint.h>
> +#include <string.h>
> +
> +#include "lj_wbuf.h"
> +#include "test.h"
> +
> +/* Size of the write buffer used in the tests. */
> +#define BUFFER_SIZE 10
> +
> +/* Size of the payload that does not fit into the write buffer. */
> +#define PAYLOAD_SIZE (2 * BUFFER_SIZE + 5)
> +
> +/* What the writer does on flush. */
> +enum writer_mode {
> +	/* Report all the data as written. */
> +	WMODE_OK,
> +	/* Report a half of the data as written and keep the buffer. */
> +	WMODE_PARTIAL,
> +	/* Imitate an unrecoverable write error, e.g. ENOSPC. */
> +	WMODE_FAIL,
> +};
> +
> +/* Writer context for simulating different write outcomes. */
> +struct writer_ctx {
> +	/* What the writer does on flush. */
> +	enum writer_mode mode;
> +	/* Number of performed flushes. */
> +	size_t flushes;
> +	/* Number of bytes reported as written. */
> +	size_t written;
> +	/* Everything reported as written, in order of writing. */
> +	uint8_t out[2 * PAYLOAD_SIZE];
> +};
> +
> +static size_t test_writer(const void **buf_addr, size_t len, void *opt)
> +{
> +	struct writer_ctx *ctx = opt;
> +	size_t n = len;
> +
> +	ctx->flushes++;
> +	if (ctx->mode == WMODE_FAIL) {
> +		errno = ENOSPC;
> +		*buf_addr = NULL;
> +		return 0;
> +	}
> +	if (ctx->mode == WMODE_PARTIAL) {
> +		errno = EIO;
> +		n = len / 2;
> +	}
Please, add an assertion to check "ctx->out[] have room for write" and 
ctx->mode == WMODE_OK (or use switch-case with halt on default branch) 
to handle possible tests misconfig.

> +	memcpy(ctx->out + ctx->written, *buf_addr, n);
> +	ctx->written += n;
> +	return n;
> +}
> +
> +static void writer_ctx_init(struct writer_ctx *ctx, enum writer_mode mode)
> +{
> +	memset(ctx, 0, sizeof(*ctx));
> +	ctx->mode = mode;
> +}
> +
> +static size_t buf_len(const struct lj_wbuf *buf)
> +{
> +	return (size_t)(buf->pos - buf->buf);
> +}
> +
Please, rename to wbuf_len as in lj_wbuf.c. May be add a comment also: a 
copy from...

> +static void assert_stream_stopped_with_error(const struct lj_wbuf *buf)
> +{
> +	assert_true(lj_wbuf_test_flag(buf, STREAM_STOP));
> +	assert_true(lj_wbuf_test_flag(buf, STREAM_ERRIO));
> +	assert_int_equal(lj_wbuf_errno(buf), ENOSPC);
> +	assert_ptr_equal(buf->buf, NULL);
> +	assert_ptr_equal(buf->pos, NULL);
> +}
> +
> +static int test_addbyte(void *state)
> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	lj_wbuf_addbyte(&buf, 0x7f);
> +	assert_sizet_equal(buf_len(&buf), 1);
> +	assert_int_equal(mem[0], 0x7f);
> +	assert_sizet_equal(ctx.flushes, 0);
> +	assert_int_equal(lj_wbuf_errno(&buf), 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_addu64(void *state)
> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	/* 128 is encoded as two bytes. */
Please, add a check assert(2 < BUFFER_SIZE) to comment why ctx.flushes 
must be zero.

> +	lj_wbuf_addu64(&buf, 128);
> +	assert_sizet_equal(buf_len(&buf), 2);
> +	assert_int_equal(mem[0], 0x80);
> +	assert_int_equal(mem[1], 0x01);
> +	assert_sizet_equal(ctx.flushes, 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_addn(void *state)
> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
assert(3 < BUFFER_SIZE);

> +	lj_wbuf_addn(&buf, "xyz", 3);
> +	assert_sizet_equal(buf_len(&buf), 3);
> +	assert_true(memcmp(mem, "xyz", 3) == 0);
> +	assert_sizet_equal(ctx.flushes, 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_addstring(void *state)
> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	/* A string is a ULEB128 length followed by the payload. */
assert(4 < BUFFER_SIZE);

> +	lj_wbuf_addstring(&buf, "xyz");
> +	assert_sizet_equal(buf_len(&buf), 4);
> +	assert_int_equal(mem[0], 3);
> +	assert_true(memcmp(mem + 1, "xyz", 3) == 0);
> +	assert_sizet_equal(ctx.flushes, 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_addn_pumped_through_buffer(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_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	/*
> +	 * The payload does not fit into the buffer, so it is
> +	 * pumped through it. Two full buffers are flushed, the
> +	 * rest stays in the buffer.
> +	 */
> +	lj_wbuf_addn(&buf, payload, sizeof(payload));
> +	assert_sizet_equal(ctx.flushes, 2);
> +	assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE);
> +	assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0);
> +	assert_sizet_equal(buf_len(&buf), PAYLOAD_SIZE - 2 * BUFFER_SIZE);
> +	assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE,
> +			   PAYLOAD_SIZE - 2 * BUFFER_SIZE) == 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
A check for 'no error case' (implicit flush) after pumping as in the 
flush test is needed.

> +
> +static int test_flush_on_overflow(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_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
Nit, may be add a macro with common header for all tests to reduce text 
size and to simplify reading? Something like:

#define TEST_INIT(mode)
	uint8_t mem[BUFFER_SIZE]; \
	struct lj_wbuf buf; \
	struct writer_ctx ctx; \
	writer_ctx_init(&ctx, (mode)); \
	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));


> +
> +	/* A byte written to a full buffer is preceded by a flush. */
> +	lj_wbuf_addn(&buf, payload, sizeof(payload));
> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
> +	lj_wbuf_addbyte(&buf, 0xaa);
> +	assert_sizet_equal(ctx.flushes, 1);
> +	assert_sizet_equal(ctx.written, sizeof(mem));
> +	assert_sizet_equal(buf_len(&buf), 1);
> +	assert_int_equal(mem[0], 0xaa);
> +
> +	/* The same for an ULEB128 value written to a full buffer. */
> +	lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
> +	lj_wbuf_addu64(&buf, 0);
> +	assert_sizet_equal(ctx.flushes, 2);
> +	assert_sizet_equal(ctx.written, 2 * sizeof(mem));
> +	assert_sizet_equal(buf_len(&buf), 1);
> +
> +	/* And for a data block written to a full buffer. */
> +	lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
> +	lj_wbuf_addn(&buf, payload, 1);
> +	assert_sizet_equal(ctx.flushes, 3);
> +	assert_sizet_equal(ctx.written, 3 * sizeof(mem));
> +	assert_sizet_equal(buf_len(&buf), 1);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_flush(void *state)
> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	lj_wbuf_addn(&buf, "xyz", 3);
> +	lj_wbuf_flush(&buf);
> +	assert_sizet_equal(ctx.flushes, 1);
> +	assert_sizet_equal(ctx.written, 3);
> +	assert_true(memcmp(ctx.out, "xyz", 3) == 0);
> +	/* The buffer is empty after a successful flush. */
> +	assert_sizet_equal(buf_len(&buf), 0);
> +	assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));
> +
> +	/* The buffer is usable after the flush. */
> +	lj_wbuf_addbyte(&buf, 0x7f);
> +	assert_sizet_equal(buf_len(&buf), 1);
> +	assert_int_equal(mem[0], 0x7f);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_flush_partial_write(void *state)
> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_PARTIAL);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	lj_wbuf_addn(&buf, "xyz", 3);
> +	lj_wbuf_flush(&buf);
> +	/* A partial write is reported as an error. */
> +	assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO));
> +	assert_int_equal(lj_wbuf_errno(&buf), EIO);
> +	/* The writer keeps the buffer, so the stream is not stopped. */
> +	assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));
> +	assert_ptr_equal(buf.buf, mem);
> +	/* The unwritten data is dropped. */
> +	assert_sizet_equal(buf_len(&buf), 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_flush_stopped_stream(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));
> +
> +	lj_wbuf_addn(&buf, "xyz", 3);
> +	lj_wbuf_flush(&buf);
> +	assert_stream_stopped_with_error(&buf);
> +	assert_sizet_equal(ctx.flushes, 1);
> +
> +	/* A stopped stream is not flushed again. */
> +	lj_wbuf_flush(&buf);
> +	assert_sizet_equal(ctx.flushes, 1);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_write_to_stopped_stream(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));
> +
> +	lj_wbuf_addn(&buf, "xyz", 3);
> +	lj_wbuf_flush(&buf);
> +	assert_stream_stopped_with_error(&buf);
> +
> +	/* Writes to a stopped 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_stream_stopped_with_error(&buf);
> +	assert_sizet_equal(ctx.flushes, 1);
> +	assert_sizet_equal(ctx.written, 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +static int test_terminate(void *state)
> +{
> +	uint8_t mem[BUFFER_SIZE];
> +	struct lj_wbuf buf;
> +	struct writer_ctx ctx;
> +
> +	UNUSED(state);
> +
> +	writer_ctx_init(&ctx, WMODE_OK);
> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> +
> +	lj_wbuf_terminate(&buf);
> +	assert_true(buf.writer == NULL);
> +	assert_ptr_equal(buf.ctx, NULL);
> +	assert_ptr_equal(buf.buf, NULL);
> +	assert_ptr_equal(buf.pos, NULL);
> +	assert_sizet_equal(buf.size, 0);
> +	assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));
> +	assert_int_equal(lj_wbuf_errno(&buf), 0);
> +
> +	return TEST_EXIT_SUCCESS;
> +}
> +
> +/*
> +** A writer may release the buffer and set *data to NULL on an
> +** unrecoverable error (e.g. ENOSPC). The stream is stopped in
> +** this case, so the write functions must not touch the released
> +** buffer.
> +*/

It seems some code was omitted or added later?
This functions:

static int test_addbyte_failed_flush(void *state)
static int test_addu64_failed_flush(void *state)
static int test_addn_failed_flush_in_loop(void *state)

--
A note about pumped write (addn) implementation: the pumping loop 
doesn't stops if an IO error occurs, it may lose some content inside 
src[n] as the flush discards unwritten data in some iterations. E.g. if 
we write to socket and errno=EAGAIN occurs, there written < len is valid 
case.

It looks strange: what is the purpose of this behavior? The error in any 
iteration corrupts the whole write. Client must check against error flag 
always and terminate or repeat. (The LuaJIT code checks against 
STREAM_ERRIO|STREAM_STOP always.)

Its a questinon: Why the loop is not terminated as it continue with an 
error is useless?

I think it will be good to somehow put this information into the test 
suite: make a test case with WMODE_PARTIAL for pumping addn() with 
corresponding checks (have an error flag only and have not all data 
written).


> +int main(void)
> +{
> +	const struct test_unit tgroup[] = {
> +		test_unit_def(test_addbyte),
> +		test_unit_def(test_addu64),
> +		test_unit_def(test_addn),
> +		test_unit_def(test_addstring),
> +		test_unit_def(test_addn_pumped_through_buffer),
> +		test_unit_def(test_flush_on_overflow),
> +		test_unit_def(test_flush),
> +		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_terminate),
> +	};
> +	const int test_result = test_run_group(tgroup, NULL);
> +
> +	return test_result;
> +}

-- 
--
Best regards,
Evgeniy Temirgaleev


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush
  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-11-11 16:34   ` Evgeniy Temirgaleev via Tarantool-patches
  2026-12-01 15:33     ` Sergey Bronnikov via Tarantool-patches
  2026-11-15 15:12   ` Evgeniy Temirgaleev via Tarantool-patches
  2 siblings, 1 reply; 16+ messages in thread
From: Evgeniy Temirgaleev via Tarantool-patches @ 2026-11-11 16:34 UTC (permalink / raw)
  To: Sergey Bronnikov; +Cc: tarantool-patches

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'?

>     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?

>       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>

-- 
--
Best regards,
Evgeniy Temirgaleev


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 3/3] sysprof: release resources on a halted profiler
  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
  1 sibling, 0 replies; 16+ messages in thread
From: Evgeniy Temirgaleev via Tarantool-patches @ 2026-11-11 23:29 UTC (permalink / raw)
  To: Sergey Bronnikov; +Cc: tarantool-patches

Thanks for the patch, Sergey! LGTM

-- 
--
Best regards,
Evgeniy Temirgaleev


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush
  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-11-11 16:34   ` Evgeniy Temirgaleev via Tarantool-patches
@ 2026-11-15 15:12   ` Evgeniy Temirgaleev via Tarantool-patches
  2026-12-01 16:09     ` Sergey Bronnikov via Tarantool-patches
  2 siblings, 1 reply; 16+ messages in thread
From: Evgeniy Temirgaleev via Tarantool-patches @ 2026-11-15 15:12 UTC (permalink / raw)
  To: Sergey Bronnikov, Sergey Kaplun; +Cc: tarantool-patches

Hi, Sergey! It's an addition to my first letter:

I saw the default implementation of the writer in the lib_misc.c:

```
/*
** Default buffer writer function.
** Just call write to the corresponding descriptor.
*/
static size_t buffer_writer_default(const void **buf_addr, size_t len,
				    void *opt)
```

It implements the writer definition

```
/*
** Buffer writer which is called on the buffer flush.
** Should return amount of written bytes on success or zero in case of 
error.
** *data should contain a buffer of at least the initial size.
** If *data == NULL stream stops.
*/
typedef size_t (*lj_wbuf_writer)(const void **data, size_t len, void *opt);
```

in a way, where unrecoverable error follows with stream stop. It's a 
more strong postcondition* ('res == 0 -> errno != 0 && *data == NULL') 
(1) than the definition requires ('res == 0 -> errno != 0 || *data == 
NULL') (2).

But all existing LuaJIT code including your lj_wbuf.c patch actually 
implies (1), not (2).

I think, it's an option: leave your patch as is with additional fix of 
the lj_wbuf_writer requirements to (1) with removing unused STREAM_ERRIO 
flag.

--

(*) Actually buffer_writer_default() implements postcondition (1'):

(1') ('res < len -> errno != 0 && *data == NULL')

This must be fixed also (`return 0` instead of `return write_total`, 
lib_misc.c, line 123).

---

First letter quoting:

On 11/2/26 4:12 PM, Sergey Bronnikov wrote:
> From: Sergey Bronnikov <sergeyb@tarantool.org>
<snipped>

> 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 +-
<snipped>

> @@ -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'?"

>     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?"

>       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;
>   }
<snipped>

-- 
--
Best regards,
Evgeniy Temirgaleev


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 1/3] test: add unit tests for the write buffer
  2026-11-11 15:24   ` Evgeniy Temirgaleev via Tarantool-patches
@ 2026-12-01 14:33     ` Sergey Bronnikov via Tarantool-patches
  0 siblings, 0 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-12-01 14:33 UTC (permalink / raw)
  To: Evgeniy Temirgaleev, Sergey Bronnikov; +Cc: tarantool-patches

Hi, Evgeniy,

thanks for review! Please see my comments below.

Sergey

On 11/11/26 18:24, Evgeniy Temirgaleev wrote:
> Hi, Sergey! Thanks for the patch!
>
> LGTM with some comments.
>
> On 11/2/26 4:12 PM, Sergey Bronnikov wrote:
>> From: Sergey Bronnikov <sergeyb@tarantool.org>
>>
>> The write buffer accumulates the profilers' output and passes it
>> to the writer on flush. It has no unit tests, so cover writing of
>> the data of all kinds, flushing of a full buffer, the explicit
>> flush and the error reporting.
>>
>> Part of tarantool/tarantool#11620
>> ---
>>   test/tarantool-c-tests/unit-wbuf.test.c | 388 ++++++++++++++++++++++++
>>   1 file changed, 388 insertions(+)
>>   create mode 100644 test/tarantool-c-tests/unit-wbuf.test.c
>>
>> diff --git a/test/tarantool-c-tests/unit-wbuf.test.c 
>> b/test/tarantool-c-tests/unit-wbuf.test.c
>> new file mode 100644
>> index 00000000..4bee6243
>> --- /dev/null
>> +++ b/test/tarantool-c-tests/unit-wbuf.test.c
>> @@ -0,0 +1,388 @@
>> +/*
>> +** Testing of the write buffer.
>> +**
>> +** The write buffer accumulates the profilers' output and passes
>> +** it to the writer on flush. The tests below cover writing of the
>> +** data of all kinds, flushing on overflow, the explicit flush and
>> +** the error reporting.
>> +*/
>> +
>> +#include <errno.h>
>> +
>> +#include <stddef.h>
>> +#include <stdint.h>
>> +#include <string.h>
>> +
>> +#include "lj_wbuf.h"
>> +#include "test.h"
>> +
>> +/* Size of the write buffer used in the tests. */
>> +#define BUFFER_SIZE 10
>> +
>> +/* Size of the payload that does not fit into the write buffer. */
>> +#define PAYLOAD_SIZE (2 * BUFFER_SIZE + 5)
>> +
>> +/* What the writer does on flush. */
>> +enum writer_mode {
>> +    /* Report all the data as written. */
>> +    WMODE_OK,
>> +    /* Report a half of the data as written and keep the buffer. */
>> +    WMODE_PARTIAL,
>> +    /* Imitate an unrecoverable write error, e.g. ENOSPC. */
>> +    WMODE_FAIL,
>> +};
>> +
>> +/* Writer context for simulating different write outcomes. */
>> +struct writer_ctx {
>> +    /* What the writer does on flush. */
>> +    enum writer_mode mode;
>> +    /* Number of performed flushes. */
>> +    size_t flushes;
>> +    /* Number of bytes reported as written. */
>> +    size_t written;
>> +    /* Everything reported as written, in order of writing. */
>> +    uint8_t out[2 * PAYLOAD_SIZE];
>> +};
>> +
>> +static size_t test_writer(const void **buf_addr, size_t len, void *opt)
>> +{
>> +    struct writer_ctx *ctx = opt;
>> +    size_t n = len;
>> +
>> +    ctx->flushes++;
>> +    if (ctx->mode == WMODE_FAIL) {
>> +        errno = ENOSPC;
>> +        *buf_addr = NULL;
>> +        return 0;
>> +    }
>> +    if (ctx->mode == WMODE_PARTIAL) {
>> +        errno = EIO;
>> +        n = len / 2;
>> +    }
> Please, add an assertion to check "ctx->out[] have room for write" and 
> ctx->mode == WMODE_OK (or use switch-case with halt on default branch) 
> to handle possible tests misconfig.


Updated:


--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -53,15 +53,23 @@ static size_t test_writer(const void **buf_addr, 
size_t len, void *opt)
         size_t n = len;

         ctx->flushes++;
-       if (ctx->mode == WMODE_FAIL) {
+       switch (ctx->mode) {
+       case WMODE_FAIL:
                 errno = ENOSPC;
                 *buf_addr = NULL;
                 return 0;
-       }
-       else if (ctx->mode == WMODE_PARTIAL) {
+       case WMODE_PARTIAL:
                 errno = EIO;
                 n = len / 2;
+               break;
+       case WMODE_OK:
+               break;
+       default:
+               /* Unreachable: a test is misconfigured. */
+               assert_true(0);
+               return 0;
         }
+       assert_true(ctx->written + n <= sizeof(ctx->out));
         memcpy(ctx->out + ctx->written, *buf_addr, n);
         /* The function is called only by lj_wbuf_flush(). */
         ctx->written += n;

>
>> +    memcpy(ctx->out + ctx->written, *buf_addr, n);
>> +    ctx->written += n;
>> +    return n;
>> +}
>> +
>> +static void writer_ctx_init(struct writer_ctx *ctx, enum writer_mode 
>> mode)
>> +{
>> +    memset(ctx, 0, sizeof(*ctx));
>> +    ctx->mode = mode;
>> +}
>> +
>> +static size_t buf_len(const struct lj_wbuf *buf)
>> +{
>> +    return (size_t)(buf->pos - buf->buf);
>> +}
>> +
> Please, rename to wbuf_len as in lj_wbuf.c. May be add a comment also: 
> a copy from...

Updated:

diff --git a/test/tarantool-c-tests/unit-wbuf.test.c 
b/test/tarantool-c-tests/unit-wbuf.test.c
index 58454c49..7d53291f 100644
--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -82,7 +82,11 @@ static void writer_ctx_init(struct writer_ctx *ctx, 
enum writer_mode mode)
      ctx->mode = mode;
  }

-static size_t buf_len(const struct lj_wbuf *buf)
+/*
+ * A copy of wbuf_len() from src/lj_wbuf.c: the original is a
+ * static function and is not visible to the tests.
+ */
+static size_t wbuf_len(const struct lj_wbuf *buf)
  {
      return (size_t)(buf->pos - buf->buf);
  }
@@ -108,7 +112,7 @@ static int test_addbyte(void *state)
      lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));

      lj_wbuf_addbyte(&buf, 0x7f);
-    assert_sizet_equal(buf_len(&buf), 1);
+    assert_sizet_equal(wbuf_len(&buf), 1);
      assert_int_equal(mem[0], 0x7f);
      assert_sizet_equal(ctx.flushes, 0);
      assert_int_equal(lj_wbuf_errno(&buf), 0);
@@ -129,7 +133,7 @@ static int test_addu64(void *state)

      /* 128 is encoded as two bytes. */
      lj_wbuf_addu64(&buf, 128);
-    assert_sizet_equal(buf_len(&buf), 2);
+    assert_sizet_equal(wbuf_len(&buf), 2);
      assert_int_equal(mem[0], 0x80);
      assert_int_equal(mem[1], 0x01);
      assert_sizet_equal(ctx.flushes, 0);
@@ -149,7 +153,7 @@ static int test_addn(void *state)
      lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));

      lj_wbuf_addn(&buf, "xyz", 3);
-    assert_sizet_equal(buf_len(&buf), 3);
+    assert_sizet_equal(wbuf_len(&buf), 3);
      assert_true(memcmp(mem, "xyz", 3) == 0);
      assert_sizet_equal(ctx.flushes, 0);

@@ -169,7 +173,7 @@ static int test_addstring(void *state)

      /* A string is a ULEB128 length followed by the payload. */
      lj_wbuf_addstring(&buf, "xyz");
-    assert_sizet_equal(buf_len(&buf), 4);
+    assert_sizet_equal(wbuf_len(&buf), 4);
      assert_int_equal(mem[0], 3);
      assert_true(memcmp(mem + 1, "xyz", 3) == 0);
      assert_sizet_equal(ctx.flushes, 0);
@@ -201,7 +205,7 @@ static int test_addn_pumped_through_buffer(void *state)
      assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE);
      assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0);
      const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE;
-    assert_sizet_equal(buf_len(&buf), remains);
+    assert_sizet_equal(wbuf_len(&buf), remains);
      assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE, remains) == 0);

      return TEST_EXIT_SUCCESS;
@@ -222,28 +226,28 @@ static int test_flush_on_overflow(void *state)

      /* A byte written to a full buffer is preceded by a flush. */
      lj_wbuf_addn(&buf, payload, sizeof(payload));
-    assert_sizet_equal(buf_len(&buf), sizeof(mem));
+    assert_sizet_equal(wbuf_len(&buf), sizeof(mem));
      lj_wbuf_addbyte(&buf, 0xaa);
      assert_sizet_equal(ctx.flushes, 1);
      assert_sizet_equal(ctx.written, sizeof(mem));
-    assert_sizet_equal(buf_len(&buf), 1);
+    assert_sizet_equal(wbuf_len(&buf), 1);
      assert_int_equal(mem[0], 0xaa);

      /* The same for an ULEB128 value written to a full buffer. */
      lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
-    assert_sizet_equal(buf_len(&buf), sizeof(mem));
+    assert_sizet_equal(wbuf_len(&buf), sizeof(mem));
      lj_wbuf_addu64(&buf, 0);
      assert_sizet_equal(ctx.flushes, 2);
      assert_sizet_equal(ctx.written, 2 * sizeof(mem));
-    assert_sizet_equal(buf_len(&buf), 1);
+    assert_sizet_equal(wbuf_len(&buf), 1);

      /* And for a data block written to a full buffer. */
      lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
-    assert_sizet_equal(buf_len(&buf), sizeof(mem));
+    assert_sizet_equal(wbuf_len(&buf), sizeof(mem));
      lj_wbuf_addn(&buf, payload, 1);
      assert_sizet_equal(ctx.flushes, 3);
      assert_sizet_equal(ctx.written, 3 * sizeof(mem));
-    assert_sizet_equal(buf_len(&buf), 1);
+    assert_sizet_equal(wbuf_len(&buf), 1);

      return TEST_EXIT_SUCCESS;
  }
@@ -265,13 +269,13 @@ static int test_flush(void *state)
      assert_sizet_equal(ctx.written, 3);
      assert_true(memcmp(ctx.out, "xyz", 3) == 0);
      /* The buffer is empty after a successful flush. */
-    assert_sizet_equal(buf_len(&buf), 0);
+    assert_sizet_equal(wbuf_len(&buf), 0);
      assert_false(lj_wbuf_test_flag(&buf,
          STREAM_ERRIO | STREAM_STOP));

      /* The buffer is usable after the flush. */
      lj_wbuf_addbyte(&buf, 0x7f);
-    assert_sizet_equal(buf_len(&buf), 1);
+    assert_sizet_equal(wbuf_len(&buf), 1);
      assert_int_equal(mem[0], 0x7f);

      return TEST_EXIT_SUCCESS;
@@ -300,7 +304,7 @@ static int test_flush_partial_write(void *state)
      assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));
      assert_ptr_equal(buf.buf, mem);
      /* The unwritten data is dropped. */
-    assert_sizet_equal(buf_len(&buf), 0);
+    assert_sizet_equal(wbuf_len(&buf), 0);

      return TEST_EXIT_SUCCESS;
  }
@@ -401,7 +405,7 @@ static int test_addbyte_failed_flush(void *state)
      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));
+    assert_sizet_equal(wbuf_len(&buf), sizeof(mem));

      /*
       * The buffer is full, so the byte below triggers a failed
@@ -427,7 +431,7 @@ static int test_addu64_failed_flush(void *state)

      /* The maximal value takes exactly BUFFER_SIZE bytes. */
      lj_wbuf_addu64(&buf, UINT64_MAX);
-    assert_sizet_equal(buf_len(&buf), sizeof(mem));
+    assert_sizet_equal(wbuf_len(&buf), sizeof(mem));

      /*
       * There is no space left, so the next write triggers

>
>> +static void assert_stream_stopped_with_error(const struct lj_wbuf *buf)
>> +{
>> +    assert_true(lj_wbuf_test_flag(buf, STREAM_STOP));
>> +    assert_true(lj_wbuf_test_flag(buf, STREAM_ERRIO));
>> +    assert_int_equal(lj_wbuf_errno(buf), ENOSPC);
>> +    assert_ptr_equal(buf->buf, NULL);
>> +    assert_ptr_equal(buf->pos, NULL);
>> +}
>> +
>> +static int test_addbyte(void *state)
>> +{
>> +    uint8_t mem[BUFFER_SIZE];
>> +    struct lj_wbuf buf;
>> +    struct writer_ctx ctx;
>> +
>> +    UNUSED(state);
>> +
>> +    writer_ctx_init(&ctx, WMODE_OK);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +    lj_wbuf_addbyte(&buf, 0x7f);
>> +    assert_sizet_equal(buf_len(&buf), 1);
>> +    assert_int_equal(mem[0], 0x7f);
>> +    assert_sizet_equal(ctx.flushes, 0);
>> +    assert_int_equal(lj_wbuf_errno(&buf), 0);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_addu64(void *state)
>> +{
>> +    uint8_t mem[BUFFER_SIZE];
>> +    struct lj_wbuf buf;
>> +    struct writer_ctx ctx;
>> +
>> +    UNUSED(state);
>> +
>> +    writer_ctx_init(&ctx, WMODE_OK);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +    /* 128 is encoded as two bytes. */
> Please, add a check assert(2 < BUFFER_SIZE) to comment why ctx.flushes 
> must be zero.
XXX
>
>> +    lj_wbuf_addu64(&buf, 128);
>> +    assert_sizet_equal(buf_len(&buf), 2);
>> +    assert_int_equal(mem[0], 0x80);
>> +    assert_int_equal(mem[1], 0x01);
>> +    assert_sizet_equal(ctx.flushes, 0);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_addn(void *state)
>> +{
>> +    uint8_t mem[BUFFER_SIZE];
>> +    struct lj_wbuf buf;
>> +    struct writer_ctx ctx;
>> +
>> +    UNUSED(state);
>> +
>> +    writer_ctx_init(&ctx, WMODE_OK);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
> assert(3 < BUFFER_SIZE);

Updated:

diff --git a/test/tarantool-c-tests/unit-wbuf.test.c 
b/test/tarantool-c-tests/unit-wbuf.test.c
index 7d53291f..c5097b89 100644
--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -13,6 +13,7 @@
  #include <stdint.h>
  #include <string.h>

+#include "lj_utils.h"
  #include "lj_wbuf.h"
  #include "test.h"

@@ -136,6 +137,12 @@ static int test_addu64(void *state)
      assert_sizet_equal(wbuf_len(&buf), 2);
      assert_int_equal(mem[0], 0x80);
      assert_int_equal(mem[1], 0x01);
+    /*
+     * addu64() reserves LEB128_U64_MAXSIZE bytes regardless of
+     * the actual encoded length, so the buffer must be large
+     * enough to avoid a flush here.
+     */
+    assert_true(LEB128_U64_MAXSIZE <= BUFFER_SIZE);
      assert_sizet_equal(ctx.flushes, 0);

      return TEST_EXIT_SUCCESS;

>
>> +    lj_wbuf_addn(&buf, "xyz", 3);
>> +    assert_sizet_equal(buf_len(&buf), 3);
>> +    assert_true(memcmp(mem, "xyz", 3) == 0);
>> +    assert_sizet_equal(ctx.flushes, 0);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_addstring(void *state)
>> +{
>> +    uint8_t mem[BUFFER_SIZE];
>> +    struct lj_wbuf buf;
>> +    struct writer_ctx ctx;
>> +
>> +    UNUSED(state);
>> +
>> +    writer_ctx_init(&ctx, WMODE_OK);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +    /* A string is a ULEB128 length followed by the payload. */
> assert(4 < BUFFER_SIZE);

Updated:

diff --git a/test/tarantool-c-tests/unit-wbuf.test.c 
b/test/tarantool-c-tests/unit-wbuf.test.c
index c5097b89..2a56e589 100644
--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -183,6 +183,12 @@ static int test_addstring(void *state)
      assert_sizet_equal(wbuf_len(&buf), 4);
      assert_int_equal(mem[0], 3);
      assert_true(memcmp(mem + 1, "xyz", 3) == 0);
+    /*
+     * addstring() writes a ULEB128 length via addu64(), which
+     * reserves LEB128_U64_MAXSIZE bytes, so the buffer must be
+     * large enough to avoid a flush here.
+     */
+    assert_true(LEB128_U64_MAXSIZE <= BUFFER_SIZE);
      assert_sizet_equal(ctx.flushes, 0);

      return TEST_EXIT_SUCCESS;

>
>> +    lj_wbuf_addstring(&buf, "xyz");
>> +    assert_sizet_equal(buf_len(&buf), 4);
>> +    assert_int_equal(mem[0], 3);
>> +    assert_true(memcmp(mem + 1, "xyz", 3) == 0);
>> +    assert_sizet_equal(ctx.flushes, 0);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_addn_pumped_through_buffer(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_OK);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +    /*
>> +     * The payload does not fit into the buffer, so it is
>> +     * pumped through it. Two full buffers are flushed, the
>> +     * rest stays in the buffer.
>> +     */
>> +    lj_wbuf_addn(&buf, payload, sizeof(payload));
>> +    assert_sizet_equal(ctx.flushes, 2);
>> +    assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE);
>> +    assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0);
>> +    assert_sizet_equal(buf_len(&buf), PAYLOAD_SIZE - 2 * BUFFER_SIZE);
>> +    assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE,
>> +               PAYLOAD_SIZE - 2 * BUFFER_SIZE) == 0);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
> A check for 'no error case' (implicit flush) after pumping as in the 
> flush test is needed.

Updated:

--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -220,6 +220,8 @@ static int test_addn_pumped_through_buffer(void *state)
         const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE;
         assert_sizet_equal(wbuf_len(&buf), remains);
         assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE, remains) == 0);
+       /* The implicit flushes completed without an error. */
+       assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));

         return TEST_EXIT_SUCCESS;
  }

>
>> +
>> +static int test_flush_on_overflow(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_OK);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
> Nit, may be add a macro with common header for all tests to reduce 
> text size and to simplify reading? Something like:
>
> #define TEST_INIT(mode)
>     uint8_t mem[BUFFER_SIZE]; \
>     struct lj_wbuf buf; \
>     struct writer_ctx ctx; \
>     writer_ctx_init(&ctx, (mode)); \
>     lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>
>
Added:


diff --git a/test/tarantool-c-tests/unit-wbuf.test.c 
b/test/tarantool-c-tests/unit-wbuf.test.c
index 798c71cd..d5e7912e 100644
--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -83,6 +83,18 @@ static void writer_ctx_init(struct writer_ctx *ctx, 
enum writer_mode mode)
      ctx->mode = mode;
  }

+/*
+ * Declares and initializes the objects shared by all the tests.
+ * Tests that need extra local variables should declare them
+ * before the macro invocation.
+ */
+#define WBUF_TEST_SETUP(mode)                        \
+    uint8_t mem[BUFFER_SIZE];                    \
+    struct lj_wbuf buf;                        \
+    struct writer_ctx ctx;                        \
+    writer_ctx_init(&ctx, (mode));                    \
+    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem))
+
  /*
   * A copy of wbuf_len() from src/lj_wbuf.c: the original is a
   * static function and is not visible to the tests.
@@ -103,15 +115,10 @@ static void assert_stream_stopped_with_error(const 
struct lj_wbuf *buf)

  static int test_addbyte(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_OK);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_OK);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      lj_wbuf_addbyte(&buf, 0x7f);
      assert_sizet_equal(wbuf_len(&buf), 1);
      assert_int_equal(mem[0], 0x7f);
@@ -123,15 +130,10 @@ static int test_addbyte(void *state)

  static int test_addu64(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_OK);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_OK);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      /* 128 is encoded as two bytes. */
      lj_wbuf_addu64(&buf, 128);
      assert_sizet_equal(wbuf_len(&buf), 2);
@@ -150,15 +152,10 @@ static int test_addu64(void *state)

  static int test_addn(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_OK);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_OK);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      lj_wbuf_addn(&buf, "xyz", 3);
      assert_sizet_equal(wbuf_len(&buf), 3);
      assert_true(memcmp(mem, "xyz", 3) == 0);
@@ -169,15 +166,10 @@ static int test_addn(void *state)

  static int test_addstring(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_OK);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_OK);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      /* A string is a ULEB128 length followed by the payload. */
      lj_wbuf_addstring(&buf, "xyz");
      assert_sizet_equal(wbuf_len(&buf), 4);
@@ -196,16 +188,11 @@ static int test_addstring(void *state)

  static int test_addn_pumped_through_buffer(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
      uint8_t payload[PAYLOAD_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;

+    WBUF_TEST_SETUP(WMODE_OK);
      UNUSED(state);
-
      memset(payload, 'a', sizeof(payload));
-    writer_ctx_init(&ctx, WMODE_OK);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));

      const int writes = PAYLOAD_SIZE / BUFFER_SIZE;
      /*
@@ -228,16 +215,11 @@ static int test_addn_pumped_through_buffer(void 
*state)

  static int test_flush_on_overflow(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
      uint8_t payload[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;

+    WBUF_TEST_SETUP(WMODE_OK);
      UNUSED(state);
-
      memset(payload, 'a', sizeof(payload));
-    writer_ctx_init(&ctx, WMODE_OK);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));

      /* A byte written to a full buffer is preceded by a flush. */
      lj_wbuf_addn(&buf, payload, sizeof(payload));
@@ -269,15 +251,10 @@ static int test_flush_on_overflow(void *state)

  static int test_flush(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_OK);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_OK);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      lj_wbuf_addn(&buf, "xyz", 3);
      lj_wbuf_flush(&buf);
      assert_sizet_equal(ctx.flushes, 1);
@@ -298,15 +275,10 @@ static int test_flush(void *state)

  static int test_flush_partial_write(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_PARTIAL);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_PARTIAL);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      lj_wbuf_addn(&buf, "xyz", 3);
      lj_wbuf_flush(&buf);
      /* A partial write is reported as an error. */
@@ -326,15 +298,10 @@ static int test_flush_partial_write(void *state)

  static int test_flush_stopped_stream(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_FAIL);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_FAIL);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      lj_wbuf_addn(&buf, "xyz", 3);
      lj_wbuf_flush(&buf);
      assert_stream_stopped_with_error(&buf);
@@ -349,15 +316,10 @@ static int test_flush_stopped_stream(void *state)

  static int test_write_to_stopped_stream(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_FAIL);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_FAIL);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      lj_wbuf_addn(&buf, "xyz", 3);
      lj_wbuf_flush(&buf);
      assert_stream_stopped_with_error(&buf);
@@ -376,15 +338,10 @@ static int test_write_to_stopped_stream(void *state)

  static int test_terminate(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_OK);

      UNUSED(state);

-    writer_ctx_init(&ctx, WMODE_OK);
-    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
-
      lj_wbuf_terminate(&buf);
      assert_true(buf.writer == NULL);
      assert_ptr_equal(buf.ctx, NULL);
@@ -406,16 +363,11 @@ static int test_terminate(void *state)
   */
  static int test_addbyte_failed_flush(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
      size_t i;

+    WBUF_TEST_SETUP(WMODE_FAIL);
      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);
@@ -435,15 +387,10 @@ static int test_addbyte_failed_flush(void *state)

  static int test_addu64_failed_flush(void *state)
  {
-    uint8_t mem[BUFFER_SIZE];
-    struct lj_wbuf buf;
-    struct writer_ctx ctx;
+    WBUF_TEST_SETUP(WMODE_FAIL);

      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(wbuf_len(&buf), sizeof(mem));
@@ -461,16 +408,11 @@ static int test_addu64_failed_flush(void *state)

  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;

+    WBUF_TEST_SETUP(WMODE_FAIL);
      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));
@@ -489,16 +431,11 @@ static int test_addn_failed_flush(void *state)

  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;

+    WBUF_TEST_SETUP(WMODE_FAIL);
      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

>> +
>> +    /* A byte written to a full buffer is preceded by a flush. */
>> +    lj_wbuf_addn(&buf, payload, sizeof(payload));
>> +    assert_sizet_equal(buf_len(&buf), sizeof(mem));
>> +    lj_wbuf_addbyte(&buf, 0xaa);
>> +    assert_sizet_equal(ctx.flushes, 1);
>> +    assert_sizet_equal(ctx.written, sizeof(mem));
>> +    assert_sizet_equal(buf_len(&buf), 1);
>> +    assert_int_equal(mem[0], 0xaa);
>> +
>> +    /* The same for an ULEB128 value written to a full buffer. */
>> +    lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
>> +    assert_sizet_equal(buf_len(&buf), sizeof(mem));
>> +    lj_wbuf_addu64(&buf, 0);
>> +    assert_sizet_equal(ctx.flushes, 2);
>> +    assert_sizet_equal(ctx.written, 2 * sizeof(mem));
>> +    assert_sizet_equal(buf_len(&buf), 1);
>> +
>> +    /* And for a data block written to a full buffer. */
>> +    lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
>> +    assert_sizet_equal(buf_len(&buf), sizeof(mem));
>> +    lj_wbuf_addn(&buf, payload, 1);
>> +    assert_sizet_equal(ctx.flushes, 3);
>> +    assert_sizet_equal(ctx.written, 3 * sizeof(mem));
>> +    assert_sizet_equal(buf_len(&buf), 1);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_flush(void *state)
>> +{
>> +    uint8_t mem[BUFFER_SIZE];
>> +    struct lj_wbuf buf;
>> +    struct writer_ctx ctx;
>> +
>> +    UNUSED(state);
>> +
>> +    writer_ctx_init(&ctx, WMODE_OK);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +    lj_wbuf_addn(&buf, "xyz", 3);
>> +    lj_wbuf_flush(&buf);
>> +    assert_sizet_equal(ctx.flushes, 1);
>> +    assert_sizet_equal(ctx.written, 3);
>> +    assert_true(memcmp(ctx.out, "xyz", 3) == 0);
>> +    /* The buffer is empty after a successful flush. */
>> +    assert_sizet_equal(buf_len(&buf), 0);
>> +    assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));
>> +
>> +    /* The buffer is usable after the flush. */
>> +    lj_wbuf_addbyte(&buf, 0x7f);
>> +    assert_sizet_equal(buf_len(&buf), 1);
>> +    assert_int_equal(mem[0], 0x7f);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_flush_partial_write(void *state)
>> +{
>> +    uint8_t mem[BUFFER_SIZE];
>> +    struct lj_wbuf buf;
>> +    struct writer_ctx ctx;
>> +
>> +    UNUSED(state);
>> +
>> +    writer_ctx_init(&ctx, WMODE_PARTIAL);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +    lj_wbuf_addn(&buf, "xyz", 3);
>> +    lj_wbuf_flush(&buf);
>> +    /* A partial write is reported as an error. */
>> +    assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO));
>> +    assert_int_equal(lj_wbuf_errno(&buf), EIO);
>> +    /* The writer keeps the buffer, so the stream is not stopped. */
>> +    assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));
>> +    assert_ptr_equal(buf.buf, mem);
>> +    /* The unwritten data is dropped. */
>> +    assert_sizet_equal(buf_len(&buf), 0);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_flush_stopped_stream(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));
>> +
>> +    lj_wbuf_addn(&buf, "xyz", 3);
>> +    lj_wbuf_flush(&buf);
>> +    assert_stream_stopped_with_error(&buf);
>> +    assert_sizet_equal(ctx.flushes, 1);
>> +
>> +    /* A stopped stream is not flushed again. */
>> +    lj_wbuf_flush(&buf);
>> +    assert_sizet_equal(ctx.flushes, 1);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_write_to_stopped_stream(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));
>> +
>> +    lj_wbuf_addn(&buf, "xyz", 3);
>> +    lj_wbuf_flush(&buf);
>> +    assert_stream_stopped_with_error(&buf);
>> +
>> +    /* Writes to a stopped 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_stream_stopped_with_error(&buf);
>> +    assert_sizet_equal(ctx.flushes, 1);
>> +    assert_sizet_equal(ctx.written, 0);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_terminate(void *state)
>> +{
>> +    uint8_t mem[BUFFER_SIZE];
>> +    struct lj_wbuf buf;
>> +    struct writer_ctx ctx;
>> +
>> +    UNUSED(state);
>> +
>> +    writer_ctx_init(&ctx, WMODE_OK);
>> +    lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +    lj_wbuf_terminate(&buf);
>> +    assert_true(buf.writer == NULL);
>> +    assert_ptr_equal(buf.ctx, NULL);
>> +    assert_ptr_equal(buf.buf, NULL);
>> +    assert_ptr_equal(buf.pos, NULL);
>> +    assert_sizet_equal(buf.size, 0);
>> +    assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));
>> +    assert_int_equal(lj_wbuf_errno(&buf), 0);
>> +
>> +    return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +/*
>> +** A writer may release the buffer and set *data to NULL on an
>> +** unrecoverable error (e.g. ENOSPC). The stream is stopped in
>> +** this case, so the write functions must not touch the released
>> +** buffer.
>> +*/
>
> It seems some code was omitted or added later?
> This functions:
>
> static int test_addbyte_failed_flush(void *state)
> static int test_addu64_failed_flush(void *state)
> static int test_addn_failed_flush_in_loop(void *state)
Yes, omitted. These functions added in the commit with fix.
>
> -- 
> A note about pumped write (addn) implementation: the pumping loop 
> doesn't stops if an IO error occurs, it may lose some content inside 
> src[n] as the flush discards unwritten data in some iterations. E.g. 
> if we write to socket and errno=EAGAIN occurs, there written < len is 
> valid case.
>
> It looks strange: what is the purpose of this behavior? The error in 
> any iteration corrupts the whole write. Client must check against 
> error flag always and terminate or repeat. (The LuaJIT code checks 
> against STREAM_ERRIO|STREAM_STOP always.)
>
> Its a questinon: Why the loop is not terminated as it continue with an 
> error is useless?
>
> I think it will be good to somehow put this information into the test 
> suite: make a test case with WMODE_PARTIAL for pumping addn() with 
> corresponding checks (have an error flag only and have not all data 
> written).

The observation is correct.

`lj_wbuf_flush()` distinguishes two failure modes:

- `written < len` - a short write (e.g. `EAGAIN`). It sets 
`STREAM_ERRIO`, but the
   buffer is still owned by us (`buf->buf != NULL`). It then resets
   `buf->pos = buf->buf`, dropping the bytes that were not reported as 
written.
- `buf->buf == NULL` - the writer released the buffer (e.g. `ENOSPC`). 
It sets
   `STREAM_STOP`.

The pumping loop in `lj_wbuf_addn()` breaks only on `STREAM_STOP` 
because that is
the only case where `buf->buf` may be `NULL`; continuing after it could
dereference `NULL`. With `STREAM_ERRIO` the buffer remains valid, so the 
loop
keeps going - each iteration drops the unwritten part and pumps the next 
chunk.

Is continuing useful? No, not really. The contract at the call sites is 
all-or-nothing:
`lj_sysprof.c` and `lj_memprof.c` check `STREAM_ERRIO | STREAM_STOP` and 
abort the
whole stream / return `PROFILE_ERRIO`. So once `STREAM_ERRIO` is set, 
the stream
is going to be discarded anyway; the remaining pumping is wasted work, 
and the
data has already been partially dropped by `lj_wbuf_flush()`.

Stopping the loop on `STREAM_ERRIO` as well would be a reasonable 
cleanup, but
that changes behaviour and is a separate change; the caller-visible 
semantics
(the whole stream fails) would stay the same.

I've added `test_addn_pumped_through_buffer_partial`. With 
`WMODE_PARTIAL` it pumps a
payload larger than the buffer. This pins down the behaviour you 
described. If we later decide to break the loop
on `STREAM_ERRIO`, the expectations in that test will be updated 
accordingly.


@@ -226,18 +213,44 @@ static int test_addn_pumped_through_buffer(void 
*state)
         return TEST_EXIT_SUCCESS;
  }

+static int test_addn_pumped_through_buffer_partial(void *state)
+{
+       uint8_t payload[PAYLOAD_SIZE];
+
+       WBUF_TEST_SETUP(WMODE_PARTIAL);
+       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.
+        */
+       lj_wbuf_addn(&buf, payload, sizeof(payload));
+       assert_sizet_equal(ctx.flushes, writes);
+       assert_sizet_equal(ctx.written, writes * (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);
+
+       return TEST_EXIT_SUCCESS;
+}
+
  static int test_flush_on_overflow(void *state)
@@ -520,6 +488,7 @@ int main(void)
                 test_unit_def(test_addn),
                 test_unit_def(test_addstring),
                 test_unit_def(test_addn_pumped_through_buffer),
+  test_unit_def(test_addn_pumped_through_buffer_partial),
                 test_unit_def(test_flush_on_overflow),
                 test_unit_def(test_flush),
                 test_unit_def(test_flush_partial_write),

>
>
>> +int main(void)
>> +{
>> +    const struct test_unit tgroup[] = {
>> +        test_unit_def(test_addbyte),
>> +        test_unit_def(test_addu64),
>> +        test_unit_def(test_addn),
>> +        test_unit_def(test_addstring),
>> +        test_unit_def(test_addn_pumped_through_buffer),
>> +        test_unit_def(test_flush_on_overflow),
>> +        test_unit_def(test_flush),
>> +        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_terminate),
>> +    };
>> +    const int test_result = test_run_group(tgroup, NULL);
>> +
>> +    return test_result;
>> +}
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush
  2026-11-06 10:59   ` Sergey Kaplun via Tarantool-patches
@ 2026-12-01 14:41     ` Sergey Bronnikov via Tarantool-patches
  0 siblings, 0 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-12-01 14:41 UTC (permalink / raw)
  To: Sergey Kaplun, Sergey Bronnikov; +Cc: 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 <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
> 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
> <snipped>
>
>> 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 <unit-leb128.test.c> 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 <errno.h>
>> +#include <signal.h>
>> +#include <stdint.h>
>> +#include <string.h>
>> +#include <unistd.h>
>> +
>> +/*
>> + * 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 {

>
> <snipped>
>
>> +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
>>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 1/3] test: add unit tests for the write buffer
  2026-11-05 18:39   ` Sergey Kaplun via Tarantool-patches
@ 2026-12-01 15:11     ` Sergey Bronnikov via Tarantool-patches
  0 siblings, 0 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-12-01 15:11 UTC (permalink / raw)
  To: Sergey Kaplun, Sergey Bronnikov; +Cc: tarantool-patches

Hi, Sergey,

thanks for review! Please see my comments below.

Sergey

On 11/5/26 21:39, Sergey Kaplun wrote:
> Hi, Sergey!
> Thanks for the patch and painstaking work with coverage of wbuf module!
> Generally LGTM with minor suggestions below.
>
> On 02.11.26, Sergey Bronnikov wrote:
>> From: Sergey Bronnikov <sergeyb@tarantool.org>
>>
>> The write buffer accumulates the profilers' output and passes it
>> to the writer on flush. It has no unit tests, so cover writing of
>> the data of all kinds, flushing of a full buffer, the explicit
>> flush and the error reporting.
>>
>> Part of tarantool/tarantool#11620
> Minor: I'm not sure that this is the part of this issue. Maybe
> | Relates to
> is more appropriate.
Fixed.
>
>> ---
>>   test/tarantool-c-tests/unit-wbuf.test.c | 388 ++++++++++++++++++++++++
>>   1 file changed, 388 insertions(+)
>>   create mode 100644 test/tarantool-c-tests/unit-wbuf.test.c
>>
>> diff --git a/test/tarantool-c-tests/unit-wbuf.test.c b/test/tarantool-c-tests/unit-wbuf.test.c
>> new file mode 100644
>> index 00000000..4bee6243
>> --- /dev/null
>> +++ b/test/tarantool-c-tests/unit-wbuf.test.c
>> @@ -0,0 +1,388 @@
>> +/*
>> +** Testing of the write buffer.
>> +**
>> +** The write buffer accumulates the profilers' output and passes
>> +** it to the writer on flush. The tests below cover writing of the
>> +** data of all kinds, flushing on overflow, the explicit flush and
>> +** the error reporting.
> Minor: It is better to use
> | /*
> |  *
> |  */
>
> Comments here as in the most C/C++ code in the tests.
> The inconsistent comment style in the <unit-leb128.test.c> is my fault
> and should be fixed.

Updated:

--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -1,11 +1,11 @@
  /*
-** Testing of the write buffer.
-**
-** The write buffer accumulates the profilers' output and passes
-** it to the writer on flush. The tests below cover writing of the
-** data of all kinds, flushing on overflow, the explicit flush and
-** the error reporting.
-*/
+ * Testing of the write buffer.
+ *
+ * The write buffer accumulates the profilers' output and passes
+ * it to the writer on flush. The tests below cover writing of the
+ * data of all kinds, flushing on overflow, the explicit flush and
+ * the error reporting.
+ */

  #include <errno.h>



>
>> +*/
>> +
>> +#include <errno.h>
>> +
>> +#include <stddef.h>
>> +#include <stdint.h>
>> +#include <string.h>
>> +
>> +#include "lj_wbuf.h"
>> +#include "test.h"
>> +
>> +/* Size of the write buffer used in the tests. */
>> +#define BUFFER_SIZE 10
>> +
>> +/* Size of the payload that does not fit into the write buffer. */
>> +#define PAYLOAD_SIZE (2 * BUFFER_SIZE + 5)
>> +
>> +/* What the writer does on flush. */
>> +enum writer_mode {
>> +	/* Report all the data as written. */
>> +	WMODE_OK,
>> +	/* Report a half of the data as written and keep the buffer. */
> Nit: Comment length is more than 66 symbols.
> Here and in:
> * `test_flush_partial_write()`
> * `test_addbyte_failed_flush()`
> * `test_addu64_failed_flush()`
> * `test_addn_failed_flush()`
> * `test_addn_failed_flush_in_loop()`
Fixed.

>
>> +	WMODE_PARTIAL,
>> +	/* Imitate an unrecoverable write error, e.g. ENOSPC. */
>> +	WMODE_FAIL,
>> +};
>> +
>> +/* Writer context for simulating different write outcomes. */
>> +struct writer_ctx {
>> +	/* What the writer does on flush. */
>> +	enum writer_mode mode;
>> +	/* Number of performed flushes. */
>> +	size_t flushes;
>> +	/* Number of bytes reported as written. */
>> +	size_t written;
>> +	/* Everything reported as written, in order of writing. */
>> +	uint8_t out[2 * PAYLOAD_SIZE];
> Why do we need 2x size from the PAYLOAD here? Please add the comment.

Updated:

--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -44,7 +44,15 @@ struct writer_ctx {
         size_t flushes;
         /* Number of bytes reported as written. */
         size_t written;
-       /* Everything reported as written, in order of writing. */
+       /*
+        * Everything reported as written, in order of writing.
+        *
+        * Must hold the bytes reported over a whole test. The
+        * largest amount is produced by test_flush_on_overflow(),
+        * which reports 3 * BUFFER_SIZE bytes. 2 * PAYLOAD_SIZE
+        * (4 * BUFFER_SIZE + 10) is a conservative upper bound
+        * that leaves some headroom.
+        */
         uint8_t out[2 * PAYLOAD_SIZE];
  };


>
>> +};
>> +
>> +static size_t test_writer(const void **buf_addr, size_t len, void *opt)
>> +{
>> +	struct writer_ctx *ctx = opt;
>> +	size_t n = len;
>> +
>> +	ctx->flushes++;
> Minor: Please add the comment that writer function is called only by
> `lj_wbuf_flush()` before this increment.
--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -63,6 +63,7 @@ static size_t test_writer(const void **buf_addr, 
size_t len, void *opt)
                 n = len / 2;
         }
         memcpy(ctx->out + ctx->written, *buf_addr, n);
+       /* The function is called only by lj_wbuf_flush(). */
         ctx->written += n;
         return n;
  }


>
>> +	if (ctx->mode == WMODE_FAIL) {
>> +		errno = ENOSPC;
>> +		*buf_addr = NULL;
>> +		return 0;
>> +	}
>> +	if (ctx->mode == WMODE_PARTIAL) {
> Minor: s/if/else if/
--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -58,11 +58,12 @@ static size_t test_writer(const void **buf_addr, 
size_t len, void *opt)
                 *buf_addr = NULL;
                 return 0;
         }
-       if (ctx->mode == WMODE_PARTIAL) {
+       else if (ctx->mode == WMODE_PARTIAL) {
                 errno = EIO;
                 n = len / 2;
         }

>> +		errno = EIO;
>> +		n = len / 2;
>> +	}
>> +	memcpy(ctx->out + ctx->written, *buf_addr, n);
>> +	ctx->written += n;
>> +	return n;
>> +}
>> +
> <snipped>
>
>> +
>> +static void assert_stream_stopped_with_error(const struct lj_wbuf *buf)
> I suggest using macro instead for such helpers to preserve the line of
> original assertion check:
>
> So instead of:
> | not ok 9 - test_flush_stopped_stream
> |   ---
> |   location:     test/tarantool-c-tests/unit-wbuf.test.c:80
> |   condition_failed:     'lj_wbuf_test_flag(buf, STREAM_STOP)'
>
> We have:
> | not ok 9 - test_flush_stopped_stream
> |   ---
> |   location:     test/tarantool-c-tests/unit-wbuf.test.c:304
> |   condition_failed:     'lj_wbuf_test_flag((&buf), STREAM_STOP)'

Added:

--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -112,14 +112,13 @@ static size_t wbuf_len(const struct lj_wbuf *buf)
         return (size_t)(buf->pos - buf->buf);
  }

-static void assert_stream_stopped_with_error(const struct lj_wbuf *buf)
-{
-       assert_true(lj_wbuf_test_flag(buf, STREAM_STOP));
-       assert_true(lj_wbuf_test_flag(buf, STREAM_ERRIO));
-       assert_int_equal(lj_wbuf_errno(buf), ENOSPC);
-       assert_ptr_equal(buf->buf, NULL);
-       assert_ptr_equal(buf->pos, NULL);
-}
+#define assert_stream_stopped_with_error(wbuf) do {       \
+       assert_true(lj_wbuf_test_flag((wbuf), STREAM_STOP));       \
+       assert_true(lj_wbuf_test_flag((wbuf), STREAM_ERRIO));      \
+       assert_int_equal(lj_wbuf_errno((wbuf)), ENOSPC);       \
+       assert_ptr_equal((wbuf)->buf, NULL);         \
+       assert_ptr_equal((wbuf)->pos, NULL);         \
+} while (0)

  static int test_addbyte(void *state)
  {

>> +{
>> +	assert_true(lj_wbuf_test_flag(buf, STREAM_STOP));
>> +	assert_true(lj_wbuf_test_flag(buf, STREAM_ERRIO));
>> +	assert_int_equal(lj_wbuf_errno(buf), ENOSPC);
> Since this precise assertion, I suggest naming the helper function/macro
> something like `assert_wbuf_stopped_enospc()`.
>
Added:

+#define assert_wbuf_stopped_enospc(wbuf) do {                  \
+       assert_true(lj_wbuf_test_flag((wbuf), STREAM_STOP));       \
+       assert_true(lj_wbuf_test_flag((wbuf), STREAM_ERRIO));      \
+       assert_int_equal(lj_wbuf_errno((wbuf)), ENOSPC);       \
+       assert_ptr_equal((wbuf)->buf, NULL);         \
+       assert_ptr_equal((wbuf)->pos, NULL);         \
+} while (0)
+

>> +	assert_ptr_equal(buf->buf, NULL);
>> +	assert_ptr_equal(buf->pos, NULL);
>> +}
>> +
>> +static int test_addbyte(void *state)
> <snipped>
>
>
>> +static int test_addstring(void *state)
>> +{
> <snipped>
>
>> +}
>> +
>> +static int test_addn_pumped_through_buffer(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_OK);
>> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +	/*
>> +	 * The payload does not fit into the buffer, so it is
>> +	 * pumped through it. Two full buffers are flushed, the
>> +	 * rest stays in the buffer.
>> +	 */
>> +	lj_wbuf_addn(&buf, payload, sizeof(payload));
>> +	assert_sizet_equal(ctx.flushes, 2);
> This `2` may be not very obvious.
> We may declare constant in this function before the comment above:
>
> | const int writes = PAYLOAD_SIZE / BUFFER_SIZE;

Fixed:

@@ -181,6 +182,7 @@ static int test_addn_pumped_through_buffer(void *state)
         writer_ctx_init(&ctx, WMODE_OK);
         lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));

+       const int writes = PAYLOAD_SIZE / BUFFER_SIZE;
         /*
          * The payload does not fit into the buffer, so it is
          * pumped through it. Two full buffers are flushed, the

>
>> +	assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE);
>> +	assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0);
>> +	assert_sizet_equal(buf_len(&buf), PAYLOAD_SIZE - 2 * BUFFER_SIZE);
> It is better to add the variable:
>
> | const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE;

Updated:

@@ -190,9 +192,9 @@ static int test_addn_pumped_through_buffer(void *state)
         assert_sizet_equal(ctx.flushes, 2);
         assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE);
         assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0);
-       assert_sizet_equal(buf_len(&buf), PAYLOAD_SIZE - 2 * BUFFER_SIZE);
-       assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE,
-                          PAYLOAD_SIZE - 2 * BUFFER_SIZE) == 0);
+       const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE;
+       assert_sizet_equal(buf_len(&buf), remains);
+       assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE, remains) == 0);

         return TEST_EXIT_SUCCESS;
  }

>
>> +	assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE,
>> +			   PAYLOAD_SIZE - 2 * BUFFER_SIZE) == 0);
>> +
>> +	return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_flush_on_overflow(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_OK);
>> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +	/* A byte written to a full buffer is preceded by a flush. */
>> +	lj_wbuf_addn(&buf, payload, sizeof(payload));
>> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
>> +	lj_wbuf_addbyte(&buf, 0xaa);
>> +	assert_sizet_equal(ctx.flushes, 1);
>> +	assert_sizet_equal(ctx.written, sizeof(mem));
>> +	assert_sizet_equal(buf_len(&buf), 1);
>> +	assert_int_equal(mem[0], 0xaa);
> I suggest splitting this 1 test into 3 subtests, since they have the
> similar logic:
> 1) Init buffer.
> 2) Write something without flushes.
> 3) Write something with a flush.
>
> It helps to avoid the specific `ctx.out` size and makes checks for
> `flushes`, `written` and `buf_len()` similar.

Splitted:

diff --git a/test/tarantool-c-tests/unit-wbuf.test.c 
b/test/tarantool-c-tests/unit-wbuf.test.c
index 27d75ad0..9574d42b 100644
--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -48,10 +48,10 @@ struct writer_ctx {
       * Everything reported as written, in order of writing.
       *
       * Must hold the bytes reported over a whole test. The
-     * largest amount is produced by test_flush_on_overflow(),
-     * which reports 3 * BUFFER_SIZE bytes. 2 * PAYLOAD_SIZE
-     * (4 * BUFFER_SIZE + 10) is a conservative upper bound
-     * that leaves some headroom.
+     * largest amount is produced by
+     * test_addn_pumped_through_buffer(), which reports
+     * 2 * BUFFER_SIZE bytes. 2 * PAYLOAD_SIZE is a conservative
+     * upper bound that leaves some headroom.
       */
      uint8_t out[2 * PAYLOAD_SIZE];
  };
@@ -251,7 +251,7 @@ static int 
test_addn_pumped_through_buffer_partial(void *state)
      return TEST_EXIT_SUCCESS;
  }

-static int test_flush_on_overflow(void *state)
+static int test_flush_on_overflow_addbyte(void *state)
  {
      uint8_t payload[BUFFER_SIZE];

@@ -259,29 +259,57 @@ static int test_flush_on_overflow(void *state)
      UNUSED(state);
      memset(payload, 'a', sizeof(payload));

-    /* A byte written to a full buffer is preceded by a flush. */
+    /* Fill the buffer without a flush. */
      lj_wbuf_addn(&buf, payload, sizeof(payload));
      assert_sizet_equal(wbuf_len(&buf), sizeof(mem));
+
+    /* A byte written to a full buffer is preceded by a flush. */
      lj_wbuf_addbyte(&buf, 0xaa);
      assert_sizet_equal(ctx.flushes, 1);
      assert_sizet_equal(ctx.written, sizeof(mem));
      assert_sizet_equal(wbuf_len(&buf), 1);
      assert_int_equal(mem[0], 0xaa);

-    /* The same for an ULEB128 value written to a full buffer. */
-    lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
+    return TEST_EXIT_SUCCESS;
+}
+
+static int test_flush_on_overflow_addu64(void *state)
+{
+    uint8_t payload[BUFFER_SIZE];
+
+    WBUF_TEST_SETUP(WMODE_OK);
+    UNUSED(state);
+    memset(payload, 'a', sizeof(payload));
+
+    /* Fill the buffer without a flush. */
+    lj_wbuf_addn(&buf, payload, sizeof(payload));
      assert_sizet_equal(wbuf_len(&buf), sizeof(mem));
+
+    /* An ULEB128 value written to a full buffer is preceded by a flush. */
      lj_wbuf_addu64(&buf, 0);
-    assert_sizet_equal(ctx.flushes, 2);
-    assert_sizet_equal(ctx.written, 2 * sizeof(mem));
+    assert_sizet_equal(ctx.flushes, 1);
+    assert_sizet_equal(ctx.written, sizeof(mem));
      assert_sizet_equal(wbuf_len(&buf), 1);

-    /* And for a data block written to a full buffer. */
-    lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
+    return TEST_EXIT_SUCCESS;
+}
+
+static int test_flush_on_overflow_addn(void *state)
+{
+    uint8_t payload[BUFFER_SIZE];
+
+    WBUF_TEST_SETUP(WMODE_OK);
+    UNUSED(state);
+    memset(payload, 'a', sizeof(payload));
+
+    /* Fill the buffer without a flush. */
+    lj_wbuf_addn(&buf, payload, sizeof(payload));
      assert_sizet_equal(wbuf_len(&buf), sizeof(mem));
+
+    /* A data block written to a full buffer is preceded by a flush. */
      lj_wbuf_addn(&buf, payload, 1);
-    assert_sizet_equal(ctx.flushes, 3);
-    assert_sizet_equal(ctx.written, 3 * sizeof(mem));
+    assert_sizet_equal(ctx.flushes, 1);
+    assert_sizet_equal(ctx.written, sizeof(mem));
      assert_sizet_equal(wbuf_len(&buf), 1);

      return TEST_EXIT_SUCCESS;
@@ -496,7 +524,9 @@ int main(void)
          test_unit_def(test_addstring),
          test_unit_def(test_addn_pumped_through_buffer),
          test_unit_def(test_addn_pumped_through_buffer_partial),
-        test_unit_def(test_flush_on_overflow),
+        test_unit_def(test_flush_on_overflow_addbyte),
+        test_unit_def(test_flush_on_overflow_addu64),
+        test_unit_def(test_flush_on_overflow_addn),
          test_unit_def(test_flush),
          test_unit_def(test_flush_partial_write),
          test_unit_def(test_flush_stopped_stream),

>> +
>> +	/* The same for an ULEB128 value written to a full buffer. */
>> +	lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
>> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
>> +	lj_wbuf_addu64(&buf, 0);
>> +	assert_sizet_equal(ctx.flushes, 2);
>> +	assert_sizet_equal(ctx.written, 2 * sizeof(mem));
>> +	assert_sizet_equal(buf_len(&buf), 1);
> Looks like there should be a check for the content as for the 0xaa
> byte.

Added:

--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -290,6 +290,8 @@ static int test_flush_on_overflow_addu64(void *state)
         assert_sizet_equal(ctx.flushes, 1);
         assert_sizet_equal(ctx.written, sizeof(mem));
         assert_sizet_equal(wbuf_len(&buf), 1);
+       /* 0 is encoded as a single 0x00 byte. */
+       assert_int_equal(mem[0], 0x00);

         return TEST_EXIT_SUCCESS;
  }
@@ -311,6 +313,7 @@ static int test_flush_on_overflow_addn(void *state)
         assert_sizet_equal(ctx.flushes, 1);
         assert_sizet_equal(ctx.written, sizeof(mem));
         assert_sizet_equal(wbuf_len(&buf), 1);
+       assert_int_equal(mem[0], payload[0]);

         return TEST_EXIT_SUCCESS;
  }

>> +
>> +	/* And for a data block written to a full buffer. */
>> +	lj_wbuf_addn(&buf, payload, sizeof(payload) - 1);
>> +	assert_sizet_equal(buf_len(&buf), sizeof(mem));
>> +	lj_wbuf_addn(&buf, payload, 1);
>> +	assert_sizet_equal(ctx.flushes, 3);
>> +	assert_sizet_equal(ctx.written, 3 * sizeof(mem));
>> +	assert_sizet_equal(buf_len(&buf), 1);
> Looks like there should be a check for the content as for the 0xaa
> byte.
Added.
>> +
>> +	return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_flush(void *state)
> I suggest renaming this as `test_flush_and_write()` or something similar.
> Otherwise, the difference between this test and the test above is
> unclear.

renamed:

--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -318,7 +318,7 @@ static int test_flush_on_overflow_addn(void *state)
         return TEST_EXIT_SUCCESS;
  }

-static int test_flush(void *state)
+static int test_flush_and_write(void *state)
  {
         WBUF_TEST_SETUP(WMODE_OK);

@@ -530,7 +530,7 @@ int main(void)
                 test_unit_def(test_flush_on_overflow_addbyte),
                 test_unit_def(test_flush_on_overflow_addu64),
                 test_unit_def(test_flush_on_overflow_addn),
-               test_unit_def(test_flush),
+               test_unit_def(test_flush_and_write),
                 test_unit_def(test_flush_partial_write),
                 test_unit_def(test_flush_stopped_stream),
                 test_unit_def(test_write_to_stopped_stream),

>> +{
>> +	uint8_t mem[BUFFER_SIZE];
>> +	struct lj_wbuf buf;
>> +	struct writer_ctx ctx;
>> +
>> +	UNUSED(state);
>> +
>> +	writer_ctx_init(&ctx, WMODE_OK);
>> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +	lj_wbuf_addn(&buf, "xyz", 3);
>> +	lj_wbuf_flush(&buf);
>> +	assert_sizet_equal(ctx.flushes, 1);
>> +	assert_sizet_equal(ctx.written, 3);
>> +	assert_true(memcmp(ctx.out, "xyz", 3) == 0);
>> +	/* The buffer is empty after a successful flush. */
>> +	assert_sizet_equal(buf_len(&buf), 0);
>> +	assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP));
>> +
>> +	/* The buffer is usable after the flush. */
>> +	lj_wbuf_addbyte(&buf, 0x7f);
>> +	assert_sizet_equal(buf_len(&buf), 1);
>> +	assert_int_equal(mem[0], 0x7f);
>> +
>> +	return TEST_EXIT_SUCCESS;
>> +}
>> +
>> +static int test_flush_partial_write(void *state)
>> +{
>> +	uint8_t mem[BUFFER_SIZE];
>> +	struct lj_wbuf buf;
>> +	struct writer_ctx ctx;
>> +
>> +	UNUSED(state);
>> +
>> +	writer_ctx_init(&ctx, WMODE_PARTIAL);
>> +	lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem));
>> +
>> +	lj_wbuf_addn(&buf, "xyz", 3);
>> +	lj_wbuf_flush(&buf);
>> +	/* A partial write is reported as an error. */
>> +	assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO));
>> +	assert_int_equal(lj_wbuf_errno(&buf), EIO);
>> +	/* The writer keeps the buffer, so the stream is not stopped. */
>> +	assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));
>> +	assert_ptr_equal(buf.buf, mem);
>> +	/* The unwritten data is dropped. */
>> +	assert_sizet_equal(buf_len(&buf), 0);
> I'm not sure that we should fixate this policy. Generally there is no
> guarantee what has happened with data. Except that it is not written
> successfully.
> Feel free to ignore.

Updated:

--- a/test/tarantool-c-tests/unit-wbuf.test.c
+++ b/test/tarantool-c-tests/unit-wbuf.test.c
@@ -359,7 +359,7 @@ static int test_flush_partial_write(void *state)
          */
         assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));
         assert_ptr_equal(buf.buf, mem);
-       /* The unwritten data is dropped. */
+       /* The buffer is reset after the flush. */
         assert_sizet_equal(wbuf_len(&buf), 0);

         return TEST_EXIT_SUCCESS;

>
>> +
>> +	return TEST_EXIT_SUCCESS;
>> +}
> <snipped>
>
>> +/*
>> +** A writer may release the buffer and set *data to NULL on an
>> +** unrecoverable error (e.g. ENOSPC). The stream is stopped in
>> +** this case, so the write functions must not touch the released
>> +** buffer.
>> +*/
> Minor: It is better to use
> | /*
> |  *
> |  */
>
> Comments here as in the most C/C++ code in the tests.
> The inconsistent comment style in the <unit-leb128.test.c> is my fault
> and should be fixed.

Fixed:

@@ -370,11 +372,11 @@ static int test_terminate(void *state)
  }

  /*
-** A writer may release the buffer and set *data to NULL on an
-** unrecoverable error (e.g. ENOSPC). The stream is stopped in
-** this case, so the write functions must not touch the released
-** buffer.
-*/
+ * A writer may release the buffer and set *data to NULL on an
+ * unrecoverable error (e.g. ENOSPC). The stream is stopped in
+ * this case, so the write functions must not touch the released
+ * buffer.
+ */
  int main(void)
  {
         const struct test_unit tgroup[] = {

>> +int main(void)
>> +{
>> +	const struct test_unit tgroup[] = {
>> +		test_unit_def(test_addbyte),
>> +		test_unit_def(test_addu64),
>> +		test_unit_def(test_addn),
>> +		test_unit_def(test_addstring),
>> +		test_unit_def(test_addn_pumped_through_buffer),
>> +		test_unit_def(test_flush_on_overflow),
>> +		test_unit_def(test_flush),
>> +		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_terminate),
>> +	};
>> +	const int test_result = test_run_group(tgroup, NULL);
> Minor: Why do we need this additional variable?

I don't like when `return` returns an expression. I would leave as is.

The same in leb128 test:

$ grep test_run_group test/tarantool-c-tests/unit-*.c
test/tarantool-c-tests/unit-leb128.test.c:      const int test_result = 
test_run_group(tgroup, NULL);
test/tarantool-c-tests/unit-tap.test.c: return test_run_group(tgroup, NULL);
test/tarantool-c-tests/unit-wbuf.test.c:        const int test_result = 
test_run_group(tgroup, NULL);

>
>> +
>> +	return test_result;
>> +}
>> -- 
>> 2.56.0
>>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush
  2026-11-11 16:34   ` Evgeniy Temirgaleev via Tarantool-patches
@ 2026-12-01 15:33     ` Sergey Bronnikov via Tarantool-patches
  0 siblings, 0 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-12-01 15:33 UTC (permalink / raw)
  To: Evgeniy Temirgaleev, Sergey Bronnikov; +Cc: 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 <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>
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush
  2026-11-15 15:12   ` Evgeniy Temirgaleev via Tarantool-patches
@ 2026-12-01 16:09     ` Sergey Bronnikov via Tarantool-patches
  0 siblings, 0 replies; 16+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-12-01 16:09 UTC (permalink / raw)
  To: Evgeniy Temirgaleev, Sergey Bronnikov, Sergey Kaplun; +Cc: tarantool-patches

Hi,

you are right regarding STREAM_ERRIO, but the proposed fix should not be 
a part of the fix for sysprof crash.

We can do this  later as a followup.

Sergey

On 11/15/26 18:12, Evgeniy Temirgaleev wrote:
> Hi, Sergey! It's an addition to my first letter:
>
> I saw the default implementation of the writer in the lib_misc.c:
>
> ```
> /*
> ** Default buffer writer function.
> ** Just call write to the corresponding descriptor.
> */
> static size_t buffer_writer_default(const void **buf_addr, size_t len,
>                     void *opt)
> ```
>
> It implements the writer definition
>
> ```
> /*
> ** Buffer writer which is called on the buffer flush.
> ** Should return amount of written bytes on success or zero in case of 
> error.
> ** *data should contain a buffer of at least the initial size.
> ** If *data == NULL stream stops.
> */
> typedef size_t (*lj_wbuf_writer)(const void **data, size_t len, void 
> *opt);
> ```
>
> in a way, where unrecoverable error follows with stream stop. It's a 
> more strong postcondition* ('res == 0 -> errno != 0 && *data == NULL') 
> (1) than the definition requires ('res == 0 -> errno != 0 || *data == 
> NULL') (2).
>
> But all existing LuaJIT code including your lj_wbuf.c patch actually 
> implies (1), not (2).
>
> I think, it's an option: leave your patch as is with additional fix of 
> the lj_wbuf_writer requirements to (1) with removing unused 
> STREAM_ERRIO flag.
>
> -- 
>
> (*) Actually buffer_writer_default() implements postcondition (1'):
>
> (1') ('res < len -> errno != 0 && *data == NULL')
>
> This must be fixed also (`return 0` instead of `return write_total`, 
> lib_misc.c, line 123).
>
> ---
>
> First letter quoting:
>
> On 11/2/26 4:12 PM, Sergey Bronnikov wrote:
>> From: Sergey Bronnikov <sergeyb@tarantool.org>
> <snipped>
>
>> 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 +-
> <snipped>
>
>> @@ -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'?"
>
>>     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?"
>
>>       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;
>>   }
> <snipped>
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2026-12-01 16:09 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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