mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
Merge pull request #269 from MacosPorters/archive/pull/371/head
Validate DBT caches with executable generations and source revalidation
This commit is contained in:
4 files changed
+338
-23
No files matched your search
@@ -33,6 +33,7 @@ remains unsupported.
|
||||
- Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits.
|
||||
|
||||
### Fixed
|
||||
- DBT cache hits now compare an executable-generation token instead of rereading every instruction byte. Address-space or executable-memory changes trigger byte and execute-permission revalidation before a translation can be reused; changed code is retranslated. Ordinary data writes retain the fast path. Tests cover cloned spaces, loader bypass writes, split writes, remapping and bounded cache exhaustion. Same-dispatch self-modification and discarded-code reclamation remain unsupported.
|
||||
- Gate A2 checks the cross-compiler driver before invoking it, then checks its helpers before fixture compilation. CI also verifies the driver architecture before requesting its version.
|
||||
- Non-canonical 64-bit guest data addresses now raise `#GP` instead of a memory fault in both the interpreter and native DBT, including accesses that cross the canonical-address boundary. The synthetic architectural test is enabled again.
|
||||
- An instruction longer than 15 bytes now stops both engines with a general-protection fault instead of an illegal-instruction exit (#384; Intel SDM Vol. 3A, 7.15). The decoder still stops before fetching a 16th byte, and the DBT translates the faulting instruction on its own so the instructions before it in a block complete first. The `defect_overlong_instruction` known answer from #155 is now verified, with three more cases.
|
||||
|
||||
+278
-17
@@ -15,7 +15,7 @@ use crate::memory::GuestMemory;
|
||||
use crate::x86_decode::{Encoding, Insn, MemOperand, RegOperand, RmOperand, Segment, decode};
|
||||
use crate::x86_exec;
|
||||
use crate::x86_table::{FORMS, Op};
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
const MAX_BLOCK_INSTRUCTIONS: usize = 16;
|
||||
|
||||
@@ -29,6 +29,9 @@ pub struct DbtMetrics {
|
||||
pub blocks_compiled: usize,
|
||||
pub cache_hits: usize,
|
||||
pub cache_misses: usize,
|
||||
/// Cached blocks discarded because their guest bytes or executable mappings
|
||||
/// changed. Each also counts as a cache miss.
|
||||
pub blocks_invalidated: usize,
|
||||
pub native_bytes_emitted: usize,
|
||||
pub dbt_dispatches: usize,
|
||||
pub instructions_executed: usize,
|
||||
@@ -37,11 +40,42 @@ pub struct DbtMetrics {
|
||||
pub service_calls: usize,
|
||||
}
|
||||
|
||||
/// A translated block and its executable-memory generation.
|
||||
///
|
||||
/// Guest stores into executable memory (Intel SDM Vol. 3A 11.6), executable
|
||||
/// mapping changes and another address space invalidate the retained token.
|
||||
/// An unchanged token takes a constant-time check. After a generation or address
|
||||
/// space change, bytes and execute permissions are revalidated once before an
|
||||
/// identical translation may be reused in that generation.
|
||||
/// Validation occurs at dispatch boundaries: a store into the currently running
|
||||
/// block's later instructions still needs a separate dispatch-boundary design.
|
||||
struct CachedBlock {
|
||||
code: *const u8,
|
||||
generation: Arc<()>,
|
||||
start: u64,
|
||||
source: Box<[u8]>,
|
||||
}
|
||||
|
||||
impl CachedBlock {
|
||||
fn is_current(&mut self, memory: &GuestMemory) -> bool {
|
||||
if memory.code_is_current(&self.generation) {
|
||||
return true;
|
||||
}
|
||||
if !self.source.iter().enumerate().all(|(offset, &byte)| {
|
||||
matches!(memory.fetch_instruction_byte(self.start.wrapping_add(offset as u64)), Ok(current) if current == byte)
|
||||
}) {
|
||||
return false;
|
||||
}
|
||||
self.generation = memory.code_generation();
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// Dynamic Binary Translator and native execution runtime.
|
||||
pub struct DbtEngine {
|
||||
jit: JitMemory,
|
||||
block_cache: HashMap<u64, *const u8>,
|
||||
limited_block_cache: HashMap<(u64, usize), *const u8>,
|
||||
block_cache: HashMap<u64, CachedBlock>,
|
||||
limited_block_cache: HashMap<(u64, usize), CachedBlock>,
|
||||
assembler: Arm64Assembler,
|
||||
/// Decoded instructions that emitted helper calls point into.
|
||||
decoded_blocks: Vec<Box<[Insn]>>,
|
||||
@@ -245,26 +279,34 @@ impl DbtEngine {
|
||||
block_limit: usize,
|
||||
) -> Result<*const u8, ExitReason> {
|
||||
if block_limit == MAX_BLOCK_INSTRUCTIONS {
|
||||
if let Some(&fn_ptr) = self.block_cache.get(&rip) {
|
||||
self.metrics.cache_hits += 1;
|
||||
return Ok(fn_ptr);
|
||||
if let Some(cached) = self.block_cache.get_mut(&rip) {
|
||||
if cached.is_current(memory) {
|
||||
self.metrics.cache_hits += 1;
|
||||
return Ok(cached.code);
|
||||
}
|
||||
self.metrics.blocks_invalidated += 1;
|
||||
}
|
||||
self.metrics.cache_misses += 1;
|
||||
let compiled_ptr = self.compile_block(rip, memory, block_limit)?;
|
||||
self.block_cache.insert(rip, compiled_ptr);
|
||||
return Ok(compiled_ptr);
|
||||
let compiled = self.compile_block(rip, memory, block_limit)?;
|
||||
let code = compiled.code;
|
||||
self.block_cache.insert(rip, compiled);
|
||||
return Ok(code);
|
||||
}
|
||||
|
||||
// Keep budget-limited blocks separate so they cannot shorten a later run.
|
||||
let key = (rip, block_limit);
|
||||
if let Some(&fn_ptr) = self.limited_block_cache.get(&key) {
|
||||
self.metrics.cache_hits += 1;
|
||||
return Ok(fn_ptr);
|
||||
if let Some(cached) = self.limited_block_cache.get_mut(&key) {
|
||||
if cached.is_current(memory) {
|
||||
self.metrics.cache_hits += 1;
|
||||
return Ok(cached.code);
|
||||
}
|
||||
self.metrics.blocks_invalidated += 1;
|
||||
}
|
||||
self.metrics.cache_misses += 1;
|
||||
let compiled_ptr = self.compile_block(rip, memory, block_limit)?;
|
||||
self.limited_block_cache.insert(key, compiled_ptr);
|
||||
Ok(compiled_ptr)
|
||||
let compiled = self.compile_block(rip, memory, block_limit)?;
|
||||
let code = compiled.code;
|
||||
self.limited_block_cache.insert(key, compiled);
|
||||
Ok(code)
|
||||
}
|
||||
|
||||
fn compile_block(
|
||||
@@ -272,7 +314,7 @@ impl DbtEngine {
|
||||
start_rip: u64,
|
||||
memory: &GuestMemory,
|
||||
block_limit: usize,
|
||||
) -> Result<*const u8, ExitReason> {
|
||||
) -> Result<CachedBlock, ExitReason> {
|
||||
// Decode the whole block first: lowering needs to know whether a later
|
||||
// instruction observes the flags an earlier one produces.
|
||||
let mut decoded = Vec::with_capacity(block_limit);
|
||||
@@ -351,8 +393,17 @@ impl DbtEngine {
|
||||
.map_err(|_| ExitReason::JitCacheExhausted)?;
|
||||
self.metrics.blocks_compiled += 1;
|
||||
self.metrics.native_bytes_emitted += asm.instructions().len() * 4;
|
||||
let source = (0..rip.wrapping_sub(start_rip))
|
||||
.map(|offset| memory.fetch_instruction_byte(start_rip.wrapping_add(offset)))
|
||||
.collect::<Result<Box<[u8]>, _>>()
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
self.decoded_blocks.push(insns);
|
||||
Ok(emitted)
|
||||
Ok(CachedBlock {
|
||||
code: emitted,
|
||||
generation: memory.code_generation(),
|
||||
start: start_rip,
|
||||
source,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1569,6 +1620,216 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
/// `mov eax, imm32; hlt`, with the immediate at `ENTRY + 1`.
|
||||
fn mov_eax(imm: u8) -> [u8; 6] {
|
||||
[0xB8, imm, 0x00, 0x00, 0x00, 0xF4]
|
||||
}
|
||||
|
||||
fn writable_fixture(code: &[u8]) -> (DbtEngine, GuestMemory) {
|
||||
let mut memory = GuestMemory::new();
|
||||
memory
|
||||
.map_region_with_data(
|
||||
ENTRY,
|
||||
code.len(),
|
||||
MemoryPermissions::READ_WRITE_EXECUTE,
|
||||
code,
|
||||
)
|
||||
.unwrap();
|
||||
(DbtEngine::new(64 * 1024).unwrap(), memory)
|
||||
}
|
||||
|
||||
fn rax_after_run(engine: &mut DbtEngine, memory: &mut GuestMemory, limit: usize) -> u64 {
|
||||
let mut cpu = CpuState::new(ENTRY, 0);
|
||||
let result = engine.run(&mut cpu, memory, limit);
|
||||
assert_eq!(result.exit_reason, ExitReason::Halt);
|
||||
cpu.get(Register::Rax)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unchanged_code_stays_cached() {
|
||||
let (mut engine, mut memory) = writable_fixture(&mov_eax(1));
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
assert_eq!(engine.metrics.blocks_compiled, 1);
|
||||
assert_eq!(engine.metrics.cache_hits, 1);
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_writes_and_allocations_do_not_invalidate_executable_code() {
|
||||
let (mut engine, mut memory) = writable_fixture(&mov_eax(1));
|
||||
memory
|
||||
.map_region(ENTRY + 0x1000, 8, MemoryPermissions::READ_WRITE)
|
||||
.unwrap();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
for value in 0..64 {
|
||||
memory.write_u64(ENTRY + 0x1000, value).unwrap();
|
||||
let allocation = memory
|
||||
.map_virtual_allocation(0, 4096, MemoryPermissions::READ_WRITE)
|
||||
.unwrap();
|
||||
assert!(memory.release_virtual_allocation(allocation));
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
}
|
||||
assert_eq!(engine.metrics.blocks_compiled, 1);
|
||||
assert_eq!(engine.metrics.cache_hits, 64);
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cloned_address_spaces_diverge_when_executable_code_changes() {
|
||||
let (mut engine, mut memory) = writable_fixture(&mov_eax(1));
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
let mut cloned = memory.clone();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut cloned, 16), 1);
|
||||
memory.write_u8(ENTRY + 1, 7).unwrap();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut cloned, 16), 1);
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 7);
|
||||
// A deep clone shares the proven identical executable snapshot. The
|
||||
// write in the original space must still force retranslation there.
|
||||
assert_eq!(engine.metrics.blocks_compiled, 2);
|
||||
assert_eq!(engine.metrics.cache_hits, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn executable_bypass_writes_invalidate_cached_translations() {
|
||||
let mut memory = GuestMemory::new();
|
||||
memory
|
||||
.map_region_with_data(
|
||||
ENTRY,
|
||||
8,
|
||||
MemoryPermissions::READ_EXECUTE,
|
||||
&[0xB8, 1, 0, 0, 0, 0xF4, 0, 0],
|
||||
)
|
||||
.unwrap();
|
||||
let mut engine = DbtEngine::new(64 * 1024).unwrap();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
assert!(memory.write_u32(ENTRY + 1, 9).is_err());
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 0);
|
||||
memory
|
||||
.write_u64_bypass(ENTRY, u64::from_le_bytes([0xB8, 2, 0, 0, 0, 0xF4, 0, 0]))
|
||||
.unwrap();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 2);
|
||||
memory.write_u32_bypass(ENTRY + 1, 3).unwrap();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 3);
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn executable_writes_crossing_regions_invalidate_cached_translations() {
|
||||
let mut memory = GuestMemory::new();
|
||||
memory
|
||||
.map_region_with_data(ENTRY, 2, MemoryPermissions::READ_WRITE_EXECUTE, &[0xB8, 1])
|
||||
.unwrap();
|
||||
memory
|
||||
.map_region_with_data(
|
||||
ENTRY + 2,
|
||||
4,
|
||||
MemoryPermissions::READ_WRITE_EXECUTE,
|
||||
&[0, 0, 0, 0xF4],
|
||||
)
|
||||
.unwrap();
|
||||
let mut engine = DbtEngine::new(64 * 1024).unwrap();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
memory.write_u32(ENTRY + 1, 9).unwrap();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 9);
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remapped_non_executable_memory_cannot_reuse_a_translation() {
|
||||
let mut memory = GuestMemory::new();
|
||||
let base = memory
|
||||
.map_virtual_allocation(0, 4096, MemoryPermissions::READ_WRITE_EXECUTE)
|
||||
.unwrap();
|
||||
memory.write_bytes(base, &mov_eax(1)).unwrap();
|
||||
let mut engine = DbtEngine::new(64 * 1024).unwrap();
|
||||
let mut cpu = CpuState::new(base, 0);
|
||||
assert_eq!(
|
||||
engine.run(&mut cpu, &mut memory, 16).exit_reason,
|
||||
ExitReason::Halt
|
||||
);
|
||||
assert!(memory.release_virtual_allocation(base));
|
||||
assert_eq!(
|
||||
memory.map_virtual_allocation(base, 4096, MemoryPermissions::READ_WRITE),
|
||||
Ok(base)
|
||||
);
|
||||
memory.write_bytes(base, &mov_eax(1)).unwrap();
|
||||
let mut cpu = CpuState::new(base, 0);
|
||||
assert!(matches!(
|
||||
engine.run(&mut cpu, &mut memory, 16).exit_reason,
|
||||
ExitReason::MemoryFault(_)
|
||||
));
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_code_rewrites_report_bounded_jit_cache_exhaustion() {
|
||||
let (mut engine, mut memory) = writable_fixture(&mov_eax(1));
|
||||
for value in 0..16_384u32 {
|
||||
memory.write_u32(ENTRY + 1, value).unwrap();
|
||||
let mut cpu = CpuState::new(ENTRY, 0);
|
||||
let result = engine.run(&mut cpu, &mut memory, 16);
|
||||
if result.exit_reason == ExitReason::JitCacheExhausted {
|
||||
return;
|
||||
}
|
||||
assert_eq!(result.exit_reason, ExitReason::Halt);
|
||||
assert_eq!(cpu.get(Register::Rax), u64::from(value));
|
||||
}
|
||||
panic!("rewritten blocks unexpectedly avoided the bounded JIT capacity");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewritten_code_is_translated_again() {
|
||||
let (mut engine, mut memory) = writable_fixture(&mov_eax(1));
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
memory.write_bytes(ENTRY + 1, &[2]).unwrap();
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 2);
|
||||
assert_eq!(engine.metrics.blocks_compiled, 2);
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 1);
|
||||
// The new translation is cached in turn.
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 2);
|
||||
assert_eq!(engine.metrics.blocks_compiled, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewriting_the_last_byte_of_a_block_is_detected() {
|
||||
let (mut engine, mut memory) = writable_fixture(&mov_eax(1));
|
||||
assert_eq!(rax_after_run(&mut engine, &mut memory, 16), 1);
|
||||
// HLT becomes NOP, so the block now runs off the end of the mapping.
|
||||
memory.write_bytes(ENTRY + 5, &[0x90]).unwrap();
|
||||
let mut cpu = CpuState::new(ENTRY, 0);
|
||||
let result = engine.run(&mut cpu, &mut memory, 16);
|
||||
assert!(matches!(result.exit_reason, ExitReason::MemoryFault(_)));
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn budget_limited_block_is_checked_against_current_bytes() {
|
||||
let (mut engine, mut memory) = writable_fixture(&mov_eax(1));
|
||||
for expected in [1, 3] {
|
||||
let mut cpu = CpuState::new(ENTRY, 0);
|
||||
let result = engine.run(&mut cpu, &mut memory, 1);
|
||||
assert_eq!(result.exit_reason, ExitReason::StepLimitExceeded);
|
||||
assert_eq!(cpu.get(Register::Rax), expected);
|
||||
memory.write_bytes(ENTRY + 1, &[3]).unwrap();
|
||||
}
|
||||
assert_eq!(engine.metrics.blocks_invalidated, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn another_address_space_does_not_reuse_a_block() {
|
||||
let (mut engine, mut first) = writable_fixture(&mov_eax(1));
|
||||
let (_, mut second) = writable_fixture(&mov_eax(7));
|
||||
assert_eq!(rax_after_run(&mut engine, &mut first, 16), 1);
|
||||
assert_eq!(rax_after_run(&mut engine, &mut second, 16), 7);
|
||||
|
||||
let mut unmapped = GuestMemory::new();
|
||||
let mut cpu = CpuState::new(ENTRY, 0);
|
||||
let result = engine.run(&mut cpu, &mut unmapped, 16);
|
||||
assert!(matches!(result.exit_reason, ExitReason::MemoryFault(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cached_block_obeys_remaining_budget() {
|
||||
let code = [
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
//! Provides isolated, page-aligned guest virtual memory with permission checking
|
||||
//! (Read, Write, Execute) and bounds enforcement without host OS mapping tricks.
|
||||
|
||||
use std::{fmt, ops::Range};
|
||||
use std::{fmt, ops::Range, sync::Arc};
|
||||
|
||||
pub(crate) const ALLOCATION_GRANULARITY: u64 = 65_536;
|
||||
|
||||
@@ -119,8 +119,13 @@ impl MappedRegion {
|
||||
}
|
||||
|
||||
/// Guest virtual memory address space.
|
||||
#[derive(Default, Clone)]
|
||||
#[derive(Default)]
|
||||
pub struct GuestMemory {
|
||||
// A retained token identifies a validated executable snapshot. Independent
|
||||
// address spaces get distinct tokens. A deep clone may share its identical
|
||||
// snapshot; an executable mutation detaches only the changed address space.
|
||||
// Cached translations retain tokens, preventing allocator-address reuse.
|
||||
executable_generation: Arc<()>,
|
||||
regions: Vec<MappedRegion>,
|
||||
private_heaps: std::collections::BTreeSet<u64>,
|
||||
private_heap_serial: u64,
|
||||
@@ -130,7 +135,32 @@ pub struct GuestMemory {
|
||||
std::collections::BTreeMap<(u64, u64), crate::imports::CallbackFrame>,
|
||||
}
|
||||
|
||||
impl Clone for GuestMemory {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
executable_generation: Arc::clone(&self.executable_generation),
|
||||
regions: self.regions.clone(),
|
||||
private_heaps: self.private_heaps.clone(),
|
||||
private_heap_serial: self.private_heap_serial,
|
||||
crt_errno: self.crt_errno.clone(),
|
||||
callback_frames: self.callback_frames.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GuestMemory {
|
||||
pub(crate) fn code_generation(&self) -> Arc<()> {
|
||||
Arc::clone(&self.executable_generation)
|
||||
}
|
||||
|
||||
pub(crate) fn code_is_current(&self, generation: &Arc<()>) -> bool {
|
||||
Arc::ptr_eq(&self.executable_generation, generation)
|
||||
}
|
||||
|
||||
fn invalidate_executable_code(&mut self) {
|
||||
self.executable_generation = Arc::new(());
|
||||
}
|
||||
|
||||
/// Create a new empty guest address space.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
@@ -197,6 +227,9 @@ impl GuestMemory {
|
||||
allocation,
|
||||
});
|
||||
self.regions.sort_by_key(|r| r.base);
|
||||
if permissions.execute && size != 0 {
|
||||
self.invalidate_executable_code();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -256,8 +289,14 @@ impl GuestMemory {
|
||||
if !self.private_heaps.remove(&heap) {
|
||||
return false;
|
||||
}
|
||||
let executable = self.regions.iter().any(|region| {
|
||||
region.allocation == AllocationKind::Heap(heap) && region.permissions.execute
|
||||
});
|
||||
self.regions
|
||||
.retain(|region| region.allocation != AllocationKind::Heap(heap));
|
||||
if executable {
|
||||
self.invalidate_executable_code();
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -377,10 +416,14 @@ impl GuestMemory {
|
||||
if size < previous {
|
||||
region.data.shrink_to_fit();
|
||||
}
|
||||
let executable = region.permissions.execute;
|
||||
if destination != base {
|
||||
region.base = destination;
|
||||
self.regions.sort_by_key(|region| region.base);
|
||||
}
|
||||
if executable && (size != previous || destination != base) {
|
||||
self.invalidate_executable_code();
|
||||
}
|
||||
Ok(destination)
|
||||
}
|
||||
|
||||
@@ -420,7 +463,10 @@ impl GuestMemory {
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
self.regions.remove(index);
|
||||
let removed = self.regions.remove(index);
|
||||
if removed.permissions.execute && !removed.data.is_empty() {
|
||||
self.invalidate_executable_code();
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -510,11 +556,16 @@ impl GuestMemory {
|
||||
required: Option<&'static str>,
|
||||
) -> Result<(), MemoryError> {
|
||||
let mut done = 0;
|
||||
let mut executable = false;
|
||||
for (index, offset, take) in self.resolve(address, src.len(), required)? {
|
||||
executable |= take != 0 && self.regions[index].permissions.execute;
|
||||
self.regions[index].data[offset..offset + take]
|
||||
.copy_from_slice(&src[done..done + take]);
|
||||
done += take;
|
||||
}
|
||||
if executable {
|
||||
self.invalidate_executable_code();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -524,6 +575,9 @@ impl GuestMemory {
|
||||
&& self.regions[index].permissions.write
|
||||
{
|
||||
self.regions[index].data[range].copy_from_slice(src);
|
||||
if !src.is_empty() && self.regions[index].permissions.execute {
|
||||
self.invalidate_executable_code();
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
self.write_checked(address, src, Some("write"))
|
||||
|
||||
@@ -5723,10 +5723,9 @@ fn interpreter_executes_modified_code_after_a_jump() {
|
||||
assert_eq!(self_modified_result(Engine::Interpreter), wanted);
|
||||
}
|
||||
|
||||
/// Known defect: the DBT keeps running the block it translated before the store.
|
||||
/// The DBT must not keep running the block it translated before the store (#84).
|
||||
#[test]
|
||||
#[ignore = "known defect (#155): the DBT keeps translated blocks after the guest rewrites their code"]
|
||||
fn defect_dbt_executes_modified_code_after_a_jump() {
|
||||
fn dbt_executes_modified_code_after_a_jump() {
|
||||
for engine in [Engine::Dbt, Engine::DbtStepped] {
|
||||
let wanted = (1, 2, ExitReason::Halt);
|
||||
assert_eq!(self_modified_result(engine), wanted, "{engine:?}");
|
||||
|
||||
Reference in new issue
Block a user