Skip to content

Commit

Permalink
Workaround sendfile() bug in WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 2, 2022
1 parent fc96af0 commit f44d887
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
49 changes: 49 additions & 0 deletions libc/intrin/__is_wsl.c
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions libc/runtime/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions test/libc/calls/fcntl_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
6 changes: 5 additions & 1 deletion test/libc/sock/sendfile_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -125,7 +126,10 @@ TEST(sendfile, testPositioning) {
ASSERT_EQ(-1, sendfile(4, 5, 0, 6));
ASSERT_TRUE(errno == EINVAL || errno == EPIPE);
errno = 0;
ASSERT_EQ(12, GetFileOffset(5));
// XXX: WSL clobbers file offset on failure!
if (!__is_wsl()) {
ASSERT_EQ(12, GetFileOffset(5));
}
_Exit(0);
}
ASSERT_SYS(0, 0, close(3));
Expand Down

0 comments on commit f44d887

Please sign in to comment.