Skip to content

Commit fc24e7f

Browse files
committed
Get tests running on wasm32-wasi
We need to use a fork of splitmix here. We /should/ be able to still point to the original repo, but Cabal doesn't run a "git fetch" to download the ref, so you can't point to a commit not on a branch. This is fixed in latest, but that won't be released for a while.
1 parent 8416bec commit fc24e7f

File tree

6 files changed

+50
-12
lines changed

6 files changed

+50
-12
lines changed

cabal.project.wasm32-wasi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
packages: unix-compat.cabal
2+
3+
-- See https://github.com/haskellari/splitmix/pull/73
4+
source-repository-package
5+
type: git
6+
location: https://github.com/amesgen/splitmix.git
7+
tag: cea9e31bdd849eb0c17611bb99e33d590e126164

cbits/mktemp.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@
4141
#include <stdlib.h>
4242
#include <string.h>
4343
#include <unistd.h>
44+
45+
#if defined(__wasm__)
46+
#include <sys/random.h>
47+
#else
4448
#include <windows.h>
4549
#include <wincrypt.h>
4650

47-
static int random(uint32_t *);
51+
#define open _open
52+
#define stat _stat
53+
#endif
54+
55+
static int unixcompat_random(uint32_t *);
4856
static int _gettemp(char *, int *);
4957

50-
static const unsigned char padchar[] =
58+
static const char padchar[] =
5159
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
5260

5361
int unixcompat_mkstemp(char *path)
@@ -64,7 +72,7 @@ static int _gettemp(char *path, int *doopen)
6472
{
6573
char *start, *trv, *suffp, *carryp;
6674
char *pad;
67-
struct _stat sbuf;
75+
struct stat sbuf;
6876
int rval;
6977
uint32_t randidx, randval;
7078
char carrybuf[MAXPATHLEN];
@@ -84,7 +92,7 @@ static int _gettemp(char *path, int *doopen)
8492

8593
/* Fill space with random characters */
8694
while (trv >= path && *trv == 'X') {
87-
if (!random(&randval)) {
95+
if (!unixcompat_random(&randval)) {
8896
/* this should never happen */
8997
errno = EIO;
9098
return 0;
@@ -104,7 +112,7 @@ static int _gettemp(char *path, int *doopen)
104112
for (; trv > path; --trv) {
105113
if (*trv == '/') {
106114
*trv = '\0';
107-
rval = _stat(path, &sbuf);
115+
rval = stat(path, &sbuf);
108116
*trv = '/';
109117
if (rval != 0)
110118
return (0);
@@ -120,11 +128,11 @@ static int _gettemp(char *path, int *doopen)
120128
for (;;) {
121129
if (doopen) {
122130
if ((*doopen =
123-
_open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
131+
open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
124132
return (1);
125133
if (errno != EEXIST)
126134
return (0);
127-
} else if (_stat(path, &sbuf))
135+
} else if (stat(path, &sbuf))
128136
return (errno == ENOENT);
129137

130138
/* If we have a collision, cycle through the space of filenames */
@@ -154,7 +162,14 @@ static int _gettemp(char *path, int *doopen)
154162
/*NOTREACHED*/
155163
}
156164

157-
static int random(uint32_t *value)
165+
#if defined(__wasm__)
166+
static int unixcompat_random(uint32_t *value)
167+
{
168+
int r = getentropy(value, sizeof(uint32_t));
169+
return r == 0 ? 1 : 0;
170+
}
171+
#else
172+
static int unixcompat_random(uint32_t *value)
158173
{
159174
/* This handle is never released. Windows will clean up when the process
160175
* exits. Python takes this approach when emulating /dev/urandom, and if
@@ -171,3 +186,4 @@ static int random(uint32_t *value)
171186

172187
return 1;
173188
}
189+
#endif

src/System/PosixCompat/Process.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ module System.PosixCompat.Process (
88
getProcessID
99
) where
1010

11-
#ifdef mingw32_HOST_OS
11+
#if defined(mingw32_HOST_OS)
1212

1313
import System.Posix.Types (ProcessID)
1414
import System.Win32.Process (getCurrentProcessId)
1515

1616
getProcessID :: IO ProcessID
1717
getProcessID = fromIntegral <$> getCurrentProcessId
1818

19+
#elif defined(wasm32_HOST_ARCH)
20+
21+
import System.Posix.Types (ProcessID)
22+
23+
getProcessID :: IO ProcessID
24+
getProcessID = pure 1
25+
1926
#else
2027

2128
import System.Posix.Process

src/System/PosixCompat/Temp.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ module System.PosixCompat.Temp (
1111
mkstemp
1212
) where
1313

14-
#ifndef mingw32_HOST_OS
14+
#if !(defined(mingw32_HOST_OS) || defined(wasm32_HOST_ARCH))
1515
-- Re-export unix package
1616

1717
import System.Posix.Temp
1818

1919
#elif defined(__GLASGOW_HASKELL__)
20-
-- Windows w/ GHC, we have fdToHandle so we
20+
-- Window/WASM w/ GHC, we have fdToHandle so we
2121
-- can use our own implementation of mkstemp.
2222

2323
import System.IO (Handle)

tests/run-wasmtime.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env sh
2+
3+
TMP="$(mktemp -d --suffix=-unix-compat)"
4+
trap 'rm -rf -- "$TMP"' EXIT
5+
wasmtime --dir "$TMP::/" "$@"
6+
exit "$?"

unix-compat.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ Library
6969

7070
else
7171
build-depends: unix >= 2.7.2.0 && < 2.9
72-
if !arch(wasm32)
72+
if arch(wasm32)
73+
c-sources: cbits/mktemp.c
74+
else
7375
include-dirs: include
7476
includes: HsUnixCompat.h
7577
install-includes: HsUnixCompat.h

0 commit comments

Comments
 (0)