[Tarantool-patches] [PATCH luajit 2/3] sysprof: fix crash on a failed stream flush

Evgeniy Temirgaleev e.temirgaleev at tarantool.org
Sun Nov 15 18:12:55 MSK 2026


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



More information about the Tarantool-patches mailing list