Skip to content

Commit

Permalink
fix:fix fmt and change heap start
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <281165273grape@gmail.com>
  • Loading branch information
GrapeBaBa committed Jun 19, 2024
1 parent 8eb2b66 commit 7b7949c
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 47 deletions.
2 changes: 1 addition & 1 deletion crates/mipsevm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ proptest = "1.4.0"

[features]
tracing = ["dep:tracing"]
#simd-keccak = ["dep:keccak256-aarch64-simd"]
simd-keccak = ["dep:keccak256-aarch64-simd"]

[[bench]]
name = "memory"
Expand Down
23 changes: 12 additions & 11 deletions crates/mipsevm/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Memory {
}

// Find the page and invalidate the address within it.
match self.page_lookup(address as u64 >> page::PAGE_ADDRESS_SIZE) {
match self.page_lookup(address >> page::PAGE_ADDRESS_SIZE) {
Some(page) => {
let mut page = page.borrow_mut();
let prev_valid = !page.valid[1];
Expand All @@ -85,7 +85,8 @@ impl Memory {
}

// Find the generalized index of the first page covering the address
let mut g_index = (1u64 << (64 - page::PAGE_ADDRESS_SIZE)) | (address >> page::PAGE_ADDRESS_SIZE);
let mut g_index =
(1u64 << (64 - page::PAGE_ADDRESS_SIZE)) | (address >> page::PAGE_ADDRESS_SIZE);
// Invalidate all nodes in the branch
while g_index > 0 {
self.nodes.insert(g_index, None);
Expand Down Expand Up @@ -277,7 +278,7 @@ impl Memory {
anyhow::bail!("Unaligned memory access: {:x}", address);
}

match self.page_lookup(address as u64 >> page::PAGE_ADDRESS_SIZE as u64) {
match self.page_lookup(address >> page::PAGE_ADDRESS_SIZE as u64) {
Some(page) => {
let page_address = address as usize & page::PAGE_ADDRESS_MASK;
Ok(u32::from_be_bytes(
Expand Down Expand Up @@ -344,7 +345,7 @@ impl Memory {
anyhow::bail!("Unaligned memory access: {:x}", address);
}

match self.page_lookup(address as u64 >> page::PAGE_ADDRESS_SIZE as u64) {
match self.page_lookup(address >> page::PAGE_ADDRESS_SIZE as u64) {
Some(page) => {
let page_address = address as usize & page::PAGE_ADDRESS_MASK;
Ok(u64::from_be_bytes(
Expand Down Expand Up @@ -452,8 +453,8 @@ impl Default for PageEntry {

impl Serialize for Memory {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
where
S: serde::Serializer,
{
let mut page_entries: Vec<PageEntry> = self
.pages
Expand All @@ -471,8 +472,8 @@ impl Serialize for Memory {

impl<'de> Deserialize<'de> for Memory {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
where
D: serde::Deserializer<'de>,
{
let page_entries: Vec<PageEntry> = Vec::deserialize(deserializer)?;

Expand Down Expand Up @@ -525,7 +526,7 @@ impl<'a> Read for MemoryReader<'a> {
let start = self.address as usize & page::PAGE_ADDRESS_MASK;
let mut end = page::PAGE_SIZE;

if page_index == (end_address as u64 >> page::PAGE_ADDRESS_SIZE as u64) {
if page_index == (end_address >> page::PAGE_ADDRESS_SIZE as u64) {
end = end_address as usize & page::PAGE_ADDRESS_MASK;
}
let n = end - start;
Expand Down Expand Up @@ -664,12 +665,12 @@ mod test {
keccak_concat_hashes(z.into(), z.into()).into(),
keccak_concat_hashes(z.into(), p3.into()).into(),
)
.into(),
.into(),
keccak_concat_hashes(
keccak_concat_hashes(z.into(), p5.into()).into(),
keccak_concat_hashes(p6.into(), z.into()).into(),
)
.into(),
.into(),
);
let r2 = memory
.merkleize_subtree(1 << (page::PAGE_KEY_SIZE - 3))
Expand Down
10 changes: 5 additions & 5 deletions crates/mipsevm/src/mips/instrumented.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! This module contains the [InstrumentedState] definition.

use crate::memory::PROOF_LEN;
use crate::{traits::PreimageOracle, Address, State, StepWitness};
use anyhow::Result;
use std::io::{BufWriter, Write};
use crate::memory::PROOF_LEN;

pub(crate) const MIPS_EBADF: u64 = 0x9;
pub(crate) const MIPS_EINVAL: u64 = 0x16;
Expand Down Expand Up @@ -39,10 +39,10 @@ pub struct InstrumentedState<O: Write, E: Write, P: PreimageOracle> {
}

impl<O, E, P> InstrumentedState<O, E, P>
where
O: Write,
E: Write,
P: PreimageOracle,
where
O: Write,
E: Write,
P: PreimageOracle,
{
pub fn new(state: State, oracle: P, std_out: O, std_err: E) -> Self {
Self {
Expand Down
36 changes: 25 additions & 11 deletions crates/mipsevm/src/mips/mips_vm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! This module contains the MIPS VM implementation for the [InstrumentedState].

use crate::utils::sign_extend;
use crate::{
memory::MemoryReader,
mips::instrumented::{MIPS_EBADF, MIPS_EINVAL},
Expand All @@ -9,13 +10,12 @@ use crate::{
};
use anyhow::Result;
use std::io::{self, BufReader, Read, Write};
use crate::utils::sign_extend;

impl<O, E, P> InstrumentedState<O, E, P>
where
O: Write,
E: Write,
P: PreimageOracle,
where
O: Write,
E: Write,
P: PreimageOracle,
{
/// Read the preimage for the given key and offset from the [PreimageOracle] server.
///
Expand Down Expand Up @@ -92,7 +92,8 @@ impl<O, E, P> InstrumentedState<O, E, P>
let link_reg = if opcode == 3 { 31 } else { 0 };
// Take the top 4 bits of the next PC (its 256MB region), and concatenate with the
// 26-bit offset
let target = self.state.next_pc & 0xFFFFFFFFF0000000 | (((instruction & 0x03FFFFFF) << 2) as u64);
let target = self.state.next_pc & 0xFFFFFFFFF0000000
| (((instruction & 0x03FFFFFF) << 2) as u64);
return self.handle_jump(link_reg, target);
}

Expand Down Expand Up @@ -322,7 +323,10 @@ impl<O, E, P> InstrumentedState<O, E, P>
let effective_address = a1 & 0xFFFFFFFFFFFFFFFC;
self.track_mem_access(effective_address as Address)?;

let memory = self.state.memory.get_memory_b4(effective_address as Address)?;
let memory = self
.state
.memory
.get_memory_b4(effective_address as Address)?;
let mut key = self.state.preimage_key;
let alignment = a1 & 0x3;
let space = 4 - alignment;
Expand Down Expand Up @@ -435,7 +439,8 @@ impl<O, E, P> InstrumentedState<O, E, P>
self.state.pc = self.state.next_pc;

if should_branch {
self.state.next_pc = prev_pc + 4 + (sign_extend((instruction & 0xFFFF) as u64, 16) << 2);
self.state.next_pc =
prev_pc + 4 + (sign_extend((instruction & 0xFFFF) as u64, 16) << 2);
} else {
// Branch not taken; proceed as normal.
self.state.next_pc += 4;
Expand Down Expand Up @@ -605,9 +610,15 @@ impl<O, E, P> InstrumentedState<O, E, P>

match fun {
// sll
0 => Ok(sign_extend((rt & 0xFFFFFFFF) << ((instruction >> 6) & 0x1F), 32)),
0 => Ok(sign_extend(
(rt & 0xFFFFFFFF) << ((instruction >> 6) & 0x1F),
32,
)),
// srl
2 => Ok(sign_extend((rt & 0xFFFFFFFF) >> ((instruction >> 6) & 0x1F), 32)),
2 => Ok(sign_extend(
(rt & 0xFFFFFFFF) >> ((instruction >> 6) & 0x1F),
32,
)),
// sra
3 => {
let shamt = (instruction >> 6) & 0x1F;
Expand Down Expand Up @@ -689,7 +700,10 @@ impl<O, E, P> InstrumentedState<O, E, P>
Ok(sign_extend((rt & !mask) | val, 32))
}
// lw / ll
0x23 | 0x30 => Ok(sign_extend((mem >> (32 - ((rs & 0x4) << 3))) & 0xFFFFFFFF, 32)),
0x23 | 0x30 => Ok(sign_extend(
(mem >> (32 - ((rs & 0x4) << 3))) & 0xFFFFFFFF,
32,
)),
// lbu
0x24 => Ok((mem >> (56 - ((rs & 0x7) << 3))) & 0xFF),
// lhu
Expand Down
34 changes: 17 additions & 17 deletions crates/mipsevm/src/patch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! This module contains utilities for loading ELF files into [State] objects.

use crate::utils::sign_extend;
use crate::{page, Address, State};
use anyhow::Result;
use elf::{abi::PT_LOAD, endian::AnyEndian, ElfBytes};
use std::io::{self, Cursor, Read};
use crate::utils::sign_extend;

/// Symbols that indicate there is a patch to be made on an ELF file that was compiled from Go.
pub(crate) const GO_SYMBOLS: [&str; 14] = [
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn load_elf(raw: &[u8]) -> Result<State> {
let mut state = State {
pc: elf.ehdr.e_entry,
next_pc: elf.ehdr.e_entry + 4,
heap: 2000000000000000u64,
heap: 0x20000000u64,
..Default::default()
};

Expand Down Expand Up @@ -79,7 +79,7 @@ pub fn load_elf(raw: &[u8]) -> Result<State> {
}
}

if header.p_vaddr + header.p_memsz >= 1 << 63 {
if header.p_vaddr + header.p_memsz >= 1 << 47 {
anyhow::bail!(
"Program segment {} out of 64-bit mem range: {} - {} (size: {})",
i,
Expand All @@ -89,9 +89,7 @@ pub fn load_elf(raw: &[u8]) -> Result<State> {
);
}

state
.memory
.set_memory_range(header.p_vaddr, reader)?;
state.memory.set_memory_range(header.p_vaddr, reader)?;
}

Ok(state)
Expand Down Expand Up @@ -142,11 +140,11 @@ pub fn patch_go(raw: &[u8], state: &mut State) -> Result<()> {
/// - `Err(_)` if the patch failed
pub fn patch_stack(state: &mut State) -> Result<()> {
// Setup stack pointer
let ptr = 0x7F_FF_FF_FF_FF_FF_D0_00_u64;
let ptr = 0x7F_FF_FF_FF_D0_00_u64;

// Allocate 1 page for the initial stack data, and 16KB = 4 pages for the stack to grow.
state.memory.set_memory_range(
sign_extend(ptr - 4 * page::PAGE_SIZE as u64, 63),
sign_extend(ptr - 4 * page::PAGE_SIZE as u64, 47),
[0u8; page::PAGE_SIZE * 5].as_slice(),
)?;
state.registers[29] = ptr;
Expand All @@ -157,19 +155,21 @@ pub fn patch_stack(state: &mut State) -> Result<()> {
}

// init argc, argv, aux on stack
store_mem(state, sign_extend(ptr + 4, 63), 0x42)?; // argc = 0 (argument count)
store_mem(state, sign_extend(ptr + 4 * 2, 63), 0x35)?; // argv[n] = 0 (terminating argv)
store_mem(state, sign_extend(ptr + 4 * 3, 63), 0)?; // envp[term] = 0 (no env vars)
store_mem(state, sign_extend(ptr + 4 * 4, 63), 6)?; // auxv[0] = _AT_PAGESZ = 6 (key)
store_mem(state, sign_extend(ptr + 4 * 5, 63), 4096)?; // auxv[1] = page size of 4 KiB (value) - (== minPhysPageSize)
store_mem(state, sign_extend(ptr + 4 * 6, 63), 25)?; // auxv[2] = AT_RANDOM
state.memory.set_memory(sign_extend(ptr + 4 * 7, 63), sign_extend(ptr + 4 * 9, 63))?; // auxv[3] = random data
store_mem(state, sign_extend(ptr + 4 * 8, 63), 0)?; // auxv[term] = 0
store_mem(state, sign_extend(ptr + 4, 47), 0x42)?; // argc = 0 (argument count)
store_mem(state, sign_extend(ptr + 4 * 2, 47), 0x35)?; // argv[n] = 0 (terminating argv)
store_mem(state, sign_extend(ptr + 4 * 3, 47), 0)?; // envp[term] = 0 (no env vars)
store_mem(state, sign_extend(ptr + 4 * 4, 47), 6)?; // auxv[0] = _AT_PAGESZ = 6 (key)
store_mem(state, sign_extend(ptr + 4 * 5, 47), 4096)?; // auxv[1] = page size of 4 KiB (value) - (== minPhysPageSize)
store_mem(state, sign_extend(ptr + 4 * 6, 47), 25)?; // auxv[2] = AT_RANDOM
state
.memory
.set_memory(sign_extend(ptr + 4 * 7, 47), sign_extend(ptr + 4 * 9, 47))?; // auxv[3] = random data
store_mem(state, sign_extend(ptr + 4 * 8, 47), 0)?; // auxv[term] = 0

// 16 bytes of "randomness"
state
.memory
.set_memory_range(sign_extend(ptr + 4 * 9, 63), b"4;byfairdiceroll".as_slice())?;
.set_memory_range(sign_extend(ptr + 4 * 9, 47), b"4;byfairdiceroll".as_slice())?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/mipsevm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ pub(crate) fn sign_extend(data: u64, index: u64) -> u64 {
} else {
data & mask
}
}
}
2 changes: 1 addition & 1 deletion crates/mipsevm/src/witness.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! This module contains the various witness types.

use crate::memory::PROOF_LEN;
use crate::{utils::keccak256, State, StateWitness, StateWitnessHasher};
use alloy_primitives::{B256, U256};
use alloy_sol_types::{sol, SolCall};
use preimage_oracle::KeyType;
use revm::primitives::Bytes;
use crate::memory::PROOF_LEN;

/// The size of an encoded [StateWitness] in bytes.
pub const STATE_WITNESS_SIZE: usize = 378;
Expand Down

0 comments on commit 7b7949c

Please sign in to comment.