me

/

vinix-ports Public
382 commits 0 issues 0 pull requests 1 contributor Discussions Projects Snippets LOC history Platform CI

musl: build the Vinix runtime and sysroot from source

diviocity committed 6 days ago · 2162768ec3548cc7332b9437368a97b5728b2cad View patch
Unverified signature 8 files changed +1143 -0
musl/PKGBUILD.vile new file +55 -0

@@ -0,0 +1,55 @@

1+; Source and patch order follow Vinix build-support/musl at a58255423ede.

2+defpkgbuild(musl,

3+ #:pkgname, "musl",

4+ #:pkgver, "1.2.6",

5+ #:pkgrel, 1,

6+ #:pkgdesc, "Vinix C runtime, development headers and GCC specs",

7+ #:arch, [#:x86-64],

8+ #:url, "https://musl.libc.org/",

9+ #:license, "MIT",

10+ #:source, ["https://musl.libc.org/releases/musl-1.2.6.tar.gz",

11+ "patches/handle-aux-at_base.patch",

12+ "patches/0001-add-stub-for-pthread_mutexattr_setprioceiling.patch",

13+ "patches/fix-loongarch64-zero-len-extcontext.patch",

14+ "patches/CVE-2026-6042.patch",

15+ "patches/CVE-2026-40200.patch",

16+ "patches/malloc-retain.patch"],

17+ #:sha256sums, ["d585fd3b613c66151fc3249e8ed44f77020cb5e6c1e635a616d3f9f82460512a",

18+ "2a65e03b94217063d72fe998e5fb2e25cd01716078a3ba1b0ab574ab205111e6",

19+ "b2067680c66af9eb9f4fdc1fa5d9337a9fd62bdab2c437b416aa97d79eb884ee",

20+ "92c8e965798ed516c15cb2a4e7c4ac45ec1eb68b6abff16c73adff2a3bae1b14",

21+ "1d0be2e72b9d5bd16546b923aa8af861d271322f01644716a81823bec4065c99",

22+ "1ee29f64f9ca8e8ad7c349779d661ff6b52126a27575d3586981357a52c406fb",

23+ "ec459e48e5c3c9946c91e805428149802d0ab1835c64e5e023811ab54a48af1d"],

24+

25+ #:prepare, fn() {

26+ for-each(fn(patch) {

27+ run("patch", "--batch", "--fuzz=0", "-d", "musl-1.2.6", "-p1", "-i", string-append("../", patch))

28+ }, list("handle-aux-at_base.patch", "0001-add-stub-for-pthread_mutexattr_setprioceiling.patch",

29+ "fix-loongarch64-zero-len-extcontext.patch", "CVE-2026-6042.patch",

30+ "CVE-2026-40200.patch", "malloc-retain.patch"))

31+ ; Match the x86-64 string routines selected by the Vinix system libc.

32+ run("rm", "musl-1.2.6/src/string/x86_64/memcpy.s", "musl-1.2.6/src/string/x86_64/memmove.s")

33+ },

34+ #:build, fn() {

35+ run("env", "CC=gcc", "AR=ar", "RANLIB=ranlib", "CFLAGS=-fstack-protector-strong -DVINIX_MALLOC_RETAIN=1",

36+ "LDFLAGS=-Wl,-soname,libc.musl-x86_64.so.1", "musl-1.2.6/configure",

37+ "--target=x86_64-linux-musl", "--prefix=/usr", "--syslibdir=/lib", "--enable-wrapper=gcc",

38+ "--enable-optimize=internal,malloc,malloc/mallocng/*.c,string")

39+ run("make")

40+ },

41+ #:package, fn() {

42+ run("make", "install", string-append("DESTDIR=", pkgdir()))

43+ ; The SONAME, ELF interpreter and development linker name refer to one file.

44+ loader := string-append(pkgdir(), "/lib/ld-musl-x86_64.so.1")

45+ devlib := string-append(pkgdir(), "/usr/lib/libc.so")

46+ run("rm", loader)

47+ run("mv", devlib, loader)

48+ run("ln", "-s", "ld-musl-x86_64.so.1", string-append(pkgdir(), "/lib/libc.musl-x86_64.so.1"))

49+ run("ln", "-s", "../../lib/ld-musl-x86_64.so.1", devlib)

50+ ; GCC expands %R to its --sysroot. Leave the interpreter path guest-absolute.

51+ specs := string-append(pkgdir(), "/usr/lib/musl-gcc-sysroot.specs")

52+ run("cp", string-append(pkgdir(), "/usr/lib/musl-gcc.specs"), specs)

53+ run("sed", "-i", "-e", "s|/usr/include|%R/usr/include|g", "-e", "s|/usr/lib|%R/usr/lib|g", specs)

54+ run("install", "-Dm644", "musl-1.2.6/COPYRIGHT", string-append(pkgdir(), "/usr/share/licenses/musl/COPYRIGHT"))

55+ })

musl/patches/.gitattributes new file +2 -0

@@ -0,0 +1,2 @@

1+# Preserve upstream patch bytes, including their diff context.

2+*.patch -text -whitespace

musl/patches/0001-add-stub-for-pthread_mutexattr_setprioceiling.patch new file +27 -0

@@ -0,0 +1,27 @@

1+From d090ac2228678e124cb39f355d9f2979094fa197 Mon Sep 17 00:00:00 2001

2+From: Ariadne Conill <ariadne@ariadne.space>

3+Date: Sat, 26 Jul 2025 14:33:25 -0700

4+Subject: [PATCH] add stub for pthread_mutexattr_setprioceiling

5+

6+this function has been in pthread.h for years, but not actually implemented.

7+like pthread_mutex_setprioceiling, we make this a stub function.

8+---

9+ src/thread/pthread_mutexattr_setprioceiling.c | 6 ++++++

10+ 1 file changed, 6 insertions(+)

11+ create mode 100644 src/thread/pthread_mutexattr_setprioceiling.c

12+

13+diff --git a/src/thread/pthread_mutexattr_setprioceiling.c b/src/thread/pthread_mutexattr_setprioceiling.c

14+new file mode 100644

15+index 00000000..d543e169

16+--- /dev/null

17++++ b/src/thread/pthread_mutexattr_setprioceiling.c

18+@@ -0,0 +1,6 @@

19++#include "pthread_impl.h"

20++

21++int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *a, int ceiling)

