Make some small fixes to recent changes

This commit is contained in:
Justine Tunney 2022-11-03 05:45:33 -07:00
parent c2590cf7a0
commit 179e048bba
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
5 changed files with 33 additions and 11 deletions

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/assert.h"
#include "libc/intrin/atomic.h" #include "libc/intrin/atomic.h"
#include "libc/intrin/kmalloc.h" #include "libc/intrin/kmalloc.h"
#include "libc/stdio/internal.h" #include "libc/stdio/internal.h"
@ -27,13 +26,13 @@ static _Atomic(FILE *) __stdio_freelist;
FILE *__stdio_alloc(void) { FILE *__stdio_alloc(void) {
FILE *f; FILE *f;
f = atomic_load_explicit(&__stdio_freelist, memory_order_relaxed); f = atomic_load_explicit(&__stdio_freelist, memory_order_acquire);
while (f) { while (f) {
if (atomic_compare_exchange_weak_explicit( if (atomic_compare_exchange_weak_explicit(
&__stdio_freelist, &f, &__stdio_freelist, &f,
atomic_load_explicit(&f->next, memory_order_relaxed), atomic_load_explicit(&f->next, memory_order_acquire),
memory_order_relaxed, memory_order_relaxed)) { memory_order_release, memory_order_relaxed)) {
atomic_store_explicit(&f->next, 0, memory_order_relaxed); atomic_store_explicit(&f->next, 0, memory_order_release);
break; break;
} }
} }
@ -48,13 +47,12 @@ FILE *__stdio_alloc(void) {
void __stdio_free(FILE *f) { void __stdio_free(FILE *f) {
FILE *g; FILE *g;
_unassert(!atomic_load_explicit(&f->next, memory_order_relaxed));
bzero(f, sizeof(*f)); bzero(f, sizeof(*f));
g = atomic_load_explicit(&__stdio_freelist, memory_order_relaxed); g = atomic_load_explicit(&__stdio_freelist, memory_order_acquire);
for (;;) { for (;;) {
atomic_store_explicit(&f->next, g, memory_order_relaxed); atomic_store_explicit(&f->next, g, memory_order_release);
if (atomic_compare_exchange_weak_explicit(&__stdio_freelist, &g, f, if (atomic_compare_exchange_weak_explicit(&__stdio_freelist, &g, f,
memory_order_relaxed, memory_order_release,
memory_order_relaxed)) { memory_order_relaxed)) {
break; break;
} }

View file

@ -18,7 +18,6 @@
╚────────────────────────────────────────────────────────────────'>/dev/null #*/ ╚────────────────────────────────────────────────────────────────'>/dev/null #*/
dir=libc/sysv/calls dir=libc/sysv/calls
. libc/sysv/gen.sh . libc/sysv/gen.sh
#include "libc/calls/syscall-sysv.internal.h"
# The Fifth Bell System Interface, Community Edition ┌─────────────────────────┐ # The Fifth Bell System Interface, Community Edition ┌─────────────────────────┐
# » so many numbers │ legend │ # » so many numbers │ legend │

View file

@ -32,7 +32,7 @@
*/ */
void _wait0(const atomic_int *ctid) { void _wait0(const atomic_int *ctid) {
int x; int x;
while ((x = atomic_load_explicit(ctid, memory_order_relaxed))) { while ((x = atomic_load_explicit(ctid, memory_order_acquire))) {
nsync_futex_wait_(ctid, x, !IsWindows(), 0); nsync_futex_wait_(ctid, x, !IsWindows(), 0);
} }
} }

View file

@ -18,11 +18,14 @@
*/ */
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/errno.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/sysv/consts/mfd.h"
#include "libc/sysv/consts/o.h" #include "libc/sysv/consts/o.h"
#include "libc/testlib/subprocess.h" #include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h" #include "libc/testlib/testlib.h"
// clang-format off
int fds[2]; int fds[2];
char buf[8]; char buf[8];
@ -64,3 +67,22 @@ TEST(fexecve, elfIsUnreadable_mayBeExecuted) {
ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 0, close(3));
ASSERT_STREQ("hi\n", buf); ASSERT_STREQ("hi\n", buf);
} }
TEST(fexecve, memfd_create) {
if (!IsLinux()) return;
SPAWN(vfork);
#define TINY_ELF_PROGRAM "\
\177\105\114\106\002\001\001\000\000\000\000\000\000\000\000\000\
\002\000\076\000\001\000\000\000\170\000\100\000\000\000\000\000\
\100\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\100\000\070\000\001\000\000\000\000\000\000\000\
\001\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000\
\000\000\100\000\000\000\000\000\000\000\100\000\000\000\000\000\
\200\000\000\000\000\000\000\000\200\000\000\000\000\000\000\000\
\000\020\000\000\000\000\000\000\152\052\137\152\074\130\017\005"
int fd = memfd_create("foo", MFD_CLOEXEC);
if (fd == -1 && errno == ENOSYS) _Exit(42);
write(fd, TINY_ELF_PROGRAM, sizeof(TINY_ELF_PROGRAM) - 1);
fexecve(fd, (char *const[]){0}, (char *const[]){0});
EXITS(42);
}

View file

@ -126,6 +126,9 @@ o/$(MODE)/test/libc/calls/lock_ofd_test.com.runs: \
o/$(MODE)/test/libc/calls/openbsd_test.com.runs: \ o/$(MODE)/test/libc/calls/openbsd_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc unveil private .PLEDGE = stdio rpath wpath cpath fattr proc unveil
o/$(MODE)/test/libc/calls/fexecve_test.com.runs: \
private .UNSANDBOXED = 1 # for memfd_create()
o/$(MODE)/test/libc/calls/read_test.com.runs: \ o/$(MODE)/test/libc/calls/read_test.com.runs: \
private .UNVEIL += /dev/zero private .UNVEIL += /dev/zero