Workaround sendfile() bug in WSL

This commit is contained in:
Justine Tunney 2022-11-02 01:38:42 -07:00
parent fc96af058b
commit f44d88707e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
4 changed files with 59 additions and 1 deletions

49
libc/intrin/__is_wsl.c Normal file
View file

@ -0,0 +1,49 @@
/*-*- 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/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/runtime/internal.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#define MAP_GROWSDOWN_linux 0x00000100
#define MAP_ANONYMOUS_linux 0x00000020
/**
* Returns true if host platform is WSL.
*/
bool __is_wsl(void) {
int e;
void *p;
bool res;
if (!IsLinux()) return false;
e = errno;
p = __sys_mmap(0, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS_linux | MAP_GROWSDOWN_linux, -1, 0,
0);
if (p != MAP_FAILED) {
__sys_munmap(p, 4096);
return false;
}
res = errno == ENOTSUP;
errno = e;
return res;
}

View file

@ -29,6 +29,7 @@ extern unsigned char _tls_size[];
extern unsigned char _tls_content[];
void _init(void) hidden;
bool __is_wsl(void);
void __morph_tls(void);
void __enable_tls(void);
void __enable_threads(void) hidden;

View file

@ -87,6 +87,10 @@ TEST(fcntl, getfd) {
ASSERT_SYS(0, 0, fcntl(3, F_GETFD));
ASSERT_SYS(0, 4, open("/dev/null", O_RDWR | O_CLOEXEC));
ASSERT_SYS(0, FD_CLOEXEC, fcntl(4, F_GETFD));
ASSERT_SYS(0, 0, fcntl(4, F_SETFD, FD_CLOEXEC));
ASSERT_SYS(0, FD_CLOEXEC, fcntl(4, F_GETFD));
ASSERT_SYS(0, 0, fcntl(4, F_SETFD, 0));
ASSERT_SYS(0, 0, fcntl(4, F_GETFD));
ASSERT_SYS(0, 0, close(4));
ASSERT_SYS(0, 0, close(3));
}

View file

@ -24,6 +24,7 @@
#include "libc/limits.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/sock/struct/sockaddr.h"
@ -125,7 +126,10 @@ TEST(sendfile, testPositioning) {
ASSERT_EQ(-1, sendfile(4, 5, 0, 6));
ASSERT_TRUE(errno == EINVAL || errno == EPIPE);
errno = 0;
// XXX: WSL clobbers file offset on failure!
if (!__is_wsl()) {
ASSERT_EQ(12, GetFileOffset(5));
}
_Exit(0);
}
ASSERT_SYS(0, 0, close(3));