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

feat(wasm-builder): remap paths to hide username in debug builds #2865

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/wasm-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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
Expand Down
25 changes: 25 additions & 0 deletions utils/wasm-builder/src/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct CargoCommand {
target_dir: PathBuf,
features: Vec<String>,
toolchain: Toolchain,
paths_to_remap: Vec<(PathBuf, &'static str)>,
}

impl CargoCommand {
Expand All @@ -44,6 +45,7 @@ impl CargoCommand {
target_dir: "target".into(),
features: vec![],
toolchain: Toolchain::nightly(),
paths_to_remap: vec![],
}
}

Expand Down Expand Up @@ -74,6 +76,13 @@ impl CargoCommand {
self.toolchain = 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<()> {
let mut cargo = Command::new(&self.path);
Expand Down Expand Up @@ -101,6 +110,22 @@ 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::<Vec<_>>()
.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(),
Expand Down
28 changes: 26 additions & 2 deletions utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{cargo_command::CargoCommand, cargo_toolchain::Toolchain, wasm_project::WasmProject};
use anyhow::Result;
use anyhow::{Context, Result};
use gmeta::{Metadata, MetadataRepr};
use regex::Regex;
use std::{env, path::Path, process};
use std::{
env,
path::{Path, PathBuf},
process,
};
use wasm_project::ProjectType;

mod builder_error;
Expand Down Expand Up @@ -103,6 +107,7 @@ impl WasmBuilder {
let profile = if profile == "debug" { "dev" } else { profile };
self.cargo.set_profile(profile.to_string());
self.cargo.set_features(&self.enabled_features()?);
self.cargo.set_paths_to_remap(&self.paths_to_remap()?);

self.cargo.run()?;
self.wasm_project.postprocess()
Expand Down Expand Up @@ -159,6 +164,25 @@ impl WasmBuilder {
}
Ok(matched_features)
}

fn paths_to_remap(&self) -> Result<Vec<(PathBuf, &'static str)>> {
breathx marked this conversation as resolved.
Show resolved Hide resolved
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, "/proj"),
breathx marked this conversation as resolved.
Show resolved Hide resolved
(cargo_dir, "/cargo"),
(cargo_checkouts_dir, "/deps"),
])
}
}

impl Default for WasmBuilder {
Expand Down
5 changes: 5 additions & 0 deletions utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ 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()
Expand Down