2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-07-01 12:10:12 +00:00
|
|
|
#include "ape/sections.internal.h"
|
|
|
|
#include "libc/assert.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/calls/struct/rlimit.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/dce.h"
|
2022-08-14 00:20:50 +00:00
|
|
|
#include "libc/errno.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-08-14 00:20:50 +00:00
|
|
|
#include "libc/intrin/bits.h"
|
|
|
|
#include "libc/intrin/safemacros.internal.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-08-14 00:20:50 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/log/check.h"
|
|
|
|
#include "libc/log/color.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/log/libfatal.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/log/log.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-05-13 20:31:21 +00:00
|
|
|
#include "libc/nexgen32e/vendor.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nexgen32e/x86feature.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
2021-05-14 12:36:58 +00:00
|
|
|
#include "libc/runtime/symbols.internal.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/runtime/sysconf.h"
|
|
|
|
#include "libc/sock/sock.h"
|
2022-08-06 10:51:50 +00:00
|
|
|
#include "libc/sock/struct/pollfd.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
|
|
|
#include "libc/sysv/consts/ex.h"
|
|
|
|
#include "libc/sysv/consts/exit.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/sysv/consts/f.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
#include "libc/sysv/consts/poll.h"
|
2023-07-01 12:10:12 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/sysv/consts/rlimit.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/testlib/testlib.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "third_party/dlmalloc/dlmalloc.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "third_party/getopt/getopt.h"
|
|
|
|
|
|
|
|
#define USAGE \
|
|
|
|
" [FLAGS]\n\
|
|
|
|
\n\
|
|
|
|
Flags:\n\
|
|
|
|
\n\
|
|
|
|
-b run benchmarks if tests pass\n\
|
|
|
|
-h show this information\n\
|
|
|
|
\n"
|
|
|
|
|
2020-11-25 16:19:00 +00:00
|
|
|
STATIC_YOINK("__die");
|
2022-09-13 06:10:38 +00:00
|
|
|
STATIC_YOINK("GetSymbolByAddr");
|
2021-09-28 05:58:51 +00:00
|
|
|
STATIC_YOINK("testlib_quota_handlers");
|
2022-05-18 23:41:29 +00:00
|
|
|
STATIC_YOINK("stack_usage_logging");
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
static bool runbenchmarks_;
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
void PrintUsage(int rc, FILE *f) {
|
2020-06-15 14:18:57 +00:00
|
|
|
fputs("Usage: ", f);
|
2022-05-13 20:31:21 +00:00
|
|
|
fputs(firstnonnull(program_invocation_name, "unknown"), f);
|
2020-06-15 14:18:57 +00:00
|
|
|
fputs(USAGE, f);
|
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
void GetOpts(int argc, char *argv[]) {
|
2020-06-15 14:18:57 +00:00
|
|
|
int opt;
|
2020-10-11 04:18:53 +00:00
|
|
|
while ((opt = getopt(argc, argv, "?hbv")) != -1) {
|
2020-06-15 14:18:57 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'b':
|
|
|
|
runbenchmarks_ = true;
|
|
|
|
break;
|
2020-10-11 04:18:53 +00:00
|
|
|
case 'v':
|
2021-03-01 07:42:35 +00:00
|
|
|
++__log_level;
|
2020-10-11 04:18:53 +00:00
|
|
|
break;
|
2020-06-15 14:18:57 +00:00
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
PrintUsage(EXIT_SUCCESS, stdout);
|
|
|
|
default:
|
|
|
|
PrintUsage(EX_USAGE, stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 16:56:53 +00:00
|
|
|
static void EmptySignalMask(void) {
|
|
|
|
sigset_t ss;
|
|
|
|
sigemptyset(&ss);
|
|
|
|
sigprocmask(SIG_SETMASK, &ss, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FixIrregularFds(void) {
|
2022-08-15 22:18:37 +00:00
|
|
|
int e, i, fd, maxfds;
|
2022-06-21 14:22:39 +00:00
|
|
|
struct rlimit rlim;
|
|
|
|
struct pollfd *pfds;
|
2022-04-20 16:56:53 +00:00
|
|
|
for (i = 0; i < 3; ++i) {
|
2022-05-13 20:31:21 +00:00
|
|
|
if (fcntl(i, F_GETFL) == -1) {
|
|
|
|
errno = 0;
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
CHECK_NE(-1, fd);
|
|
|
|
if (fd != i) {
|
|
|
|
close(fd);
|
|
|
|
}
|
2022-04-20 16:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-15 22:18:37 +00:00
|
|
|
e = errno;
|
|
|
|
if (!closefrom(3)) return;
|
|
|
|
errno = e;
|
2022-06-21 14:22:39 +00:00
|
|
|
if (IsWindows()) {
|
|
|
|
maxfds = 64;
|
|
|
|
} else {
|
|
|
|
maxfds = 256;
|
|
|
|
if (!getrlimit(RLIMIT_NOFILE, &rlim)) {
|
|
|
|
maxfds = MIN(maxfds, (uint64_t)rlim.rlim_cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pfds = malloc(maxfds * sizeof(struct pollfd));
|
|
|
|
for (i = 0; i < maxfds; ++i) {
|
2022-04-20 16:56:53 +00:00
|
|
|
pfds[i].fd = i + 3;
|
|
|
|
pfds[i].events = POLLIN;
|
|
|
|
}
|
2022-06-21 14:22:39 +00:00
|
|
|
if (poll(pfds, maxfds, 0) != -1) {
|
|
|
|
for (i = 0; i < maxfds; ++i) {
|
2022-04-20 16:56:53 +00:00
|
|
|
if (pfds[i].revents & POLLNVAL) continue;
|
|
|
|
CHECK_EQ(0, close(pfds[i].fd));
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 14:22:39 +00:00
|
|
|
free(pfds);
|
2022-04-20 16:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SetLimit(int resource, uint64_t soft, uint64_t hard) {
|
|
|
|
struct rlimit old;
|
|
|
|
struct rlimit lim = {soft, hard};
|
|
|
|
if (resource == 127) return;
|
|
|
|
if (setrlimit(resource, &lim) == -1) {
|
|
|
|
if (!getrlimit(resource, &old)) {
|
|
|
|
lim.rlim_max = MIN(hard, old.rlim_max);
|
|
|
|
lim.rlim_cur = MIN(soft, lim.rlim_max);
|
|
|
|
setrlimit(resource, &lim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 01:53:52 +00:00
|
|
|
#pragma weak main
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Generic test program main function.
|
|
|
|
*/
|
2021-10-14 00:27:13 +00:00
|
|
|
noasan int main(int argc, char *argv[]) {
|
2022-04-20 16:56:53 +00:00
|
|
|
unsigned cpus;
|
2021-05-14 12:36:58 +00:00
|
|
|
const char *comdbg;
|
2021-03-01 07:42:35 +00:00
|
|
|
__log_level = kLogInfo;
|
2020-06-15 14:18:57 +00:00
|
|
|
GetOpts(argc, argv);
|
2022-04-28 16:42:36 +00:00
|
|
|
setenv("GDB", "", true);
|
2022-05-17 11:14:28 +00:00
|
|
|
GetSymbolTable();
|
2022-04-20 16:56:53 +00:00
|
|
|
|
|
|
|
// normalize this process
|
|
|
|
FixIrregularFds();
|
|
|
|
EmptySignalMask();
|
2021-08-18 21:21:30 +00:00
|
|
|
ShowCrashReports();
|
2022-04-20 16:56:53 +00:00
|
|
|
|
|
|
|
// now get down to business
|
2020-06-15 14:18:57 +00:00
|
|
|
g_testlib_shoulddebugbreak = IsDebuggerPresent(false);
|
2022-04-20 16:56:53 +00:00
|
|
|
if (!IsWindows()) sys_getpid(); // make strace easier to read
|
2020-08-25 11:23:25 +00:00
|
|
|
testlib_clearxmmregisters();
|
2020-06-15 14:18:57 +00:00
|
|
|
testlib_runalltests();
|
2022-09-13 06:10:38 +00:00
|
|
|
if (!g_testlib_failed && runbenchmarks_ &&
|
|
|
|
_weaken(testlib_runallbenchmarks)) {
|
|
|
|
_weaken(testlib_runallbenchmarks)();
|
2022-09-11 18:02:07 +00:00
|
|
|
if (IsAsan() && !g_testlib_failed) {
|
2022-03-18 09:33:37 +00:00
|
|
|
CheckForMemoryLeaks();
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
2021-10-08 15:11:51 +00:00
|
|
|
if (!g_testlib_failed && IsRunningUnderMake()) {
|
2022-04-20 16:56:53 +00:00
|
|
|
return 254; // compile.com considers this 0 and propagates output
|
2021-09-28 05:58:51 +00:00
|
|
|
}
|
2022-09-11 18:02:07 +00:00
|
|
|
} else if (IsAsan() && !g_testlib_failed) {
|
2022-03-18 09:33:37 +00:00
|
|
|
CheckForMemoryLeaks();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-20 16:56:53 +00:00
|
|
|
|
|
|
|
// we're done!
|
2021-10-15 02:36:49 +00:00
|
|
|
exit(min(255, g_testlib_failed));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|