<feed xmlns='http://www.w3.org/2005/Atom'>
<title>musl/src/malloc, branch master</title>
<subtitle>musl - an implementation of the standard library for Linux-based systems</subtitle>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/'/>
<entry>
<title>fix linkage namespace violations in mallocng</title>
<updated>2026-06-12T16:15:13+00:00</updated>
<author>
<name>Rich Felker</name>
<email>dalias@aerifal.cx</email>
</author>
<published>2026-06-12T16:15:13+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=3cc477a2cec8f591f04a74c32ff18a5150e704f3'/>
<id>3cc477a2cec8f591f04a74c32ff18a5150e704f3</id>
<content type='text'>
the namespace-safety remappings in glue.h handled mmap, madvise, and
mremap correctly but somehow overlooked munmap and mprotect.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
the namespace-safety remappings in glue.h handled mmap, madvise, and
mremap correctly but somehow overlooked munmap and mprotect.
</pre>
</div>
</content>
</entry>
<entry>
<title>mallocng: fix handling of allocations with extreme alignment</title>
<updated>2026-05-12T13:19:26+00:00</updated>
<author>
<name>Rich Felker</name>
<email>dalias@aerifal.cx</email>
</author>
<published>2026-05-12T13:19:26+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=bf96b52a4429eeeb60e6c961c8687e52d90c5fa1'/>
<id>bf96b52a4429eeeb60e6c961c8687e52d90c5fa1</id>
<content type='text'>
aligned allocations are handled by over-allocating enough to ensure an
aligned subrange exists and framing the usable space to that subrange.
the framing can only handle offsets up to a 32-bit multiple of the
allocation UNIT (16 bytes), and aligned_alloc correctly checks for and
rejects larger alignments.

however, when get_meta reads back the offset, the type of the variable
and everything else in the expression where it's used was int, not
size_t, and offset*UNIT can overflow. modulo the "anything can happen"
aspect of overflow being undefined, a clean trap will occur and the
program will terminate. this would happen on any call to free,
realloc, or malloc_usable_size call on the large-alignment object.

switch the type of offset in get_meta from int to size_t. this has
been checked not to break any of the subsequent assertions:

- assert(offset &gt; 0xffff) was wrongly rejecting offsets that would be
  interpreted as negative when converted to signed int. this is fixed
  by processing offset as unsigned.

- the check against the slot boundaries switches which assert would
  catch values that were previously interpreted as negative, but the
  net effect is the same.

- the check against maplen was already converting to unsigned long due
  to the 4096UL in the expression. in doing so, it was incorrectly
  sign-extending the offset rather than zero-extending. this is fixed
  by using unsigned type to begin with.

in addition, take the opportunity to trap on offsets were offset*UNIT
would overflow. this cannot happen on 64-bit archs (and it should be
optimized out by the compiler there), but it's an additional signal we
can use to catch out-of-bounds writes on 32-bit ones. and the check
only happens when operating on extremely large, overaligned objects,
so the relative cost of checking is essentially zero.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
aligned allocations are handled by over-allocating enough to ensure an
aligned subrange exists and framing the usable space to that subrange.
the framing can only handle offsets up to a 32-bit multiple of the
allocation UNIT (16 bytes), and aligned_alloc correctly checks for and
rejects larger alignments.

however, when get_meta reads back the offset, the type of the variable
and everything else in the expression where it's used was int, not
size_t, and offset*UNIT can overflow. modulo the "anything can happen"
aspect of overflow being undefined, a clean trap will occur and the
program will terminate. this would happen on any call to free,
realloc, or malloc_usable_size call on the large-alignment object.

switch the type of offset in get_meta from int to size_t. this has
been checked not to break any of the subsequent assertions:

- assert(offset &gt; 0xffff) was wrongly rejecting offsets that would be
  interpreted as negative when converted to signed int. this is fixed
  by processing offset as unsigned.

- the check against the slot boundaries switches which assert would
  catch values that were previously interpreted as negative, but the
  net effect is the same.

- the check against maplen was already converting to unsigned long due
  to the 4096UL in the expression. in doing so, it was incorrectly
  sign-extending the offset rather than zero-extending. this is fixed
  by using unsigned type to begin with.