22++{

23++ return EINVAL;

24++}

25+--

26+2.50.1

27+

musl/patches/CVE-2026-40200.patch new file +179 -0

@@ -0,0 +1,179 @@

1+>From 228da39e38c1cae13cbe637e771412c1984dba5d Mon Sep 17 00:00:00 2001

2+From: Rich Felker <dalias@aerifal.cx>

3+Date: Thu, 9 Apr 2026 22:51:30 -0400

4+Subject: [PATCH 1/3] qsort: fix leonardo heap corruption from bug in

5+ doubleword ctz primitive

6+

7+the pntz function, implementing a "count trailing zeros" variant for a

8+bit vector consisting of two size_t words, erroneously returned zero

9+rather than the number of bits in the low word when the first bit set

10+was the low bit of the high word.

11+

12+as a result, a loop in the trinkle function which should have a

13+guaranteed small bound on the number of iterations, could run

14+unboundedly, thereby overflowing a stack-based working-space array

15+which was sized for the bound.

16+

17+CVE-2026-40200 has been assigned for this issue.

18+---

19+ src/stdlib/qsort.c | 8 ++++----

20+ 1 file changed, 4 insertions(+), 4 deletions(-)

21+

22+diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c

23+index ab79dc6f..13219ab3 100644

24+--- a/src/stdlib/qsort.c

25++++ b/src/stdlib/qsort.c

26+@@ -34,11 +34,11 @@

27+

28+ typedef int (*cmpfun)(const void *, const void *, void *);

29+

30++/* returns index of first bit set, excluding the low bit assumed to always

31++ * be set, starting from low bit of p[0] up through high bit of p[1] */

32+ static inline int pntz(size_t p[2]) {

33+- int r = ntz(p[0] - 1);

34+- if(r != 0 || (r = 8*sizeof(size_t) + ntz(p[1])) != 8*sizeof(size_t)) {

35+- return r;

36+- }

37++ if (p[0] != 1) return ntz(p[0] - 1);

38++ if (p[1]) return 8*sizeof(size_t) + ntz(p[1]);

39+ return 0;

40+ }

41+

42+--

43+2.21.0

44+

45+

46+>From b3291b9a9f77f1f993d2b4f8c68a26cf09221ae7 Mon Sep 17 00:00:00 2001

47+From: Rich Felker <dalias@aerifal.cx>

48+Date: Thu, 9 Apr 2026 23:40:53 -0400

49+Subject: [PATCH 2/3] qsort: hard-preclude oob array writes independent of any

50+ invariants

51+

52+while the root cause of CVE-2026-40200 was a faulty ctz primitive, the

53+fallout of the bug would have been limited to erroneous sorting or

54+infinite loop if not for the stores to a stack-based array that

55+depended on trusting invariants in order not to go out of bounds.

56+

57+increase the size of the array to a power of two so that we can mask

58+indices into it to force them into range. in the absence of any

59+further bug, the masking is a no-op, but it does not have any

60+measurable performance cost, and it makes spatial memory safety

61+trivial to prove (and for readers not familiar with the algorithms to

62+trust).

63+---

64+ src/stdlib/qsort.c | 20 +++++++++++++-------

65+ 1 file changed, 13 insertions(+), 7 deletions(-)

66+

67+diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c

68+index 13219ab3..e4bce9f7 100644

69+--- a/src/stdlib/qsort.c

70++++ b/src/stdlib/qsort.c

71+@@ -89,10 +89,16 @@ static inline void shr(size_t p[2], int n)

72+ p[1] >>= n;

73+ }

74+

75++/* power-of-two length for working array so that we can mask indices and

76++ * not depend on any invariant of the algorithm for spatial memory safety.

77++ * the original size was just 14*sizeof(size_t)+1 */

78++#define AR_LEN (16 * sizeof(size_t))

79++#define AR_MASK (AR_LEN - 1)

80++

81+ static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int pshift, size_t lp[])

82+ {

83+ unsigned char *rt, *lf;

84+- unsigned char *ar[14 * sizeof(size_t) + 1];

85++ unsigned char *ar[AR_LEN];

86+ int i = 1;

87+

88+ ar[0] = head;

89+@@ -104,16 +110,16 @@ static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int p

90+ break;

91+ }

92+ if(cmp(lf, rt, arg) >= 0) {

93+- ar[i++] = lf;

94++ ar[i++ & AR_MASK] = lf;

95+ head = lf;

96+ pshift -= 1;

97+ } else {

98+- ar[i++] = rt;

99++ ar[i++ & AR_MASK] = rt;

100+ head = rt;

101+ pshift -= 2;

102+ }

103+ }

104+- cycle(width, ar, i);

105++ cycle(width, ar, i & AR_MASK);

106+ }

107+

108+ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, size_t pp[2], int pshift, int trusty, size_t lp[])

109+@@ -121,7 +127,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si

110+ unsigned char *stepson,

111+ *rt, *lf;

112+ size_t p[2];

113+- unsigned char *ar[14 * sizeof(size_t) + 1];

114++ unsigned char *ar[AR_LEN];

115+ int i = 1;

116+ int trail;

117+

118+@@ -142,7 +148,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si

119+ }

120+ }

121+

122+- ar[i++] = stepson;

123++ ar[i++ & AR_MASK] = stepson;

124+ head = stepson;

125+ trail = pntz(p);

126+ shr(p, trail);

127+@@ -150,7 +156,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si

128+ trusty = 0;

129+ }

130+ if(!trusty) {

131+- cycle(width, ar, i);

132++ cycle(width, ar, i & AR_MASK);

133+ sift(head, width, cmp, arg, pshift, lp);

134+ }

135+ }

136+--

137+2.21.0

138+

139+

140+>From 5122f9f3c99fee366167c5de98b31546312921ab Mon Sep 17 00:00:00 2001

141+From: Luca Kellermann <mailto.luca.kellermann@gmail.com>

142+Date: Fri, 10 Apr 2026 03:03:22 +0200

143+Subject: [PATCH 3/3] qsort: fix shift UB in shl and shr

144+

145+if shl() or shr() are called with n==8*sizeof(size_t), n is adjusted

146+to 0. the shift by (sizeof(size_t) * 8 - n) that then follows will

147+consequently shift by the width of size_t, which is UB and in practice

148+produces an incorrect result.

149+

150+return early in this case. the bitvector p was already shifted by the

151+required amount.

152+---

