Skip to content

Commit 2b96704

Browse files
authored
Merge pull request wasm3#2 from oasisprotocol/kostko/fix/sgx-stack-protector
wasm3-sys: Compile with -fno-stack-protector on SGX
2 parents eb08659 + 93bb9f2 commit 2b96704

File tree

5 files changed

+97
-58
lines changed

5 files changed

+97
-58
lines changed

.rustfmt.toml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
max_width = 100
2+
hard_tabs = false
3+
tab_spaces = 4
4+
newline_style = "Auto"
5+
use_small_heuristics = "Default"
6+
indent_style = "Block"
7+
wrap_comments = false
8+
format_code_in_doc_comments = false
9+
comment_width = 80
10+
normalize_comments = false
11+
normalize_doc_attributes = false
12+
format_strings = false
13+
format_macro_matchers = false
14+
format_macro_bodies = true
15+
empty_item_single_line = true
16+
struct_lit_single_line = true
17+
fn_single_line = false
18+
where_single_line = false
19+
imports_indent = "Block"
20+
imports_layout = "Mixed"
21+
imports_granularity = "Crate"
22+
reorder_imports = true
23+
reorder_modules = true
24+
reorder_impl_items = false
25+
type_punctuation_density = "Wide"
26+
space_before_colon = false
27+
space_after_colon = true
28+
spaces_around_ranges = false
29+
binop_separator = "Front"
30+
remove_nested_parens = true
31+
combine_control_expr = true
32+
overflow_delimited_expr = false
33+
struct_field_align_threshold = 0
34+
enum_discrim_align_threshold = 0
35+
match_arm_blocks = true
36+
force_multiline_blocks = false
37+
fn_args_layout = "Tall"
38+
brace_style = "SameLineWhere"
39+
control_brace_style = "AlwaysSameLine"
40+
trailing_semicolon = true
41+
trailing_comma = "Vertical"
42+
match_block_trailing_comma = false
43+
blank_lines_upper_bound = 1
44+
blank_lines_lower_bound = 0
45+
edition = "2015"
46+
version = "One"
47+
merge_derives = true
48+
use_try_shorthand = false
49+
use_field_init_shorthand = false
50+
force_explicit_abi = true
51+
condense_wildcard_suffixes = false
52+
color = "Auto"
53+
unstable_features = false
54+
disable_all_formatting = false
55+
skip_children = false
56+
hide_parse_errors = false
57+
error_on_line_overflow = false
58+
error_on_unformatted = false
59+
report_todo = "Never"
60+
report_fixme = "Never"
61+
ignore = []
62+
emit_mode = "Files"
63+
make_backup = false

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wasm3"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
authors = [
55
"Oasis Protocol Foundation <[email protected]>",
66
"Lukas Tobias Wirth <[email protected]>"

src/lib.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
#[allow(unused_extern_crates)]
66
extern crate rs_libc;
77

8-
use std::any::Any;
9-
use std::boxed::Box;
10-
use std::cell::{RefCell, UnsafeCell};
11-
use std::convert::TryInto;
12-
use std::ffi::{CStr, CString};
13-
use std::marker::PhantomData;
14-
use std::pin::Pin;
15-
use std::ptr::{self, NonNull};
8+
use std::{
9+
any::Any,
10+
boxed::Box,
11+
cell::{RefCell, UnsafeCell},
12+
convert::TryInto,
13+
ffi::{CStr, CString},
14+
marker::PhantomData,
15+
pin::Pin,
16+
ptr::{self, NonNull},
17+
};
1618

1719
use impl_trait_for_tuples::impl_for_tuples;
1820
use thiserror::Error;

wasm3-sys/Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
[package]
22
name = "wasm3-sys"
3-
version = "0.2.0"
4-
authors = ["Lukas Tobias Wirth <[email protected]>"]
3+
version = "0.3.0"
4+
authors = [
5+
"Oasis Protocol Foundation <[email protected]>",
6+
"Lukas Tobias Wirth <[email protected]>"
7+
]
58
edition = "2018"
6-
description = "Raw ffi bindings for wasm3"
7-
homepage = "https://github.com/veykril/wasm3-rs"
8-
repository = "https://github.com/veykril/wasm3-rs"
9+
description = "Raw FFI bindings for wasm3"
910
categories = ["external-ffi-bindings"]
1011
build = "build.rs"
1112
license = "MIT"
1213
links = "wasm3"
1314
include = ["wasm3/source/**/*", "src/**/*", "Cargo.toml", "build.rs"]
1415

1516
[features]
16-
wasi = []
1717
use-32bit-slots = []
1818
floats = []
1919

wasm3-sys/build.rs

+17-43
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use std::env;
2-
use std::ffi::OsStr;
3-
use std::fmt::Write as _;
4-
use std::fs;
5-
use std::path::{Path, PathBuf};
1+
use std::{env, ffi::OsStr, fs, path::PathBuf};
62

73
static WASM3_SOURCE: &str = "wasm3/source";
84
const WHITELIST_REGEX_FUNCTION: &str = "([A-Z]|m3_).*";
@@ -12,36 +8,12 @@ const PRIMITIVES: &[&str] = &[
128
"f64", "f32", "u64", "i64", "u32", "i32", "u16", "i16", "u8", "i8",
139
];
1410

15-
fn gen_wrapper(out_path: &Path) -> PathBuf {
16-
// TODO: we currently need the field definitions of the structs of wasm3. These aren't exposed
17-
// in the wasm3.h header so we have to generate bindings for more.
18-
let wrapper_file = out_path.join("wrapper.h");
19-
20-
let mut buffer = String::new();
21-
fs::read_dir(WASM3_SOURCE)
22-
.unwrap_or_else(|_| panic!("failed to read {} directory", WASM3_SOURCE))
23-
.filter_map(Result::ok)
24-
.map(|entry| entry.path())
25-
.filter(|path| path.extension().and_then(OsStr::to_str) == Some("h"))
26-
.for_each(|path| {
27-
writeln!(
28-
&mut buffer,
29-
"#include \"{}\"",
30-
path.file_name().unwrap().to_str().unwrap()
31-
)
32-
.unwrap()
33-
});
34-
fs::write(&wrapper_file, buffer).expect("failed to create wasm3 wrapper file");
35-
wrapper_file
36-
}
37-
3811
fn gen_bindings() {
3912
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
4013

41-
let wrapper_file = gen_wrapper(&out_path);
42-
4314
let mut bindgen = bindgen::builder()
44-
.header(wrapper_file.to_str().unwrap())
15+
.header("wasm3/source/wasm3.h")
16+
.header("wasm3/source/m3_env.h")
4517
.use_core()
4618
.ctypes_prefix("cty")
4719
.layout_tests(false)
@@ -69,13 +41,8 @@ fn gen_bindings() {
6941
),
7042
&format!(
7143
"-Dd_m3HasFloat={}",
72-
if cfg!(feature = "floats") {
73-
1
74-
} else {
75-
0
76-
}
44+
if cfg!(feature = "floats") { 1 } else { 0 }
7745
),
78-
"-Dd_m3LogOutput=0",
7946
"-Dd_m3VerboseErrorMessages=0",
8047
"-Iwasm3/source",
8148
]
@@ -101,13 +68,24 @@ fn main() {
10168
);
10269

10370
cfg.cpp(false)
104-
.define("d_m3LogOutput", Some("0"))
10571
.warnings(false)
10672
.extra_warnings(false)
10773
.include(WASM3_SOURCE);
10874

75+
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
76+
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
77+
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
78+
79+
// Set options specific for x86_64-fortanix-unknown-sgx target.
80+
if target_arch == "x86_64" && target_env == "sgx" && target_vendor == "fortanix" {
81+
// Disable the stack protector as the Fortanix ABI currently sets FS and GS bases to the
82+
// same value and the stack protector assumes the canary value is at FS:0x28 but with the
83+
// Fortanix ABI that contains a copy of RSP.
84+
cfg.flag("-fno-stack-protector");
85+
}
86+
10987
// Add any extra arguments from the environment to the CC command line.
110-
if let Ok(extra_clang_args) = std::env::var("BINDGEN_EXTRA_CLANG_ARGS") {
88+
if let Ok(extra_clang_args) = env::var("BINDGEN_EXTRA_CLANG_ARGS") {
11189
// Try to parse it with shell quoting. If we fail, make it one single big argument.
11290
if let Some(strings) = shlex::split(&extra_clang_args) {
11391
strings.iter().for_each(|string| {
@@ -118,10 +96,6 @@ fn main() {
11896
};
11997
}
12098

121-
if cfg!(feature = "wasi") {
122-
cfg.define("d_m3HasWASI", None);
123-
}
124-
12599
cfg.define(
126100
"d_m3Use32BitSlots",
127101
if cfg!(feature = "use-32bit-slots") {

0 commit comments

Comments
 (0)