From 73c0df18e22297f61342a0ee65bc565cabe96a86 Mon Sep 17 00:00:00 2001 From: mohanson Date: Fri, 17 May 2024 13:49:14 +0800 Subject: [PATCH] Set an executable page at the top of the stack --- Cargo.toml | 1 + src/machine/mod.rs | 10 ++++++++++ tests/test_asm.rs | 1 + tests/test_dy_memory.rs | 1 + tests/test_resume.rs | 1 + tests/test_resume2.rs | 1 + 6 files changed, 15 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index f1eea408..6046d70d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ enable-chaos-mode-by-default = ["ckb-vm-definitions/enable-chaos-mode-by-default # Disable slow tests to run miri on CI miri-ci = [] pprof = [] +heap-stack-overlap-detect = [] [dependencies] byteorder = "1" diff --git a/src/machine/mod.rs b/src/machine/mod.rs index be231177..5bf3e459 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -175,6 +175,16 @@ pub trait SupportMachine: CoreMachine { stack_start: u64, stack_size: u64, ) -> Result { + #[cfg(feature = "heap-stack-overlap-detect")] + { + use super::memory::FLAG_EXECUTABLE; + use super::RISCV_PAGESIZE; + // When the heap or stack attempts to write data to this area, + // ckb-vm will return an error due to wxorx rules. + self.memory_mut() + .set_flag(stack_start / RISCV_PAGESIZE as u64, FLAG_EXECUTABLE)?; + } + // When we re-ordered the sections of a program, writing data in high memory // will cause unnecessary changes. At the same time, for ckb, argc is always 0 // and the memory is initialized to 0, so memory writing can be safely skipped. diff --git a/tests/test_asm.rs b/tests/test_asm.rs index 1fe93313..2ca5a28e 100644 --- a/tests/test_asm.rs +++ b/tests/test_asm.rs @@ -266,6 +266,7 @@ pub fn test_asm_wxorx_crash_64() { ); } +#[cfg(not(feature = "heap-stack-overlap-detect"))] #[test] pub fn test_asm_alloc_many() { let buffer = fs::read("tests/programs/alloc_many").unwrap().into(); diff --git a/tests/test_dy_memory.rs b/tests/test_dy_memory.rs index d5b8c6d4..49372528 100644 --- a/tests/test_dy_memory.rs +++ b/tests/test_dy_memory.rs @@ -42,6 +42,7 @@ fn run_memory_suc(memory_size: usize, bin_path: String, bin_name: String) { } } +#[cfg(not(feature = "heap-stack-overlap-detect"))] #[test] fn test_dy_memory() { run_memory_suc( diff --git a/tests/test_resume.rs b/tests/test_resume.rs index f0200195..6efc2fde 100644 --- a/tests/test_resume.rs +++ b/tests/test_resume.rs @@ -1,4 +1,5 @@ #![cfg(has_asm)] +#![cfg(not(feature = "heap-stack-overlap-detect"))] pub mod machine_build; use bytes::Bytes; use ckb_vm::cost_model::constant_cycles; diff --git a/tests/test_resume2.rs b/tests/test_resume2.rs index d3360f2f..3ded6bda 100644 --- a/tests/test_resume2.rs +++ b/tests/test_resume2.rs @@ -1,4 +1,5 @@ #![cfg(has_asm)] +#![cfg(not(feature = "heap-stack-overlap-detect"))] pub mod machine_build; use bytes::Bytes; use ckb_vm::cost_model::constant_cycles;