in addition, take the opportunity to trap on offsets were offset*UNIT
would overflow. this cannot happen on 64-bit archs (and it should be
optimized out by the compiler there), but it's an additional signal we
can use to catch out-of-bounds writes on 32-bit ones. and the check
only happens when operating on extremely large, overaligned objects,
so the relative cost of checking is essentially zero.
</pre>
</div>
</content>
</entry>
<entry>
<title>mallocng: prevent stray ';' at top-level</title>
<updated>2026-03-11T02:48:21+00:00</updated>
<author>
<name>Michael Forney</name>
<email>mforney@mforney.org</email>
</author>
<published>2024-04-15T10:26:17+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=5c26c890ede40b5649d2ea23c51ea3ef34216486'/>
<id>5c26c890ede40b5649d2ea23c51ea3ef34216486</id>
<content type='text'>
The LOCK_OBJ_DEF macro is used with a trailing semicolon. However,
since the macro definition ends with the closing brace of a function
definition, the ISO C grammar does not allow an extra semicolon.

To fix this, swap the order of the two definitions, and drop the
semicolon from the __malloc_lock declaration.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
The LOCK_OBJ_DEF macro is used with a trailing semicolon. However,
since the macro definition ends with the closing brace of a function
definition, the ISO C grammar does not allow an extra semicolon.

To fix this, swap the order of the two definitions, and drop the
semicolon from the __malloc_lock declaration.
</pre>
</div>
</content>
</entry>
<entry>
<title>disable MADV_FREE usage in mallocng</title>
<updated>2022-10-19T18:01:31+00:00</updated>
<author>
<name>Rich Felker</name>
<email>dalias@aerifal.cx</email>
</author>
<published>2022-09-28T12:33:05+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=e6e8213244a816511e95e14fb99176442922abac'/>
<id>e6e8213244a816511e95e14fb99176442922abac</id>
<content type='text'>
the entire intent of using madvise/MADV_FREE on freed slots is to
improve system performance by avoiding evicting cache of useful data,
or swapping useless data to disk, by marking any whole pages in the
freed slot as discardable by the kernel. in particular, unlike
unmapping the memory or replacing it with a PROT_NONE region, use of
MADV_FREE does not make any difference to memory accounting for commit
charge purposes, and so does not increase the memory available to
other processes in a non-overcommitted environment.

however, various measurements have shown that inordinate amounts of
time are spent performing madvise syscalls in processes which
frequently allocate and free medium sized objects in the size range
roughly between PAGESIZE and MMAP_THRESHOLD, to the point that the net
effect is almost surely significant performance degredation. so, turn
it off.

the code, which has some nontrivial logic for efficiently determining
whether there is a whole-page range to apply madvise to, is left in
place so that it can easily be re-enabled if desired, or later tuned
to only apply to certain sizes or to use additional heuristics.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
the entire intent of using madvise/MADV_FREE on freed slots is to
improve system performance by avoiding evicting cache of useful data,
or swapping useless data to disk, by marking any whole pages in the
freed slot as discardable by the kernel. in particular, unlike
unmapping the memory or replacing it with a PROT_NONE region, use of
MADV_FREE does not make any difference to memory accounting for commit
charge purposes, and so does not increase the memory available to
other processes in a non-overcommitted environment.

however, various measurements have shown that inordinate amounts of
time are spent performing madvise syscalls in processes which
frequently allocate and free medium sized objects in the size range
roughly between PAGESIZE and MMAP_THRESHOLD, to the point that the net
effect is almost surely significant performance degredation. so, turn
it off.

