forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move snmalloc-edp from rust-sgx to std library
- Loading branch information
aditijannu
committed
Jun 26, 2024
1 parent
f35b0aa
commit 7188e5c
Showing
189 changed files
with
28,508 additions
and
3 deletions.
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
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,21 @@ | ||
[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" | ||
|
||
[dependencies] | ||
core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" } | ||
compiler_builtins = { version = "0.1.0", optional = true } | ||
|
||
[features] | ||
docs = [] | ||
rustc-dep-of-std = ["core", "compiler_builtins/rustc-dep-of-std"] |
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,111 @@ | ||
--- | ||
Language: Cpp | ||
# BasedOnStyle: LLVM | ||
AccessModifierOffset: -2 | ||
AlignAfterOpenBracket: AlwaysBreak | ||
AlignConsecutiveAssignments: false | ||
AlignConsecutiveDeclarations: false | ||
AlignEscapedNewlines: DontAlign | ||
AlignOperands: false | ||
AlignTrailingComments: false | ||
AllowAllParametersOfDeclarationOnNextLine: true | ||
AllowShortBlocksOnASingleLine: false | ||
AllowShortCaseLabelsOnASingleLine: false | ||
AllowShortFunctionsOnASingleLine: Empty | ||
AllowShortIfStatementsOnASingleLine: false | ||
AllowShortLoopsOnASingleLine: false | ||
AlwaysBreakAfterDefinitionReturnType: None | ||
AlwaysBreakAfterReturnType: None | ||
AlwaysBreakBeforeMultilineStrings: true | ||
AlwaysBreakTemplateDeclarations: true | ||
BinPackArguments: false | ||
BinPackParameters: false | ||
BraceWrapping: | ||
AfterCaseLabel: true | ||
AfterClass: true | ||
AfterControlStatement: true | ||
AfterEnum: true | ||
AfterFunction: true | ||
AfterNamespace: true | ||
AfterObjCDeclaration: true | ||
AfterStruct: true | ||
AfterUnion: true | ||
AfterExternBlock: true | ||
BeforeCatch: true | ||
BeforeElse: true | ||
IndentBraces: false | ||
SplitEmptyFunction: false | ||
SplitEmptyRecord: false | ||
SplitEmptyNamespace: false | ||
BreakBeforeBinaryOperators: None | ||
BreakBeforeBraces: Custom | ||
BreakBeforeInheritanceComma: false | ||
BreakBeforeTernaryOperators: false | ||
BreakConstructorInitializersBeforeComma: false | ||
BreakConstructorInitializers: BeforeColon | ||
BreakAfterJavaFieldAnnotations: false | ||
BreakStringLiterals: true | ||
ColumnLimit: 80 | ||
CommentPragmas: '^ IWYU pragma:' | ||
CompactNamespaces: false | ||
ConstructorInitializerAllOnOneLineOrOnePerLine: true | ||
ConstructorInitializerIndentWidth: 0 | ||
ContinuationIndentWidth: 2 | ||
Cpp11BracedListStyle: true | ||
DerivePointerAlignment: false | ||
DisableFormat: false | ||
ExperimentalAutoDetectBinPacking: false | ||
FixNamespaceComments: false | ||
ForEachMacros: | ||
- Q_FOREACH | ||
- BOOST_FOREACH | ||
IncludeBlocks: Regroup | ||
IncludeCategories: | ||
- Regex: '^"(llvm|llvm-c|clang|clang-c)/' | ||
Priority: 2 | ||
- Regex: '^(<|"(gtest|gmock|isl|json)/)' | ||
Priority: 3 | ||
- Regex: '.*' | ||
Priority: 1 | ||
IncludeIsMainRegex: '(Test)?$' | ||
IndentCaseLabels: true | ||
IndentPPDirectives: AfterHash | ||
IndentWidth: 2 | ||
IndentWrappedFunctionNames: false | ||
JavaScriptQuotes: Leave | ||
JavaScriptWrapImports: true | ||
KeepEmptyLinesAtTheStartOfBlocks: false | ||
MacroBlockBegin: '' | ||
MacroBlockEnd: '' | ||
MaxEmptyLinesToKeep: 1 | ||
NamespaceIndentation: All | ||
ObjCBlockIndentWidth: 2 | ||
ObjCSpaceAfterProperty: false | ||
ObjCSpaceBeforeProtocolList: true | ||
PenaltyBreakAssignment: 2 | ||
PenaltyBreakBeforeFirstCallParameter: 19 | ||
PenaltyBreakComment: 300 | ||
PenaltyBreakFirstLessLess: 120 | ||
PenaltyBreakString: 1000 | ||
PenaltyExcessCharacter: 1000000 | ||
PenaltyReturnTypeOnItsOwnLine: 60 | ||
PointerAlignment: Left | ||
ReflowComments: true | ||
SortIncludes: true | ||
SortUsingDeclarations: true | ||
SpaceAfterCStyleCast: false | ||
SpaceAfterTemplateKeyword: false | ||
SpaceBeforeAssignmentOperators: true | ||
SpaceBeforeParens: ControlStatements | ||
SpaceInEmptyParentheses: false | ||
SpacesBeforeTrailingComments: 1 | ||
SpacesInAngles: false | ||
SpacesInContainerLiterals: false | ||
SpacesInCStyleCastParentheses: false | ||
SpacesInParentheses: false | ||
SpacesInSquareBrackets: false | ||
Standard: Cpp11 | ||
TabWidth: 2 | ||
UseTab: Never | ||
... | ||
|
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,9 @@ | ||
Checks: '-clang-analyzer-security.insecureAPI.bzero,clang-diagnostic-*,google-readability-casting,readability-else-after-return,performance-unnecessary-copy-initialization,bugprone-use-after-move,modernize-use-nullptr,modernize-redundant-void-arg,modernize-return-braced-init-list,modernize-use-default-member-init,modernize-use-equals-default,modernize-use-equals-delete,modernize-use-nodiscard,modernize-use-override,cppcoreguidelines-avoid-goto,misc-unconventional-assign-operator,cppcoreguidelines-narrowing-conversions,bugprone-assert-side-effect,bugprone-bool-pointer-implicit-conversion,bugprone-copy-constructor-init,bugprone-forward-declaration-namespace,bugprone-forwarding-reference-overload,bugprone-macro-parentheses,bugprone-macro-repeated-side-effects,bugprone-move-forwarding-reference,bugprone-misplaced-widening-cast,bugprone-swapped-arguments,bugprone-undelegated-constructor,bugprone-unused-raii,cert-dcl21-cpp,llvm-namespace-comment,misc-static-assert,misc-redundant-expression,modernize-loop-convert,modernize-use-using,performance-noexcept-move-constructor,readability-non-const-parameter' | ||
# It would be nice to enable: | ||
# - readability-magic-numbers | ||
# - modernize-avoid-c-arrays | ||
# - cppcoreguidelines-pro-bounds-array-to-pointer-decay (assert breaks it). | ||
# - readability-braces-around-statements (mostly works, but is very confused by constexpr if). | ||
CheckOptions: | ||
- key: modernize-use-default-member-init.UseAssignment | ||
value: '1' |
Oops, something went wrong.