001-git-2015-06-16.patch 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. commit 1b0cdc8700d29ef018bf226d74b2b58b23bce91c
  2. Author: Rich Felker <[email protected]>
  3. Date: Tue Jun 16 07:11:19 2015 +0000
  4. refactor stdio open file list handling, move it out of global libc struct
  5. functions which open in-memory FILE stream variants all shared a tail
  6. with __fdopen, adding the FILE structure to stdio's open file list.
  7. replacing this common tail with a function call reduces code size and
  8. duplication of logic. the list is also partially encapsulated now.
  9. function signatures were chosen to facilitate tail call optimization
  10. and reduce the need for additional accessor functions.
  11. with these changes, static linked programs that do not use stdio no
  12. longer have an open file list at all.
  13. commit f22a9edaf8a6f2ca1d314d18b3785558279a5c03
  14. Author: Rich Felker <[email protected]>
  15. Date: Tue Jun 16 06:18:00 2015 +0000
  16. byte-based C locale, phase 3: make MB_CUR_MAX variable to activate code
  17. this patch activates the new byte-based C locale (high bytes treated
  18. as abstract code unit "characters" rather than decoded as multibyte
  19. characters) by making the value of MB_CUR_MAX depend on the active
  20. locale. for the C locale, the LC_CTYPE category pointer is null,
  21. yielding a value of 1. all other locales yield a value of 4.
  22. commit 16f18d036d9a7bf590ee6eb86785c0a9658220b6
  23. Author: Rich Felker <[email protected]>
  24. Date: Tue Jun 16 05:35:31 2015 +0000
  25. byte-based C locale, phase 2: stdio and iconv (multibyte callers)
  26. this patch adjusts libc components which use the multibyte functions
  27. internally, and which depend on them operating in a particular
  28. encoding, to make the appropriate locale changes before calling them
  29. and restore the calling thread's locale afterwards. activating the
  30. byte-based C locale without these changes would cause regressions in
  31. stdio and iconv.
  32. in the case of iconv, the current implementation was simply using the
  33. multibyte functions as UTF-8 conversions. setting a multibyte UTF-8
  34. locale for the duration of the iconv operation allows the code to
  35. continue working.
  36. in the case of stdio, POSIX requires that FILE streams have an
  37. encoding rule bound at the time of setting wide orientation. as long
  38. as all locales, including the C locale, used the same encoding,
  39. treating high bytes as UTF-8, there was no need to store an encoding
  40. rule as part of the stream's state.
  41. a new locale field in the FILE structure points to the locale that
  42. should be made active during fgetwc/fputwc/ungetwc on the stream. it
  43. cannot point to the locale active at the time the stream becomes
  44. oriented, because this locale could be mutable (the global locale) or
  45. could be destroyed (locale_t objects produced by newlocale) before the
  46. stream is closed. instead, a pointer to the static C or C.UTF-8 locale
  47. object added in commit commit aeeac9ca5490d7d90fe061ab72da446c01ddf746
  48. is used. this is valid since categories other than LC_CTYPE will not
  49. affect these functions.
  50. commit 1507ebf837334e9e07cfab1ca1c2e88449069a80
  51. Author: Rich Felker <[email protected]>
  52. Date: Tue Jun 16 04:44:17 2015 +0000
  53. byte-based C locale, phase 1: multibyte character handling functions
  54. this patch makes the functions which work directly on multibyte
  55. characters treat the high bytes as individual abstract code units
  56. rather than as multibyte sequences when MB_CUR_MAX is 1. since
  57. MB_CUR_MAX is presently defined as a constant 4, all of the new code
  58. added is dead code, and optimizing compilers' code generation should
  59. not be affected at all. a future commit will activate the new code.
  60. as abstract code units, bytes 0x80 to 0xff are represented by wchar_t
  61. values 0xdf80 to 0xdfff, at the end of the surrogates range. this
  62. ensures that they will never be misinterpreted as Unicode characters,
  63. and that all wctype functions return false for these "characters"
  64. without needing locale-specific logic. a high range outside of Unicode
  65. such as 0x7fffff80 to 0x7fffffff was also considered, but since C11's
  66. char16_t also needs to be able to represent conversions of these
  67. bytes, the surrogate range was the natural choice.
  68. commit 38e2f727237230300fea6aff68802db04625fd23
  69. Author: Rich Felker <[email protected]>
  70. Date: Tue Jun 16 04:21:38 2015 +0000
  71. fix btowc corner case
  72. btowc is required to interpret its argument by conversion to unsigned
  73. char, unless the argument is equal to EOF. since the conversion to
  74. produces a non-character value anyway, we can just unconditionally
  75. convert, for now.
  76. commit ee59c296d56bf26f49f354d6eb32b4b6d4190188
  77. Author: Szabolcs Nagy <[email protected]>
  78. Date: Wed Jun 3 10:32:14 2015 +0100
  79. arm: add vdso support
  80. vdso will be available on arm in linux v4.2, the user-space code
  81. for it is in kernel commit 8512287a8165592466cb9cb347ba94892e9c56a5
  82. commit e3bc22f1eff87b8f029a6ab31f1a269d69e4b053
  83. Author: Rich Felker <[email protected]>
  84. Date: Sun Jun 14 01:59:02 2015 +0000
  85. refactor malloc's expand_heap to share with __simple_malloc
  86. this extends the brk/stack collision protection added to full malloc
  87. in commit 276904c2f6bde3a31a24ebfa201482601d18b4f9 to also protect the
  88. __simple_malloc function used in static-linked programs that don't
  89. reference the free function.
  90. it also extends support for using mmap when brk fails, which full
  91. malloc got in commit 5446303328adf4b4e36d9fba21848e6feb55fab4, to
  92. __simple_malloc.
  93. since __simple_malloc may expand the heap by arbitrarily large
  94. increments, the stack collision detection is enhanced to detect
  95. interval overlap rather than just proximity of a single address to the
  96. stack. code size is increased a bit, but this is partly offset by the
  97. sharing of code between the two malloc implementations, which due to
  98. linking semantics, both get linked in a program that needs the full
  99. malloc with realloc/free support.
  100. commit 4ef9b828c1f39553a69e0635ac91f0fcadd6e8c6
  101. Author: Rich Felker <[email protected]>
  102. Date: Sat Jun 13 20:53:02 2015 +0000
  103. remove cancellation points in stdio
  104. commit 58165923890865a6ac042fafce13f440ee986fd9 added these optional
  105. cancellation points on the basis that cancellable stdio could be
  106. useful, to unblock threads stuck on stdio operations that will never
  107. complete. however, the only way to ensure that cancellation can
  108. achieve this is to violate the rules for side effects when
  109. cancellation is acted upon, discarding knowledge of any partial data
  110. transfer already completed. our implementation exhibited this behavior
  111. and was thus non-conforming.
  112. in addition to improving correctness, removing these cancellation
  113. points moderately reduces code size, and should significantly improve
  114. performance on i386, where sysenter/syscall instructions can be used
  115. instead of "int $128" for non-cancellable syscalls.
  116. commit 536c6d5a4205e2a3f161f2983ce1e0ac3082187d
  117. Author: Rich Felker <[email protected]>
  118. Date: Sat Jun 13 05:17:16 2015 +0000
  119. fix idiom for setting stdio stream orientation to wide
  120. the old idiom, f->mode |= f->mode+1, was adapted from the idiom for
  121. setting byte orientation, f->mode |= f->mode-1, but the adaptation was
  122. incorrect. unless the stream was alreasdy set byte-oriented, this code
  123. incremented f->mode each time it was executed, which would eventually
  124. lead to overflow. it could be fixed by changing it to f->mode |= 1,
  125. but upcoming changes will require slightly more work at the time of
  126. wide orientation, so it makes sense to just call fwide. as an
  127. optimization in the single-character functions, fwide is only called
  128. if the stream is not already wide-oriented.
  129. commit f8f565df467c13248104223f99abf7f37cef7584
  130. Author: Rich Felker <[email protected]>
  131. Date: Sat Jun 13 04:42:38 2015 +0000
  132. add printing of null %s arguments as "(null)" in wide printf
  133. this is undefined, but supported in our implementation of the normal
  134. printf, so for consistency the wide variant should support it too.
  135. commit f9e25d813860d53cd1e9b6145cc63375d2fe2529
  136. Author: Rich Felker <[email protected]>
  137. Date: Sat Jun 13 04:37:27 2015 +0000
  138. add %m support to wide printf
  139. commit ec634aad91f57479ef17525e33ed446c780a61f4
  140. Author: Rich Felker <[email protected]>
  141. Date: Thu Jun 11 05:01:04 2015 +0000
  142. add sh asm for vfork
  143. commit c30cbcb0a646b1f13a22c645616dce624465b883
  144. Author: Rich Felker <[email protected]>
  145. Date: Wed Jun 10 02:27:40 2015 +0000
  146. implement arch-generic version of __unmapself
  147. this can be used to put off writing an asm version of __unmapself for
  148. new archs, or as a permanent solution on archs where it's not
  149. practical or even possible to run momentarily with no stack.
  150. the concept here is simple: the caller takes a lock on a global shared
  151. stack and uses it to make the munmap and exit syscalls. the only trick
  152. is unlocking, which must be done after the thread exits, and this is
  153. achieved by using the set_tid_address syscall to have the kernel zero
  154. and futex-wake the lock word as part of the exit syscall.
  155. commit 276904c2f6bde3a31a24ebfa201482601d18b4f9
  156. Author: Rich Felker <[email protected]>
  157. Date: Tue Jun 9 20:30:35 2015 +0000
  158. in malloc, refuse to use brk if it grows into stack
  159. the linux/nommu fdpic ELF loader sets up the brk range to overlap
  160. entirely with the main thread's stack (but growing from opposite
  161. ends), so that the resulting failure mode for malloc is not to return
  162. a null pointer but to start returning pointers to memory that overlaps
  163. with the caller's stack. needless to say this extremely dangerous and
  164. makes brk unusable.
  165. since it's non-trivial to detect execution environments that might be
  166. affected by this kernel bug, and since the severity of the bug makes
  167. any sort of detection that might yield false-negatives unsafe, we
  168. instead check the proximity of the brk to the stack pointer each time
  169. the brk is to be expanded. both the main thread's stack (where the
  170. real known risk lies) and the calling thread's stack are checked. an
  171. arbitrary gap distance of 8 MB is imposed, chosen to be larger than
  172. linux default main-thread stack reservation sizes and larger than any
  173. reasonable stack configuration on nommu.
  174. the effeciveness of this patch relies on an assumption that the amount
  175. by which the brk is being grown is smaller than the gap limit, which
  176. is always true for malloc's use of brk. reliance on this assumption is
  177. why the check is being done in malloc-specific code and not in __brk.
  178. commit bd1eaceaa3975bd2a2a34e211cff896affaecadf
  179. Author: Rich Felker <[email protected]>
  180. Date: Tue Jun 9 20:09:27 2015 +0000
  181. fix spurious errors from pwd/grp functions when nscd backend is absent
  182. for several pwd/grp functions, the only way the caller can distinguish
  183. between a successful negative result ("no such user/group") and an
  184. internal error is by clearing errno before the call and checking errno
  185. afterwards. the nscd backend support code correctly simulated a
  186. not-found response on systems where such a backend is not running, but
  187. failed to restore errno.
  188. this commit also fixed an outdated/incorrect comment.
  189. commit 75ce4503950621b11fcc7f1fd1187dbcf3cde312
  190. Author: Rich Felker <[email protected]>
  191. Date: Sun Jun 7 20:55:23 2015 +0000
  192. fix regression in pre-v7 arm on kernels with kuser helper removed
  193. the arm atomics/TLS runtime selection code is called from
  194. __set_thread_area and depends on having libc.auxv and __hwcap
  195. available. commit 71f099cb7db821c51d8f39dfac622c61e54d794c moved the
  196. first call to __set_thread_area to the top of dynamic linking stage 3,
  197. before this data is made available, causing the runtime detection code
  198. to always see __hwcap as zero and thereby select the atomics/TLS
  199. implementations based on kuser helper.
  200. upcoming work on superh will use similar runtime detection.
  201. ideally this early-init code should be cleanly refactored and shared
  202. between the dynamic linker and static-linked startup.
  203. commit 32f3c4f70633488550c29a2444f819aafdf345ff
  204. Author: Rich Felker <[email protected]>
  205. Date: Sun Jun 7 03:09:16 2015 +0000
  206. add multiple inclusion guard to locale_impl.h
  207. commit 04b8360adbb6487f61aa0c00e53ec3a90a5a0d29
  208. Author: Rich Felker <[email protected]>
  209. Date: Sun Jun 7 02:59:49 2015 +0000
  210. remove redefinition of MB_CUR_MAX in locale_impl.h
  211. unless/until the byte-based C locale is implemented, defining
  212. MB_CUR_MAX to 1 in the C locale is wrong. no internal code currently
  213. uses the MB_CUR_MAX macro, but having it defined inconsistently is
  214. error-prone. applications get the value from stdlib.h and were
  215. unaffected.
  216. commit 16bf466532d7328e971012b0731ad493b017ad29
  217. Author: Rich Felker <[email protected]>
  218. Date: Sat Jun 6 18:53:02 2015 +0000
  219. make static C and C.UTF-8 locales available outside of newlocale
  220. commit 312eea2ea4f4363fb01b73660c08bfcf43dd3bb4
  221. Author: Rich Felker <[email protected]>
  222. Date: Sat Jun 6 18:20:30 2015 +0000
  223. remove another invalid skip of locking in ungetwc
  224. commit 3d7e32d28dc9962e9efc1c317c5b44b5b2df3008
  225. Author: Rich Felker <[email protected]>
  226. Date: Sat Jun 6 18:16:22 2015 +0000
  227. add macro version of ctype.h isascii function
  228. presumably internal code (ungetwc and fputwc) was written assuming a
  229. macro implementation existed; otherwise use of isascii is just a
  230. pessimization.
  231. commit 7e816a6487932cbb3cb71d94b609e50e81f4e5bf
  232. Author: Rich Felker <[email protected]>
  233. Date: Sat Jun 6 18:11:17 2015 +0000
  234. remove invalid skip of locking in ungetwc
  235. aside from being invalid, the early check only optimized the error
  236. case, and likely pessimized the common case by separating the
  237. two branches on isascii(c) at opposite ends of the function.
  238. commit 63f4b9f18f3674124d8bcb119739fec85e6da005
  239. Author: Timo Teräs <[email protected]>
  240. Date: Fri Jun 5 10:39:42 2015 +0300
  241. fix uselocale((locale_t)0) not to modify locale
  242. commit 68630b55c0c7219fe9df70dc28ffbf9efc8021d8 made the new locale to
  243. be assigned unconditonally resulting in crashes later on.
  244. --- a/arch/arm/syscall_arch.h
  245. +++ b/arch/arm/syscall_arch.h
  246. @@ -72,3 +72,7 @@ static inline long __syscall6(long n, lo
  247. register long r5 __asm__("r5") = f;
  248. __asm_syscall("r"(r7), "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5));
  249. }
  250. +
  251. +#define VDSO_USEFUL
  252. +#define VDSO_CGT_SYM "__vdso_clock_gettime"
  253. +#define VDSO_CGT_VER "LINUX_2.6"
  254. --- a/include/ctype.h
  255. +++ b/include/ctype.h
  256. @@ -64,6 +64,7 @@ int isascii(int);
  257. int toascii(int);
  258. #define _tolower(a) ((a)|0x20)
  259. #define _toupper(a) ((a)&0x5f)
  260. +#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128)
  261. #endif
  262. --- a/include/stdlib.h
  263. +++ b/include/stdlib.h
  264. @@ -76,7 +76,8 @@ size_t wcstombs (char *__restrict, const
  265. #define EXIT_FAILURE 1
  266. #define EXIT_SUCCESS 0
  267. -#define MB_CUR_MAX ((size_t)+4)
  268. +size_t __ctype_get_mb_cur_max(void);
  269. +#define MB_CUR_MAX (__ctype_get_mb_cur_max())
  270. #define RAND_MAX (0x7fffffff)
  271. --- a/src/ctype/__ctype_get_mb_cur_max.c
  272. +++ b/src/ctype/__ctype_get_mb_cur_max.c
  273. @@ -1,6 +1,7 @@
  274. -#include <stddef.h>
  275. +#include <stdlib.h>
  276. +#include "locale_impl.h"
  277. size_t __ctype_get_mb_cur_max()
  278. {
  279. - return 4;
  280. + return MB_CUR_MAX;
  281. }
  282. --- a/src/ctype/isascii.c
  283. +++ b/src/ctype/isascii.c
  284. @@ -1,4 +1,5 @@
  285. #include <ctype.h>
  286. +#undef isascii
  287. int isascii(int c)
  288. {
  289. --- a/src/internal/libc.h
  290. +++ b/src/internal/libc.h
  291. @@ -17,8 +17,6 @@ struct __libc {
  292. int secure;
  293. volatile int threads_minus_1;
  294. size_t *auxv;
  295. - FILE *ofl_head;
  296. - volatile int ofl_lock[2];
  297. size_t tls_size;
  298. size_t page_size;
  299. struct __locale_struct global_locale;
  300. --- a/src/internal/locale_impl.h
  301. +++ b/src/internal/locale_impl.h
  302. @@ -1,3 +1,6 @@
  303. +#ifndef _LOCALE_IMPL_H
  304. +#define _LOCALE_IMPL_H
  305. +
  306. #include <locale.h>
  307. #include <stdlib.h>
  308. #include "libc.h"
  309. @@ -12,6 +15,10 @@ struct __locale_map {
  310. const struct __locale_map *next;
  311. };
  312. +extern const struct __locale_map __c_dot_utf8;
  313. +extern const struct __locale_struct __c_locale;
  314. +extern const struct __locale_struct __c_dot_utf8_locale;
  315. +
  316. const struct __locale_map *__get_locale(int, const char *);
  317. const char *__mo_lookup(const void *, size_t, const char *);
  318. const char *__lctrans(const char *, const struct __locale_map *);
  319. @@ -20,9 +27,14 @@ const char *__lctrans_cur(const char *);
  320. #define LCTRANS(msg, lc, loc) __lctrans(msg, (loc)->cat[(lc)])
  321. #define LCTRANS_CUR(msg) __lctrans_cur(msg)
  322. +#define C_LOCALE ((locale_t)&__c_locale)
  323. +#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale)
  324. +
  325. #define CURRENT_LOCALE (__pthread_self()->locale)
  326. #define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE])
  327. #undef MB_CUR_MAX
  328. #define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)
  329. +
  330. +#endif
  331. --- a/src/internal/stdio_impl.h
  332. +++ b/src/internal/stdio_impl.h
  333. @@ -47,6 +47,7 @@ struct _IO_FILE {
  334. unsigned char *shend;
  335. off_t shlim, shcnt;
  336. FILE *prev_locked, *next_locked;
  337. + struct __locale_struct *locale;
  338. };
  339. size_t __stdio_read(FILE *, unsigned char *, size_t);
  340. @@ -75,8 +76,9 @@ int __putc_unlocked(int, FILE *);
  341. FILE *__fdopen(int, const char *);
  342. int __fmodeflags(const char *);
  343. -#define OFLLOCK() LOCK(libc.ofl_lock)
  344. -#define OFLUNLOCK() UNLOCK(libc.ofl_lock)
  345. +FILE *__ofl_add(FILE *f);
  346. +FILE **__ofl_lock(void);
  347. +void __ofl_unlock(void);
  348. #define feof(f) ((f)->flags & F_EOF)
  349. #define ferror(f) ((f)->flags & F_ERR)
  350. --- a/src/ldso/dynlink.c
  351. +++ b/src/ldso/dynlink.c
  352. @@ -1192,6 +1192,17 @@ _Noreturn void __dls3(size_t *sp)
  353. char **argv_orig = argv;
  354. char **envp = argv+argc+1;
  355. + /* Find aux vector just past environ[] and use it to initialize
  356. + * global data that may be needed before we can make syscalls. */
  357. + __environ = envp;
  358. + for (i=argc+1; argv[i]; i++);
  359. + libc.auxv = auxv = (void *)(argv+i+1);
  360. + decode_vec(auxv, aux, AUX_CNT);
  361. + __hwcap = aux[AT_HWCAP];
  362. + libc.page_size = aux[AT_PAGESZ];
  363. + libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
  364. + || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
  365. +
  366. /* Setup early thread pointer in builtin_tls for ldso/libc itself to
  367. * use during dynamic linking. If possible it will also serve as the
  368. * thread pointer at runtime. */
  369. @@ -1200,25 +1211,11 @@ _Noreturn void __dls3(size_t *sp)
  370. a_crash();
  371. }
  372. - /* Find aux vector just past environ[] */
  373. - for (i=argc+1; argv[i]; i++)
  374. - if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
  375. - env_path = argv[i]+16;
  376. - else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
  377. - env_preload = argv[i]+11;
  378. - auxv = (void *)(argv+i+1);
  379. -
  380. - decode_vec(auxv, aux, AUX_CNT);
  381. -
  382. /* Only trust user/env if kernel says we're not suid/sgid */
  383. - if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
  384. - || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
  385. - env_path = 0;
  386. - env_preload = 0;
  387. - libc.secure = 1;
  388. + if (!libc.secure) {
  389. + env_path = getenv("LD_LIBRARY_PATH");
  390. + env_preload = getenv("LD_PRELOAD");
  391. }
  392. - libc.page_size = aux[AT_PAGESZ];
  393. - libc.auxv = auxv;
  394. /* If the main program was already loaded by the kernel,
  395. * AT_PHDR will point to some location other than the dynamic
  396. --- /dev/null
  397. +++ b/src/locale/c_locale.c
  398. @@ -0,0 +1,15 @@
  399. +#include "locale_impl.h"
  400. +#include <stdint.h>
  401. +
  402. +static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
  403. +
  404. +const struct __locale_map __c_dot_utf8 = {
  405. + .map = empty_mo,
  406. + .map_size = sizeof empty_mo,
  407. + .name = "C.UTF-8"
  408. +};
  409. +
  410. +const struct __locale_struct __c_locale = { 0 };
  411. +const struct __locale_struct __c_dot_utf8_locale = {
  412. + .cat[LC_CTYPE] = &__c_dot_utf8
  413. +};
  414. --- a/src/locale/iconv.c
  415. +++ b/src/locale/iconv.c
  416. @@ -5,6 +5,7 @@
  417. #include <stdlib.h>
  418. #include <limits.h>
  419. #include <stdint.h>
  420. +#include "locale_impl.h"
  421. #define UTF_32BE 0300
  422. #define UTF_16LE 0301
  423. @@ -165,9 +166,12 @@ size_t iconv(iconv_t cd0, char **restric
  424. int err;
  425. unsigned char type = map[-1];
  426. unsigned char totype = tomap[-1];
  427. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  428. if (!in || !*in || !*inb) return 0;
  429. + *ploc = UTF8_LOCALE;
  430. +
  431. for (; *inb; *in+=l, *inb-=l) {
  432. c = *(unsigned char *)*in;
  433. l = 1;
  434. @@ -431,6 +435,7 @@ size_t iconv(iconv_t cd0, char **restric
  435. break;
  436. }
  437. }
  438. + *ploc = loc;
  439. return x;
  440. ilseq:
  441. err = EILSEQ;
  442. @@ -445,5 +450,6 @@ starved:
  443. x = -1;
  444. end:
  445. errno = err;
  446. + *ploc = loc;
  447. return x;
  448. }
  449. --- a/src/locale/langinfo.c
  450. +++ b/src/locale/langinfo.c
  451. @@ -33,7 +33,8 @@ char *__nl_langinfo_l(nl_item item, loca
  452. int idx = item & 65535;
  453. const char *str;
  454. - if (item == CODESET) return "UTF-8";
  455. + if (item == CODESET)
  456. + return MB_CUR_MAX==1 ? "UTF-8-CODE-UNITS" : "UTF-8";
  457. switch (cat) {
  458. case LC_NUMERIC:
  459. --- a/src/locale/locale_map.c
  460. +++ b/src/locale/locale_map.c
  461. @@ -24,14 +24,6 @@ static const char envvars[][12] = {
  462. "LC_MESSAGES",
  463. };
  464. -static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
  465. -
  466. -const struct __locale_map __c_dot_utf8 = {
  467. - .map = empty_mo,
  468. - .map_size = sizeof empty_mo,
  469. - .name = "C.UTF-8"
  470. -};
  471. -
  472. const struct __locale_map *__get_locale(int cat, const char *val)
  473. {
  474. static int lock[2];
  475. @@ -107,8 +99,8 @@ const struct __locale_map *__get_locale(
  476. * sake of being able to do message translations at the
  477. * application level. */
  478. if (!new && (new = malloc(sizeof *new))) {
  479. - new->map = empty_mo;
  480. - new->map_size = sizeof empty_mo;
  481. + new->map = __c_dot_utf8.map;
  482. + new->map_size = __c_dot_utf8.map_size;
  483. memcpy(new->name, val, n);
  484. new->name[n] = 0;
  485. new->next = loc_head;
  486. --- a/src/locale/newlocale.c
  487. +++ b/src/locale/newlocale.c
  488. @@ -3,16 +3,9 @@
  489. #include "locale_impl.h"
  490. #include "libc.h"
  491. -extern const struct __locale_map __c_dot_utf8;
  492. -
  493. -static const struct __locale_struct c_locale = { 0 };
  494. -static const struct __locale_struct c_dot_utf8_locale = {
  495. - .cat[LC_CTYPE] = &__c_dot_utf8
  496. -};
  497. -
  498. int __loc_is_allocated(locale_t loc)
  499. {
  500. - return loc && loc != &c_locale && loc != &c_dot_utf8_locale;
  501. + return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
  502. }
  503. locale_t __newlocale(int mask, const char *name, locale_t loc)
  504. @@ -44,9 +37,9 @@ locale_t __newlocale(int mask, const cha
  505. }
  506. if (!j)
  507. - return (locale_t)&c_locale;
  508. - if (j==1 && tmp.cat[LC_CTYPE]==c_dot_utf8_locale.cat[LC_CTYPE])
  509. - return (locale_t)&c_dot_utf8_locale;
  510. + return C_LOCALE;
  511. + if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
  512. + return UTF8_LOCALE;
  513. if ((loc = malloc(sizeof *loc))) *loc = tmp;
  514. --- a/src/locale/uselocale.c
  515. +++ b/src/locale/uselocale.c
  516. @@ -8,9 +8,7 @@ locale_t __uselocale(locale_t new)
  517. locale_t old = self->locale;
  518. locale_t global = &libc.global_locale;
  519. - if (new == LC_GLOBAL_LOCALE) new = global;
  520. -
  521. - self->locale = new;
  522. + if (new) self->locale = new == LC_GLOBAL_LOCALE ? global : new;
  523. return old == global ? LC_GLOBAL_LOCALE : old;
  524. }
  525. --- /dev/null
  526. +++ b/src/malloc/expand_heap.c
  527. @@ -0,0 +1,72 @@
  528. +#include <limits.h>
  529. +#include <stdint.h>
  530. +#include <errno.h>
  531. +#include <sys/mman.h>
  532. +#include "libc.h"
  533. +#include "syscall.h"
  534. +
  535. +/* This function returns true if the interval [old,new]
  536. + * intersects the 'len'-sized interval below &libc.auxv
  537. + * (interpreted as the main-thread stack) or below &b
  538. + * (the current stack). It is used to defend against
  539. + * buggy brk implementations that can cross the stack. */
  540. +
  541. +static int traverses_stack_p(uintptr_t old, uintptr_t new)
  542. +{
  543. + const uintptr_t len = 8<<20;
  544. + uintptr_t a, b;
  545. +
  546. + b = (uintptr_t)libc.auxv;
  547. + a = b > len ? b-len : 0;
  548. + if (new>a && old<b) return 1;
  549. +
  550. + b = (uintptr_t)&b;
  551. + a = b > len ? b-len : 0;
  552. + if (new>a && old<b) return 1;
  553. +
  554. + return 0;
  555. +}
  556. +
  557. +void *__mmap(void *, size_t, int, int, int, off_t);
  558. +
  559. +/* Expand the heap in-place if brk can be used, or otherwise via mmap,
  560. + * using an exponential lower bound on growth by mmap to make
  561. + * fragmentation asymptotically irrelevant. The size argument is both
  562. + * an input and an output, since the caller needs to know the size
  563. + * allocated, which will be larger than requested due to page alignment
  564. + * and mmap minimum size rules. The caller is responsible for locking
  565. + * to prevent concurrent calls. */
  566. +
  567. +void *__expand_heap(size_t *pn)
  568. +{
  569. + static uintptr_t brk;
  570. + static unsigned mmap_step;
  571. + size_t n = *pn;
  572. +
  573. + if (n > SIZE_MAX/2 - PAGE_SIZE) {
  574. + errno = ENOMEM;
  575. + return 0;
  576. + }
  577. + n += -n & PAGE_SIZE-1;
  578. +
  579. + if (!brk) {
  580. + brk = __syscall(SYS_brk, 0);
  581. + brk += -brk & PAGE_SIZE-1;
  582. + }
  583. +
  584. + if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
  585. + && __syscall(SYS_brk, brk+n)==brk+n) {
  586. + *pn = n;
  587. + brk += n;
  588. + return (void *)(brk-n);
  589. + }
  590. +
  591. + size_t min = (size_t)PAGE_SIZE << mmap_step/2;
  592. + if (n < min) n = min;
  593. + void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
  594. + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  595. + if (area == MAP_FAILED) return 0;
  596. + *pn = n;
  597. + mmap_step++;
  598. + return area;
  599. +}
  600. --- a/src/malloc/lite_malloc.c
  601. +++ b/src/malloc/lite_malloc.c
  602. @@ -4,43 +4,46 @@
  603. #include <errno.h>
  604. #include "libc.h"
  605. -uintptr_t __brk(uintptr_t);
  606. -
  607. #define ALIGN 16
  608. +void *__expand_heap(size_t *);
  609. +
  610. void *__simple_malloc(size_t n)
  611. {
  612. - static uintptr_t cur, brk;
  613. - uintptr_t base, new;
  614. + static char *cur, *end;
  615. static volatile int lock[2];
  616. - size_t align=1;
  617. + size_t align=1, pad;
  618. + void *p;
  619. if (!n) n++;
  620. - if (n > SIZE_MAX/2) goto toobig;
  621. -
  622. while (align<n && align<ALIGN)
  623. align += align;
  624. - n = n + align - 1 & -align;
  625. LOCK(lock);
  626. - if (!cur) cur = brk = __brk(0)+16;
  627. - base = cur + align-1 & -align;
  628. - if (n > SIZE_MAX - PAGE_SIZE - base) goto fail;
  629. - if (base+n > brk) {
  630. - new = base+n + PAGE_SIZE-1 & -PAGE_SIZE;
  631. - if (__brk(new) != new) goto fail;
  632. - brk = new;
  633. - }
  634. - cur = base+n;
  635. - UNLOCK(lock);
  636. - return (void *)base;
  637. + pad = -(uintptr_t)cur & align-1;
  638. +
  639. + if (n <= SIZE_MAX/2 + ALIGN) n += pad;
  640. +
  641. + if (n > end-cur) {
  642. + size_t m = n;
  643. + char *new = __expand_heap(&m);
  644. + if (!new) {
  645. + UNLOCK(lock);
  646. + return 0;
  647. + }
  648. + if (new != end) {
  649. + cur = new;
  650. + n -= pad;
  651. + pad = 0;
  652. + }
  653. + end = new + m;
  654. + }
  655. -fail:
  656. + p = cur + pad;
  657. + cur += n;
  658. UNLOCK(lock);
  659. -toobig:
  660. - errno = ENOMEM;
  661. - return 0;
  662. + return p;
  663. }
  664. weak_alias(__simple_malloc, malloc);
  665. --- a/src/malloc/malloc.c
  666. +++ b/src/malloc/malloc.c
  667. @@ -13,7 +13,6 @@
  668. #define inline inline __attribute__((always_inline))
  669. #endif
  670. -uintptr_t __brk(uintptr_t);
  671. void *__mmap(void *, size_t, int, int, int, off_t);
  672. int __munmap(void *, size_t);
  673. void *__mremap(void *, size_t, size_t, int, ...);
  674. @@ -31,13 +30,9 @@ struct bin {
  675. };
  676. static struct {
  677. - uintptr_t brk;
  678. - size_t *heap;
  679. volatile uint64_t binmap;
  680. struct bin bins[64];
  681. - volatile int brk_lock[2];
  682. volatile int free_lock[2];
  683. - unsigned mmap_step;
  684. } mal;
  685. @@ -152,69 +147,52 @@ void __dump_heap(int x)
  686. }
  687. #endif
  688. +void *__expand_heap(size_t *);
  689. +
  690. static struct chunk *expand_heap(size_t n)
  691. {
  692. - static int init;
  693. + static int heap_lock[2];
  694. + static void *end;
  695. + void *p;
  696. struct chunk *w;
  697. - uintptr_t new;
  698. -
  699. - lock(mal.brk_lock);
  700. - if (!init) {
  701. - mal.brk = __brk(0);
  702. -#ifdef SHARED
  703. - mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
  704. -#endif
  705. - mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
  706. - mal.heap = (void *)mal.brk;
  707. - init = 1;
  708. + /* The argument n already accounts for the caller's chunk
  709. + * overhead needs, but if the heap can't be extended in-place,
  710. + * we need room for an extra zero-sized sentinel chunk. */
  711. + n += SIZE_ALIGN;
  712. +
  713. + lock(heap_lock);
  714. +
  715. + p = __expand_heap(&n);
  716. + if (!p) {
  717. + unlock(heap_lock);
  718. + return 0;
  719. }
  720. - if (n > SIZE_MAX - mal.brk - 2*PAGE_SIZE) goto fail;
  721. - new = mal.brk + n + SIZE_ALIGN + PAGE_SIZE - 1 & -PAGE_SIZE;
  722. - n = new - mal.brk;
  723. -
  724. - if (__brk(new) != new) {
  725. - size_t min = (size_t)PAGE_SIZE << mal.mmap_step/2;
  726. - n += -n & PAGE_SIZE-1;
  727. - if (n < min) n = min;
  728. - void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
  729. - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  730. - if (area == MAP_FAILED) goto fail;
  731. -
  732. - mal.mmap_step++;
  733. - area = (char *)area + SIZE_ALIGN - OVERHEAD;
  734. - w = area;
  735. + /* If not just expanding existing space, we need to make a
  736. + * new sentinel chunk below the allocated space. */
  737. + if (p != end) {
  738. + /* Valid/safe because of the prologue increment. */
  739. n -= SIZE_ALIGN;
  740. + p = (char *)p + SIZE_ALIGN;
  741. + w = MEM_TO_CHUNK(p);
  742. w->psize = 0 | C_INUSE;
  743. - w->csize = n | C_INUSE;
  744. - w = NEXT_CHUNK(w);
  745. - w->psize = n | C_INUSE;
  746. - w->csize = 0 | C_INUSE;
  747. -
  748. - unlock(mal.brk_lock);
  749. -
  750. - return area;
  751. }
  752. - w = MEM_TO_CHUNK(mal.heap);
  753. - w->psize = 0 | C_INUSE;
  754. -
  755. - w = MEM_TO_CHUNK(new);
  756. + /* Record new heap end and fill in footer. */
  757. + end = (char *)p + n;
  758. + w = MEM_TO_CHUNK(end);
  759. w->psize = n | C_INUSE;
  760. w->csize = 0 | C_INUSE;
  761. - w = MEM_TO_CHUNK(mal.brk);
  762. + /* Fill in header, which may be new or may be replacing a
  763. + * zero-size sentinel header at the old end-of-heap. */
  764. + w = MEM_TO_CHUNK(p);
  765. w->csize = n | C_INUSE;
  766. - mal.brk = new;
  767. -
  768. - unlock(mal.brk_lock);
  769. +
  770. + unlock(heap_lock);
  771. return w;
  772. -fail:
  773. - unlock(mal.brk_lock);
  774. - errno = ENOMEM;
  775. - return 0;
  776. }
  777. static int adjust_size(size_t *n)
  778. --- a/src/multibyte/btowc.c
  779. +++ b/src/multibyte/btowc.c
  780. @@ -1,7 +1,10 @@
  781. #include <stdio.h>
  782. #include <wchar.h>
  783. +#include <stdlib.h>
  784. +#include "internal.h"
  785. wint_t btowc(int c)
  786. {
  787. - return c<128U ? c : EOF;
  788. + int b = (unsigned char)c;
  789. + return b<128U ? b : (MB_CUR_MAX==1 && c!=EOF) ? CODEUNIT(c) : WEOF;
  790. }
  791. --- a/src/multibyte/internal.h
  792. +++ b/src/multibyte/internal.h
  793. @@ -23,3 +23,10 @@ extern const uint32_t bittab[];
  794. #define SA 0xc2u
  795. #define SB 0xf4u
  796. +
  797. +/* Arbitrary encoding for representing code units instead of characters. */
  798. +#define CODEUNIT(c) (0xdfff & (signed char)(c))
  799. +#define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80)
  800. +
  801. +/* Get inline definition of MB_CUR_MAX. */
  802. +#include "locale_impl.h"
  803. --- a/src/multibyte/mbrtowc.c
  804. +++ b/src/multibyte/mbrtowc.c
  805. @@ -4,6 +4,7 @@
  806. * unnecessary.
  807. */
  808. +#include <stdlib.h>
  809. #include <wchar.h>
  810. #include <errno.h>
  811. #include "internal.h"
  812. @@ -27,6 +28,7 @@ size_t mbrtowc(wchar_t *restrict wc, con
  813. if (!n) return -2;
  814. if (!c) {
  815. if (*s < 0x80) return !!(*wc = *s);
  816. + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
  817. if (*s-SA > SB-SA) goto ilseq;
  818. c = bittab[*s++-SA]; n--;
  819. }
  820. --- a/src/multibyte/mbsrtowcs.c
  821. +++ b/src/multibyte/mbsrtowcs.c
  822. @@ -7,6 +7,8 @@
  823. #include <stdint.h>
  824. #include <wchar.h>
  825. #include <errno.h>
  826. +#include <string.h>
  827. +#include <stdlib.h>
  828. #include "internal.h"
  829. size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
  830. @@ -24,6 +26,23 @@ size_t mbsrtowcs(wchar_t *restrict ws, c
  831. }
  832. }
  833. + if (MB_CUR_MAX==1) {
  834. + if (!ws) return strlen((const char *)s);
  835. + for (;;) {
  836. + if (!wn) {
  837. + *src = (const void *)s;
  838. + return wn0;
  839. + }
  840. + if (!*s) break;
  841. + c = *s++;
  842. + *ws++ = CODEUNIT(c);
  843. + wn--;
  844. + }
  845. + *ws = 0;
  846. + *src = 0;
  847. + return wn0-wn;
  848. + }
  849. +
  850. if (!ws) for (;;) {
  851. if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
  852. while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
  853. --- a/src/multibyte/mbtowc.c
  854. +++ b/src/multibyte/mbtowc.c
  855. @@ -4,6 +4,7 @@
  856. * unnecessary.
  857. */
  858. +#include <stdlib.h>
  859. #include <wchar.h>
  860. #include <errno.h>
  861. #include "internal.h"
  862. @@ -19,6 +20,7 @@ int mbtowc(wchar_t *restrict wc, const c
  863. if (!wc) wc = &dummy;
  864. if (*s < 0x80) return !!(*wc = *s);
  865. + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
  866. if (*s-SA > SB-SA) goto ilseq;
  867. c = bittab[*s++-SA];
  868. --- a/src/multibyte/wcrtomb.c
  869. +++ b/src/multibyte/wcrtomb.c
  870. @@ -4,8 +4,10 @@
  871. * unnecessary.
  872. */
  873. +#include <stdlib.h>
  874. #include <wchar.h>
  875. #include <errno.h>
  876. +#include "internal.h"
  877. size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
  878. {
  879. @@ -13,6 +15,13 @@ size_t wcrtomb(char *restrict s, wchar_t
  880. if ((unsigned)wc < 0x80) {
  881. *s = wc;
  882. return 1;
  883. + } else if (MB_CUR_MAX == 1) {
  884. + if (!IS_CODEUNIT(wc)) {
  885. + errno = EILSEQ;
  886. + return -1;
  887. + }
  888. + *s = wc;
  889. + return 1;
  890. } else if ((unsigned)wc < 0x800) {
  891. *s++ = 0xc0 | (wc>>6);
  892. *s = 0x80 | (wc&0x3f);
  893. --- a/src/multibyte/wctob.c
  894. +++ b/src/multibyte/wctob.c
  895. @@ -1,8 +1,10 @@
  896. -#include <stdio.h>
  897. #include <wchar.h>
  898. +#include <stdlib.h>
  899. +#include "internal.h"
  900. int wctob(wint_t c)
  901. {
  902. if (c < 128U) return c;
  903. + if (MB_CUR_MAX==1 && IS_CODEUNIT(c)) return (unsigned char)c;
  904. return EOF;
  905. }
  906. --- a/src/passwd/nscd_query.c
  907. +++ b/src/passwd/nscd_query.c
  908. @@ -32,6 +32,7 @@ FILE *__nscd_query(int32_t req, const ch
  909. },
  910. .msg_iovlen = 2
  911. };
  912. + int errno_save = errno;
  913. *swap = 0;
  914. retry:
  915. @@ -50,11 +51,14 @@ retry:
  916. return f;
  917. if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  918. - /* If there isn't a running nscd we return -1 to indicate that
  919. - * that is precisely what happened
  920. - */
  921. - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
  922. + /* If there isn't a running nscd we simulate a "not found"
  923. + * result and the caller is responsible for calling
  924. + * fclose on the (unconnected) socket. The value of
  925. + * errno must be left unchanged in this case. */
  926. + if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
  927. + errno = errno_save;
  928. return f;
  929. + }
  930. goto error;
  931. }
  932. --- /dev/null
  933. +++ b/src/process/sh/vfork.s
  934. @@ -0,0 +1,23 @@
  935. +.global __vfork
  936. +.weak vfork
  937. +.type __vfork,@function
  938. +.type vfork,@function
  939. +__vfork:
  940. +vfork:
  941. + mov #95, r3
  942. + add r3, r3
  943. +
  944. + trapa #16
  945. + or r0, r0
  946. + or r0, r0
  947. + or r0, r0
  948. + or r0, r0
  949. + or r0, r0
  950. +
  951. + mov r0, r4
  952. + mov.l 1f, r0
  953. +2: braf r0
  954. + nop
  955. + .align 2
  956. + .hidden __syscall_ret
  957. +1: .long __syscall_ret@PLT-(2b+4-.)
  958. --- a/src/regex/fnmatch.c
  959. +++ b/src/regex/fnmatch.c
  960. @@ -18,6 +18,7 @@
  961. #include <stdlib.h>
  962. #include <wchar.h>
  963. #include <wctype.h>
  964. +#include "locale_impl.h"
  965. #define END 0
  966. #define UNMATCHABLE -2
  967. @@ -229,7 +230,7 @@ static int fnmatch_internal(const char *
  968. * On illegal sequences we may get it wrong, but in that case
  969. * we necessarily have a matching failure anyway. */
  970. for (s=endstr; s>str && tailcnt; tailcnt--) {
  971. - if (s[-1] < 128U) s--;
  972. + if (s[-1] < 128U || MB_CUR_MAX==1) s--;
  973. else while ((unsigned char)*--s-0x80U<0x40 && s>str);
  974. }
  975. if (tailcnt) return FNM_NOMATCH;
  976. --- a/src/stdio/__fdopen.c
  977. +++ b/src/stdio/__fdopen.c
  978. @@ -54,13 +54,7 @@ FILE *__fdopen(int fd, const char *mode)
  979. if (!libc.threaded) f->lock = -1;
  980. /* Add new FILE to open file list */
  981. - OFLLOCK();
  982. - f->next = libc.ofl_head;
  983. - if (libc.ofl_head) libc.ofl_head->prev = f;
  984. - libc.ofl_head = f;
  985. - OFLUNLOCK();
  986. -
  987. - return f;
  988. + return __ofl_add(f);
  989. }
  990. weak_alias(__fdopen, fdopen);
  991. --- a/src/stdio/__stdio_exit.c
  992. +++ b/src/stdio/__stdio_exit.c
  993. @@ -16,8 +16,7 @@ static void close_file(FILE *f)
  994. void __stdio_exit(void)
  995. {
  996. FILE *f;
  997. - OFLLOCK();
  998. - for (f=libc.ofl_head; f; f=f->next) close_file(f);
  999. + for (f=*__ofl_lock(); f; f=f->next) close_file(f);
  1000. close_file(__stdin_used);
  1001. close_file(__stdout_used);
  1002. }
  1003. --- a/src/stdio/__stdio_read.c
  1004. +++ b/src/stdio/__stdio_read.c
  1005. @@ -1,12 +1,5 @@
  1006. #include "stdio_impl.h"
  1007. #include <sys/uio.h>
  1008. -#include <pthread.h>
  1009. -
  1010. -static void cleanup(void *p)
  1011. -{
  1012. - FILE *f = p;
  1013. - if (!f->lockcount) __unlockfile(f);
  1014. -}
  1015. size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
  1016. {
  1017. @@ -16,9 +9,7 @@ size_t __stdio_read(FILE *f, unsigned ch
  1018. };
  1019. ssize_t cnt;
  1020. - pthread_cleanup_push(cleanup, f);
  1021. - cnt = syscall_cp(SYS_readv, f->fd, iov, 2);
  1022. - pthread_cleanup_pop(0);
  1023. + cnt = syscall(SYS_readv, f->fd, iov, 2);
  1024. if (cnt <= 0) {
  1025. f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
  1026. return cnt;
  1027. --- a/src/stdio/__stdio_write.c
  1028. +++ b/src/stdio/__stdio_write.c
  1029. @@ -1,12 +1,5 @@
  1030. #include "stdio_impl.h"
  1031. #include <sys/uio.h>
  1032. -#include <pthread.h>
  1033. -
  1034. -static void cleanup(void *p)
  1035. -{
  1036. - FILE *f = p;
  1037. - if (!f->lockcount) __unlockfile(f);
  1038. -}
  1039. size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
  1040. {
  1041. @@ -19,9 +12,7 @@ size_t __stdio_write(FILE *f, const unsi
  1042. int iovcnt = 2;
  1043. ssize_t cnt;
  1044. for (;;) {
  1045. - pthread_cleanup_push(cleanup, f);
  1046. - cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
  1047. - pthread_cleanup_pop(0);
  1048. + cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
  1049. if (cnt == rem) {
  1050. f->wend = f->buf + f->buf_size;
  1051. f->wpos = f->wbase = f->buf;
  1052. @@ -34,11 +25,8 @@ size_t __stdio_write(FILE *f, const unsi
  1053. }
  1054. rem -= cnt;
  1055. if (cnt > iov[0].iov_len) {
  1056. - f->wpos = f->wbase = f->buf;
  1057. cnt -= iov[0].iov_len;
  1058. iov++; iovcnt--;
  1059. - } else if (iovcnt == 2) {
  1060. - f->wbase += cnt;
  1061. }
  1062. iov[0].iov_base = (char *)iov[0].iov_base + cnt;
  1063. iov[0].iov_len -= cnt;
  1064. --- a/src/stdio/fclose.c
  1065. +++ b/src/stdio/fclose.c
  1066. @@ -14,11 +14,11 @@ int fclose(FILE *f)
  1067. __unlist_locked_file(f);
  1068. if (!(perm = f->flags & F_PERM)) {
  1069. - OFLLOCK();
  1070. + FILE **head = __ofl_lock();
  1071. if (f->prev) f->prev->next = f->next;
  1072. if (f->next) f->next->prev = f->prev;
  1073. - if (libc.ofl_head == f) libc.ofl_head = f->next;
  1074. - OFLUNLOCK();
  1075. + if (*head == f) *head = f->next;
  1076. + __ofl_unlock();
  1077. }
  1078. r = fflush(f);
  1079. --- a/src/stdio/fflush.c
  1080. +++ b/src/stdio/fflush.c
  1081. @@ -35,13 +35,12 @@ int fflush(FILE *f)
  1082. r = __stdout_used ? fflush(__stdout_used) : 0;
  1083. - OFLLOCK();
  1084. - for (f=libc.ofl_head; f; f=f->next) {
  1085. + for (f=*__ofl_lock(); f; f=f->next) {
  1086. FLOCK(f);
  1087. if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
  1088. FUNLOCK(f);
  1089. }
  1090. - OFLUNLOCK();
  1091. + __ofl_unlock();
  1092. return r;
  1093. }
  1094. --- a/src/stdio/fgetwc.c
  1095. +++ b/src/stdio/fgetwc.c
  1096. @@ -1,8 +1,9 @@
  1097. #include "stdio_impl.h"
  1098. +#include "locale_impl.h"
  1099. #include <wchar.h>
  1100. #include <errno.h>
  1101. -wint_t __fgetwc_unlocked(FILE *f)
  1102. +static wint_t __fgetwc_unlocked_internal(FILE *f)
  1103. {
  1104. mbstate_t st = { 0 };
  1105. wchar_t wc;
  1106. @@ -10,8 +11,6 @@ wint_t __fgetwc_unlocked(FILE *f)
  1107. unsigned char b;
  1108. size_t l;
  1109. - f->mode |= f->mode+1;
  1110. -
  1111. /* Convert character from buffer if possible */
  1112. if (f->rpos < f->rend) {
  1113. l = mbrtowc(&wc, (void *)f->rpos, f->rend - f->rpos, &st);
  1114. @@ -39,6 +38,16 @@ wint_t __fgetwc_unlocked(FILE *f)
  1115. return wc;
  1116. }
  1117. +wint_t __fgetwc_unlocked(FILE *f)
  1118. +{
  1119. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  1120. + if (f->mode <= 0) fwide(f, 1);
  1121. + *ploc = f->locale;
  1122. + wchar_t wc = __fgetwc_unlocked_internal(f);
  1123. + *ploc = loc;
  1124. + return wc;
  1125. +}
  1126. +
  1127. wint_t fgetwc(FILE *f)
  1128. {
  1129. wint_t c;
  1130. --- a/src/stdio/fmemopen.c
  1131. +++ b/src/stdio/fmemopen.c
  1132. @@ -110,11 +110,5 @@ FILE *fmemopen(void *restrict buf, size_
  1133. if (!libc.threaded) f->lock = -1;
  1134. - OFLLOCK();
  1135. - f->next = libc.ofl_head;
  1136. - if (libc.ofl_head) libc.ofl_head->prev = f;
  1137. - libc.ofl_head = f;
  1138. - OFLUNLOCK();
  1139. -
  1140. - return f;
  1141. + return __ofl_add(f);
  1142. }
  1143. --- a/src/stdio/fopen.c
  1144. +++ b/src/stdio/fopen.c
  1145. @@ -18,7 +18,7 @@ FILE *fopen(const char *restrict filenam
  1146. /* Compute the flags to pass to open() */
  1147. flags = __fmodeflags(mode);
  1148. - fd = sys_open_cp(filename, flags, 0666);
  1149. + fd = sys_open(filename, flags, 0666);
  1150. if (fd < 0) return 0;
  1151. if (flags & O_CLOEXEC)
  1152. __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
  1153. --- a/src/stdio/fputwc.c
  1154. +++ b/src/stdio/fputwc.c
  1155. @@ -1,4 +1,5 @@
  1156. #include "stdio_impl.h"
  1157. +#include "locale_impl.h"
  1158. #include <wchar.h>
  1159. #include <limits.h>
  1160. #include <ctype.h>
  1161. @@ -7,8 +8,10 @@ wint_t __fputwc_unlocked(wchar_t c, FILE
  1162. {
  1163. char mbc[MB_LEN_MAX];
  1164. int l;
  1165. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  1166. - f->mode |= f->mode+1;
  1167. + if (f->mode <= 0) fwide(f, 1);
  1168. + *ploc = f->locale;
  1169. if (isascii(c)) {
  1170. c = putc_unlocked(c, f);
  1171. @@ -20,6 +23,8 @@ wint_t __fputwc_unlocked(wchar_t c, FILE
  1172. l = wctomb(mbc, c);
  1173. if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF;
  1174. }
  1175. + if (c==WEOF) f->flags |= F_ERR;
  1176. + *ploc = loc;
  1177. return c;
  1178. }
  1179. --- a/src/stdio/fputws.c
  1180. +++ b/src/stdio/fputws.c
  1181. @@ -1,23 +1,28 @@
  1182. #include "stdio_impl.h"
  1183. +#include "locale_impl.h"
  1184. #include <wchar.h>
  1185. int fputws(const wchar_t *restrict ws, FILE *restrict f)
  1186. {
  1187. unsigned char buf[BUFSIZ];
  1188. size_t l=0;
  1189. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  1190. FLOCK(f);
  1191. - f->mode |= f->mode+1;
  1192. + fwide(f, 1);
  1193. + *ploc = f->locale;
  1194. while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1)
  1195. if (__fwritex(buf, l, f) < l) {
  1196. FUNLOCK(f);
  1197. + *ploc = loc;
  1198. return -1;
  1199. }
  1200. FUNLOCK(f);
  1201. + *ploc = loc;
  1202. return l; /* 0 or -1 */
  1203. }
  1204. --- a/src/stdio/fwide.c
  1205. +++ b/src/stdio/fwide.c
  1206. @@ -1,13 +1,14 @@
  1207. -#include <wchar.h>
  1208. #include "stdio_impl.h"
  1209. -
  1210. -#define SH (8*sizeof(int)-1)
  1211. -#define NORMALIZE(x) ((x)>>SH | -((-(x))>>SH))
  1212. +#include "locale_impl.h"
  1213. int fwide(FILE *f, int mode)
  1214. {
  1215. FLOCK(f);
  1216. - if (!f->mode) f->mode = NORMALIZE(mode);
  1217. + if (mode) {
  1218. + if (!f->locale) f->locale = MB_CUR_MAX==1
  1219. + ? C_LOCALE : UTF8_LOCALE;
  1220. + if (!f->mode) f->mode = mode>0 ? 1 : -1;
  1221. + }
  1222. mode = f->mode;
  1223. FUNLOCK(f);
  1224. return mode;
  1225. --- /dev/null
  1226. +++ b/src/stdio/ofl.c
  1227. @@ -0,0 +1,16 @@
  1228. +#include "stdio_impl.h"
  1229. +#include "libc.h"
  1230. +
  1231. +static FILE *ofl_head;
  1232. +static volatile int ofl_lock[2];
  1233. +
  1234. +FILE **__ofl_lock()
  1235. +{
  1236. + LOCK(ofl_lock);
  1237. + return &ofl_head;
  1238. +}
  1239. +
  1240. +void __ofl_unlock()
  1241. +{
  1242. + UNLOCK(ofl_lock);
  1243. +}
  1244. --- /dev/null
  1245. +++ b/src/stdio/ofl_add.c
  1246. @@ -0,0 +1,11 @@
  1247. +#include "stdio_impl.h"
  1248. +
  1249. +FILE *__ofl_add(FILE *f)
  1250. +{
  1251. + FILE **head = __ofl_lock();
  1252. + f->next = *head;
  1253. + if (*head) (*head)->prev = f;
  1254. + *head = f;
  1255. + __ofl_unlock();
  1256. + return f;
  1257. +}
  1258. --- a/src/stdio/open_memstream.c
  1259. +++ b/src/stdio/open_memstream.c
  1260. @@ -79,11 +79,5 @@ FILE *open_memstream(char **bufp, size_t
  1261. if (!libc.threaded) f->lock = -1;
  1262. - OFLLOCK();
  1263. - f->next = libc.ofl_head;
  1264. - if (libc.ofl_head) libc.ofl_head->prev = f;
  1265. - libc.ofl_head = f;
  1266. - OFLUNLOCK();
  1267. -
  1268. - return f;
  1269. + return __ofl_add(f);
  1270. }
  1271. --- a/src/stdio/open_wmemstream.c
  1272. +++ b/src/stdio/open_wmemstream.c
  1273. @@ -81,11 +81,5 @@ FILE *open_wmemstream(wchar_t **bufp, si
  1274. if (!libc.threaded) f->lock = -1;
  1275. - OFLLOCK();
  1276. - f->next = libc.ofl_head;
  1277. - if (libc.ofl_head) libc.ofl_head->prev = f;
  1278. - libc.ofl_head = f;
  1279. - OFLUNLOCK();
  1280. -
  1281. - return f;
  1282. + return __ofl_add(f);
  1283. }
  1284. --- a/src/stdio/ungetwc.c
  1285. +++ b/src/stdio/ungetwc.c
  1286. @@ -1,4 +1,5 @@
  1287. #include "stdio_impl.h"
  1288. +#include "locale_impl.h"
  1289. #include <wchar.h>
  1290. #include <limits.h>
  1291. #include <ctype.h>
  1292. @@ -8,21 +9,19 @@ wint_t ungetwc(wint_t c, FILE *f)
  1293. {
  1294. unsigned char mbc[MB_LEN_MAX];
  1295. int l=1;
  1296. -
  1297. - if (c == WEOF) return c;
  1298. -
  1299. - /* Try conversion early so we can fail without locking if invalid */
  1300. - if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
  1301. - return WEOF;
  1302. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  1303. FLOCK(f);
  1304. - f->mode |= f->mode+1;
  1305. + if (f->mode <= 0) fwide(f, 1);
  1306. + *ploc = f->locale;
  1307. if (!f->rpos) __toread(f);
  1308. - if (!f->rpos || f->rpos < f->buf - UNGET + l) {
  1309. + if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF ||
  1310. + (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)) {
  1311. FUNLOCK(f);
  1312. - return EOF;
  1313. + *ploc = loc;
  1314. + return WEOF;
  1315. }
  1316. if (isascii(c)) *--f->rpos = c;
  1317. @@ -31,5 +30,6 @@ wint_t ungetwc(wint_t c, FILE *f)
  1318. f->flags &= ~F_EOF;
  1319. FUNLOCK(f);
  1320. + *ploc = loc;
  1321. return c;
  1322. }
  1323. --- a/src/stdio/vfwprintf.c
  1324. +++ b/src/stdio/vfwprintf.c
  1325. @@ -293,7 +293,10 @@ static int wprintf_core(FILE *f, const w
  1326. if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
  1327. l=w;
  1328. continue;
  1329. + case 'm':
  1330. + arg.p = strerror(errno);
  1331. case 's':
  1332. + if (!arg.p) arg.p = "(null)";
  1333. bs = arg.p;
  1334. if (p<0) p = INT_MAX;
  1335. for (i=l=0; l<p && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
  1336. @@ -356,7 +359,7 @@ int vfwprintf(FILE *restrict f, const wc
  1337. }
  1338. FLOCK(f);
  1339. - f->mode |= f->mode+1;
  1340. + fwide(f, 1);
  1341. olderr = f->flags & F_ERR;
  1342. f->flags &= ~F_ERR;
  1343. ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
  1344. --- a/src/stdio/vfwscanf.c
  1345. +++ b/src/stdio/vfwscanf.c
  1346. @@ -104,7 +104,7 @@ int vfwscanf(FILE *restrict f, const wch
  1347. FLOCK(f);
  1348. - f->mode |= f->mode+1;
  1349. + fwide(f, 1);
  1350. for (p=fmt; *p; p++) {
  1351. --- /dev/null
  1352. +++ b/src/thread/__unmapself.c
  1353. @@ -0,0 +1,29 @@
  1354. +#include "pthread_impl.h"
  1355. +#include "atomic.h"
  1356. +#include "syscall.h"
  1357. +/* cheat and reuse CRTJMP macro from dynlink code */
  1358. +#include "dynlink.h"
  1359. +
  1360. +static volatile int lock;
  1361. +static void *unmap_base;
  1362. +static size_t unmap_size;
  1363. +static char shared_stack[256];
  1364. +
  1365. +static void do_unmap()
  1366. +{
  1367. + __syscall(SYS_munmap, unmap_base, unmap_size);
  1368. + __syscall(SYS_exit);
  1369. +}
  1370. +
  1371. +void __unmapself(void *base, size_t size)
  1372. +{
  1373. + int tid=__pthread_self()->tid;
  1374. + char *stack = shared_stack + sizeof shared_stack;
  1375. + stack -= (uintptr_t)stack % 16;
  1376. + while (lock || a_cas(&lock, 0, tid))
  1377. + a_spin();
  1378. + __syscall(SYS_set_tid_address, &lock);
  1379. + unmap_base = base;
  1380. + unmap_size = size;
  1381. + CRTJMP(do_unmap, stack);
  1382. +}
  1383. --- a/src/thread/pthread_create.c
  1384. +++ b/src/thread/pthread_create.c
  1385. @@ -191,8 +191,9 @@ int __pthread_create(pthread_t *restrict
  1386. if (!libc.can_do_threads) return ENOSYS;
  1387. self = __pthread_self();
  1388. if (!libc.threaded) {
  1389. - for (FILE *f=libc.ofl_head; f; f=f->next)
  1390. + for (FILE *f=*__ofl_lock(); f; f=f->next)
  1391. init_file_lock(f);
  1392. + __ofl_unlock();
  1393. init_file_lock(__stdin_used);
  1394. init_file_lock(__stdout_used);
  1395. init_file_lock(__stderr_used);