153+ src/stdlib/qsort.c | 2 ++

154+ 1 file changed, 2 insertions(+)

155+

156+diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c

157+index e4bce9f7..28607450 100644

158+--- a/src/stdlib/qsort.c

159++++ b/src/stdlib/qsort.c

160+@@ -71,6 +71,7 @@ static inline void shl(size_t p[2], int n)

161+ n -= 8 * sizeof(size_t);

162+ p[1] = p[0];

163+ p[0] = 0;

164++ if (!n) return;

165+ }

166+ p[1] <<= n;

167+ p[1] |= p[0] >> (sizeof(size_t) * 8 - n);

168+@@ -83,6 +84,7 @@ static inline void shr(size_t p[2], int n)

169+ n -= 8 * sizeof(size_t);

170+ p[0] = p[1];

171+ p[1] = 0;

172++ if (!n) return;

173+ }

174+ p[0] >>= n;

175+ p[0] |= p[1] << (sizeof(size_t) * 8 - n);

176+--

177+2.21.0

178+

179+

musl/patches/CVE-2026-6042.patch new file +320 -0

@@ -0,0 +1,320 @@

1+From 67219f0130ec7c876ac0b299046460fad31caabf Mon Sep 17 00:00:00 2001

2+From: Rich Felker <dalias@aerifal.cx>

3+Date: Mon, 30 Mar 2026 16:00:50 -0400

4+Subject: fix pathological slowness & incorrect mappings in iconv gb18030

5+ decoder

6+

7+in order to implement the "UTF" aspect of gb18030 (ability to

8+represent arbitrary unicode characters not present in the 2-byte

9+mapping), we have to apply the index obtained from the encoded 4-byte

10+sequence into the set of unmapped characters. this was done by

11+scanning repeatedly over the table of mapped characters and counting

12+off mapped characters below a running index by which to adjust the

13+running index by on each iteration. this iterative process eventually

14+leaves us with the value of the Nth unmapped character replacing the

15+index, but depending on which particular character that is, the number

16+of iterations needed to find it can be in the tens of thousands, and

17+each iteration traverses the whole 126x190 table in the inner loop.

18+this can lead to run times exceeding an entire second per character on

19+moderate-speed machines.

20+

21+on top of that, the transformation logic produced wrong results for

22+BMP characters above the the surrogate range, as a result of not

23+correctly accounting for it being excluded, and for characters outside

24+the BMP, as a result of a misunderstanding of how gb18030 encodes

25+them.

26+

27+this patch replaces the unmapped character lookup with a single linear

28+search of a list of unmapped ranges. there are only 206 such ranges,

29+and these are permanently assigned and unchangeable as a consequence

30+of the character encoding having to be stable, so a simple array of

31+16-bit start/length values for each range consumes only 824 bytes, a

32+very reasonable size cost here.

33+

34+this new table accounts for the previously-incorrect surrogate

35+handling, and non-BMP characters are handled correctly by a single

36+offset, without the need for any unmapped-range search.

37+

38+there are still a small number of mappings that are incorrect due to

39+late changes made in the definition of gb18030, swapping PUA

40+codepoints with proper Unicode characters. correcting these requires a

41+postprocessing step that will be added later.

42+---

43+ src/locale/gb18030utf.h | 206 ++++++++++++++++++++++++++++++++++++++++++++++++

44+ src/locale/iconv.c | 33 +++++---

45+ 2 files changed, 230 insertions(+), 9 deletions(-)

46+ create mode 100644 src/locale/gb18030utf.h

47+

48+diff --git a/src/locale/gb18030utf.h b/src/locale/gb18030utf.h

49+new file mode 100644

50+index 00000000..322a2440

51+--- /dev/null

52++++ b/src/locale/gb18030utf.h

53+@@ -0,0 +1,206 @@

54++{ 0x80, 36 },

55++{ 0xa5, 2 },

56++{ 0xa9, 7 },

57++{ 0xb2, 5 },

58++{ 0xb8, 31 },

59++{ 0xd8, 8 },

60++{ 0xe2, 6 },

61++{ 0xeb, 1 },

62++{ 0xee, 4 },

63++{ 0xf4, 3 },

64++{ 0xf8, 1 },

65++{ 0xfb, 1 },

66++{ 0xfd, 4 },

67++{ 0x102, 17 },

68++{ 0x114, 7 },

69++{ 0x11c, 15 },

70++{ 0x12c, 24 },

71++{ 0x145, 3 },

72++{ 0x149, 4 },

73++{ 0x14e, 29 },

74++{ 0x16c, 98 },

75++{ 0x1cf, 1 },

76++{ 0x1d1, 1 },

77++{ 0x1d3, 1 },

78++{ 0x1d5, 1 },

79++{ 0x1d7, 1 },

80++{ 0x1d9, 1 },

81++{ 0x1db, 1 },

82++{ 0x1dd, 28 },

83++{ 0x1fa, 87 },

84++{ 0x252, 15 },

85++{ 0x262, 101 },

86++{ 0x2c8, 1 },

87++{ 0x2cc, 13 },

88++{ 0x2da, 183 },

89++{ 0x3a2, 1 },

90++{ 0x3aa, 7 },

91++{ 0x3c2, 1 },

92++{ 0x3ca, 55 },

93++{ 0x402, 14 },

94++{ 0x450, 1 },

95++{ 0x452, 7102 },

96++{ 0x2011, 2 },

97++{ 0x2017, 1 },

98++{ 0x201a, 2 },

99++{ 0x201e, 7 },

100++{ 0x2027, 9 },

101++{ 0x2031, 1 },

102++{ 0x2034, 1 },

103++{ 0x2036, 5 },

104++{ 0x203c, 112 },

105++{ 0x20ad, 86 },

106++{ 0x2104, 1 },

107++{ 0x2106, 3 },

108++{ 0x210a, 12 },

109++{ 0x2117, 10 },

110++{ 0x2122, 62 },

111++{ 0x216c, 4 },

112++{ 0x217a, 22 },

113++{ 0x2194, 2 },

114++{ 0x219a, 110 },

115++{ 0x2209, 6 },

116++{ 0x2210, 1 },

117++{ 0x2212, 3 },

118++{ 0x2216, 4 },

119++{ 0x221b, 2 },

120++{ 0x2221, 2 },

121++{ 0x2224, 1 },

122++{ 0x2226, 1 },

