cosmopolitan/test/libc/calls/poll_test.c

179 lines
6.6 KiB
C
Raw Normal View History

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
*/
#include "libc/calls/calls.h"
#include "libc/calls/pledge.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/log/libfatal.internal.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/nexgen32e/rdtscp.h"
#include "libc/nt/synchronization.h"
#include "libc/runtime/gc.internal.h"
2020-06-15 14:18:57 +00:00
#include "libc/sock/sock.h"
Prove that Makefile is fully defined The whole repository is now buildable with GNU Make Landlock sandboxing. This proves that no Makefile targets exist which touch files other than their declared prerequisites. In order to do this, we had to: 1. Stop code morphing GCC output in package.com and instead run a newly introduced FIXUPOBJ.COM command after GCC invocations. 2. Disable all the crumby Python unit tests that do things like create files in the current directory, or rename() files between folders. This ended up being a lot of tests, but most of them are still ok. 3. Introduce an .UNSANDBOXED variable to GNU Make to disable Landlock. We currently only do this for things like `make tags`. 4. This change deletes some GNU Make code that was preventing the execve() optimization from working. This means it should no longer be necessary in most cases for command invocations to be indirected through the cocmd interpreter. 5. Missing dependencies had to be declared in certain places, in cases where they couldn't be automatically determined by MKDEPS.COM 6. The libcxx header situation has finally been tamed. One of the things that makes this difficult is MKDEPS.COM only wants to consider the first 64kb of a file, in order to go fast. But libcxx likes to have #include lines buried after huge documentation. 7. An .UNVEIL variable has been introduced to GNU Make just in case we ever wish to explicitly specify additional things that need to be whitelisted which aren't strictly prerequisites. This works in a manner similar to the recently introduced .EXTRA_PREREQS feature. There's now a new build/bootstrap/make.com prebuilt binary available. It should no longer be possible to write invalid Makefile code.
2022-08-06 10:51:50 +00:00
#include "libc/sock/struct/pollfd.h"
#include "libc/sock/struct/sockaddr.h"
2021-08-26 04:35:58 +00:00
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/inaddr.h"
#include "libc/sysv/consts/ipproto.h"
Make numerous improvements - Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 05:58:51 +00:00
#include "libc/sysv/consts/poll.h"
#include "libc/sysv/consts/sig.h"
2021-08-26 04:35:58 +00:00
#include "libc/sysv/consts/sock.h"
2020-06-15 14:18:57 +00:00
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#include "third_party/chibicc/test/test.h"
2020-06-15 14:18:57 +00:00
#include "tool/decode/lib/flagger.h"
#include "tool/decode/lib/pollnames.h"
bool gotsig;
2022-07-25 05:34:13 +00:00
void SetUpOnce(void) {
__pledge_mode = PLEDGE_PENALTY_KILL_PROCESS | PLEDGE_STDERR_LOGGING;
ASSERT_SYS(0, 0, pledge("stdio proc inet", 0));
2022-06-27 20:01:58 +00:00
}
void SetUp(void) {
gotsig = false;
}
void OnSig(int sig) {
gotsig = true;
}
dontdiscard char *FormatPollFd(struct pollfd p[2]) {
2021-08-26 04:35:58 +00:00
return xasprintf("fd:%d revents:%s\n"
"fd:%d revents:%s\n",
p[0].fd, gc(RecreateFlags(kPollNames, p[0].revents)),
p[1].fd, gc(RecreateFlags(kPollNames, p[1].revents)));
2020-06-15 14:18:57 +00:00
}
TEST(poll, allZero_doesNothingPrettyMuch) {
EXPECT_SYS(0, 0, poll(0, 0, 0));
}
TEST(ppoll, weCanProveItChecksForSignals) {
if (IsXnu()) return;
if (IsNetbsd()) return;
int pipefds[2];
sigset_t set, old;
struct sigaction oldss;
struct sigaction sa = {.sa_handler = OnSig};
EXPECT_SYS(0, 0, pipe(pipefds));
struct pollfd fds[] = {{pipefds[0], POLLIN}};
ASSERT_SYS(0, 0, sigaction(SIGUSR1, &sa, &oldss));
ASSERT_SYS(0, 0, sigfillset(&set));
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &set, &old));
EXPECT_SYS(0, 0, kill(getpid(), SIGUSR1));
EXPECT_FALSE(gotsig);
EXPECT_SYS(EINTR, -1, ppoll(fds, 1, 0, &old));
EXPECT_TRUE(gotsig);
EXPECT_SYS(0, 0, sigprocmask(SIG_SETMASK, &old, 0));
EXPECT_SYS(0, 0, sigaction(SIGUSR1, &oldss, 0));
EXPECT_SYS(0, 0, close(pipefds[0]));
EXPECT_SYS(0, 0, close(pipefds[1]));
}
2021-08-26 04:35:58 +00:00
TEST(poll, testNegativeOneFd_isIgnored) {
ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
struct sockaddr_in addr = {AF_INET, 0, {htonl(INADDR_LOOPBACK)}};
ASSERT_SYS(0, 0, bind(3, &addr, sizeof(addr)));
ASSERT_SYS(0, 0, listen(3, 10));
struct pollfd fds[] = {{-1}, {3}};
EXPECT_SYS(0, 0, poll(fds, ARRAYLEN(fds), 1));
EXPECT_STREQ("fd:-1 revents:0\n"
"fd:3 revents:0\n",
gc(FormatPollFd(&fds[0])));
ASSERT_SYS(0, 0, close(3));
2020-06-15 14:18:57 +00:00
}
TEST(poll, pipe_noInput) {
// we can't test stdin here since
// we can't assume it isn't /dev/null
// since nil is always pollin as eof
int pipefds[2];
EXPECT_SYS(0, 0, pipe(pipefds));
struct pollfd fds[] = {{pipefds[0], POLLIN}};
EXPECT_SYS(0, 0, poll(fds, 1, 0));
EXPECT_EQ(0, fds[0].revents);
EXPECT_SYS(0, 0, close(pipefds[0]));
EXPECT_SYS(0, 0, close(pipefds[1]));
}
TEST(poll, pipe_hasInputFromSameProcess) {
char buf[2];
int pipefds[2];
EXPECT_SYS(0, 0, pipe(pipefds));
struct pollfd fds[] = {{pipefds[0], POLLIN}};
EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2));
EXPECT_SYS(0, 1, poll(fds, 1, 1000)); // flake nt!
EXPECT_EQ(POLLIN, fds[0].revents);
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
EXPECT_SYS(0, 0, poll(fds, 1, 0));
EXPECT_SYS(0, 0, close(pipefds[0]));
EXPECT_SYS(0, 0, close(pipefds[1]));
}
TEST(poll, pipe_hasInput) {
char buf[2];
sigset_t chldmask, savemask;
int ws, pid, sync[2], pipefds[2];
EXPECT_EQ(0, sigemptyset(&chldmask));
EXPECT_EQ(0, sigaddset(&chldmask, SIGCHLD));
EXPECT_EQ(0, sigprocmask(SIG_BLOCK, &chldmask, &savemask));
EXPECT_SYS(0, 0, pipe(pipefds));
EXPECT_NE(-1, (pid = fork()));
if (!pid) {
EXPECT_SYS(0, 0, close(pipefds[0]));
EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2));
EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2));
EXPECT_SYS(0, 0, close(pipefds[1]));
_Exit(0);
}
EXPECT_SYS(0, 0, close(pipefds[1]));
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
struct pollfd fds[] = {{pipefds[0], POLLIN}};
EXPECT_SYS(0, 1, poll(fds, 1, -1));
EXPECT_EQ(POLLIN, fds[0].revents & POLLIN);
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
EXPECT_SYS(0, 0, close(pipefds[0]));
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(0, WEXITSTATUS(ws));
EXPECT_EQ(0, sigprocmask(SIG_SETMASK, &savemask, 0));
}
#if 0
TEST(poll, emptyFds_becomesSleep) {
// timing tests w/o mocks are always the hardest
int64_t a, b, c, p, i = 0;
do {
if (++i == 5) {
kprintf("too much cpu churn%n");
return;
}
p = TSC_AUX_CORE(rdpid());
a = rdtsc();
EXPECT_SYS(0, 0, poll(0, 0, 5));
b = rdtsc();
EXPECT_SYS(0, 0, poll(0, 0, 50));
c = rdtsc();
} while (TSC_AUX_CORE(rdpid()) != p);
EXPECT_LT((b - a) * 2, c - b);
}
#endif