Make improvements

- Clean up sigaction() code
- Add a port scanner example
- Introduce a ParseCidr() API
- Clean up our futex abstraction code
- Fix a harmless integer overflow in ParseIp()
- Use kernel semaphores on NetBSD to make threads much faster
This commit is contained in:
Justine Tunney 2022-11-07 02:22:09 -08:00
parent 539bddce8c
commit c995838e5c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
107 changed files with 1085 additions and 492 deletions

View file

@ -228,11 +228,13 @@ TEST(uc_sigmask, signalHandlerCanChangeSignalMaskOfTrappedThread) {
struct sigaction oldsa;
struct sigaction sa = {.sa_sigaction = OnSigMask, .sa_flags = SA_SIGINFO};
sigemptyset(&want);
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &want, 0));
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &want, &got));
ASSERT_FALSE(sigismember(&got, SIGUSR1));
ASSERT_SYS(0, 0, sigaction(SIGUSR1, &sa, &oldsa));
ASSERT_SYS(0, 0, raise(SIGUSR1));
ASSERT_TRUE(gotusr1);
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, 0, &got));
ASSERT_TRUE(sigismember(&got, SIGUSR1));
sigaddset(&want, SIGUSR1);
ASSERT_STREQ(DescribeSigset(0, &want), DescribeSigset(0, &got));
ASSERT_SYS(0, 0, sigaction(SIGUSR1, &oldsa, 0));

View file

@ -34,6 +34,7 @@ TEST_LIBC_INTRIN_DIRECTDEPS = \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_THREAD \
LIBC_TESTLIB \
LIBC_TINYMATH \

View file

