Fix bugs and make code tinier

- Fixed bug where stdio eof wasn't being sticky
- Fixed bug where fseeko() wasn't clearing eof state
- Removed assert() usage from libc favoring _unassert() / _npassert()
This commit is contained in:
Justine Tunney 2022-10-09 22:38:28 -07:00
parent 9b7c8db846
commit d5910e2673
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
115 changed files with 510 additions and 290 deletions

View file

@ -26,7 +26,10 @@
*
* This calls functions registered by atexit() before terminating
* the current process, and any associated threads. It also calls
* all the legacy linker registered destructors in reeverse order
* all the legacy linker registered destructors in reversed order
*
* This implementation allows exit() to be called recursively via
* atexit() handlers.
*
* @param exitcode is masked with 255
* @see _Exit()

View file

@ -41,9 +41,9 @@ bool __grow(void *pp, size_t *capacity, size_t itemsize, size_t extra) {
size_t t1, t2;
extra += GUARANTEE_TERMINATOR;
p = (void **)pp;
assert(itemsize);
assert((*p && *capacity) || (!*p && !*capacity));
assert(!_isheap(*p) || ((intptr_t)*p & 15) == 0);
_unassert(itemsize);
_unassert((*p && *capacity) || (!*p && !*capacity));
_unassert(!_isheap(*p) || ((intptr_t)*p & 15) == 0);
p1 = _isheap(*p) ? *p : NULL;
p2 = NULL;
n1 = *capacity;

View file

@ -25,6 +25,7 @@
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/asancodes.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/bsr.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/safemacros.internal.h"
@ -63,6 +64,10 @@
#define SHADE(x) (((intptr_t)(x) >> 3) + 0x7fff8000)
#define FRAME(x) ((int)((intptr_t)(x) >> 16))
static unsigned long RoundDownTwoPow(unsigned long x) {
return x ? 1ul << _bsrl(x) : 0;
}
static wontreturn void OnUnrecoverableMmapError(const char *s) {
if (_weaken(__die)) _weaken(__die)();
STRACE("%s %m", s);
@ -322,7 +327,7 @@ static noasan inline void *Mmap(void *addr, size_t size, int prot, int flags,
return VIP(einval());
}
a = max(1, _rounddown2pow(size) >> 16);
a = max(1, RoundDownTwoPow(size) >> 16);
f = (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED;
if (flags & MAP_FIXED) {

View file

@ -91,7 +91,7 @@ void *mremap(void *p, size_t n, size_t m, int f, ... /* void *q */) {
size_t i, j, k;
struct DirectMap dm;
int a, b, prot, flags;
assert(!__vforked);
_unassert(!__vforked);
if (UNLIKELY(!m)) {
STRACE("m=0");
return VIP(einval());

View file

@ -18,10 +18,10 @@
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/macros.internal.h"
#include "libc/sysv/consts/msync.h"
@ -37,7 +37,7 @@
*/
int msync(void *addr, size_t size, int flags) {
int rc;
assert(((flags & MS_SYNC) ^ (flags & MS_ASYNC)) || !(MS_SYNC && MS_ASYNC));
_unassert(((flags & MS_SYNC) ^ (flags & MS_ASYNC)) || !(MS_SYNC && MS_ASYNC));
if (!IsWindows()) {
rc = sys_msync(addr, size, flags);
} else {

View file

@ -95,9 +95,7 @@ static noasan void MunmapImpl(char *p, size_t n) {
beg = MAX(_mmi.p[i].x, l);
end = _mmi.p[i].y;
} else {
// shouldn't be possible
assert(!"binary search panic");
continue;
unreachable;
}
// openbsd even requires that if we mapped, for instance a 5 byte
// file, that we be sure to call munmap(file, 5). let's abstract!
@ -107,8 +105,7 @@ static noasan void MunmapImpl(char *p, size_t n) {
q = (char *)a;
m = MIN(b, c) - a;
if (!IsWindows()) {
rc = sys_munmap(q, m);
assert(!rc);
_npassert(!sys_munmap(q, m));
} else {
// Handled by UntrackMemoryIntervals() on Windows
}
@ -122,7 +119,7 @@ static noasan int Munmap(char *p, size_t n) {
unsigned i;
char poison;
intptr_t a, b, x, y;
assert(!__vforked);
_unassert(!__vforked);
if (UNLIKELY(!n)) {
STRACE("n=0");
return einval();

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/promises.internal.h"
@ -48,30 +49,33 @@ size_t GetStackUsage(char *s, size_t n) {
}
static textexit void LogStackUse(void) {
int i, fd;
bool quote;
char *p, *q;
int i, e, fd;
size_t n, usage;
if (!PLEDGED(STDIO) || !PLEDGED(WPATH) || !PLEDGED(CPATH)) return;
usage = GetStackUsage((char *)GetStackAddr(), GetStackSize());
fd = open(stacklog, O_APPEND | O_CREAT | O_WRONLY, 0644);
p = FormatUint64(stacklog, usage);
for (i = 0; i < __argc; ++i) {
n = strlen(__argv[i]);
if ((q = memchr(__argv[i], '\n', n))) n = q - __argv[i];
if (p - stacklog + 1 + 1 + n + 1 + 1 < sizeof(stacklog)) {
quote = !!memchr(__argv[i], ' ', n);
*p++ = ' ';
if (quote) *p++ = '\'';
p = mempcpy(p, __argv[i], n);
if (quote) *p++ = '\'';
} else {
break;
e = errno;
if ((fd = open(stacklog, O_APPEND | O_CREAT | O_WRONLY, 0644)) != -1) {
p = FormatUint64(stacklog, usage);
for (i = 0; i < __argc; ++i) {
n = strlen(__argv[i]);
if ((q = memchr(__argv[i], '\n', n))) n = q - __argv[i];
if (p - stacklog + 1 + 1 + n + 1 + 1 < sizeof(stacklog)) {
quote = !!memchr(__argv[i], ' ', n);
*p++ = ' ';
if (quote) *p++ = '\'';
p = mempcpy(p, __argv[i], n);
if (quote) *p++ = '\'';
} else {
break;
}
}
*p++ = '\n';
write(fd, stacklog, p - stacklog);
close(fd);
}
*p++ = '\n';
write(fd, stacklog, p - stacklog);
close(fd);
errno = e;
}
static textstartup void LogStackUseInit(void) {

View file

@ -23,7 +23,7 @@
int UntrackMemoryIntervals(void *addr, size_t size) {
int a, b;
assert(size > 0);
_unassert(size > 0);
a = ROUNDDOWN((intptr_t)addr, FRAMESIZE) >> 16;
b = ROUNDDOWN((intptr_t)addr + size - 1, FRAMESIZE) >> 16;
return ReleaseMemoryIntervals(&_mmi, a, b,