From 07b46acdbbe34d29c66fd0d46d19e87a11d49a64 Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Fri, 2 Aug 2024 14:09:09 +0300 Subject: [PATCH 1/3] feat(gstd) use stabilized `panic_info_message` feature from rust 1.81 --- gstd/Cargo.toml | 20 +++--- gstd/src/common/handlers.rs | 132 +----------------------------------- gstd/src/lib.rs | 2 +- 3 files changed, 11 insertions(+), 143 deletions(-) diff --git a/gstd/Cargo.toml b/gstd/Cargo.toml index dede05663de..74394dd51ef 100644 --- a/gstd/Cargo.toml +++ b/gstd/Cargo.toml @@ -4,8 +4,8 @@ description = "Gear programs standard library" documentation = "https://docs.rs/gstd" keywords = ["gear", "std", "no-std", "wasm", "smart-contracts"] categories = ["api-bindings"] -# Stable version of Rust >=1.73 is required due to new format of panic message. -rust-version = "1.73" +# Stable version of Rust >=1.81 is required due to `panic_info_message` feature. +rust-version = "1.81" version.workspace = true edition.workspace = true authors.workspace = true @@ -53,24 +53,20 @@ panic-message = ["panic-handler", "arrayvec"] ## because it displays the code path. panic-location = ["panic-message"] -#! ## Nightly features -#! -#! The `panic-message` and `panic-location` features gets additional -#! optimizations when using the nightly compiler. -#! #! For example, if you don't use the `panic-location` feature, the compiler #! will remove all locations such as `/home/username/dapp/src/lib.rs:1:2` #! from the binary. The size of program will be reduced and #! `/home/username/...` information will not be included in the binary. +#! ## Nightly features +#! +#! The final binary gets additional optimizations when using the nightly compiler. + ## Enables all features below. ## These features depend on unstable Rust API and require nightly toolchain. -nightly = ["panic-info-message", "oom-handler"] -## When enabled, a panic handler will use a more efficient implementation. -## Relies on [`panic_info_message`][rust-66745]. -panic-info-message = [] +nightly = ["oom-handler"] ## When enabled, an OOM error handler is provided. -## Relies on [`alloc_error_handler`][rust-51540], +## Relies on [`alloc_error_handler`][rust-51540]. oom-handler = [] #! ## Additional features diff --git a/gstd/src/common/handlers.rs b/gstd/src/common/handlers.rs index 7c9aa0178a8..44b2a5d970c 100644 --- a/gstd/src/common/handlers.rs +++ b/gstd/src/common/handlers.rs @@ -35,19 +35,6 @@ pub fn oom(_: core::alloc::Layout) -> ! { /// - `panic-handler`: it displays `panicked with ''` /// - `panic-message`: it displays `panicked with '{message}'` /// - `panic-location`: it displays `panicked with '{message}' at '{location}'` -/// -/// How we get the panic message in different versions of Rust: -/// - In nightly Rust, we use `#![feature(panic_info_message)]` and the -/// [`write!`] macro. -/// - In stable Rust, we need to modify the default panic handler message -/// format. -/// -/// Default panic handler message format (according to ): -/// `panicked at {location}:\n{message}` -/// -/// We parse the output of `impl Display for PanicInfo<'_>` and -/// then convert it to custom format: -/// `panicked with '{message}'[ at '{location}']`. #[cfg(target_arch = "wasm32")] #[cfg(feature = "panic-handler")] mod panic_handler { @@ -61,11 +48,6 @@ mod panic_handler { /// internal errors occur. #[cfg(not(feature = "panic-message"))] pub const UNKNOWN_REASON: &str = ""; - - /// This prefix is used by `impl Display for PanicInfo<'_>`. - #[cfg(all(not(feature = "panic-info-message"), feature = "panic-message"))] - pub const PANICKED_AT: &str = "panicked at "; - /// Max amount of bytes allowed to be thrown as string explanation /// of the error. #[cfg(feature = "panic-message")] @@ -87,8 +69,8 @@ mod panic_handler { ext::panic(MESSAGE) } - /// Panic handler for nightly Rust. - #[cfg(all(feature = "panic-info-message", feature = "panic-message"))] + /// Panic handler with extra information. + #[cfg(feature = "panic-message")] #[panic_handler] pub fn panic(panic_info: &PanicInfo) -> ! { use crate::prelude::fmt::Write; @@ -112,114 +94,4 @@ mod panic_handler { ext::panic(&debug_msg) } - - /// Panic handler for stable Rust. - #[cfg(all(not(feature = "panic-info-message"), feature = "panic-message"))] - #[panic_handler] - pub fn panic(panic_info: &PanicInfo) -> ! { - use crate::prelude::fmt::{self, Write}; - use arrayvec::ArrayString; - - #[derive(Default)] - struct TempBuffer { - overflowed: bool, - buffer: ArrayString, - } - - impl TempBuffer { - #[inline] - fn write_str(&mut self, s: &str) { - if !self.overflowed && self.buffer.write_str(s).is_err() { - self.overflowed = true; - } - } - } - - #[derive(Default)] - struct TempOutput { - found_prefix: bool, - found_delimiter: bool, - #[cfg(feature = "panic-location")] - location: TempBuffer, - message: TempBuffer, - } - - impl fmt::Write for TempOutput { - fn write_str(&mut self, s: &str) -> fmt::Result { - if !self.found_prefix && s.len() == PANICKED_AT.len() { - self.found_prefix = true; - return Ok(()); - } - - if !self.found_delimiter { - if s == ":\n" { - self.found_delimiter = true; - return Ok(()); - } - #[cfg(feature = "panic-location")] - self.location.write_str(s); - } else { - self.message.write_str(s); - } - - Ok(()) - } - } - - let mut output = TempOutput::default(); - let _ = write!(&mut output, "{panic_info}"); - - #[cfg(feature = "panic-location")] - let location = &*output.location.buffer; - let message = &*output.message.buffer; - - let mut debug_msg = ArrayString::::new(); - let _ = debug_msg.try_push_str(PANIC_PREFIX); - - #[cfg(feature = "panic-location")] - for s in ["'", message, "' at '", location, "'"] { - if debug_msg.try_push_str(s).is_err() { - break; - } - } - - #[cfg(not(feature = "panic-location"))] - for s in ["'", message, "'"] { - if debug_msg.try_push_str(s).is_err() { - break; - } - } - - #[cfg(feature = "debug")] - let _ = ext::debug(&debug_msg); - - ext::panic(&debug_msg) - } -} - -#[cfg(test)] -mod tests { - extern crate std; - - use std::{format, panic, prelude::v1::*}; - - /// Here is a test to verify that the default panic handler message - /// format has not changed. - #[test] - fn panic_msg_format_not_changed() { - const MESSAGE: &str = "message"; - - panic::set_hook(Box::new(|panic_info| { - let location = panic_info.location().unwrap(); - assert_eq!( - panic_info.to_string(), - format!("panicked at {location}:\n{MESSAGE}") - ); - })); - - let result = panic::catch_unwind(|| { - panic!("{MESSAGE}"); - }); - assert!(result.is_err()); - } } diff --git a/gstd/src/lib.rs b/gstd/src/lib.rs index 9fb61eab073..29259736d01 100644 --- a/gstd/src/lib.rs +++ b/gstd/src/lib.rs @@ -29,7 +29,7 @@ //! providing convenient instruments for creating programs from programs, etc. //! //! # Minimum supported Rust version -//! This crate requires **Rust >= 1.73** due to the implementation of the panic +//! This crate requires **Rust >= 1.81** due to the implementation of the panic //! handler in the stable version. //! //! # Crate features From 339469d8e2317db75ad017f50e769821e41b2008 Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Fri, 2 Aug 2024 14:09:44 +0300 Subject: [PATCH 2/3] revert #2865 in favor of stabilized panic_info_message --- Cargo.lock | 1 - utils/wasm-builder/Cargo.toml | 1 - utils/wasm-builder/src/cargo_command.rs | 25 ------------------------ utils/wasm-builder/src/lib.rs | 26 ++----------------------- utils/wasm-builder/src/wasm_project.rs | 5 ----- 5 files changed, 2 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0372d5b9c73..2c00abf51c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6785,7 +6785,6 @@ dependencies = [ "cargo_metadata 0.18.1", "chrono", "colored", - "dirs 4.0.0", "gear-core", "gear-pwasm-utils", "gear-wasm-instrument", diff --git a/utils/wasm-builder/Cargo.toml b/utils/wasm-builder/Cargo.toml index 66150601706..35e7a1ba41b 100644 --- a/utils/wasm-builder/Cargo.toml +++ b/utils/wasm-builder/Cargo.toml @@ -20,7 +20,6 @@ log.workspace = true pathdiff.workspace = true which.workspace = true colored.workspace = true -dirs.workspace = true gmeta.workspace = true gear-core.workspace = true gear-wasm-instrument.workspace = true diff --git a/utils/wasm-builder/src/cargo_command.rs b/utils/wasm-builder/src/cargo_command.rs index f1efe094e88..0af21e00c25 100644 --- a/utils/wasm-builder/src/cargo_command.rs +++ b/utils/wasm-builder/src/cargo_command.rs @@ -34,7 +34,6 @@ pub struct CargoCommand { toolchain: Toolchain, check_recommended_toolchain: bool, force_recommended_toolchain: bool, - paths_to_remap: Vec<(PathBuf, &'static str)>, } impl CargoCommand { @@ -50,7 +49,6 @@ impl CargoCommand { toolchain: Toolchain::try_from_rustup().expect("Failed to get toolchain from rustup"), check_recommended_toolchain: false, force_recommended_toolchain: false, - paths_to_remap: vec![], } } } @@ -94,13 +92,6 @@ impl CargoCommand { self.force_recommended_toolchain = force_recommended_toolchain; } - /// Set paths to remap. - /// - /// Used to hide the username from the panic message. - pub fn set_paths_to_remap(&mut self, paths_to_remap: &[(PathBuf, &'static str)]) { - self.paths_to_remap = paths_to_remap.into(); - } - /// Execute the `cargo` command with invoking supplied arguments. pub fn run(&self) -> Result<()> { if self.check_recommended_toolchain { @@ -141,22 +132,6 @@ impl CargoCommand { self.remove_cargo_encoded_rustflags(&mut cargo); - if !self.paths_to_remap.is_empty() { - // `--remap-path-prefix` is used to remove username from panic messages - // https://doc.rust-lang.org/rustc/command-line-arguments.html#--remap-path-prefix-remap-source-names-in-output - let global_encoded_rustflags = self - .paths_to_remap - .iter() - .map(|(from, to)| format!("--remap-path-prefix={from}={to}", from = from.display())) - .collect::>() - .join("\x1f"); - - // The environment variable `CARGO_ENCODED_RUSTFLAGS` is used to globally remap path prefix. - // It is also separated by `\x1f` to support folders with spaces or any unusual characters. - // Unlike `cargo rust`, this is useful for passing flags to all dependencies (i.e. globally). - cargo.env("CARGO_ENCODED_RUSTFLAGS", global_encoded_rustflags); - } - let status = cargo.status().context("unable to execute cargo command")?; ensure!( status.success(), diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index 092fd895f4d..df499817e8b 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -23,10 +23,10 @@ pub use cargo_command::CargoCommand; pub use wasm_project::{PreProcessor, PreProcessorResult, PreProcessorTarget}; use crate::wasm_project::WasmProject; -use anyhow::{Context, Result}; +use anyhow::Result; use gmeta::{Metadata, MetadataRepr}; use regex::Regex; -use std::{env, path::PathBuf, process}; +use std::{env, process}; use wasm_project::ProjectType; mod builder_error; @@ -128,9 +128,6 @@ impl WasmBuilder { let profile = if profile == "debug" { "dev" } else { profile }; self.cargo.set_profile(profile.to_string()); self.cargo.set_features(&self.enabled_features()?); - if env::var("GEAR_WASM_BUILDER_PATH_REMAPPING").is_ok() { - self.cargo.set_paths_to_remap(&self.paths_to_remap()?); - } self.cargo.run()?; self.wasm_project.postprocess() @@ -199,25 +196,6 @@ impl WasmBuilder { .filter(|feature| feature != "gcli") .collect()) } - - fn paths_to_remap(&self) -> Result> { - let home_dir = dirs::home_dir().context("unable to get home directory")?; - - let project_dir = self.wasm_project.original_dir(); - - let cargo_dir = std::env::var_os("CARGO_HOME") - .map(PathBuf::from) - .context("unable to get cargo home directory")?; - - let cargo_checkouts_dir = cargo_dir.join("git").join("checkouts"); - - Ok(vec![ - (home_dir, "/home"), - (project_dir, "/code"), - (cargo_dir, "/cargo"), - (cargo_checkouts_dir, "/deps"), - ]) - } } impl Default for WasmBuilder { diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index d360b15d1cd..18955df6d07 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -160,11 +160,6 @@ impl WasmProject { self.out_dir.join("Cargo.toml") } - /// Return the path to the original project directory. - pub fn original_dir(&self) -> PathBuf { - self.original_dir.clone() - } - /// Return the path to the target directory. pub fn target_dir(&self) -> PathBuf { self.target_dir.clone() From 9858ac60d3bb2d3481fe8061c82134459b7e8101 Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Thu, 5 Sep 2024 19:51:22 +0300 Subject: [PATCH 3/3] fix import --- utils/wasm-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index 00411e0dd2e..5ad99a99757 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -26,7 +26,7 @@ use crate::wasm_project::WasmProject; use anyhow::Result; use gmeta::{Metadata, MetadataRepr}; use regex::Regex; -use std::{env, process}; +use std::{env, path::PathBuf, process}; use wasm_project::ProjectType; mod builder_error;