diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ddad341..61c7e53 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -21,10 +21,6 @@ jobs: components: rustfmt - uses: r7kamura/rust-problem-matchers@v1 - run: cargo fmt --all -- --check - - run: cargo fmt --all -- --check - working-directory: panic/abort - - run: cargo fmt --all -- --check - working-directory: panic/log markdown-lint: runs-on: ubuntu-22.04 @@ -60,10 +56,6 @@ jobs: - uses: actions/checkout@v3 - run: cargo install cargo-sort - run: cargo sort --workspace --check >/dev/null - - run: cargo sort --workspace --check >/dev/null - working-directory: panic/abort - - run: cargo sort --workspace --check >/dev/null - working-directory: panic/log clippy: runs-on: ubuntu-22.04 @@ -82,10 +74,6 @@ jobs: components: clippy - uses: r7kamura/rust-problem-matchers@v1 - run: cargo +${{ matrix.rust }} clippy --all --all-features -- -D warnings - - run: cargo +${{ matrix.rust }} clippy --all --all-features -- -D warnings - working-directory: panic/abort - - run: cargo +${{ matrix.rust }} clippy --all --all-features -- -D warnings - working-directory: panic/log build: runs-on: ubuntu-22.04 @@ -106,10 +94,6 @@ jobs: toolchain: ${{ matrix.rust }} - uses: r7kamura/rust-problem-matchers@v1 - run: cargo +${{ matrix.rust }} build --release - - run: cargo +${{ matrix.rust }} build --release - working-directory: panic/abort - - run: cargo +${{ matrix.rust }} build --release - working-directory: panic/log test: runs-on: ubuntu-22.04 @@ -147,10 +131,6 @@ jobs: toolchain: ${{ matrix.rust }} - uses: r7kamura/rust-problem-matchers@v1 - run: cargo +${{ matrix.rust }} doc --release --no-deps - - run: cargo +${{ matrix.rust }} doc --release --no-deps - working-directory: panic/abort - - run: cargo +${{ matrix.rust }} doc --release --no-deps - working-directory: panic/log coverage: runs-on: ubuntu-22.04 diff --git a/Cargo.toml b/Cargo.toml index 3ba99ce..413307a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,10 @@ members = [ "io", "io/untrusted", "panic", + "panic/sys", "sync", ] exclude = [ - "panic/abort", - "panic/log", "test_enclave", ] diff --git a/panic/Cargo.toml b/panic/Cargo.toml index 5d65e55..00a1da4 100644 --- a/panic/Cargo.toml +++ b/panic/Cargo.toml @@ -7,8 +7,13 @@ rust-version = "1.62.1" license = "Apache-2.0" readme = "README.md" repository = "https://github.com/mobilecoinfoundation/sgx-std" -description = "Common panic handling behavior for SGX enclaves" +description = "Panic handler for SGX enclaves" categories = ["hardware-support", "no-std"] keywords = ["sgx", "no-std", "panic"] +[features] +log = ["dep:mc-sgx-io", "dep:mc-sgx-sync"] + [dependencies] +mc-sgx-io = { path = "../io", version = "0.1.0", optional = true } +mc-sgx-sync = { path = "../sync", version = "0.1.0", optional = true } diff --git a/panic/README.md b/panic/README.md index db980ea..992d27a 100644 --- a/panic/README.md +++ b/panic/README.md @@ -7,7 +7,16 @@ -->[![Docs Status][docs-image]][docs-link][![Dependency Status][deps-image]][deps-link] -Common panic handling behavior for SGX enclaves +Panic handler for use in SGX enclaves + +The panic handler will redirect to the SGX SDK `abort()` method to mark the +enclave as crashed. + +## Features + +- `log`: Log panic messages during panic handling. The panic messages will be +directed to the host via +[mc-sgx-io::stderr_write_all](https://docs.rs/mc-sgx-io/latest/mc_sgx_io/fn.stderr_write_all.html). [chat-image]: https://img.shields.io/discord/844353360348971068?style=flat-square [chat-link]: https://mobilecoin.chat diff --git a/panic/abort/Cargo.toml b/panic/abort/Cargo.toml deleted file mode 100644 index f7464aa..0000000 --- a/panic/abort/Cargo.toml +++ /dev/null @@ -1,36 +0,0 @@ -[package] -name = "mc-sgx-panic-abort" -version = "0.1.0" -edition = "2021" -authors = ["MobileCoin"] -rust-version = "1.62.1" -license = "Apache-2.0" -readme = "README.md" -repository = "https://github.com/mobilecoinfoundation/sgx-std" -description = "Panic handler for SGX enclaves that aborts" -categories = ["hardware-support", "no-std"] -keywords = ["sgx", "no-std", "panic"] - -[workspace] - -[dependencies] -# To get link in the abort() function -mc-sgx-trts-sys = "0.4.0" - -# This is a crate that can only be built for an SGX target, so it's not part of -# the root workspace. Because of this limitation we must re-iterate the -# `cargo release` settings here -[package.metadata.release] -shared-version = true -dev-version-ext = "beta.0" -consolidate-commits = true -consolidate-pushes = true -tag-name = "v{{version}}" - -# The file names in this key are relative to the each crate that gets released. -# So we only need one `README.md` entry if all the README's follow a -# common format and are always next to the Cargo.toml for their respective -# package. -pre-release-replacements = [ - {file="README.md", search="mc-[a-z-]+/[0-9.]+", replace="{{crate_name}}/{{version}}"}, -] diff --git a/panic/abort/README.md b/panic/abort/README.md deleted file mode 100644 index 693cac3..0000000 --- a/panic/abort/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# MobileCoin SGX: Aborting Panic Handler - -[![Project Chat][chat-image]][chat-link]![License][license-image]![Target][target-image][![Crates.io][crate-image]][crate-link][![Docs Status][docs-image]][docs-link][![Dependency Status][deps-image]][deps-link] - -Aborting panic handler for use in an SGX enclave - -[chat-image]: https://img.shields.io/discord/844353360348971068?style=flat-square -[chat-link]: https://mobilecoin.chat -[license-image]: https://img.shields.io/crates/l/mc-sgx-panic-abort?style=flat-square -[target-image]: https://img.shields.io/badge/target-sgx-red?style=flat-square -[crate-image]: https://img.shields.io/crates/v/mc-sgx-panic-abort.svg?style=flat-square -[crate-link]: https://crates.io/crates/mc-sgx-panic-abort -[docs-image]: https://img.shields.io/docsrs/mc-sgx-panic-abort?style=flat-square -[docs-link]: https://docs.rs/crate/mc-sgx-panic-abort -[deps-image]: https://deps.rs/crate/mc-sgx-panic-abort/0.1.0/status.svg?style=flat-square -[deps-link]: https://deps.rs/crate/mc-sgx-panic-abort/0.1.0 diff --git a/panic/abort/src/lib.rs b/panic/abort/src/lib.rs deleted file mode 100644 index 0b99627..0000000 --- a/panic/abort/src/lib.rs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2022 The MobileCoin Foundation -#![doc = include_str!("../README.md")] -#![deny(missing_docs, missing_debug_implementations)] -#![no_std] - -use core::panic::PanicInfo; - -#[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - extern "C" { - fn abort() -> !; - } - - unsafe { abort() } -} diff --git a/panic/log/Cargo.toml b/panic/log/Cargo.toml deleted file mode 100644 index bbfa2e7..0000000 --- a/panic/log/Cargo.toml +++ /dev/null @@ -1,36 +0,0 @@ -[package] -name = "mc-sgx-panic-log" -version = "0.1.0" -edition = "2021" -authors = ["MobileCoin"] -rust-version = "1.62.1" -license = "Apache-2.0" -readme = "README.md" -repository = "https://github.com/mobilecoinfoundation/sgx-std" -description = "Panic handler for an SGX enclave that logs to the untrusted (host)" -categories = ["hardware-support", "no-std"] -keywords = ["sgx", "no-std", "panic"] - -[workspace] - -[dependencies] -mc-sgx-io = { path = "../../io", version = "0.1.0" } -mc-sgx-sync = { path = "../../sync", version = "0.1.0" } - -# This is a crate that can only be built for an SGX target, so it's not part of -# the root workspace. Because of this limitation we must re-iterate the -# `cargo release` settings here -[package.metadata.release] -shared-version = true -dev-version-ext = "beta.0" -consolidate-commits = true -consolidate-pushes = true -tag-name = "v{{version}}" - -# The file names in this key are relative to the each crate that gets released. -# So we only need one `README.md` entry if all the README's follow a -# common format and are always next to the Cargo.toml for their respective -# package. -pre-release-replacements = [ - {file="README.md", search="mc-[a-z-]+/[0-9.]+", replace="{{crate_name}}/{{version}}"}, -] diff --git a/panic/log/README.md b/panic/log/README.md deleted file mode 100644 index d229999..0000000 --- a/panic/log/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# MobileCoin SGX: Panic handler that logs to untrusted (host) - -[![Project Chat][chat-image]][chat-link]![License][license-image]![Target][target-image][![Crates.io][crate-image]][crate-link][![Docs Status][docs-image]][docs-link][![Dependency Status][deps-image]][deps-link] - -Panic handler for an SGX enclave that logs to the untrusted (host) - -[chat-image]: https://img.shields.io/discord/844353360348971068?style=flat-square -[chat-link]: https://mobilecoin.chat -[license-image]: https://img.shields.io/crates/l/mc-sgx-panic-log?style=flat-square -[target-image]: https://img.shields.io/badge/target-sgx-red?style=flat-square -[crate-image]: https://img.shields.io/crates/v/mc-sgx-panic-log.svg?style=flat-square -[crate-link]: https://crates.io/crates/mc-sgx-panic-log -[docs-image]: https://img.shields.io/docsrs/mc-sgx-panic-log?style=flat-square -[docs-link]: https://docs.rs/crate/mc-sgx-panic-log -[deps-image]: https://deps.rs/crate/mc-sgx-panic-log/0.1.0/status.svg?style=flat-square -[deps-link]: https://deps.rs/crate/mc-sgx-panic-log/0.1.0 diff --git a/panic/src/lib.rs b/panic/src/lib.rs index bcf3947..da4f38e 100644 --- a/panic/src/lib.rs +++ b/panic/src/lib.rs @@ -1,8 +1,24 @@ -// Copyright (c) 2023 The MobileCoin Foundation -#![feature(thread_local)] +// Copyright (c) 2022-2023 The MobileCoin Foundation + #![doc = include_str!("../README.md")] -#![deny(missing_docs, missing_debug_implementations, unsafe_code)] +#![deny(missing_docs, missing_debug_implementations)] #![no_std] -mod panicking; -pub mod thread; +#[cfg(not(test))] +use core::panic::PanicInfo; + +#[cfg(feature = "log")] +mod log; + +#[cfg(not(test))] +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + #[cfg(feature = "log")] + log::log_panic_info(_info); + + extern "C" { + fn abort() -> !; + } + + unsafe { abort() } +} diff --git a/panic/log/src/lib.rs b/panic/src/log.rs similarity index 79% rename from panic/log/src/lib.rs rename to panic/src/log.rs index d743a69..43e5e44 100644 --- a/panic/log/src/lib.rs +++ b/panic/src/log.rs @@ -1,8 +1,6 @@ -// Copyright (c) 2022 The MobileCoin Foundation +// Copyright (c) 2022-2023 The MobileCoin Foundation -#![doc = include_str!("../README.md")] -#![deny(missing_docs, missing_debug_implementations)] -#![no_std] +//! Logging utilities used during panic handling use core::fmt::Write; use core::panic::PanicInfo; @@ -14,12 +12,6 @@ use mc_sgx_sync::Mutex; /// the cause of the panic. static MESSAGE_BUFFER: Mutex = Mutex::new(WriteBuffer::new()); -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - log_panic_info(info); - loop {} -} - /// Log information during a panic /// /// If for some reason the `info` exceeds the size of the [`MESSAGE_BUFFER`] @@ -27,7 +19,7 @@ fn panic(info: &PanicInfo) -> ! { /// /// # Arguments: /// * `info` - The panic information to log -fn log_panic_info(info: &PanicInfo) { +pub(crate) fn log_panic_info(info: &PanicInfo) { if let Ok(mut buffer) = MESSAGE_BUFFER.lock() { buffer.clear(); let message = match write!(buffer, "{info}") { diff --git a/panic/sys/Cargo.toml b/panic/sys/Cargo.toml new file mode 100644 index 0000000..aba6307 --- /dev/null +++ b/panic/sys/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "mc-sgx-panic-sys" +version = "0.1.0" +edition = "2021" +authors = ["MobileCoin"] +rust-version = "1.65" +license = "Apache-2.0" +readme = "README.md" +repository = "https://github.com/mobilecoinfoundation/sgx-std" +description = "Common panic handling behavior for SGX enclaves" +categories = ["hardware-support", "no-std"] +keywords = ["sgx", "no-std", "panic"] + +[dependencies] diff --git a/panic/sys/README.md b/panic/sys/README.md new file mode 100644 index 0000000..1e56ec7 --- /dev/null +++ b/panic/sys/README.md @@ -0,0 +1,21 @@ +# MobileCoin: System specific logic for panic handling + +[![Project Chat][chat-image]][chat-link]![License][license-image]![Target][target-image][![Crates.io][crate-image]][crate-link][![Docs Status][docs-image]][docs-link][![Dependency Status][deps-image]][deps-link] + +System specific logic for panic handling + +[chat-image]: https://img.shields.io/discord/844353360348971068?style=flat-square +[chat-link]: https://mobilecoin.chat +[license-image]: https://img.shields.io/crates/l/mc-sgx-panic-sys?style=flat-square +[target-image]: https://img.shields.io/badge/target-x86__64-blue?style=flat-square +[crate-image]: https://img.shields.io/crates/v/mc-sgx-panic-sys.svg?style=flat-square +[crate-link]: https://crates.io/crates/mc-sgx-panic-sys +[docs-image]: https://img.shields.io/docsrs/mc-sgx-panic-sys?style=flat-square +[docs-link]: https://docs.rs/crate/mc-sgx-panic-sys +[deps-image]: https://deps.rs/crate/mc-sgx-panic-sys/0.1.0/status.svg?style=flat-square +[deps-link]: https://deps.rs/crate/mc-sgx-panic-sys/0.1.0 diff --git a/panic/sys/src/lib.rs b/panic/sys/src/lib.rs new file mode 100644 index 0000000..71e87ab --- /dev/null +++ b/panic/sys/src/lib.rs @@ -0,0 +1,8 @@ +// Copyright (c) 2022-2023 The MobileCoin Foundation +#![feature(thread_local)] +#![doc = include_str!("../README.md")] +#![deny(missing_docs, missing_debug_implementations, unsafe_code)] +#![no_std] + +mod panicking; +pub mod thread; diff --git a/panic/src/panicking.rs b/panic/sys/src/panicking.rs similarity index 100% rename from panic/src/panicking.rs rename to panic/sys/src/panicking.rs diff --git a/panic/src/thread.rs b/panic/sys/src/thread.rs similarity index 98% rename from panic/src/thread.rs rename to panic/sys/src/thread.rs index 7580ad7..b12cbdb 100644 --- a/panic/src/thread.rs +++ b/panic/sys/src/thread.rs @@ -24,7 +24,7 @@ use crate::panicking; /// # Examples /// /// ```should_panic -/// use mc_sgx_panic::thread; +/// use mc_sgx_panic_sys::thread; /// /// struct SomeStruct; /// diff --git a/sync/Cargo.toml b/sync/Cargo.toml index 2806a65..cca0396 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -17,5 +17,5 @@ test = false doctest = false [dependencies] -mc-sgx-panic = { path = "../panic", version = "=0.1.0" } +mc-sgx-panic-sys = { path = "../panic/sys", version = "=0.1.0" } mc-sgx-tstdc = "0.4.1" diff --git a/sync/src/poison.rs b/sync/src/poison.rs index 9654e8b..7f72221 100644 --- a/sync/src/poison.rs +++ b/sync/src/poison.rs @@ -15,7 +15,7 @@ use core::error::Error; use core::fmt; use core::sync::atomic::{AtomicBool, Ordering}; -use mc_sgx_panic::thread; +use mc_sgx_panic_sys::thread; pub(crate) struct Flag { failed: AtomicBool,