Skip to content

Commit ca9046d

Browse files
authored
Use consistent style for wasi-libc C source files. (WebAssembly#131)
For now, this means using `//`-style comments in .c source files (though not public header files), and spaces rather than tabs. No strong opinion here; this is just what the majority of the current code is using. This also synchronizes basics/crt/crt1.c with libc-bottom-half's version, though this is just a cleanup as the former isn't currently used by the main wasi-libc build.
1 parent 951cc3e commit ca9046d

File tree

6 files changed

+136
-134
lines changed

6 files changed

+136
-134
lines changed

basics/crt/crt1.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
extern void __wasm_call_ctors(void);
2-
extern int main(int, char *[]);
2+
extern int __original_main(void);
33
extern void __prepare_for_exit(void);
44
void _Exit(int) __attribute__((noreturn));
55

66
void _start(void) {
7-
/* The linker synthesizes this to call constructors. */
7+
// The linker synthesizes this to call constructors.
88
__wasm_call_ctors();
99

10-
/* Call main with no arguments. */
11-
int r = main(0, 0);
10+
// Call `__original_main` which will either be the application's
11+
// zero-argument `main` function (renamed by the compiler) or a libc
12+
// routine which populates `argv` and `argc` and calls the application's
13+
// two-argument `main`.
14+
int r = __original_main();
1215

13-
/* Call atexit functions, destructors, stdio cleanup, etc. */
16+
// Call atexit functions, destructors, stdio cleanup, etc.
1417
__prepare_for_exit();
1518

16-
/* If main exited successfully, just return, otherwise call _Exit. */
19+
// If main exited successfully, just return, otherwise call _Exit.
1720
if (r != 0) {
1821
_Exit(r);
1922
}

libc-bottom-half/mman/mman.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
/*
2-
* Userspace emulation of mmap and munmap. Restrictions apply.
3-
*
4-
* This is meant to be complete enough to be compatible with code that uses
5-
* mmap for simple file I/O. It just allocates memory with malloc and reads
6-
* and writes data with pread and pwrite.
7-
*/
1+
// Userspace emulation of mmap and munmap. Restrictions apply.
2+
//
3+
// This is meant to be complete enough to be compatible with code that uses
4+
// mmap for simple file I/O. It just allocates memory with malloc and reads
5+
// and writes data with pread and pwrite.
86

97
#define _WASI_EMULATED_MMAN
108
#include <stdlib.h>
@@ -110,7 +108,7 @@ void *mmap(void *addr, size_t length, int prot, int flags,
110108

111109
int munmap(void *addr, size_t length) {
112110
struct map *map = (struct map *)addr - 1;
113-
111+
114112
// We don't support partial munmapping.
115113
if (map->length != length) {
116114
errno = EINVAL;

libc-bottom-half/sources/errno.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <errno.h>
22
#include <threads.h>
33

4-
/* These values are used by reference-sysroot's dlmalloc. */
4+
// These values are used by reference-sysroot's dlmalloc.
55
const int __EINVAL = EINVAL;
66
const int __ENOMEM = ENOMEM;

libc-bottom-half/sources/sbrk.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
#include <errno.h>
44
#include <__macro_PAGESIZE.h>
55

6-
/* Bare-bones implementation of sbrk. */
6+
// Bare-bones implementation of sbrk.
77
void *sbrk(intptr_t increment) {
8-
/* sbrk(0) returns the current memory size. */
8+
// sbrk(0) returns the current memory size.
99
if (increment == 0) {
10-
/* The wasm spec doesn't guarantee that memory.grow of 0 always succeeds. */
10+
// The wasm spec doesn't guarantee that memory.grow of 0 always succeeds.
1111
return (void *)(__builtin_wasm_memory_size(0) * PAGESIZE);
1212
}
1313

14-
/* We only support page-size increments. */
14+
// We only support page-size increments.
1515
if (increment % PAGESIZE != 0) {
1616
abort();
1717
}
1818

19-
/* WebAssembly doesn't support shrinking linear memory. */
19+
// WebAssembly doesn't support shrinking linear memory.
2020
if (increment < 0) {
2121
abort();
2222
}
2323

2424
uintptr_t old = __builtin_wasm_memory_grow(0, (uintptr_t)increment / PAGESIZE);
25-
25+
2626
if (old == SIZE_MAX) {
2727
errno = ENOMEM;
2828
return (void *)-1;

libc-top-half/headers/private/printscan.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void long_double_not_supported(void) {
5353

5454
#else
5555

56-
/* Full long double support. */
56+
// Full long double support.
5757
typedef long double long_double;
5858

5959
#endif

libc-top-half/sources/arc4random.c

+113-112
Original file line numberDiff line numberDiff line change
@@ -12,148 +12,149 @@
1212
#define ROTL32(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b))))
1313

1414
#define CHACHA20_QUARTERROUND(A, B, C, D) \
15-
A += B; \
16-
D = ROTL32(D ^ A, 16); \
17-
C += D; \
18-
B = ROTL32(B ^ C, 12); \
19-
A += B; \
20-
D = ROTL32(D ^ A, 8); \
21-
C += D; \
22-
B = ROTL32(B ^ C, 7)
15+
A += B; \
16+
D = ROTL32(D ^ A, 16); \
17+
C += D; \
18+
B = ROTL32(B ^ C, 12); \
19+
A += B; \
20+
D = ROTL32(D ^ A, 8); \
21+
C += D; \
22+
B = ROTL32(B ^ C, 7)
2323

2424
static void CHACHA20_ROUNDS(uint32_t st[16])
2525
{
26-
int i;
27-
28-
for (i = 0; i < 20; i += 2) {
29-
CHACHA20_QUARTERROUND(st[0], st[4], st[8], st[12]);
30-
CHACHA20_QUARTERROUND(st[1], st[5], st[9], st[13]);
31-
CHACHA20_QUARTERROUND(st[2], st[6], st[10], st[14]);
32-
CHACHA20_QUARTERROUND(st[3], st[7], st[11], st[15]);
33-
CHACHA20_QUARTERROUND(st[0], st[5], st[10], st[15]);
34-
CHACHA20_QUARTERROUND(st[1], st[6], st[11], st[12]);
35-
CHACHA20_QUARTERROUND(st[2], st[7], st[8], st[13]);
36-
CHACHA20_QUARTERROUND(st[3], st[4], st[9], st[14]);
37-
}
26+
int i;
27+
28+
for (i = 0; i < 20; i += 2) {
29+
CHACHA20_QUARTERROUND(st[0], st[4], st[8], st[12]);
30+
CHACHA20_QUARTERROUND(st[1], st[5], st[9], st[13]);
31+
CHACHA20_QUARTERROUND(st[2], st[6], st[10], st[14]);
32+
CHACHA20_QUARTERROUND(st[3], st[7], st[11], st[15]);
33+
CHACHA20_QUARTERROUND(st[0], st[5], st[10], st[15]);
34+
CHACHA20_QUARTERROUND(st[1], st[6], st[11], st[12]);
35+
CHACHA20_QUARTERROUND(st[2], st[7], st[8], st[13]);
36+
CHACHA20_QUARTERROUND(st[3], st[4], st[9], st[14]);
37+
}
3838
}
3939

4040
static void chacha20_update(uint8_t out[CHACHA20_BLOCKBYTES], uint32_t st[16])
4141
{
42-
uint32_t ks[16];
43-
int i;
44-
45-
memcpy(ks, st, 4 * 16);
46-
CHACHA20_ROUNDS(st);
47-
for (i = 0; i < 16; i++) {
48-
ks[i] += st[i];
49-
}
50-
memcpy(out, ks, CHACHA20_BLOCKBYTES);
51-
st[12]++;
42+
uint32_t ks[16];
43+
int i;
44+
45+
memcpy(ks, st, 4 * 16);
46+
CHACHA20_ROUNDS(st);
47+
for (i = 0; i < 16; i++) {
48+
ks[i] += st[i];
49+
}
50+
memcpy(out, ks, CHACHA20_BLOCKBYTES);
51+
st[12]++;
5252
}
5353

5454
static void chacha20_init(uint32_t st[16], const uint8_t key[CHACHA20_KEYBYTES])
5555
{
56-
static const uint32_t constants[4] = { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
57-
memcpy(&st[0], constants, 4 * 4);
58-
memcpy(&st[4], key, CHACHA20_KEYBYTES);
59-
memset(&st[12], 0, 4 * 4);
56+
static const uint32_t constants[4] = { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
57+
memcpy(&st[0], constants, 4 * 4);
58+
memcpy(&st[4], key, CHACHA20_KEYBYTES);
59+
memset(&st[12], 0, 4 * 4);
6060
}
6161

6262
static int chacha20_rng(uint8_t* out, size_t len, uint8_t key[CHACHA20_KEYBYTES])
6363
{
64-
uint32_t st[16];
65-
size_t off;
66-
67-
chacha20_init(st, key);
68-
chacha20_update(&out[0], st);
69-
memcpy(key, out, CHACHA20_KEYBYTES);
70-
off = 0;
71-
while (len >= CHACHA20_BLOCKBYTES) {
72-
chacha20_update(&out[off], st);
73-
len -= CHACHA20_BLOCKBYTES;
74-
off += CHACHA20_BLOCKBYTES;
75-
}
76-
if (len > 0) {
77-
uint8_t tmp[CHACHA20_BLOCKBYTES];
78-
chacha20_update(tmp, st);
79-
memcpy(&out[off], tmp, len);
80-
}
81-
return 0;
64+
uint32_t st[16];
65+
size_t off;
66+
67+
chacha20_init(st, key);
68+
chacha20_update(&out[0], st);
69+
memcpy(key, out, CHACHA20_KEYBYTES);
70+
off = 0;
71+
while (len >= CHACHA20_BLOCKBYTES) {
72+
chacha20_update(&out[off], st);
73+
len -= CHACHA20_BLOCKBYTES;
74+
off += CHACHA20_BLOCKBYTES;
75+
}
76+
if (len > 0) {
77+
uint8_t tmp[CHACHA20_BLOCKBYTES];
78+
chacha20_update(tmp, st);
79+
memcpy(&out[off], tmp, len);
80+
}
81+
return 0;
8282
}
8383

8484
struct rng_state {
85-
int initialized;
86-
size_t off;
87-
uint8_t key[CHACHA20_KEYBYTES];
88-
uint8_t reserve[RNG_RESERVE_LEN];
85+
int initialized;
86+
size_t off;
87+
uint8_t key[CHACHA20_KEYBYTES];
88+
uint8_t reserve[RNG_RESERVE_LEN];
8989
};
9090

9191
void arc4random_buf(void* buffer, size_t len)
9292
{
93-
static _Thread_local struct rng_state rng_state;
94-
95-
unsigned char* buffer_ = (unsigned char*)buffer;
96-
size_t off;
97-
size_t remaining;
98-
size_t partial;
99-
100-
if (!rng_state.initialized) {
101-
if (getentropy(rng_state.key, sizeof rng_state.key) != 0) {
102-
assert(0);
103-
}
104-
rng_state.off = RNG_RESERVE_LEN;
105-
rng_state.initialized = 1;
106-
}
107-
off = 0;
108-
remaining = len;
109-
while (remaining > 0) {
110-
if (rng_state.off == RNG_RESERVE_LEN) {
111-
while (remaining >= RNG_RESERVE_LEN) {
112-
chacha20_rng(&buffer_[off], RNG_RESERVE_LEN, rng_state.key);
113-
off += RNG_RESERVE_LEN;
114-
remaining -= RNG_RESERVE_LEN;
115-
}
116-
if (remaining == 0) {
117-
break;
118-
}
119-
chacha20_rng(&rng_state.reserve[0], RNG_RESERVE_LEN, rng_state.key);
120-
rng_state.off = 0;
121-
}
122-
partial = RNG_RESERVE_LEN - rng_state.off;
123-
if (remaining < partial) {
124-
partial = remaining;
125-
}
126-
memcpy(&buffer_[off], &rng_state.reserve[rng_state.off], partial);
127-
memset(&rng_state.reserve[rng_state.off], 0, partial);
128-
rng_state.off += partial;
129-
remaining -= partial;
130-
off += partial;
131-
}
93+
static _Thread_local struct rng_state rng_state;
94+
95+
unsigned char* buffer_ = (unsigned char*)buffer;
96+
size_t off;
97+
size_t remaining;
98+
size_t partial;
99+
100+
if (!rng_state.initialized) {
101+
if (getentropy(rng_state.key, sizeof rng_state.key) != 0) {
102+
assert(0);
103+
}
104+
rng_state.off = RNG_RESERVE_LEN;
105+
rng_state.initialized = 1;
106+
}
107+
off = 0;
108+
remaining = len;
109+
while (remaining > 0) {
110+
if (rng_state.off == RNG_RESERVE_LEN) {
111+
while (remaining >= RNG_RESERVE_LEN) {
112+
chacha20_rng(&buffer_[off], RNG_RESERVE_LEN, rng_state.key);
113+
off += RNG_RESERVE_LEN;
114+
remaining -= RNG_RESERVE_LEN;
115+
}
116+
if (remaining == 0) {
117+
break;
118+
}
119+
chacha20_rng(&rng_state.reserve[0], RNG_RESERVE_LEN, rng_state.key);
120+
rng_state.off = 0;
121+
}
122+
partial = RNG_RESERVE_LEN - rng_state.off;
123+
if (remaining < partial) {
124+
partial = remaining;
125+
}
126+
memcpy(&buffer_[off], &rng_state.reserve[rng_state.off], partial);
127+
memset(&rng_state.reserve[rng_state.off], 0, partial);
128+
rng_state.off += partial;
129+
remaining -= partial;
130+
off += partial;
131+
}
132132
}
133133

134134
uint32_t arc4random(void)
135135
{
136-
uint32_t v;
136+
uint32_t v;
137137

138-
arc4random_buf(&v, sizeof v);
138+
arc4random_buf(&v, sizeof v);
139139

140-
return v;
140+
return v;
141141
}
142142

143143
uint32_t arc4random_uniform(const uint32_t upper_bound)
144144
{
145-
uint32_t min;
146-
uint32_t r;
147-
148-
if (upper_bound < 2U) {
149-
return 0;
150-
}
151-
min = (1U + ~upper_bound) % upper_bound; /* = 2**32 mod upper_bound */
152-
do {
153-
r = arc4random();
154-
} while (r < min);
155-
/* r is now clamped to a set whose size mod upper_bound == 0
156-
* the worst case (2**31+1) requires 2 attempts on average */
157-
158-
return r % upper_bound;
145+
uint32_t min;
146+
uint32_t r;
147+
148+
if (upper_bound < 2U) {
149+
return 0;
150+
}
151+
min = (1U + ~upper_bound) % upper_bound; // = 2**32 mod upper_bound
152+
do {
153+
r = arc4random();
154+
} while (r < min);
155+
156+
// r is now clamped to a set whose size mod upper_bound == 0.
157+
// The worst case (2**31+1) requires 2 attempts on average.
158+
159+
return r % upper_bound;
159160
}

0 commit comments

Comments
 (0)