the code, which has some nontrivial logic for efficiently determining
whether there is a whole-page range to apply madvise to, is left in
place so that it can easily be re-enabled if desired, or later tuned
to only apply to certain sizes or to use additional heuristics.
</pre>
</div>
</content>
</entry>
<entry>
<title>remove return with expression in void function</title>
<updated>2021-04-27T23:31:48+00:00</updated>
<author>
<name>Michael Forney</name>
<email>mforney@mforney.org</email>
</author>
<published>2021-04-27T22:59:55+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=d8cb888db9329d0e1b41060ec953c586c99a4a9f'/>
<id>d8cb888db9329d0e1b41060ec953c586c99a4a9f</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>mallocng/aligned_alloc: check for malloc failure</title>
<updated>2021-04-16T14:17:25+00:00</updated>
<author>
<name>Dominic Chen</name>
<email>d.c.ddcc@gmail.com</email>
</author>
<published>2021-03-25T22:20:14+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=95a540e176b3ebd57d6033fd23cf9fec8a5ddbe8'/>
<id>95a540e176b3ebd57d6033fd23cf9fec8a5ddbe8</id>
<content type='text'>
With mallocng, calling posix_memalign() or aligned_alloc() will
SIGSEGV if the internal malloc() call returns NULL. This does not
occur with oldmalloc, which explicitly checks for allocation failure.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
With mallocng, calling posix_memalign() or aligned_alloc() will
SIGSEGV if the internal malloc() call returns NULL. This does not
occur with oldmalloc, which explicitly checks for allocation failure.
</pre>
</div>
</content>
</entry>
<entry>
<title>oldmalloc: preserve errno across free</title>
<updated>2021-01-30T22:28:08+00:00</updated>
<author>
<name>Rich Felker</name>
<email>dalias@aerifal.cx</email>
</author>
<published>2021-01-30T22:28:08+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=9b77aaca86b53c367f23505c24dd3c02e240efad'/>
<id>9b77aaca86b53c367f23505c24dd3c02e240efad</id>
<content type='text'>
as an outcome of Austin Group issue #385, future versions of the
standard will require free not to alter the value of errno. save and
restore it individually around the calls to madvise and munmap so that
the cost is not imposed on calls to free that do not result in any
syscall.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
as an outcome of Austin Group issue #385, future versions of the
standard will require free not to alter the value of errno. save and
restore it individually around the calls to madvise and munmap so that
the cost is not imposed on calls to free that do not result in any
syscall.
</pre>
</div>
</content>
</entry>
<entry>
<title>fix build regression in oldmalloc</title>
<updated>2021-01-30T22:26:34+00:00</updated>
<author>
<name>Rich Felker</name>
<email>dalias@aerifal.cx</email>
</author>
<published>2021-01-30T22:26:34+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=98b9df994c85dcb6a8a5a9099495dd44c7cf2bce'/>
<id>98b9df994c85dcb6a8a5a9099495dd44c7cf2bce</id>
<content type='text'>
commit 8d37958d58cf36f53d5fcc7a8aa6d633da6071b2 inadvertently broke
oldmalloc by having it implement __libc_malloc rather than
__libc_malloc_impl.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit 8d37958d58cf36f53d5fcc7a8aa6d633da6071b2 inadvertently broke
oldmalloc by having it implement __libc_malloc rather than
__libc_malloc_impl.
</pre>
</div>
</content>
</entry>
<entry>
<title>preserve errno across free</title>
<updated>2021-01-30T22:14:20+00:00</updated>
<author>
<name>Rich Felker</name>
<email>dalias@aerifal.cx</email>
</author>
<published>2021-01-30T22:14:20+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=2010df0d64570db4ce29cc7df0e31f81aa26ae4a'/>
<id>2010df0d64570db4ce29cc7df0e31f81aa26ae4a</id>
<content type='text'>
as an outcome of Austin Group issue #385, future versions of the
standard will require free not to alter the value of errno. save and
restore it individually around the calls to madvise and munmap so that
the cost is not imposed on calls to free that do not result in any
syscall.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
as an outcome of Austin Group issue #385, future versions of the
standard will require free not to alter the value of errno. save and
restore it individually around the calls to madvise and munmap so that
the cost is not imposed on calls to free that do not result in any
syscall.
</pre>
</div>
</content>
</entry>
<entry>
<title>implement reallocarray</title>
<updated>2020-11-30T21:49:58+00:00</updated>
<author>
<name>Ariadne Conill</name>
<email>ariadne@dereferenced.org</email>
</author>
<published>2020-08-01T14:26:35+00:00</published>
<link rel='alternate' type='text/html' href='http://git.musl-libc.org/cgit/musl/commit/?id=821083ac7b54eaa040d5a8ddc67c6206a175e0ca'/>
<id>821083ac7b54eaa040d5a8ddc67c6206a175e0ca</id>
<content type='text'>
reallocarray is an extension introduced by OpenBSD, which introduces
calloc overflow checking to realloc.

glibc 2.28 introduced support for this function behind _GNU_SOURCE,
while glibc 2.29 allows its usage in _DEFAULT_SOURCE.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
reallocarray is an extension introduced by OpenBSD, which introduces
calloc overflow checking to realloc.

glibc 2.28 introduced support for this function behind _GNU_SOURCE,
while glibc 2.29 allows its usage in _DEFAULT_SOURCE.
</pre>
</div>
</content>
</entry>
</feed>
