From cf421987872303a45cee47d821f5207520ce4340 Mon Sep 17 00:00:00 2001 From: George Pollard Date: Wed, 14 Dec 2022 21:00:32 +0000 Subject: [PATCH] Use `elsa` for debuggable_module::loader --- src/agent/Cargo.lock | 45 +++++++-------- src/agent/debuggable-module/Cargo.toml | 1 + src/agent/debuggable-module/src/loader.rs | 68 ++--------------------- 3 files changed, 29 insertions(+), 85 deletions(-) diff --git a/src/agent/Cargo.lock b/src/agent/Cargo.lock index 80362e7bd08..dbd3c1f716e 100644 --- a/src/agent/Cargo.lock +++ b/src/agent/Cargo.lock @@ -784,13 +784,14 @@ version = "0.1.0" dependencies = [ "anyhow", "clap 4.0.26", + "elsa", "gimli", "goblin 0.6.0", "iced-x86", "log", "pdb 0.8.0", "regex", - "symbolic 10.1.4", + "symbolic 10.2.0", "thiserror", ] @@ -3305,14 +3306,14 @@ dependencies = [ [[package]] name = "symbolic" -version = "10.1.4" +version = "10.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b13e4f7150867965f7e7f29c4e6632fbdf1f2d028812012c930b970d1e61ac0" +checksum = "27ac8ad1ebe348393d71802e8b0f5084c51fde21ad4c29ba8f8fb4d7ad6ed671" dependencies = [ - "symbolic-common 10.1.4", - "symbolic-debuginfo 10.1.4", - "symbolic-demangle 10.1.4", - "symbolic-symcache 10.1.4", + "symbolic-common 10.2.1", + "symbolic-debuginfo 10.2.0", + "symbolic-demangle 10.2.1", + "symbolic-symcache 10.2.0", ] [[package]] @@ -3329,9 +3330,9 @@ dependencies = [ [[package]] name = "symbolic-common" -version = "10.1.4" +version = "10.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "878e1296b4dbe9f77102748d66abc25f6089a020f03a4bba8530b48221e131f3" +checksum = "1b55cdc318ede251d0957f07afe5fed912119b8c1bc5a7804151826db999e737" dependencies = [ "debugid 0.8.0", "memmap2", @@ -3371,9 +3372,9 @@ dependencies = [ [[package]] name = "symbolic-debuginfo" -version = "10.1.4" +version = "10.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f609bccfbb7a009b8e20ea388f9169716721f1e9d375b9c2cd6710c41a3a590" +checksum = "8f94766a96b5834eaf72f9cb99a5a45e63fa44f1084705b705d9d31bb6455434" dependencies = [ "bitvec 1.0.0", "dmsort", @@ -3394,7 +3395,7 @@ dependencies = [ "serde", "serde_json", "smallvec", - "symbolic-common 10.1.4", + "symbolic-common 10.2.1", "symbolic-ppdb", "thiserror", "wasmparser 0.94.0", @@ -3416,25 +3417,25 @@ dependencies = [ [[package]] name = "symbolic-demangle" -version = "10.1.4" +version = "10.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cb721346d1f73b20c50b3122f1b1b519465894b9378e47762623dc05e44b5b3" +checksum = "79be897be8a483a81fff6a3a4e195b4ac838ef73ca42d348b3f722da9902e489" dependencies = [ "cc", "cpp_demangle 0.4.0", "msvc-demangler", "rustc-demangle", - "symbolic-common 10.1.4", + "symbolic-common 10.2.1", ] [[package]] name = "symbolic-ppdb" -version = "10.1.4" +version = "10.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b929ab808f03b65d46c2f65e0b21cc24eefc5b2357f93442be6ef9170478e490" +checksum = "125fcd987182e46cd828416a9f2bdb7752c42081b33fa6d80a94afb1fdd4109b" dependencies = [ "indexmap", - "symbolic-common 10.1.4", + "symbolic-common 10.2.1", "thiserror", "uuid 1.2.1", "watto", @@ -3456,13 +3457,13 @@ dependencies = [ [[package]] name = "symbolic-symcache" -version = "10.1.4" +version = "10.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31608caf5e6797dafdcda5fe835122fb821fb3b14c0eaa472ee5979d6d2835e2" +checksum = "789369a242bacbe89d2f4f6a364f54ea5df1dae774750eb30b335550b315749a" dependencies = [ "indexmap", - "symbolic-common 10.1.4", - "symbolic-debuginfo 10.1.4", + "symbolic-common 10.2.1", + "symbolic-debuginfo 10.2.0", "thiserror", "tracing", "watto", diff --git a/src/agent/debuggable-module/Cargo.toml b/src/agent/debuggable-module/Cargo.toml index 27f72c8a314..207b8af6a94 100644 --- a/src/agent/debuggable-module/Cargo.toml +++ b/src/agent/debuggable-module/Cargo.toml @@ -6,6 +6,7 @@ license = "MIT" [dependencies] anyhow = "1.0" +elsa = "1.7.0" gimli = "0.26.2" goblin = "0.6.0" iced-x86 = "1.17" diff --git a/src/agent/debuggable-module/src/loader.rs b/src/agent/debuggable-module/src/loader.rs index 2998e95a2a4..dbbd44e2771 100644 --- a/src/agent/debuggable-module/src/loader.rs +++ b/src/agent/debuggable-module/src/loader.rs @@ -1,33 +1,13 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::collections::HashMap; -use std::sync::Mutex; - use anyhow::Result; -use thiserror::Error; use crate::path::FilePath; -#[derive(Clone, Copy)] -struct Leaked(&'static [u8]); - -impl Leaked { - pub fn into_raw(self) -> *mut [u8] { - self.0 as *const _ as *mut _ - } -} - -impl From> for Leaked { - fn from(data: Vec) -> Self { - let data = Box::leak(data.into_boxed_slice()); - Leaked(data) - } -} - #[derive(Default)] pub struct Loader { - loaded: Mutex>, + loaded: elsa::FrozenMap>, } impl Loader { @@ -36,49 +16,11 @@ impl Loader { } pub fn load(&self, path: &FilePath) -> Result<&[u8]> { - if let Some(data) = self.get(path)? { - Ok(data) - } else { - self.load_new(path) + if let Some(data) = self.loaded.get(path) { + return Ok(data); } - } - fn load_new(&self, path: &FilePath) -> Result<&[u8]> { - let mut loaded = self.loaded.lock().map_err(|_| LoaderError::PoisonedMutex)?; - let data = std::fs::read(path)?; - let leaked = Leaked::from(data); - loaded.insert(path.clone(), leaked); - - Ok(leaked.0) + let data: Box<[u8]> = std::fs::read(path)?.into(); + Ok(self.loaded.insert(path.clone(), data)) } - - pub fn get(&self, path: &FilePath) -> Result> { - let loaded = self.loaded.lock().map_err(|_| LoaderError::PoisonedMutex)?; - - let data = loaded.get(path).map(|l| l.0); - - Ok(data) - } -} - -impl Drop for Loader { - fn drop(&mut self) { - if let Ok(mut loaded) = self.loaded.lock() { - for (_, leaked) in loaded.drain() { - unsafe { - let raw = leaked.into_raw(); - let owned = Box::from_raw(raw); - drop(owned); - } - } - - debug_assert!(loaded.is_empty()); - } - } -} - -#[derive(Error, Debug)] -pub enum LoaderError { - #[error("internal mutex poisoned")] - PoisonedMutex, }