Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c6f2c05

Browse files
authoredFeb 7, 2020
gen-headers: Generate assertions of layout from witx (#149)
* use pch/layout branch for witx; generate assertions of layout * address review comments, add asserts for handle * change wasm32 support comment to a preprocessor error * expose `to_c_header` in wasi-headers crate for use in external test harness * main.rs: inputs and output arguments are optional so that generate-libc command works * regen header
1 parent 37c663f commit c6f2c05

File tree

5 files changed

+302
-29
lines changed

5 files changed

+302
-29
lines changed
 

‎libc-bottom-half/headers/public/wasi/api.h

+204-23
Large diffs are not rendered by default.

‎tools/wasi-headers/src/c_header.rs

+94-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use heck::ShoutySnakeCase;
22
use witx::*;
33

4-
pub(crate) fn to_c_header(doc: &Document, inputs_str: &str) -> String {
4+
pub fn to_c_header(doc: &Document, inputs_str: &str) -> String {
55
let mut ret = String::new();
66

77
ret.push_str(&format!(
@@ -27,6 +27,10 @@ pub(crate) fn to_c_header(doc: &Document, inputs_str: &str) -> String {
2727
#error <wasi/api.h> is only supported on WASI platforms.
2828
#endif
2929
30+
#ifndef __wasm32__
31+
#error <wasi/api.h> only supports wasm32; doesn't yet support wasm64
32+
#endif
33+
3034
#include <stddef.h>
3135
#include <stdint.h>
3236
@@ -38,6 +42,7 @@ _Static_assert(_Alignof(int32_t) == 4, "non-wasi data layout");
3842
_Static_assert(_Alignof(uint32_t) == 4, "non-wasi data layout");
3943
_Static_assert(_Alignof(int64_t) == 8, "non-wasi data layout");
4044
_Static_assert(_Alignof(uint64_t) == 8, "non-wasi data layout");
45+
_Static_assert(_Alignof(void*) == 4, "non-wasi data layout");
4146
4247
#ifdef __cplusplus
4348
extern "C" {{
@@ -117,6 +122,19 @@ fn print_alias(ret: &mut String, name: &Id, dest: &TypeRef) {
117122
));
118123
}
119124
ret.push_str("\n");
125+
126+
ret.push_str(&format!(
127+
"_Static_assert(sizeof(__wasi_{}_t) == {}, \"witx calculated size\");\n",
128+
ident_name(name),
129+
dest.mem_size_align().size
130+
));
131+
ret.push_str(&format!(
132+
"_Static_assert(_Alignof(__wasi_{}_t) == {}, \"witx calculated align\");\n",
133+
ident_name(name),
134+
dest.mem_size_align().align
135+
));
136+
137+
ret.push_str("\n");
120138
}
121139
}
122140
}
@@ -146,6 +164,19 @@ fn print_enum(ret: &mut String, name: &Id, e: &EnumDatatype) {
146164
));
147165
ret.push_str("\n");
148166
}
167+
168+
ret.push_str(&format!(
169+
"_Static_assert(sizeof(__wasi_{}_t) == {}, \"witx calculated size\");\n",
170+
ident_name(name),
171+
e.repr.mem_size()
172+
));
173+
ret.push_str(&format!(
174+
"_Static_assert(_Alignof(__wasi_{}_t) == {}, \"witx calculated align\");\n",
175+
ident_name(name),
176+
e.repr.mem_align()
177+
));
178+
179+
ret.push_str("\n");
149180
}
150181

151182
fn print_int(ret: &mut String, name: &Id, i: &IntDatatype) {
@@ -213,6 +244,19 @@ fn print_flags(ret: &mut String, name: &Id, f: &FlagsDatatype) {
213244
));
214245
ret.push_str("\n");
215246
}
247+
248+
ret.push_str(&format!(
249+
"_Static_assert(sizeof(__wasi_{}_t) == {}, \"witx calculated size\");\n",
250+
ident_name(name),
251+
f.repr.mem_size(),
252+
));
253+
ret.push_str(&format!(
254+
"_Static_assert(_Alignof(__wasi_{}_t) == {}, \"witx calculated align\");\n",
255+
ident_name(name),
256+
f.repr.mem_align(),
257+
));
258+
259+
ret.push_str("\n");
216260
}
217261