123++{ 0x222c, 2 },

124++{ 0x222f, 5 },

125++{ 0x2238, 5 },

126++{ 0x223e, 10 },

127++{ 0x2249, 3 },

128++{ 0x224d, 5 },

129++{ 0x2253, 13 },

130++{ 0x2262, 2 },

131++{ 0x2268, 6 },

132++{ 0x2270, 37 },

133++{ 0x2296, 3 },

134++{ 0x229a, 11 },

135++{ 0x22a6, 25 },

136++{ 0x22c0, 82 },

137++{ 0x2313, 333 },

138++{ 0x246a, 10 },

139++{ 0x249c, 100 },

140++{ 0x254c, 4 },

141++{ 0x2574, 13 },

142++{ 0x2590, 3 },

143++{ 0x2596, 10 },

144++{ 0x25a2, 16 },

145++{ 0x25b4, 8 },

146++{ 0x25be, 8 },

147++{ 0x25c8, 3 },

148++{ 0x25cc, 2 },

149++{ 0x25d0, 18 },

150++{ 0x25e6, 31 },

151++{ 0x2607, 2 },

152++{ 0x260a, 54 },

153++{ 0x2641, 1 },

154++{ 0x2643, 2110 },

155++{ 0x2e82, 2 },

156++{ 0x2e85, 3 },

157++{ 0x2e89, 2 },

158++{ 0x2e8d, 10 },

159++{ 0x2e98, 15 },

160++{ 0x2ea8, 2 },

161++{ 0x2eab, 3 },

162++{ 0x2eaf, 4 },

163++{ 0x2eb4, 2 },

164++{ 0x2eb8, 3 },

165++{ 0x2ebc, 14 },

166++{ 0x2ecb, 293 },

167++{ 0x2ffc, 4 },

168++{ 0x3004, 1 },

169++{ 0x3018, 5 },

170++{ 0x301f, 2 },

171++{ 0x302a, 20 },

172++{ 0x303f, 2 },

173++{ 0x3094, 7 },

174++{ 0x309f, 2 },

175++{ 0x30f7, 5 },

176++{ 0x30ff, 6 },

177++{ 0x312a, 246 },

178++{ 0x322a, 7 },

179++{ 0x3232, 113 },

180++{ 0x32a4, 234 },

181++{ 0x3390, 12 },

182++{ 0x339f, 2 },

183++{ 0x33a2, 34 },

184++{ 0x33c5, 9 },

185++{ 0x33cf, 2 },

186++{ 0x33d3, 2 },

187++{ 0x33d6, 113 },

188++{ 0x3448, 43 },

189++{ 0x3474, 298 },

190++{ 0x359f, 111 },

191++{ 0x360f, 11 },

192++{ 0x361b, 765 },

193++{ 0x3919, 85 },

194++{ 0x396f, 96 },

195++{ 0x39d1, 14 },

196++{ 0x39e0, 147 },

197++{ 0x3a74, 218 },

198++{ 0x3b4f, 287 },

199++{ 0x3c6f, 113 },

200++{ 0x3ce1, 885 },

201++{ 0x4057, 264 },

202++{ 0x4160, 471 },

203++{ 0x4338, 116 },

204++{ 0x43ad, 4 },

205++{ 0x43b2, 43 },

206++{ 0x43de, 248 },

207++{ 0x44d7, 373 },

208++{ 0x464d, 20 },

209++{ 0x4662, 193 },

210++{ 0x4724, 5 },

211++{ 0x472a, 82 },

212++{ 0x477d, 16 },

213++{ 0x478e, 441 },

214++{ 0x4948, 50 },

215++{ 0x497b, 2 },

216++{ 0x497e, 4 },

217++{ 0x4984, 1 },

218++{ 0x4987, 20 },

219++{ 0x499c, 3 },

220++{ 0x49a0, 22 },

221++{ 0x49b8, 703 },

222++{ 0x4c78, 39 },

223++{ 0x4ca4, 111 },

224++{ 0x4d1a, 148 },

225++{ 0x4daf, 81 },

226++{ 0x9fa6, 14426 },

227++{ 0xe76c, 1 },

228++{ 0xe7c8, 1 },

229++{ 0xe7e7, 13 },

230++{ 0xe815, 1 },

231++{ 0xe819, 5 },

232++{ 0xe81f, 7 },

233++{ 0xe827, 4 },

234++{ 0xe82d, 4 },

235++{ 0xe833, 8 },

236++{ 0xe83c, 7 },

237++{ 0xe844, 16 },

238++{ 0xe856, 14 },

239++{ 0xe865, 4295 },

240++{ 0xf92d, 76 },

241++{ 0xf97a, 27 },

242++{ 0xf996, 81 },

243++{ 0xf9e8, 9 },

244++{ 0xf9f2, 26 },

245++{ 0xfa10, 1 },

246++{ 0xfa12, 1 },

247++{ 0xfa15, 3 },

248++{ 0xfa19, 6 },

249++{ 0xfa22, 1 },

250++{ 0xfa25, 2 },

251++{ 0xfa2a, 1030 },

252++{ 0xfe32, 1 },

253++{ 0xfe45, 4 },

254++{ 0xfe53, 1 },

255++{ 0xfe58, 1 },

256++{ 0xfe67, 1 },

257++{ 0xfe6c, 149 },

258++{ 0xff5f, 129 },

259++{ 0xffe6, 26 },

260+diff --git a/src/locale/iconv.c b/src/locale/iconv.c

261+index 52178950..4151411d 100644

262+--- a/src/locale/iconv.c

263++++ b/src/locale/iconv.c

264+@@ -74,6 +74,10 @@ static const unsigned short gb18030[126][190] = {

265+ #include "gb18030.h"

266+ };

267+

268++static const unsigned short gb18030utf[][2] = {

269++#include "gb18030utf.h"

270++};

271++

272+ static const unsigned short big5[89][157] = {

273+ #include "big5.h"

274+ };

275+@@ -224,6 +228,8 @@ static unsigned uni_to_jis(unsigned c)

276+ }

277+ }

278+

279++#define countof(a) (sizeof (a) / sizeof *(a))

280++

281+ size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)

