-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Glue code for using snmalloc in EDP #601
Open
jethrogb
wants to merge
7
commits into
master
Choose a base branch
from
jb/snmalloc-edp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f4724c1
Glue code for using snmalloc in EDP
d81d2d1
Putting a limit to random number generation retries
NirjharRoyiitk 313d443
Removing the formal parameters in sn_global_init() function
NirjharRoyiitk 8998236
minor edits
NirjharRoyiitk e84e65c
fixing build issues related to Unverified struct
NirjharRoyiitk 1461ce5
Make available compile-time constants for C Alloc type
8478e0c
Add rerun-if-changed commands for C sources
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "snmalloc-edp/snmalloc"] | ||
path = snmalloc-edp/snmalloc | ||
url = https://github.com/microsoft/snmalloc |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) | ||
project(snmalloc-edp CXX) | ||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED True) | ||
set(SNMALLOC_HEADER_ONLY_LIBRARY ON) | ||
add_subdirectory(snmalloc EXCLUDE_FROM_ALL) | ||
add_library(snmalloc-edp src/rust-sgx-snmalloc-shim.cpp) | ||
target_link_libraries(snmalloc-edp PRIVATE snmalloc_lib) | ||
target_compile_options(snmalloc-edp PRIVATE -nostdlib -ffreestanding -fno-exceptions -mrdrnd -fPIC) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "snmalloc-edp" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
build = "build.rs" | ||
|
||
[build-dependencies] | ||
cc = "1.0.86" | ||
cmake = "0.1.50" | ||
elf = "0.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::fs::{DirEntry, File}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
fn files_in_dir(p: &Path) -> impl Iterator<Item = DirEntry> { | ||
p.read_dir().unwrap().map(|e| e.unwrap()).filter(|e| e.file_type().unwrap().is_file()) | ||
} | ||
|
||
fn main() { | ||
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); | ||
|
||
// # Use CMake to build the shim | ||
let mut dst = cmake::build("."); | ||
dst.push("build"); | ||
println!("cargo:rustc-link-search=native={}", dst.display()); | ||
|
||
// ideally, the cmake crate would have a way to output this | ||
println!("cargo:rerun-if-changed=CMakeLists.txt"); | ||
println!("cargo:rerun-if-changed=src/rust-sgx-snmalloc-shim.cpp"); | ||
|
||
// # Extract the static library archive into a temporary directory | ||
let mut objs = out_dir.clone(); | ||
objs.push("objs"); | ||
std::fs::create_dir_all(&objs).unwrap(); | ||
// clear existing files in the temp dir | ||
for file in files_in_dir(&objs) { | ||
std::fs::remove_file(file.path()).unwrap(); | ||
} | ||
|
||
dst.push("libsnmalloc-edp.a"); | ||
|
||
let mut ar = cc::Build::new().get_archiver(); | ||
ar.args(&["x", "--output"]); | ||
ar.arg(&objs); | ||
ar.arg(dst); | ||
assert!(ar.status().unwrap().success()); | ||
|
||
// # Read the symbols from the shim ELF object | ||
let f = files_in_dir(&objs).next().unwrap(); | ||
let mut elf = elf::ElfStream::<elf::endian::LittleEndian, _>::open_stream(File::open(f.path()).unwrap()).unwrap(); | ||
let (symtab, strtab) = elf.symbol_table().unwrap().unwrap(); | ||
let mut sn_alloc_size = None; | ||
let mut sn_alloc_align = None; | ||
for sym in symtab { | ||
match strtab.get(sym.st_name as _).unwrap() { | ||
"sn_alloc_size" => assert!(sn_alloc_size.replace(sym).is_none()), | ||
"sn_alloc_align" => assert!(sn_alloc_align.replace(sym).is_none()), | ||
_ => {} | ||
} | ||
} | ||
let sn_alloc_size = sn_alloc_size.expect("sn_alloc_size"); | ||
let sn_alloc_align = sn_alloc_align.expect("sn_alloc_align"); | ||
|
||
let mut get_u64_at_symbol = |sym: elf::symbol::Symbol| { | ||
assert_eq!(sym.st_size, 8); | ||
let (data, _) = elf.section_data(&elf.section_headers()[sym.st_shndx as usize].clone()).unwrap(); | ||
let data: &[u8; 8] = data.split_at(8).0.try_into().unwrap(); | ||
u64::from_le_bytes(*data) | ||
}; | ||
|
||
let sn_alloc_size = get_u64_at_symbol(sn_alloc_size); | ||
let sn_alloc_align = get_u64_at_symbol(sn_alloc_align); | ||
|
||
// # Write the type | ||
let contents = format!("#[repr(align({}), C)] pub struct Alloc {{ _0: [u8; {}] }}", sn_alloc_align, sn_alloc_size); | ||
let mut alloc_type_rs = out_dir.clone(); | ||
alloc_type_rs.push("alloc-type.rs"); | ||
std::fs::write(alloc_type_rs, contents).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#![no_std] | ||
|
||
include!(concat!(env!("OUT_DIR"), "/alloc-type.rs")); | ||
|
||
#[link(name = "snmalloc-edp", kind = "static")] | ||
extern { | ||
pub fn sn_global_init(); | ||
pub fn sn_thread_init(allocator: *mut Alloc); | ||
pub fn sn_thread_cleanup(allocator: *mut Alloc); | ||
|
||
pub fn sn_rust_alloc(alignment: usize, size: usize) -> *mut u8; | ||
pub fn sn_rust_alloc_zeroed(alignment: usize, size: usize) -> *mut u8; | ||
pub fn sn_rust_dealloc(ptr: *mut u8, alignment: usize, size: usize); | ||
pub fn sn_rust_realloc(ptr: *mut u8, alignment: usize, old_size: usize, new_size: usize) -> *mut u8; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use
CXX_x86_64_fortanix_unknown_sgx