Skip to content

Commit

Permalink
Write more tests attempting to break windows
Browse files Browse the repository at this point in the history
This time I haven't succeeded in breaking anything which is a good sign.
  • Loading branch information
jart committed Sep 22, 2024
1 parent 4769267 commit 126a44d
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 37 deletions.
6 changes: 1 addition & 5 deletions libc/calls/readwrite-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/calls/createfileflags.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
Expand Down Expand Up @@ -170,14 +169,11 @@ sys_readwrite_nt(int fd, void *data, size_t size, ssize_t offset,
}

// the i/o operation was successfully canceled
if (got_eagain) {
unassert(!got_sig);
if (got_eagain)
return eagain();
}

// it's now reasonable to report semaphore creation error
if (other_error) {
unassert(!got_sig);
errno = __dos2errno(other_error);
return -1;
}
Expand Down
8 changes: 4 additions & 4 deletions libc/calls/sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ static textwindows int __sig_killer(struct PosixThread *pt, int sig, int sic) {
return 0;
}

// we can't preempt threads that masked sig or are blocked. we aso
// need to ensure we don't the target thread's stack if many signals
// need to be delivered at once. we also need to make sure two threads
// can't deadlock by killing each other at the same time.
// we can't preempt threads that masked sigs or are blocked. we also
// need to ensure we don't overflow the target thread's stack if many
// signals need to be delivered at once. we also need to make sure two
// threads can't deadlock by killing each other at the same time.
if ((atomic_load_explicit(&pt->tib->tib_sigmask, memory_order_acquire) &
(1ull << (sig - 1))) ||
atomic_exchange_explicit(&pt->pt_intoff, 1, memory_order_acquire)) {
Expand Down
107 changes: 107 additions & 0 deletions test/posix/pipe_write_eagain_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2024 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 <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/**
* @fileoverview Tests that EAGAIN won't corrupt pipe.
*
* This is a real bug when using CancelIoEx() on winsock writes, so we
* need to make sure it doesn't happen on pipes too.
*/

#define ITERATIONS 100000
#define ASYMMETRY 3

int fds[2];
int got_read_eagains;
int got_write_eagains;

void *worker(void *arg) {
for (int expect = 0; expect < ITERATIONS;) {
int number;
ssize_t rc = read(fds[0], &number, sizeof(number));
if (rc == -1) {
if (errno == EAGAIN) {
++got_read_eagains;
if (poll(&(struct pollfd){fds[0], POLLIN}, 1, -1) == -1)
exit(11);
continue;
}
perror("read");
exit(8);
}
size_t got = rc;
if (got != sizeof(int))
exit(9);
if (expect != number)
exit(10);
++expect;
}
return 0;
}

int main(int argc, char *argv[]) {

if (pipe2(fds, O_NONBLOCK))
return 1;

pthread_t th;
if (pthread_create(&th, 0, worker, 0))
return 2;

int number = 0;
for (;;) {
int chunk = 0;
int numbers[ASYMMETRY];
for (;;) {
numbers[chunk] = number + chunk;
if (++chunk == ASYMMETRY)
break;
if (number + chunk == ITERATIONS)
break;
}
for (;;) {
ssize_t rc = write(fds[1], numbers, chunk * sizeof(int));
if (rc == -1) {
if (errno == EAGAIN) {
++got_write_eagains;
if (poll(&(struct pollfd){fds[1], POLLOUT}, 1, -1) == -1)
return 10;
continue;
}
return 3;
}
if (rc % sizeof(int))
return 4;
chunk = rc / sizeof(int);
number += chunk;
break;
}
if (number == ITERATIONS)
break;
}

if (pthread_join(th, 0))
return 5;

if (!got_read_eagains && !got_write_eagains)
return 7;
}
96 changes: 70 additions & 26 deletions test/posix/sa_resethand_test.c
Original file line number Diff line number Diff line change
@@ -1,46 +1,90 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2023 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. │
╚─────────────────────────────────────────────────────────────────────────────*/
// Copyright 2024 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 <errno.h>
#include <signal.h>
#include <stdlib.h>

volatile int gotsig;

void OnSig(int sig) {
gotsig = sig;
}

int main() {
void test_sa_resethand_raise(void) {
struct sigaction sa;
sa.sa_handler = OnSig;
sa.sa_flags = SA_RESETHAND;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGUSR1, &sa, 0))
return 1;
exit(1);
if (sigaction(SIGUSR1, 0, &sa))
return 2;
exit(2);
if (sa.sa_handler != OnSig)
return 3;
exit(3);
if (raise(SIGUSR1))
return 4;
exit(4);
if (gotsig != SIGUSR1)
return 5;
exit(5);
if (sigaction(SIGUSR1, 0, &sa))
return 6;
exit(6);
if (sa.sa_handler != SIG_DFL)
exit(7);
}

void test_sa_resethand_pause(void) {
struct sigaction sa;
sa.sa_handler = OnSig;
sa.sa_flags = SA_RESETHAND;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGALRM, &sa, 0))
exit(10);
ualarm(10000, 0);
if (pause() != -1 || errno != EINTR)
exit(11);
if (gotsig != SIGALRM)
exit(12);
if (sigaction(SIGALRM, 0, &sa))
exit(13);
if (sa.sa_handler != SIG_DFL)
return 7;
exit(14);
}

void test_sa_resethand_read(void) {
struct sigaction sa;
sa.sa_handler = OnSig;
sa.sa_flags = SA_RESETHAND;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGALRM, &sa, 0))
exit(20);
int fds[2];
if (pipe(fds))
exit(21);
ualarm(10000, 0);
if (read(fds[0], (char[]){0}, 1) != -1 || errno != EINTR)
exit(22);
if (gotsig != SIGALRM)
exit(23);
if (sigaction(SIGALRM, 0, &sa))
exit(24);
if (sa.sa_handler != SIG_DFL)
exit(25);
}

int main() {
test_sa_resethand_raise();
test_sa_resethand_pause();
test_sa_resethand_read();
}
Loading

0 comments on commit 126a44d

Please sign in to comment.