282+ {

283+ size_t x=0;

284+@@ -430,15 +436,24 @@ size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restri

285+ d = *((unsigned char *)*in + 3);

286+ if (d-'0'>9) goto ilseq;

287+ c += d-'0';

288+- c += 128;

289+- for (d=0; d<=c; ) {

290+- k = 0;

291+- for (int i=0; i<126; i++)

292+- for (int j=0; j<190; j++)

293+- if (gb18030[i][j]-d <= c-d)

294+- k++;

295+- d = c+1;

296+- c += k;

297++ /* Starting at 90 30 81 30 (189000), mapping is

298++ * linear without gaps, to U+10000 and up. */

299++ if (c >= 189000) {

300++ c -= 189000;

301++ c += 0x10000;

302++ if (c >= 0x110000) goto ilseq;

303++ break;

304++ }

305++ /* Otherwise we must process an index into set

306++ * of characters unmapped by 2-byte table. */

307++ for (int i=0; ; i++) {

308++ if (i==countof(gb18030utf))

309++ goto ilseq;

310++ if (c<gb18030utf[i][1]) {

311++ c += gb18030utf[i][0];

312++ break;

313++ }

314++ c -= gb18030utf[i][1];

315+ }

316+ break;

317+ }

318+--

319+cgit v1.2.1

320+

musl/patches/fix-loongarch64-zero-len-extcontext.patch new file +22 -0

@@ -0,0 +1,22 @@

1+diff --git a/arch/loongarch64/bits/signal.h b/arch/loongarch64/bits/signal.h

2+index 1487c0d..2725276 100644

3+--- a/arch/loongarch64/bits/signal.h

4++++ b/arch/loongarch64/bits/signal.h

5+@@ -32,7 +32,7 @@ struct sigcontext {

6+ unsigned long sc_pc;

7+ unsigned long sc_regs[32];

8+ unsigned sc_flags;

9+- unsigned long sc_extcontext[] __attribute__((__aligned__(16)));

10++ unsigned long sc_extcontext[0] __attribute__((__aligned__(16)));

11+ };

12+

13+ #define CONTEXT_INFO_ALIGN 16

14+@@ -82,7 +82,7 @@ typedef struct {

15+ unsigned long __pc;

16+ unsigned long __gregs[32];

17+ unsigned __flags;

18+- unsigned long __extcontext[] __attribute__((__aligned__(16)));

19++ unsigned long __extcontext[0] __attribute__((__aligned__(16)));

20+ } mcontext_t;

21+

22+ struct sigaltstack {

musl/patches/handle-aux-at_base.patch new file +46 -0

@@ -0,0 +1,46 @@

1+This is required to make the gcompat ELF interpreter stub work with some

2+packed binaries.

3+

4+diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c

5+index b125eb1..616c6a6 100644

6+--- a/src/env/__init_tls.c

7++++ b/src/env/__init_tls.c

8+@@ -66,8 +66,10 @@ void *__copy_tls(unsigned char *mem)

9+ }

10+

11+ #if ULONG_MAX == 0xffffffff

12++typedef Elf32_Ehdr Ehdr;

13+ typedef Elf32_Phdr Phdr;

14+ #else

15++typedef Elf64_Ehdr Ehdr;

16+ typedef Elf64_Phdr Phdr;

17+ #endif

18+

19+@@ -77,15 +79,23 @@ extern const size_t _DYNAMIC[];

20+ static void static_init_tls(size_t *aux)

21+ {

22+ unsigned char *p;

23+- size_t n;

24++ size_t n, e;

25+ Phdr *phdr, *tls_phdr=0;

26+ size_t base = 0;

27+ void *mem;

28+

29+- for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {

30++ if (aux[AT_BASE]) {

31++ Ehdr *ehdr = (void *)aux[AT_BASE];

32++ p = (unsigned char *)aux[AT_BASE] + ehdr->e_phoff;

33++ n = ehdr->e_phnum;

34++ e = ehdr->e_phentsize;

35++ } else {

36++ p = (void *)aux[AT_PHDR];

37++ n = aux[AT_PHNUM];

38++ e = aux[AT_PHENT];

39++ }

40++ for (; n; n--, p+=e) {

41+ phdr = (void *)p;

42+- if (phdr->p_type == PT_PHDR)

43+- base = aux[AT_PHDR] - phdr->p_vaddr;

44+ if (phdr->p_type == PT_DYNAMIC && _DYNAMIC)

45+ base = (size_t)_DYNAMIC - phdr->p_vaddr;

46+ if (phdr->p_type == PT_TLS)

musl/patches/malloc-retain.patch new file +492 -0

@@ -0,0 +1,492 @@

1+--- a/src/malloc/mallocng/meta.h

2++++ b/src/malloc/mallocng/meta.h

3+@@ -10,6 +10,17 @@

4+ extern const uint16_t size_classes[];

5+

6+ #define MMAP_THRESHOLD 131052

7++

8++/* Vinix anonymous mappings cost page tables, zeroing and TLB maintenance.

9++ * Reuse a bounded number of ordinary groups and large mappings. Set this

10++ * to zero at build time to retain upstream release policy. */

11++#ifndef VINIX_MALLOC_RETAIN

12++#define VINIX_MALLOC_RETAIN 1

13++#endif

14++#define LARGE_CACHE_COUNT 5

15++#define LARGE_CACHE_MAX_PAGES 512

16++#define RETAIN_GROUP_MAX_BYTES (128UL*1024)

17++

18+

19+ #define UNIT 16

20+ #define IB 4

21+@@ -51,6 +62,7 @@

22+ struct meta_area *meta_area_head, *meta_area_tail;

23+ unsigned char *avail_meta_areas;

24+ struct meta *active[48];

25++ struct meta *large_cache[LARGE_CACHE_COUNT];

26+ size_t usage_by_class[48];

27+ uint8_t unmap_seq[32], bounces[32];

28+ uint8_t seq;

29+@@ -116,8 +128,14 @@

30+ {

31+ assert(!m->avail_mask);

32+ uint32_t mask, act = (2u<<m->mem->active_idx)-1;

33+- do mask = m->freed_mask;

34+- while (a_cas(&m->freed_mask, mask, mask&~act)!=mask);

35++ /* No other thread can publish frees while need_locks is zero. */

36++ if (!MT) {

37++ mask = m->freed_mask;

38++ m->freed_mask = mask & ~act;

39++ } else {

40++ do mask = m->freed_mask;

41++ while (a_cas(&m->freed_mask, mask, mask&~act)!=mask);

42++ }

43+ return m->avail_mask = mask & act;

44+ }

45+

46+@@ -126,7 +144,14 @@

47+ return p[-3] & 31;

48+ }

49+

50+-static inline struct meta *get_meta(const unsigned char *p)

51++struct slot_info {

52++ size_t stride, class_stride;

53++ int index;

54++};

55++

56++/* Share values already needed for header validation with free(). Other

57++ * callers keep the same pointer-only get_meta interface. */

58++static inline struct meta *get_meta_with_info(const unsigned char *p, struct slot_info *info)

59+ {

60+ assert(!((uintptr_t)p & 15));

61+ int offset = *(const uint16_t *)(p - 2);

62+@@ -144,16 +169,35 @@

63+ assert(!(meta->freed_mask & (1u<<index)));

64+ const struct meta_area *area = (void *)((uintptr_t)meta & -4096);

65+ assert(area->check == ctx.secret);

66+- if (meta->sizeclass < 48) {

67+- assert(offset >= size_classes[meta->sizeclass]*index);

68+- assert(offset < size_classes[meta->sizeclass]*(index+1));

69+- } else {

70+- assert(meta->sizeclass == 63);

71+- }

72+- if (meta->maplen) {

73+- assert(offset <= meta->maplen*4096UL/UNIT - 1);

74++ size_t class_stride = 0;

75++ int sc = meta->sizeclass;

76++ if (sc < 48) {

77++ int units = size_classes[sc];

78++ assert(offset >= units*index);

79++ assert(offset < units*(index+1));

80++ class_stride = UNIT*units;

81++ } else {

82++ assert(sc == 63);

83++ /* Direct mappings always own one slot and a nonempty map. */

84++ assert(!meta->last_idx);

85++ assert(meta->maplen);

86++ }

87++ size_t maplen = meta->maplen;

88++ if (maplen) {

89++ assert(offset <= maplen*4096UL/UNIT - 1);

90++ }

91++ if (info) {

92++ info->index = index;

93++ info->class_stride = class_stride;

94++ info->stride = !meta->last_idx && maplen ? maplen*4096UL-UNIT :

95++ class_stride;

96+ }

97+ return (struct meta *)meta;

98++}

99++

100++static inline struct meta *get_meta(const unsigned char *p)

101++{

102++ return get_meta_with_info(p, 0);

103+ }

104+

105+ static inline size_t get_nominal_size(const unsigned char *p, const unsigned char *end)

106+@@ -181,7 +225,7 @@

107+ }

108+ }

109+

110+-static inline void set_size(unsigned char *p, unsigned char *end, size_t n)

111++static inline void set_size_at(unsigned char *p, unsigned char *end, size_t n, int idx)

112+ {

113+ int reserved = end-p-n;

114+ if (reserved) end[-reserved] = 0;

115+@@ -190,7 +234,12 @@

116+ end[-5] = 0;

117+ reserved = 5;

118+ }

119+- p[-3] = (p[-3]&31) + (reserved<<5);

120++ p[-3] = idx + (reserved<<5);

121++}

122++

123++static inline void set_size(unsigned char *p, unsigned char *end, size_t n)

124++{

125++ set_size_at(p, end, n, p[-3]&31);

126+ }

127+

128+ static inline void *enframe(struct meta *g, int idx, size_t n, int ctr)

129+@@ -201,14 +250,20 @@

130+ unsigned char *end = p+stride-IB;

131+ // cycle offset within slot to increase interval to address

132+ // reuse, facilitate trapping double-free.

133+- int off = (p[-3] ? *(uint16_t *)(p-2) + 1 : ctr) & 255;

134++ int off = 0;

135+ assert(!p[-4]);

136+- if (off > slack) {

137+- size_t m = slack;

138+- m |= m>>1; m |= m>>2; m |= m>>4;

139+- off &= m;

140+- if (off > slack) off -= slack+1;

141+- assert(off <= slack);

142++ /* With no whole unit of slack, clamping always chooses offset zero.

143++ * Skip reading and cycling the old offset in that case. Slot selection

144++ * still rotates, and positive-slack slots keep upstream offset cycling. */

145++ if (slack) {

146++ off = (p[-3] ? *(uint16_t *)(p-2) + 1 : ctr) & 255;

147++ if (off > slack) {

148++ size_t m = slack;

149++ m |= m>>1; m |= m>>2; m |= m>>4;

150++ off &= m;

151++ if (off > slack) off -= slack+1;

152++ assert(off <= slack);

153++ }

154+ }

155+ if (off) {

156+ // store offset in unused header at offset zero

157+@@ -221,8 +276,8 @@

158+ p[-4] = 0;

159+ }

160+ *(uint16_t *)(p-2) = (size_t)(p-g->mem->storage)/UNIT;

161+- p[-3] = idx;

162+- set_size(p, end, n);

163++ /* idx is already known here; preserve it in the single final stamp. */

164++ set_size_at(p, end, n, idx);

165+ return p;

166+ }

167+

168+@@ -231,7 +286,7 @@

169+ n = (n+IB-1)>>4;

170+ if (n<10) return n;

171+ n++;

172+- int i = (28-a_clz_32(n))*4 + 8;

173++ int i = (28-__builtin_clz(n))*4 + 8;

174+ if (n>size_classes[i+1]) i+=2;

175+ if (n>size_classes[i]) i++;

176+ return i;

177+@@ -244,6 +299,18 @@

178+ return 1;

179+ }

