Skip to content

Commit 320054e

Browse files
committed
WASI libc prototype implementation.
This incoporates pieces from musl-libc, cloudlibc, cloudabi, libpreopen, and dlmalloc, as well as a significant amount of new code.
1 parent 0e98504 commit 320054e

File tree

2,691 files changed

+131460
-173
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,691 files changed

+131460
-173
lines changed

LICENSE

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@ Please see the LICENSE file in each top-level directory for the terms applicable
22

33
The relevant directories and licenses are:
44

5-
basics/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
6-
dlmalloc/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
5+
basics/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
6+
dlmalloc/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
7+
libc-bottom-half/cloudlibc/ - BSD-2-Clause; see libc-bottom-half/cloudlibc/LICENSE for details
8+
libc-bottom-half/libpreopen/ - BSD-2-Clause; see the individual files for details
9+
libc-bottom-half/headers/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
10+
libc-bottom-half/sources/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
11+
libc-bottom-half/mman/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
12+
libc-top-half/musl - MIT; see libc-top-half/musl/COPYRIGHT for details
13+
libc-top-half/headers - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
14+
libc-top-half/sources - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication

Makefile

Lines changed: 427 additions & 30 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
# WebAssembly Reference Sysroot
1+
# WASI Sysroot
22

3-
Caution: This is currently quite experimental and not generally usable yet!
3+
This is a work in progress. It's usable for many purposes, though the APIs
4+
aren't stable yet.
45

56
## What is this?
67

7-
This is a "reference sysroot", which is meant to be part of a common C ABI
8-
that can be shared across C libraries and compilers. While it's intended to
9-
(eventually) be usable in its own right, we fully expect other
10-
implementations to be used in practice by many different systems, though
11-
we do hope that in those cases, this library defines a useful ABI that can
12-
be followed.
8+
It's several things.
9+
10+
First, it's a usable libc. It builds a "sysroot" which can be pointed to by
11+
compilers, such as Clang 8.0, using the wasm32-unknown-wasi target triple.
12+
It's a work in progress, but it is already sufficient to run basic programs.
13+
14+
Second, it's a "reference" implementation, which means the interfaces defined
15+
here can be used by other tools and libraries, even if they don't use all the
16+
actual implementations here. For example, we don't expect everyone will want
17+
to use the exact `malloc` implementation provided here, but tools and
18+
libraries using an ABI-compatible `malloc` interface will be able to
19+
interoperate regardless of which actual implementation is used.
20+
21+
Third, it's an example showing the use of the WASI API. The libc functionality
22+
is implemented using calls to WASI functions.
1323

1424
## Usage
1525

16-
Obtain a WebAssembly-supporting C compiler, and then run:
26+
The easiest way to get started with this is to use one of the
27+
[prepackaged releases](https://github.com/CraneStation/wasmtime-wasi/blob/wasi/docs/WASI-intro.md#how-can-i-write-programs-that-use-wasi).
28+
29+
## Building from source
30+
31+
To build a WASI sysroot from source, obtain a WebAssembly-supporting C compiler
32+
(currently this is only clang, though we'd like to support other compilers as well),
33+
and then run:
1734

1835
```
1936
make WASM_CC=/path/to/wasm/supporting/c/compiler
@@ -29,20 +46,3 @@ To use the sysroot, use the `--sysroot=` option:
2946
```
3047

3148
to run the compiler using the newly built sysroot.
32-
33-
## Why doesn't this contain a full libc implementation?
34-
35-
In the short term, one of the main goals is just to provide a reference
36-
point for people already maintaining their own libc codebases, to help
37-
reduce interface incompatibilities between the several different
38-
environments out there.
39-
40-
In the long term, there may some day be some form of standardized
41-
syscall/import layer for wasm which would could support a full
42-
"reference libc", at which point this repository might make sense as
43-
a place to host such a thing.
44-
45-
In between, if there are specific pieces of libc functionality which
46-
people would find useful to have here, and which don't depend on any
47-
syscalls, we could add them, using code from existing third-party
48-
codebases as appropriate.

basics/include/__errno.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __wasm_basics___errno_h
2+
#define __wasm_basics___errno_h
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#ifdef __cplusplus
9+
extern thread_local int errno;
10+
#else
11+
extern _Thread_local int errno;
12+
#endif
13+
14+
#define errno errno
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif

basics/include/__functions_malloc.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef __wasm___functions_malloc_h
2+
#define __wasm___functions_malloc_h
3+
4+
#define __need_size_t
5+
#define __need_wchar_t
6+
#define __need_NULL
7+
#include <stddef.h>
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
void *malloc(size_t size) __attribute__((__malloc__, __warn_unused_result__));
14+
void free(void *ptr);
15+
void *calloc(size_t nmemb, size_t size) __attribute__((__malloc__, __warn_unused_result__));
16+
void *realloc(void *ptr, size_t size) __attribute__((__warn_unused_result__));
17+
18+
#ifdef __cplusplus
19+
}
20+
#endif
21+
22+
#endif

basics/include/__functions_memcpy.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __wasm___functions_memcpy_h
2+
#define __wasm___functions_memcpy_h
3+
4+
#define __need_size_t
5+
#define __need_NULL
6+
#include <stddef.h>
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
void *memcpy(void *__restrict__ dst, const void *__restrict__ src, size_t n) __attribute__((__nothrow__, __leaf__, __nonnull__(1, 2)));
13+
void *memmove(void *dst, const void *src, size_t n) __attribute__((__nothrow__, __leaf__, __nonnull__(1, 2)));
14+
void *memset(void *dst, int c, size_t n) __attribute__((__nothrow__, __leaf__, __nonnull__(1)));
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif

basics/include/__macro_PAGESIZE.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __wasm_basics___macro_PAGESIZE_h
2+
#define __wasm_basics___macro_PAGESIZE_h
3+
4+
/*
5+
* The page size in WebAssembly is fixed at 64 KiB. If this ever changes,
6+
* it's expected that applications will need to opt in, so we can change
7+
* this.
8+
*/
9+
#define PAGESIZE (0x10000)
10+
11+
#endif

basics/include/__struct_stat.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef __wasm_sysroot___struct_stat_h
2-
#define __wasm_sysroot___struct_stat_h
1+
#ifndef __wasm_basics___struct_stat_h
2+
#define __wasm_basics___struct_stat_h
33

44
#include <__typedef_dev_t.h>
55
#include <__typedef_ino_t.h>
@@ -29,7 +29,7 @@ struct stat {
2929
struct timespec st_atim;
3030
struct timespec st_mtim;
3131
struct timespec st_ctim;
32-
long long __unused[3];
32+
long long __reserved[3];
3333
};
3434

3535
#endif

basics/include/__struct_timespec.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#ifndef __wasm_sysroot___struct_timespec_h
2-
#define __wasm_sysroot___struct_timespec_h
1+
#ifndef __wasm_basics___struct_timespec_h
2+
#define __wasm_basics___struct_timespec_h
33

4-
#include "__typedef_time_t.h"
4+
#include <__typedef_time_t.h>
55

66
/* As specified in POSIX. */
77
struct timespec {

basics/include/__typedef_blkcnt_t.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef __wasm_sysroot___typedef_blkcnt_t_h
2-
#define __wasm_sysroot___typedef_blkcnt_t_h
1+
#ifndef __wasm_basics___typedef_blkcnt_t_h
2+
#define __wasm_basics___typedef_blkcnt_t_h
33

44
/* Define these as 64-bit signed integers to support files larger than 2 GiB. */
55
typedef long long blkcnt_t;

0 commit comments

Comments
 (0)