218262
fn print_struct(ret: &mut String, name: &Id, s: &StructDatatype) {
@@ -239,6 +283,28 @@ fn print_struct(ret: &mut String, name: &Id, s: &StructDatatype) {
239283

240284
ret.push_str(&format!("}} __wasi_{}_t;\n", ident_name(name)));
241285
ret.push_str("\n");
286+
287+
ret.push_str(&format!(
288+
"_Static_assert(sizeof(__wasi_{}_t) == {}, \"witx calculated size\");\n",
289+
ident_name(name),
290+
s.mem_size()
291+
));
292+
ret.push_str(&format!(
293+
"_Static_assert(_Alignof(__wasi_{}_t) == {}, \"witx calculated align\");\n",
294+
ident_name(name),
295+
s.mem_align()
296+
));
297+
298+
for layout in s.member_layout() {
299+
ret.push_str(&format!(
300+
"_Static_assert(offsetof(__wasi_{}_t, {}) == {}, \"witx calculated offset\");\n",
301+
ident_name(name),
302+
ident_name(&layout.member.name),
303+
layout.offset
304+
));
305+
}
306+
307+
ret.push_str("\n");
242308
}
243309

244310
fn print_union(ret: &mut String, name: &Id, u: &UnionDatatype) {
@@ -262,10 +328,36 @@ fn print_union(ret: &mut String, name: &Id, u: &UnionDatatype) {
262328

263329
ret.push_str(&format!("}} __wasi_{}_t;\n", ident_name(name)));
264330
ret.push_str("\n");
331+
332+
ret.push_str(&format!(
333+
"_Static_assert(sizeof(__wasi_{}_t) == {}, \"witx calculated size\");\n",
334+
ident_name(name),
335+
u.mem_size()
336+
));
337+
ret.push_str(&format!(
338+
"_Static_assert(_Alignof(__wasi_{}_t) == {}, \"witx calculated align\");\n",
339+
ident_name(name),
340+
u.mem_align()
341+
));
342+
343+
ret.push_str("\n");
265344
}
266345

267-
fn print_handle(ret: &mut String, name: &Id, _h: &HandleDatatype) {
346+
fn print_handle(ret: &mut String, name: &Id, h: &HandleDatatype) {
268347
ret.push_str(&format!("typedef int __wasi_{}_t;", ident_name(name)));
348+
349+
ret.push_str(&format!(
350+
"_Static_assert(sizeof(__wasi_{}_t) == {}, \"witx calculated size\");\n",
351+
ident_name(name),
352+
h.mem_size()
353+
));
354+
ret.push_str(&format!(
355+
"_Static_assert(_Alignof(__wasi_{}_t) == {}, \"witx calculated align\");\n",
356+
ident_name(name),
357+
h.mem_align()
358+
));
359+
360+
ret.push_str("\n");
269361
}
270362

271363
fn print_module(ret: &mut String, m: &Module) {

‎tools/wasi-headers/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod c_header;
22

33
use anyhow::{anyhow, Result};
4-
use c_header::to_c_header;
4+
pub use c_header::to_c_header;
55
use std::fs;
66
use std::io;
77
use std::path::PathBuf;

‎tools/wasi-headers/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ impl GenerateCommand {
2626

2727
fn main() -> Result<()> {
2828
let matches = app_from_crate!()
29-
.arg(Arg::with_name("inputs").required(true).multiple(true))
29+
.arg(Arg::with_name("inputs").required(false).multiple(true))
3030
.arg(
3131
Arg::with_name("output")
3232
.short("o")
3333
.long("output")
3434
.takes_value(true)
35-
.required(true),
35+
.required(false),
3636
)
3737
.subcommand(
3838
SubCommand::with_name("generate-libc")

0 commit comments

Comments
 (0)
Please sign in to comment.