Skip to content

Commit

Permalink
Merge pull request wasm3#2 from oasisprotocol/kostko/fix/sgx-stack-pr…
Browse files Browse the repository at this point in the history
…otector

wasm3-sys: Compile with -fno-stack-protector on SGX
  • Loading branch information
kostko authored Nov 28, 2021
2 parents eb08659 + 93bb9f2 commit 2b96704
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 58 deletions.
63 changes: 63 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = false
format_code_in_doc_comments = false
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
imports_granularity = "Crate"
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
force_multiline_blocks = false
fn_args_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2015"
version = "One"
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Never"
report_fixme = "Never"
ignore = []
emit_mode = "Files"
make_backup = false
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm3"
version = "0.2.0"
version = "0.3.0"
authors = [
"Oasis Protocol Foundation <info@oasisprotocol.org>",
"Lukas Tobias Wirth <lukastw97@gmail.com>"
Expand Down
18 changes: 10 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#[allow(unused_extern_crates)]
extern crate rs_libc;

use std::any::Any;
use std::boxed::Box;
use std::cell::{RefCell, UnsafeCell};
use std::convert::TryInto;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::pin::Pin;
use std::ptr::{self, NonNull};
use std::{
any::Any,
boxed::Box,
cell::{RefCell, UnsafeCell},
convert::TryInto,
ffi::{CStr, CString},
marker::PhantomData,
pin::Pin,
ptr::{self, NonNull},
};

use impl_trait_for_tuples::impl_for_tuples;
use thiserror::Error;
Expand Down
12 changes: 6 additions & 6 deletions wasm3-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "wasm3-sys"
version = "0.2.0"
authors = ["Lukas Tobias Wirth <lukastw97@gmail.com>"]
version = "0.3.0"
authors = [
"Oasis Protocol Foundation <info@oasisprotocol.org>",
"Lukas Tobias Wirth <lukastw97@gmail.com>"
]
edition = "2018"
description = "Raw ffi bindings for wasm3"
homepage = "https://github.com/veykril/wasm3-rs"
repository = "https://github.com/veykril/wasm3-rs"
description = "Raw FFI bindings for wasm3"
categories = ["external-ffi-bindings"]
build = "build.rs"
license = "MIT"
links = "wasm3"
include = ["wasm3/source/**/*", "src/**/*", "Cargo.toml", "build.rs"]

[features]
wasi = []
use-32bit-slots = []
floats = []

Expand Down
60 changes: 17 additions & 43 deletions wasm3-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::env;
use std::ffi::OsStr;
use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};
use std::{env, ffi::OsStr, fs, path::PathBuf};

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

fn gen_wrapper(out_path: &Path) -> PathBuf {
// TODO: we currently need the field definitions of the structs of wasm3. These aren't exposed
// in the wasm3.h header so we have to generate bindings for more.
let wrapper_file = out_path.join("wrapper.h");

let mut buffer = String::new();
fs::read_dir(WASM3_SOURCE)
.unwrap_or_else(|_| panic!("failed to read {} directory", WASM3_SOURCE))
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().and_then(OsStr::to_str) == Some("h"))
.for_each(|path| {
writeln!(
&mut buffer,
"#include \"{}\"",
path.file_name().unwrap().to_str().unwrap()
)
.unwrap()
});
fs::write(&wrapper_file, buffer).expect("failed to create wasm3 wrapper file");
wrapper_file
}

fn gen_bindings() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

let wrapper_file = gen_wrapper(&out_path);

let mut bindgen = bindgen::builder()
.header(wrapper_file.to_str().unwrap())
.header("wasm3/source/wasm3.h")
.header("wasm3/source/m3_env.h")
.use_core()
.ctypes_prefix("cty")
.layout_tests(false)
Expand Down Expand Up @@ -69,13 +41,8 @@ fn gen_bindings() {
),
&format!(
"-Dd_m3HasFloat={}",
if cfg!(feature = "floats") {
1
} else {
0
}
if cfg!(feature = "floats") { 1 } else { 0 }
),
"-Dd_m3LogOutput=0",
"-Dd_m3VerboseErrorMessages=0",
"-Iwasm3/source",
]
Expand All @@ -101,13 +68,24 @@ fn main() {
);

cfg.cpp(false)
.define("d_m3LogOutput", Some("0"))
.warnings(false)
.extra_warnings(false)
.include(WASM3_SOURCE);

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();

// Set options specific for x86_64-fortanix-unknown-sgx target.
if target_arch == "x86_64" && target_env == "sgx" && target_vendor == "fortanix" {
// Disable the stack protector as the Fortanix ABI currently sets FS and GS bases to the
// same value and the stack protector assumes the canary value is at FS:0x28 but with the
// Fortanix ABI that contains a copy of RSP.
cfg.flag("-fno-stack-protector");
}

// Add any extra arguments from the environment to the CC command line.
if let Ok(extra_clang_args) = std::env::var("BINDGEN_EXTRA_CLANG_ARGS") {
if let Ok(extra_clang_args) = env::var("BINDGEN_EXTRA_CLANG_ARGS") {
// Try to parse it with shell quoting. If we fail, make it one single big argument.
if let Some(strings) = shlex::split(&extra_clang_args) {
strings.iter().for_each(|string| {
Expand All @@ -118,10 +96,6 @@ fn main() {
};
}

if cfg!(feature = "wasi") {
cfg.define("d_m3HasWASI", None);
}

cfg.define(
"d_m3Use32BitSlots",
if cfg!(feature = "use-32bit-slots") {
Expand Down

0 comments on commit 2b96704

Please sign in to comment.