@ -22,7 +22,9 @@
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/limits.h"
#include "libc/log/log.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
@ -39,6 +41,23 @@ FILE *f;
char buf[32];
char testlib_enable_tmp_setup_teardown;
void CheckForFdLeaks(void) {
int rc, i, l = 0, e = errno;
for (i = 3; i < 16; ++i) {
rc = fcntl(i, F_GETFL);
if (rc == -1) {
ASSERT_EQ(EBADF, errno);
errno = e;
} else {
kprintf("file descriptor %d leaked!\n", i);
++l;
}
}
if (l) {
__die();
}
}
TEST(popen, command) {
char foo[6];
testlib_extract("/zip/echo.com", "echo.com", 0755);
@ -46,6 +65,7 @@ TEST(popen, command) {
ASSERT_NE(NULL, fgets(foo, sizeof(foo), f));
ASSERT_STREQ("hello", foo);
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, semicolon) {
@ -53,6 +73,7 @@ TEST(popen, semicolon) {
ASSERT_STREQ("hello\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("there\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, singleQuotes) {
@ -61,6 +82,7 @@ TEST(popen, singleQuotes) {
ASSERT_STREQ("hello $there\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("yo\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, doubleQuotes) {
@ -68,6 +90,7 @@ TEST(popen, doubleQuotes) {
ASSERT_NE(NULL, (f = popen("echo -l \"$hello there\"", "r")));
ASSERT_STREQ("a b c there\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, quoteless) {
@ -77,6 +100,7 @@ TEST(popen, quoteless) {
ASSERT_STREQ("aa b c\n", fgets(buf, sizeof(buf), f)); // mixed feelings
ASSERT_STREQ("yo\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, pipe) {
@ -84,6 +108,7 @@ TEST(popen, pipe) {
ASSERT_NE(NULL, (f = popen("echo hello | toupper", "r")));
ASSERT_STREQ("HELLO\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
sig_atomic_t gotsig;
@ -102,6 +127,7 @@ TEST(popen, complicated) {
ASSERT_EQ(0, pclose(f));
ASSERT_EQ(1, gotsig);
signal(SIGUSR1, SIG_DFL);
CheckForFdLeaks();
}
void *Worker(void *arg) {
@ -129,10 +155,10 @@ void *Worker(void *arg) {
}
TEST(popen, torture) {
int i, n = 8;
int i, n = 4;
pthread_t *t = _gc(malloc(sizeof(pthread_t) * n));
testlib_extract("/zip/echo.com", "echo.com", 0755);
for (i = 0; i < n; ++i) ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
for (i = 0; i < n; ++i) ASSERT_EQ(0, pthread_join(t[i], 0));
for (i = 3; i < 16; ++i) ASSERT_SYS(EBADF, -1, fcntl(i, F_GETFL));
CheckForFdLeaks();
}

View file

@ -170,12 +170,6 @@ TEST(sem_timedwait, threads) {
TEST(sem_timedwait, processes) {
int i, r, rc, n = 4, pshared = 1;
sem_t *sm = _mapshared(FRAMESIZE), *s[2] = {sm, sm + 1};
if (IsOpenbsd()) {
// TODO(jart): why?
ASSERT_SYS(EPERM, -1, sem_init(s[0], pshared, 0));
ASSERT_SYS(0, 0, munmap(sm, FRAMESIZE));
return;
}
ASSERT_SYS(0, 0, sem_init(s[0], pshared, 0));
ASSERT_SYS(0, 0, sem_init(s[1], pshared, 0));
for (i = 0; i < n; ++i) {

View file

@ -0,0 +1,59 @@
/*-*- 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 2022 Justine Alexandra Roberts Tunney
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.
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.
*/
#include "libc/testlib/testlib.h"
#include "net/http/ip.h"
TEST(ParseCidr, test) {
struct Cidr in;
in = ParseCidr("10.10.10.1/24", -1);
EXPECT_EQ(0x0a0a0a01, in.addr);
EXPECT_EQ(24, in.cidr);
in = ParseCidr("168430081/1", -1);
EXPECT_EQ(0x0a0a0a01, in.addr);
EXPECT_EQ(1, in.cidr);
}
TEST(ParseCidr, noCidr_defaultsTo32) {
struct Cidr in;
in = ParseCidr("10.10.10.255", -1);
EXPECT_EQ(0x0a0a0aff, in.addr);
EXPECT_EQ(32, in.cidr);
}
TEST(ParseCidr, badIp_returnsNeg1) {
struct Cidr in;
in = ParseCidr("10.10.10.a", -1);
EXPECT_EQ(-1, in.addr);
in = ParseCidr("10.10.10.256", -1);
EXPECT_EQ(-1, in.addr);
in = ParseCidr("10.10.10.256/24", -1);
EXPECT_EQ(-1, in.addr);
}
TEST(ParseCidr, badCidr_returnsNeg1) {
struct Cidr in;
in = ParseCidr("10.10.10.1/", -1);
EXPECT_EQ(-1, in.addr);
in = ParseCidr("10.10.10.1/a", -1);
EXPECT_EQ(-1, in.addr);
in = ParseCidr("10.10.10.1/0", -1);
EXPECT_EQ(-1, in.addr);
in = ParseCidr("10.10.10.1/33", -1);
EXPECT_EQ(-1, in.addr);
}

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/testlib/testlib.h"
#include "net/http/http.h"
#include "net/http/ip.h"
TEST(ParseIp, test) {
EXPECT_EQ(-1, ParseIp("", -1));
@ -30,6 +30,8 @@ TEST(ParseIp, test) {
EXPECT_EQ(0x00000000, ParseIp("...", -1)); /* meh */
EXPECT_EQ(0x80000304, ParseIp("128.0.3.4", -1));
EXPECT_EQ(0x80000304, ParseIp("128..3.4", -1));
EXPECT_EQ(-1, ParseIp("4294967296", -1));
EXPECT_EQ(-1, ParseIp("255.255.255.256", -1));
EXPECT_EQ(-1, ParseIp("256.255.255.255", -1));
EXPECT_EQ(-1, ParseIp("hello", -1));
EXPECT_EQ(-1, ParseIp("hello\177", -1));

View file

@ -26,6 +26,7 @@
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
#include "net/http/http.h"
#include "net/http/ip.h"
#include "net/http/url.h"
TEST(ParseUrl, testEmpty) {