wasi-libc icon indicating copy to clipboard operation
wasi-libc copied to clipboard

emmalloc: set errno on error paths

Open jedisct1 opened this issue 5 months ago • 3 comments

emmalloc functions were returning NULL on error, but didn't set errno.

jedisct1 avatar Sep 23 '25 08:09 jedisct1

Errno is not DCEable, requires an awkward TLS slot that user might not have asked for, and very rarely used in web space. (in the close to decade of emmalloc, nobody ever asked for errno support)

I think if something, we should make the alignment failures abort in development mode, rather than return 0. So that way if emmalloc return 0, one will know it was due to an OOM.

In -sABORTING_MALLOC builds, we could optimize out the return 0;s altogether. I.e. allocations will either return a valid pointer, or halt.

The main reason to use emmalloc instead of dlmalloc is to get smaller code size. So it would be best to optimize it for the smallest code size to keep that angle intact.

juj avatar Sep 24 '25 19:09 juj

I think if something, we should make the alignment failures abort in development mode, rather than return 0. So that way if emmalloc return 0, one will know it was due to an OOM.

I agree, except for the posx_memalign usage. That should probably still set errno and return on bad alignment.

sbc100 avatar Sep 24 '25 20:09 sbc100

I think if something, we should make the alignment failures abort in development mode, rather than return 0. So that way if emmalloc return 0, one will know it was due to an OOM.

I agree, except for the posx_memalign usage. That should probably still set errno and return on bad alignment.

In fact malloc itself can only ever fail with ENOMEM, according the man page. No other errors are possible.

sbc100 avatar Sep 24 '25 20:09 sbc100