From 2d33ba9ac80fac2ed25291b412cda09a205b79d1 Mon Sep 17 00:00:00 2001 From: Martin Hughes Date: Wed, 24 Jun 2026 15:57:50 +0100 Subject: [PATCH] Keep a copy of block stream instead of raw pointer This mitigates the possibility of the pointed-to code stream being dropped, which would result in a use-after-free. Fixes #300. The use of a Vec isn't the most memory or CPU efficient. I tried using borrows but it was leading to a horrible mess of lifetime specifiers and there were still a few areas of code I couldn't figure out how to make compile... --- src/aml/mod.rs | 10 +++++----- tests/uacpi_examples.rs | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/aml/mod.rs b/src/aml/mod.rs index 65707db8..d489e6b0 100644 --- a/src/aml/mod.rs +++ b/src/aml/mod.rs @@ -2778,14 +2778,14 @@ struct MethodContext { } struct Block { - stream: *const [u8], + stream: Vec, pc: usize, kind: BlockKind, } impl Block { fn stream(&self) -> &[u8] { - unsafe { &*self.stream } + &self.stream } } @@ -2911,7 +2911,7 @@ impl OpInFlight { impl MethodContext { unsafe fn new_from_table(stream: &[u8]) -> MethodContext { - let block = Block { stream: stream as *const [u8], pc: 0, kind: BlockKind::Table }; + let block = Block { stream: Vec::from(stream), pc: 0, kind: BlockKind::Table }; MethodContext { current_block: block, block_stack: Vec::new(), @@ -2933,7 +2933,7 @@ impl MethodContext { return Err(AmlError::MethodArgCountIncorrect); } let block = Block { - stream: code as &[u8] as *const [u8], + stream: code.clone(), pc: 0, kind: BlockKind::Method { method_scope: scope.clone() }, }; @@ -2981,7 +2981,7 @@ impl MethodContext { fn start_new_block(&mut self, kind: BlockKind, length: usize) { let block = Block { - stream: &self.current_block.stream()[..(self.current_block.pc + length)] as *const [u8], + stream: self.current_block.stream()[..(self.current_block.pc + length)].into(), pc: self.current_block.pc, kind, }; diff --git a/tests/uacpi_examples.rs b/tests/uacpi_examples.rs index 4d76d669..d5050aa0 100644 --- a/tests/uacpi_examples.rs +++ b/tests/uacpi_examples.rs @@ -181,7 +181,6 @@ DefinitionBlock("", "DSDT", 1, "RSACPI", "UACPI", 1) { } #[test] -#[ignore] // See issue #300 fn copy_object_to_self() { const ASL: &str = r#" DefinitionBlock("", "DSDT", 1, "RSACPI", "UACPI", 1) {