summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
Commit message (Collapse)AuthorAgeFilesLines
...
* Add a complete example to tsearch(3)Brad Davis2017-07-141-1/+59
| | | | | | | | | Reviewed by: wblock, sevan, bruffer MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D11053 Notes: svn path=/head/; revision=320992
* Update jemalloc to 5.0.0.Jason Evans2017-06-151-4/+4
| | | | Notes: svn path=/head/; revision=319971
* quick_exit(3): delete trailing whitespace in licensing tortEnji Cooper2017-05-231-3/+3
| | | | | | | | | Reported by: make manlint MFC after: 2 weeks Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=318710
* The current qsort(3) implementation ignores the sizes of partitions, andXin LI2017-05-191-15/+38
| | | | | | | | | | | | | | always perform recursion on the left partition, then use a tail call to handle the right partition. In the worst case this could require O(N) levels of recursions. Reduce the possible recursion level to log2(N) by always recursing on the smaller partition instead. Obtained from: PostgreSQL 9d6077abf9d6efd992a59f05ef5aba981ea32096 Notes: svn path=/head/; revision=318515
* Use size_t.Xin LI2017-05-191-3/+3
| | | | | | | Inspired by: OpenBSD src/lib/libc/stdlib/qsort.c,v 1.11 Notes: svn path=/head/; revision=318514
* Style.Konstantin Belousov2017-05-151-9/+7
| | | | | | | | Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=318303
* Simplify cleanup on failure in realpath(3).Konstantin Belousov2017-05-151-44/+31
| | | | | | | | | | | | | | | | | | If realpath() allocated memory for result and failed, the memory is freed in each place where return is performed. More, the function needs to track the allocation status, to not free user-supplied buffer. Consolidate the memory handling in the wrapper, freeing the buffer if the actual worker failed. Reviewed by: emaste (previous version) Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D10670 Notes: svn path=/head/; revision=318299
* Fix several buffer overflows in realpath(3).Konstantin Belousov2017-05-151-19/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - The statement "left_len -= s - left;" does not take the slash into account if one was found. This results in the invariant "left[left_len] == '\0'" being violated (and possible buffer overflows). The patch replaces the variable "s" with a size_t "next_token_len" for more clarity. - "slen" from readlink(2) can be 0 when encountering empty symlinks. Then, further down, "symlink[slen - 1]" underflows the buffer. When slen == 0, realpath(3) should probably return ENOENT (http://austingroupbugs.net/view.php?id=825, https://lwn.net/Articles/551224/). Some other minor issues: - The condition "resolved_len >= PATH_MAX" cannot be true. - Similarly, "s - left >= sizeof(next_token)" cannot be true, as long as "sizeof(next_token) >= sizeof(left)". - Return ENAMETOOLONG when a resolved symlink from readlink(2) is too long for the symlink buffer (instead of just truncating it). - "resolved_len > 1" below the call to readlink(2) is always true as "strlcat(resolved, next_token, PATH_MAX);" always results in a string of length > 1. Also, "resolved[resolved_len - 1] = '\0';" is not needed; there can never be a trailing slash here. - The truncation check for "strlcat(symlink, left, sizeof(symlink));" should be against "sizeof(symlink)" (the third argument to strlcat) instead of "sizeof(left)". Submitted by: Jan Kokemц╪ller <jan.kokemueller@gmail.com> PR: 219154 MFC after: 2 weeks Notes: svn path=/head/; revision=318298
* Implement the memset_s(3) function as specified by the C11 ISO/IECKonstantin Belousov2017-03-303-2/+100
| | | | | | | | | | | | | | | | | 9899:2011 Appendix K 3.7.4.1. Other needed supporting types, defines and constraint_handler infrastructure is added as specified in the C11 spec. Submitted by: Tom Rix <trix@juniper.net> Sponsored by: Juniper Networks Discussed with: ed MFC after: 3 weeks Differential revision: https://reviews.freebsd.org/D9903 Differential revision: https://reviews.freebsd.org/D10161 Notes: svn path=/head/; revision=316213
* Minor libc cleanup: let calloc(3) do the multiplication.Pedro F. Giffuni2017-03-131-1/+1
| | | | | | | MFC after: 3 days Notes: svn path=/head/; revision=315187
* libc: provide some bounds-checking through reallocarray(3).Pedro F. Giffuni2017-03-121-4/+4
| | | | | | | | | | | | reallocarray(3) is a non portable extension that originated in OpenBSD. Given that it is already in FreeBSD's libc it is useful for the cases where reallocation involves a multiplication. MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D9955 Notes: svn path=/head/; revision=315162
* hcreate(3): fix the ERRORS section and bump .DdEnji Cooper2017-02-071-4/+4
| | | | | | | | | | | | - Add missing comma between functions that trigger ENOMEM error. - Fix the description for ESRCH. The action that triggers this error is FIND, not SEARCH (SEARCH does not exist). MFC after: 1 week Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=313374
* Replace dot-dot relative pathing with SRCTOP-relative paths where possibleEnji Cooper2017-01-201-3/+3
| | | | | | | | | | | | | | | | | | | | This reduces build output, need for recalculating paths, and makes it clearer which paths are relative to what areas in the source tree. The change in performance over a locally mounted UFS filesystem was negligible in my testing, but this may more positively impact other filesystems like NFS. LIBC_SRCTOP was left alone so Juniper (and other users) can continue to manipulate lib/libc/Makefile (and other Makefile.inc's under lib/libc) as include Makefiles with custom options. Discussed with: marcel, sjg MFC after: 1 week Reviewed by: emaste Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D9207 Notes: svn path=/head/; revision=312451
* Export __cxa_thread_atexit_impl as an alias for __cxa_thread_atexit.Konstantin Belousov2017-01-074-106/+162
| | | | | | | | | | | | | | | | | | | | | | | | | | libstdc++ before gcc r244057 expected that libc provided __cxa_thread_atexit_impl, and libstdc++ implemented __cxa_thread_atexit, by forwarding the calls to _impl. Mentioned gcc revision checks for __cxa_thread_atexit in libc and does not provide the symbol from libstdc++ if found. This change helps older gcc, in particular, all released versions which implement thread_local, by consolidating the implementation into libc. For that versions, if configured with the current libc, the __cxa_thread_atexit is exported from libstdc++ as a trivial wrapper around libc::__cxa_thread_atexit_impl. The __cxa_thread_atexit implementation is put into separate source file to allow for static linking with older libstdc++.a. gcc bugzilla: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78968 Reported by: Hannes Hauswedell <h2+fbsdports@fsfe.org> PR: 215709 Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=311651
* Update jemalloc to 4.3.1.Jason Evans2016-11-091-2/+2
| | | | Notes: svn path=/head/; revision=308473
* Improve phrasing of the STANDARDS section.Ed Schouten2016-10-151-2/+2
| | | | | | | | | Reported by: wblock MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D8205 Notes: svn path=/head/; revision=307343
* Improve typing of POSIX search tree functions.Ed Schouten2016-10-135-52/+54
| | | | | | | | | | | | | | | | | | | | | | | | | | Back in 2015 when I reimplemented these functions to use an AVL tree, I was annoyed by the weakness of the typing of these functions. Both tree nodes and keys are represented by 'void *', meaning that things like the documentation for these functions are an absolute train wreck. To make things worse, users of these functions need to cast the return value of tfind()/tsearch() from 'void *' to 'type_of_key **' in order to access the key. Technically speaking such casts violate aliasing rules. I've observed actual breakages as a result of this by enabling features like LTO. I've filed a bug report at the Austin Group. Looking at the way the bug got resolved, they made a pretty good step in the right direction. A new type 'posix_tnode' has been added to correspond to tree nodes. It is still defined as 'void' for source-level compatibility, but in the very far future it could be replaced by a proper structure type containing a key pointer. MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D8205 Notes: svn path=/head/; revision=307227
* Add comment on use of abort() in libcEd Maste2016-10-121-1/+8
| | | | | | | Suggested by: jonathan (in review D8133) Notes: svn path=/head/; revision=307148
* abort in srandomdev if kern.arandom sysctl failsEd Maste2016-10-051-4/+5
| | | | | | | | | | | | | | | | | | | The sysctl cannot fail. If it does fail on some FreeBSD derivative or after some future change, just abort() so that the problem will be found and fixed. While abort() is not normally suitable for a library, it makes sense here. This is akin to r306636 for arc4random. Reviewed by: ed MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D8077 Notes: svn path=/head/; revision=306708
* When MAKEOBJDIRPREFIX points to a case-insensitive file system, theMarcel Moolenaar2016-09-161-1/+8
| | | | | | | | | | | | | | build can break when different source files create the same object files (case-insensitivity speaking). This is the case for _Exit.c and _exit.s. Compile _Exit.c as C99_Exit.c Reviewed by: sjg@ MFC after: completion Sponsored by: Bracket Computing Differential Revision: https://reviews.freebsd.org/D7893 Notes: svn path=/head/; revision=305855
* Add __cxa_thread_atexit(3) API implementation.Konstantin Belousov2016-08-064-1/+152
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is the backing feature to implement C++11 thread storage duration specified by the thread_local keyword. A destructor for given thread-local object is registered to be executed at the thread termination time using __cxa_thread_atexit(). Libc calls the __cxa_thread_calls_dtors() during exit(3), before finalizers and atexit functions, and libthr calls the function at the thread termination time, after the stack unwinding and thread-specific key destruction. There are several uncertainties in the API which lacks a formal specification. Among them: - is it allowed to register destructors during destructing; we allow, but limiting the nesting level. If too many iterations detected, a diagnostic is issued to stderr and thread forcibly terminates for now. - how to handle destructors which belong to an unloading dso; for now, we ignore destructor calls for such entries, and issue a diagnostic. Linux does prevent dso unload until all threads with destructors from the dso terminated. It is supposed that the diagnostics allow to detect real-world applications relying on the above details and possibly adjust our implementation. Right now the choices were to provide the slim API (but that rarely stands the practice test). Tests are added to check generic functionality and to specify some of the above implementation choices. Submitted by: Mahdi Mokhtari <mokhi64_gmail.com> Reviewed by: theraven Discussed with: dim (detection of -std=c++11 supoort for tests) Sponsored by: The FreeBSD Foundation (my involvement) MFC after: 2 weeks Differential revisions: https://reviews.freebsd.org/D7224, https://reviews.freebsd.org/D7427 Notes: svn path=/head/; revision=303795
* Fix typing of srandom() and initstate().Ed Schouten2016-07-262-5/+5
| | | | | | | | | | | | | | | | POSIX requires that these functions have an unsigned int for their first argument; not an unsigned long. My reasoning is that we can safely change these functions without breaking the ABI. As far as I know, our supported architectures either use registers for passing function arguments that are at least as big as long (e.g., amd64), or int and long are of the same size (e.g., i386). Reviewed by: ache Differential Revision: https://reviews.freebsd.org/D6644 Notes: svn path=/head/; revision=303342
* Reflect error indication according to POSIX and what those functionsAndrey A. Chernov2016-06-051-2/+2
| | | | | | | currently do. Notes: svn path=/head/; revision=301448
* Don't use fixup for C99 and up, the compiler result is already correct.Andrey A. Chernov2016-06-014-0/+8
| | | | | | | | | Suggested by: bde MFC after: 1 week Notes: svn path=/head/; revision=301115
* Micro optimize: C standard guarantees that right shift for unsigned valueAndrey A. Chernov2016-05-291-1/+1
| | | | | | | | | | fills left bits with zero, and we have exact 32bit unsigned value (uint32_t), so there is no reason to add "& 0x7fffffff" here. MFC after: 1 week Notes: svn path=/head/; revision=300965
* 1) Unifdef USE_WEAK_SEEDING since it is too obsolete to support and makesAndrey A. Chernov2016-05-291-41/+11
| | | | | | | | | | | | | | | | reading hard. 2) Instead of doing range transformation in each and every function here, do it single time directly in do_rand(). One "mod" operation overhead is not a big deal, but the code looks nicer and possible future functions additions or PRNG change do not miss range transformations neither have unneeded ones. 3) Use POSIX argument types for visible functions (cosmetic). MFC after: 1 week Notes: svn path=/head/; revision=300956
* 1) Unifdef USE_WEAK_SEEDING it is too obsolete to support and makes readingAndrey A. Chernov2016-05-291-46/+14
| | | | | | | | | | | | | | | | harder. 2) ACM paper require seed to be in [1, 2^31-2] range, so use the same range shifting as already done for rand(3). Also protect srandomdev() + TYPE_0 case (non default) from negative seeds. 3) Don't check for valid "type" range in setstate(), it is always valid as calculated. Instead add a check that rear pointer not exceeed end pointer. MFC after: 1 week Notes: svn path=/head/; revision=300953
* Let l64a() properly null terminate its result.Ed Schouten2016-05-261-22/+14
| | | | | | | | | | | | | | | | | Though the buffer used by l64a() is initialized with null bytes, repetetive calls may end up having trailing garbage of previous invocations because we don't end up terminating the string. Instead of importing NetBSD's fix, use this opportunity to simplify this function dramatically, for example by just storing the Base64 character set in a string. There is also no need to do the bitmasking, as we can just use the proper integer type from <stdint.h>. MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D6511 Notes: svn path=/head/; revision=300775
* 1) POSIX prohibits printing errors to stderr here and requireAndrey A. Chernov2016-05-221-15/+10
| | | | | | | | | | | | | | | | | | returning NULL: "Upon successful completion, initstate() and setstate() shall return a pointer to the previous state array; otherwise, a null pointer shall be returned. Although some implementations of random() have written messages to standard error, such implementations do not conform to POSIX.1-2008." 2) Move error detections earlier to prevent state modifying. MFC after: 1 week Notes: svn path=/head/; revision=300397
* Update jemalloc to 4.2.0.Jason Evans2016-05-131-1/+1
| | | | Notes: svn path=/head/; revision=299587
* libc: spelling fixes.Pedro F. Giffuni2016-04-302-3/+3
| | | | | | | Mostly on comments. Notes: svn path=/head/; revision=298830
* libc: do not include <sys/types.h> where <sys/param.h> was already includedAndriy Voskoboinyk2016-04-181-1/+0
| | | | | | | | | According to style(9): > normally, include <sys/types.h> OR <sys/param.h>, but not both. (<sys/param.h> already includes <sys/types.h> when LOCORE is not defined). Notes: svn path=/head/; revision=298226
* Fixed indentation, minor style.Sergey Kandaurov2016-04-181-12/+12
| | | | Notes: svn path=/head/; revision=298201
* Fix markup on "\n" in printf so it renders correctly.Warren Block2016-04-171-4/+4
| | | | | | | | | PR: 208852 Submitted by: coder@tuxfamily.org MFC after: 1 week Notes: svn path=/head/; revision=298156
* Update jemalloc to 4.1.0.Jason Evans2016-02-292-2/+7
| | | | | | | | | | | | | Add missing Symbol.map entry for __aligned_alloc. Add weak-->strong symbol binding for {malloc_stats_print,mallctl,mallctlnametomib,mallctlbymib} --> {__malloc_stats_print,__mallctl,__mallctlnametomib,__mallctlbymib}. These bindings complete the set necessary to allow applications to replace all malloc-related symbols. Notes: svn path=/head/; revision=296221
* Add .NOMETA missed in r291320.Bryan Drewery2016-01-201-1/+1
| | | | | | | Sponsored by: EMC / Isilon Storage Division Notes: svn path=/head/; revision=294458
* Remove an unneeded assignment of the return value.Ed Schouten2016-01-141-1/+0
| | | | | | | | | | tdelete() is supposed to return the address of the parent node that has been deleted. We already keep track of this node in the loop between lines 94-107. The GO_LEFT()/GO_RIGHT() macros are used later on as well, so we must make sure not to change it to something else. Notes: svn path=/head/; revision=293879
* Replace implementation of hsearch() by one that scales.Ed Schouten2015-12-277-185/+335
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Traditionally the hcreate() function creates a hash table that uses chaining, using a fixed user-provided size. The problem with this approach is that this often either wastes memory (table too big) or yields bad performance (table too small). For applications it may not always be easy to estimate the right hash table size. A fixed number only increases performance compared to a linked list by a constant factor. This problem can be solved easily by dynamically resizing the hash table. If the size of the hash table is at least doubled, this has no negative on the running time complexity. If a dynamically sized hash table is used, we can also switch to using open addressing instead of chaining, which has the advantage of just using a single allocation for the entire table, instead of allocating many small objects. Finally, a problem with the existing implementation is that its deterministic algorithm for hashing makes it possible to come up with fixed patterns to trigger an excessive number of collisions. We can easily solve this by using FNV-1a as a hashing algorithm in combination with a randomly generated offset basis. Measurements have shown that this implementation is about 20-25% faster than the existing implementation (even if the existing implementation is given an excessive number of buckets). Though it allocates more memory through malloc() than the old implementation (between 4-8 pointers per used entry instead of 3), process memory use is similar to the old implementation as if the estimated size was underestimated by a factor 10. This is due to the fact that malloc() needs to perform less bookkeeping. Reviewed by: jilles, pfg Obtained from: https://github.com/NuxiNL/cloudlibc Differential Revision: https://reviews.freebsd.org/D4644 Notes: svn path=/head/; revision=292767
* Let tsearch()/tdelete() use an AVL tree.Ed Schouten2015-12-224-86/+473
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The existing implementations of POSIX tsearch() and tdelete() don't attempt to perform any balancing at all. Testing reveals that inserting 100k nodes into a tree sequentially takes approximately one minute on my system. Though most other BSDs also don't use any balanced tree internally, C libraries like glibc and musl do provide better implementations. glibc uses a red-black tree and musl uses an AVL tree. Red-black trees have the advantage over AVL trees that they only require O(1) rotations after insertion and deletion, but have the disadvantage that the tree has a maximum depth of 2*log2(n) instead of 1.44*log2(n). My take is that it's better to focus on having a lower maximum depth, for the reason that in the case of tsearch() the invocation of the comparator likely dominates the running time. This change replaces the tsearch() and tdelete() functions by versions that create an AVL tree. Compared to musl's implementation, this version is different in two different ways: - We don't keep track of heights; just balances. This is sufficient. This has the advantage that it reduces the number of nodes that are being accessed. Storing heights requires us to also access all of the siblings along the path. - Don't use any recursion at all. We know that the tree cannot 2^64 elements in size, so the height of the tree can never be larger than 96. Use a 128-bit bitmask to keep track of the path that is computed. This allows us to iterate over the same path twice, meaning we can apply rotations from top to bottom. Inserting 100k nodes into a tree now only takes 0.015 seconds. Insertion seems to be twice as fast as glibc, whereas deletion has about the same performance. Unlike glibc, it uses a fixed amount of memory. I also experimented with both recursive and iterative bottom-up implementations of the same algorithm. This iterative top-down version performs similar to the recursive bottom-up version in terms of speed and code size. For some reason, the iterative bottom-up algorithm was actually 30% faster for deletion, but has a quadratic memory complexity to keep track of all the parent pointers. Reviewed by: jilles Obtained from: https://github.com/NuxiNL/cloudlibc Differential Revision: https://reviews.freebsd.org/D4412 Notes: svn path=/head/; revision=292613
* META MODE: Don't create .meta files when symlinking sources into the obj ↵Bryan Drewery2015-11-251-4/+4
| | | | | | | | | | | | | | directory. Tracking these leads to situations where meta mode will consider the file to be out of date if /bin/sh or /bin/ln are newer than the source file. There's no reason for meta mode to do this as make is already handling the rebuild dependency fine. Sponsored by: EMC / Isilon Storage Division Notes: svn path=/head/; revision=291320
* Use ANSI C prototypes. Eliminates -Wold-style-definition warnings.Craig Rodrigues2015-09-2012-51/+28
| | | | Notes: svn path=/head/; revision=288030
* Remove names from prototypesCraig Rodrigues2015-09-203-7/+7
| | | | Notes: svn path=/head/; revision=288026
* Add declarations to eliminate -Wmissing-prototypes warnings.Craig Rodrigues2015-09-203-0/+15
| | | | Notes: svn path=/head/; revision=288005
* Use ANSI C prototypes.Craig Rodrigues2015-09-143-45/+14
| | | | | | | Eliminates gcc 4.9 warnings. Notes: svn path=/head/; revision=287793
* Switch libc from using _sig{procmask,action,suspend} symbols, whichKonstantin Belousov2015-08-292-12/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | are aliases for the syscall stubs and are plt-interposed, to the libc-private aliases of internally interposed sigprocmask() etc. Since e.g. _sigaction is not interposed by libthr, calling signal() removes thr_sighandler() from the handler slot etc. The result was breaking signal semantic and rtld locking. The added __libc_sigprocmask and other symbols are hidden, they are not exported and cannot be called through PLT. The setjmp/longjmp functions for x86 were changed to use direct calls, and since PIC_PROLOGUE only needed for functional PLT indirection on i386, it is removed as well. The PowerPC bug of calling the syscall directly in the setjmp/longjmp implementation is kept as is. Reported by: Pete French <petefrench@ingresso.co.uk> Tested by: Michiel Boland <boland37@xs4all.nl> Reviewed by: jilles (previous version) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=287292
* Add missing sdallocx updates and remove *allocm manpage links.Jason Evans2015-08-182-5/+6
| | | | | | | Submitted by: jbeich Notes: svn path=/head/; revision=286872
* Update jemalloc to version 4.0.0.Jason Evans2015-08-181-1/+2
| | | | Notes: svn path=/head/; revision=286866
* Document the fact that system(3) can easily be misused due to shell metaXin LI2015-07-251-2/+14
| | | | | | | | | | characters are honored. While I'm there also mention posix_spawn in the SEE ALSO section. MFC after: 2 weeks Notes: svn path=/head/; revision=285864
* Fix major copy/paste and other style errors.Sergey Kandaurov2015-05-051-5/+10
| | | | Notes: svn path=/head/; revision=282472
* Apply the copyright the the same owners as the original malloc(3) where most ofBaptiste Daroussin2015-05-011-2/+3
| | | | | | | | | | | the text here comes from Reported by: many Discussed with: miod@OpenBSD.org Pointyhat to: bapt Notes: svn path=/head/; revision=282321