Skip to content
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

Dynamic memory allocation for enclaves running on SGXv2 processors #83927

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
source = "git+https://github.com/bitflags/bitflags#c2ba43141307a97484518ad7728534855e1a51f2"
dependencies = [
"compiler_builtins",
"rustc-std-workspace-core",
]

[[package]]
name = "bitmaps"
Expand Down Expand Up @@ -1220,8 +1223,7 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "fortanix-sgx-abi"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c56c422ef86062869b2d57ae87270608dc5929969dd130a6e248979cf4fb6ca6"
source = "git+https://github.com/fortanix/rust-sgx?branch=raoul/edmm#4d629550dfa71e6a5830fc914c1c2d40caa677a6"
dependencies = [
"compiler_builtins",
"rustc-std-workspace-core",
Expand Down Expand Up @@ -4864,6 +4866,16 @@ dependencies = [
"syn",
]

[[package]]
name = "sgx-isa"
version = "0.3.3"
source = "git+https://github.com/fortanix/rust-sgx?branch=raoul/edmm#4d629550dfa71e6a5830fc914c1c2d40caa677a6"
dependencies = [
"bitflags",
"compiler_builtins",
"rustc-std-workspace-core",
]

[[package]]
name = "sha-1"
version = "0.8.2"
Expand Down Expand Up @@ -5018,6 +5030,7 @@ version = "0.0.0"
dependencies = [
"addr2line",
"alloc",
"bitflags",
"cfg-if 0.1.10",
"compiler_builtins",
"core",
Expand All @@ -5033,6 +5046,7 @@ dependencies = [
"profiler_builtins",
"rand 0.7.3",
"rustc-demangle",
"sgx-isa",
"unwind",
"wasi",
]
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ cargo = { path = "src/tools/cargo" }
rustfmt-nightly = { path = "src/tools/rustfmt" }

[patch.crates-io]
fortanix-sgx-abi = { git = "https://github.com/fortanix/rust-sgx", branch = "raoul/edmm" }
sgx-isa = { git = "https://github.com/fortanix/rust-sgx", branch = "raoul/edmm" }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patches need to be removed when fortanix/rust-sgx#292 merges

bitflags = { git = "https://github.com/bitflags/bitflags" }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bitflags patch can be removed once a new version is published on crates.io


# See comments in `src/tools/rustc-workspace-hack/README.md` for what's going on
# here
rustc-workspace-hack = { path = 'src/tools/rustc-workspace-hack' }
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/x86_64_fortanix_unknown_sgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub fn target() -> Target {
"EH_FRM_LEN",
"TEXT_BASE",
"TEXT_SIZE",
"UNMAPPED_BASE",
"UNMAPPED_SIZE",
];
let opts = TargetOptions {
os: "unknown".into(),
Expand Down
2 changes: 2 additions & 0 deletions library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ dlmalloc = { version = "0.2.1", features = ['rustc-dep-of-std'] }

[target.x86_64-fortanix-unknown-sgx.dependencies]
fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }
sgx-isa = { version = "0.3.2", optional = false, features = ['rustc-dep-of-std', 'nightly'] }
bitflags = { version = "1.2.1", features = ['rustc-dep-of-std'] }

[target.'cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), target_os = "hermit"))'.dependencies]
hermit-abi = { version = "0.1.17", features = ['rustc-dep-of-std'] }
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/sgx/abi/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ IMAGE_BASE:
globvar HEAP_BASE 8
/* The heap size in bytes */
globvar HEAP_SIZE 8
/* The base address (relative to enclave start) of the dynamic memory area */
globvar UNMAPPED_BASE 8
/* The dynamic memory size in bytes */
globvar UNMAPPED_SIZE 8
/* Value of the RELA entry in the dynamic table */
globvar RELA 8
/* Value of the RELACOUNT entry in the dynamic table */
Expand Down
24 changes: 24 additions & 0 deletions library/std/src/sys/sgx/abi/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern "C" {
static ENCLAVE_SIZE: usize;
static HEAP_BASE: u64;
static HEAP_SIZE: usize;
static UNMAPPED_SIZE: u64;
static UNMAPPED_BASE: u64;
}

/// Returns the base memory address of the heap
Expand Down Expand Up @@ -89,3 +91,25 @@ pub fn is_user_range(p: *const u8, len: usize) -> bool {
let base = image_base() as usize;
end < base || start > base + (unsafe { ENCLAVE_SIZE } - 1) // unsafe ok: link-time constant
}

/// Returns the base memory address of the unmapped memory area. On platforms with SGXv2 features,
/// this region can be used to dynamically add enclave pages
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn unmapped_base() -> u64 {
unsafe { image_base() + UNMAPPED_BASE }
}

/// Returns the size of the unmapped memory area
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn unmapped_size() -> u64 {
unsafe { UNMAPPED_SIZE }
}

/// Returns whether the pointer is part of the unmapped memory range
/// `p + len` must not overflow
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn is_unmapped_range(p: *const u8, len: usize) -> bool {
let start = p as u64;
let end = start + (len as u64);
start >= unmapped_base() && end <= unmapped_base() + unmapped_size() // unsafe ok: link-time constant
}
18 changes: 18 additions & 0 deletions library/std/src/sys/sgx/abi/usercalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ pub fn alloc(size: usize, alignment: usize) -> IoResult<*mut u8> {
unsafe { raw::alloc(size, alignment).from_sgx_result() }
}

/// Usercall `trim`. See the ABI documentation for more information.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn trim(region: *const u8, size: usize) -> IoResult<()> {
unsafe {
raw::trim(region, size).from_sgx_result()?;
Ok(())
}
}

/// Usercall `remove_trimmed`. See the ABI documentation for more information.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn remove_trimmed(region: *const u8, size: usize) -> IoResult<()> {
unsafe {
raw::remove_trimmed(region, size).from_sgx_result()?;
Ok(())
}
}

#[unstable(feature = "sgx_platform", issue = "56975")]
#[doc(inline)]
pub use self::raw::free;
Expand Down
Loading