180+ return 0;

181++}

182++

183++/* One mapping in each power-of-two bucket, from 128 KiB to 2 MiB.

184++ * Their combined upper bounds total 3968 KiB. A larger mapping is never

185++ * cached. Metadata stays owned by the cache until it is reused or trimmed. */

186++static inline int large_cache_index(size_t maplen)

187++{

188++ if (!VINIX_MALLOC_RETAIN || maplen < 32 || maplen > LARGE_CACHE_MAX_PAGES)

189++ return -1;

190++ int i = 0;

191++ for (size_t upper = 32; maplen > upper; upper <<= 1) i++;

192++ return i;

193+ }

194+

195+ static inline void step_seq(void)

196+--- a/src/malloc/mallocng/malloc.c

197++++ b/src/malloc/mallocng/malloc.c

198+@@ -201,6 +201,13 @@

199+ cnt >>= 1;

200+ }

201+

202++ /* A cache should cover a modest burst before allocating another map.

203++ * Keep upstream's larger usage-driven groups; only raise small groups.

204++ * The storage budget caps both the minimum and retained map footprint. */

205++ int retain_cnt = VINIX_MALLOC_RETAIN ? (RETAIN_GROUP_MAX_BYTES-UNIT)/size : 0;

206++ if (retain_cnt > 8) retain_cnt = 8;

207++ if (cnt < retain_cnt) cnt = retain_cnt;

208++

209+ // If we selected a count of 1 above but it's not sufficient to use

210+ // mmap, increase to 2. Then it might be; if not it will nest.

211+ if (cnt==1 && size*cnt+UNIT <= pagesize/2) cnt = 2;

212+@@ -211,7 +218,7 @@

213+ // check/update bounce counter to start/increase retention

214+ // of freed maps, and inhibit use of low-count, odd-size

215+ // small mappings and single-slot groups if activated.

216+- int nosmall = is_bouncing(sc);

217++ int nosmall = retain_cnt || is_bouncing(sc);

218+ account_bounce(sc);

219+ step_seq();

220+

221+@@ -286,7 +293,7 @@

222+ static int alloc_slot(int sc, size_t req)

223+ {

224+ uint32_t first = try_avail(&ctx.active[sc]);

225+- if (first) return a_ctz_32(first);

226++ if (first) return __builtin_ctz(first);

227+

228+ struct meta *g = alloc_group(sc, req);

229+ if (!g) return -1;

230+@@ -296,7 +303,7 @@

231+ return 0;

232+ }

233+

234+-void *malloc(size_t n)

235++static __attribute__((noinline)) void *malloc_slow(size_t n)

236+ {

237+ if (size_overflows(n)) return 0;

238+ struct meta *g;

239+@@ -307,6 +314,20 @@

240+

241+ if (n >= MMAP_THRESHOLD) {

242+ size_t needed = n + IB + UNIT;

243++ int bucket = large_cache_index((needed+4095)/4096);

244++ if (bucket >= 0) {

245++ wrlock();

246++ g = ctx.large_cache[bucket];

247++ if (g && g->maplen*4096UL >= needed) {

248++ ctx.large_cache[bucket] = 0;

249++ g->freed_mask = 0;

250++ step_seq();

251++ ctx.mmap_counter++;

252++ idx = 0;

253++ goto success;

254++ }

255++ unlock();

256++ }

257+ void *p = mmap(0, needed, PROT_READ|PROT_WRITE,

258+ MAP_PRIVATE|MAP_ANON, -1, 0);

259+ if (p==MAP_FAILED) return 0;

260+@@ -341,7 +362,11 @@

261+ // any groups of desired size. this allows counts of 2 or 3

262+ // to be allocated at first rather than having to start with

263+ // 7 or 5, the min counts for even size classes.

264+- if (!g && sc>=4 && sc<32 && sc!=6 && !(sc&1) && !ctx.usage_by_class[sc]) {

265++ /* Every coarse-eligible class is below 32, with stride at most

266++ * 8176 bytes. The 128 KiB retained budget gives each at least eight

267++ * slots, so coarsening saves no storage under this policy. */

268++ if (!VINIX_MALLOC_RETAIN && !g && sc>=4 && sc<32 && sc!=6 && !(sc&1)

269++ && !ctx.usage_by_class[sc]) {

270+ size_t usage = ctx.usage_by_class[sc|1];

271+ // if a new group may be allocated, count it toward

272+ // usage in deciding if we can use coarse class.

273+@@ -361,7 +386,7 @@

274+ g->avail_mask = mask-first;

275+ else if (a_cas(&g->avail_mask, mask, mask-first)!=mask)

276+ continue;

277+- idx = a_ctz_32(first);

278++ idx = __builtin_ctz(first);

279+ goto success;

280+ }

281+ upgradelock();

282+@@ -379,9 +404,40 @@

283+ return enframe(g, idx, n, ctr);

284+ }

285+

286++void *malloc(size_t n)

287++{

288++ /* Keep the ordinary single-thread path free of mapping and lock calls.

289++ * Negative need_locks transitions and any held/congested malloc lock

290++ * use the original path, including its actual-state unlock. */

291++ if (n < MMAP_THRESHOLD && !MT && !*(volatile int *)__malloc_lock) {

292++ int sc = size_to_class(n);

293++ struct meta *g = ctx.active[sc];

294++ if (g) {

295++ uint32_t mask = g->avail_mask;

296++ /* Refilling the sole group needs no queue changes when some

297++ * freed slots are already active. Grow inactive pages and

298++ * traverse other groups through the unchanged slow path. */

299++ if (!mask && g->next == g &&

300++ (g->freed_mask & ((2u<<g->mem->active_idx)-1))) {

301++ mask = activate_group(g);

302++ assert(mask);

303++ decay_bounces(g->sizeclass);

304++ }

305++ if (mask) {

306++ uint32_t first = mask & -mask;

307++ g->avail_mask = mask-first;

308++ return enframe(g, __builtin_ctz(first), n, ctx.mmap_counter);

309++ }

310++ }

311++ }

312++ return malloc_slow(n);

313++}

314++

315+ int is_allzero(void *p)

316+ {

317+ struct meta *g = get_meta(p);

318+- return g->sizeclass >= 48 ||

319+- get_stride(g) < UNIT*size_classes[g->sizeclass];

320+-}

321++ /* Cached large mappings contain the previous caller's data. calloc

322++ * must clear them; fresh large mappings take the same safe path. */

323++ return (!VINIX_MALLOC_RETAIN && g->sizeclass >= 48) ||

324++ (g->sizeclass < 48 && get_stride(g) < UNIT*size_classes[g->sizeclass]);

325++}

326+--- a/src/malloc/mallocng/free.c

327++++ b/src/malloc/mallocng/free.c

328+@@ -15,6 +15,26 @@

329+ {

330+ struct mapinfo mi = { 0 };

331+ int sc = g->sizeclass;

332++ if (sc == 63) {

333++ int bucket = large_cache_index(g->maplen);

334++ if (bucket >= 0) {

335++ struct meta *old = ctx.large_cache[bucket];

336++ if (!old || old->maplen < g->maplen) {

337++ /* Keep the larger map so smaller prior requests cannot

338++ * cause permanent misses within this size bucket. */

339++ if (old) {

340++ mi.base = old->mem;

341++ mi.len = old->maplen*4096UL;

342++ free_meta(old);

343++ }

344++ /* free() validated and invalidated the caller's header.

345++ * Keep its metadata and mark the sole slot free. */

346++ g->freed_mask = 1;

347++ ctx.large_cache[bucket] = g;

348++ return mi; /* old mapping is unmapped after unlock */

349++ }

350++ }

351++ }

352+ if (sc < 48) {

353+ ctx.usage_by_class[sc] -= g->last_idx+1;

354+ }

355+@@ -45,6 +65,14 @@

356+ if (sc >= 48 || get_stride(g) < UNIT*size_classes[sc])

357+ return 1;

358+

359++ /* Keep only the final ordinary group in a class. Nested small groups

360++ * are cheap in bytes but rebuilding their metadata on every free is

361++ * expensive. Multiple groups still consolidate via upstream policy. */

362++ if (VINIX_MALLOC_RETAIN && (g->next == g || (!g->next && !ctx.active[sc])) &&

363++ (g->maplen ? g->maplen*4096UL :

364++ UNIT + UNIT*size_classes[sc]*(g->last_idx+1)) <= RETAIN_GROUP_MAX_BYTES)

365++ return 0;

366++

367+ // always free groups allocated inside another group's slot

368+ // since recreating them should not be expensive and they

369+ // might be blocking freeing of a much larger group.

370+@@ -94,7 +122,8 @@

371+ queue(&ctx.active[sc], g);

372+ }

373+ }

374+- a_or(&g->freed_mask, self);

375++ if (!MT) g->freed_mask |= self;

376++ else a_or(&g->freed_mask, self);

377+ return (struct mapinfo){ 0 };

378+ }

379+

380+@@ -102,9 +131,10 @@

381+ {

382+ if (!p) return;

383+

384+- struct meta *g = get_meta(p);

385+- int idx = get_slot_index(p);

386+- size_t stride = get_stride(g);

387++ struct slot_info info;

388++ struct meta *g = get_meta_with_info(p, &info);

389++ int idx = info.index;

390++ size_t stride = info.stride;

391+ unsigned char *start = g->mem->storage + stride*idx;

392+ unsigned char *end = start + stride - IB;

393+ get_nominal_size(p, end);

394+@@ -113,6 +143,18 @@

395+ // invalidate offset to group header, and cycle offset of

396+ // used region within slot if current offset is zero.

397+ *(uint16_t *)((char *)p-2) = 0;

398++

399++ /* With one thread, the sole retained ordinary group cannot need

400++ * queue maintenance or reclamation. Keep the normal freed-mask

401++ * publication so activation still rotates through all its slots.

402++ * Metadata, redzones and the caller header were checked above. */

403++ if (VINIX_MALLOC_RETAIN && !MT && g->sizeclass < 48 && g->next == g &&

404++ stride >= info.class_stride &&

405++ (g->maplen ? g->maplen*4096UL : UNIT + stride*(g->last_idx+1))

406++ <= RETAIN_GROUP_MAX_BYTES) {

407++ g->freed_mask |= self;

408++ return;

409++ }

410+

411+ // release any whole pages contained in the slot to be freed

412+ // unless it's a single-slot group that will be unmapped.

413+@@ -149,3 +191,45 @@

414+ errno = e;

415+ }

416+ }

417++

418++/* Compatible with the GNU extension: release free groups and cached direct

419++ * mappings, preserving every live allocation. The padding hint is ignored.

420++ * The existing malloc lock also participates in musl's fork protocol. */

421++int malloc_trim(size_t pad)

422++{

423++ (void)pad;

424++ int released = 0, saved_errno = errno;

425++ wrlock();

426++ for (int i=0; i<LARGE_CACHE_COUNT; i++) {

427++ struct meta *g = ctx.large_cache[i];

428++ if (!g) continue;

429++ ctx.large_cache[i] = 0;

430++ void *base = g->mem;

431++ size_t len = g->maplen*4096UL;

432++ free_meta(g);

433++ if (!munmap(base, len)) released = 1;

434++ }

435++ /* Nested groups have a larger parent class, so release children first.

436++ * free_group() can make their parent free during this traversal. */

437++ for (int sc=0; sc<48; sc++) {

438++ for (;;) {

439++ struct meta *head = ctx.active[sc], *g = head;

440++ if (!head) break;

441++ do {

442++ if (g->freeable && (g->avail_mask|g->freed_mask)

443++ == (2u<<g->last_idx)-1) break;

444++ g = g->next;

445++ } while (g != head);

446++ if (!g->freeable || (g->avail_mask|g->freed_mask)

447++ != (2u<<g->last_idx)-1) break;

448++ dequeue(&ctx.active[sc], g);

449++ if (ctx.active[sc] && !ctx.active[sc]->avail_mask)

450++ activate_group(ctx.active[sc]);

451++ struct mapinfo mi = free_group(g);

452++ if (mi.len && !munmap(mi.base, mi.len)) released = 1;

453++ }

454++ }

455++ unlock();

456++ errno = saved_errno;

457++ return released;

458++}

459+--- a/src/malloc/mallocng/glue.h

460++++ b/src/malloc/mallocng/glue.h

461+@@ -30,7 +30,9 @@

462+ #include <assert.h>

463+ #else

464+ #undef assert

465+-#define assert(x) do { if (!(x)) a_crash(); } while(0)

466++/* Generic a_crash uses a null store, whose undefined behavior lets GCC

467++ * discard unused-result validation. A defined trap keeps every check. */

468++#define assert(x) do { if (!(x)) __builtin_trap(); } while(0)

469+ #endif

470+

471+ #define brk(p) ((uintptr_t)__syscall(SYS_brk, p))

472+@@ -75,7 +77,9 @@

473+ }

474+ static inline void unlock()

475+ {

476+- UNLOCK(__malloc_lock);

477++/* LOCK can turn negative need_locks into zero while still taking

478++ * this lock. Its actual state, not MT, decides whether to unlock. */

479++ if (*(volatile int *)__malloc_lock < 0) UNLOCK(__malloc_lock);

480+ }

481+ static inline void upgradelock()

482+ {

483+--- a/include/malloc.h

484++++ b/include/malloc.h

485+@@ -17,6 +17,7 @@

486+ void *memalign(size_t, size_t);

487+

488+ size_t malloc_usable_size(void *);

489++int malloc_trim(size_t);

490+

491+ #ifdef __cplusplus

492+ }