Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mozak-loader tests based on empty-elf #1049

Merged
merged 11 commits into from
Jan 10, 2024
1 change: 1 addition & 0 deletions runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ im = "15.1"
itertools = "0.12"
log = "0.4"
mimalloc = "0.1"
mozak-examples = { path = "../examples-builder", features = ["empty"] }
mozak-system = { path = "../system" }
plonky2 = "0.1"
proptest = { version = "1.4", optional = true }
Expand Down
49 changes: 44 additions & 5 deletions runner/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::decode::decode_instruction;
use crate::instruction::{DecodingError, Instruction};
use crate::util::load_u32;

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct MozakMemoryRegion {
pub starting_address: u32,
pub capacity: u32,
Expand All @@ -47,7 +47,7 @@ impl MozakMemoryRegion {
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct MozakMemory {
// context variables
pub context_variables: MozakMemoryRegion,
Expand Down Expand Up @@ -230,7 +230,7 @@ pub struct Code(pub HashMap<u32, Result<Instruction, DecodingError>>);
/// Memory of RISC-V Program
///
/// A wrapper around a map from a 32-bit address to a byte of memory
#[derive(Clone, Debug, Default, Deref, Serialize, Deserialize, DerefMut)]
#[derive(Clone, Debug, Default, Deref, Serialize, Deserialize, DerefMut, PartialEq)]
pub struct Data(pub HashMap<u32, u8>);

impl Code {
Expand Down Expand Up @@ -531,8 +531,7 @@ impl Program {

#[cfg(test)]
mod test {
use crate::elf::{MozakMemoryRegion, Program};

use crate::elf::{MozakMemory, MozakMemoryRegion, Program, RuntimeArguments};
#[test]
fn test_serialize_deserialize() {
let program = Program::default();
Expand Down Expand Up @@ -561,4 +560,44 @@ mod test {
assert_eq!(data[usize::try_from(*k).unwrap()], *v);
});
}
#[test]
fn test_empty_elf_with_empty_args() {
let mozak_ro_memory = Program::mozak_load_program(
mozak_examples::EMPTY_ELF,
&RuntimeArguments::new(&[], &[], &[]),
)
.unwrap()
.mozak_ro_memory
.unwrap();
assert_eq!(mozak_ro_memory.context_variables.data.len(), 0);
assert_eq!(mozak_ro_memory.io_tape_private.data.len(), 0);
assert_eq!(mozak_ro_memory.io_tape_public.data.len(), 0);
}
#[test]
fn test_empty_elf_with_args() {
let mozak_ro_memory = Program::mozak_load_program(
mozak_examples::EMPTY_ELF,
&RuntimeArguments::new(&[0], &[0, 1], &[0, 1, 2]),
)
.unwrap()
.mozak_ro_memory
.unwrap();
assert_eq!(mozak_ro_memory.context_variables.data.len(), 1);
assert_eq!(mozak_ro_memory.io_tape_private.data.len(), 2);
assert_eq!(mozak_ro_memory.io_tape_public.data.len(), 3);
}

#[test]
fn test_empty_elf_check_assumed_values() {
// This test ensures mozak-loader & mozak-linker-script is indeed aligned
let mozak_ro_memory = Program::mozak_load_program(
mozak_examples::EMPTY_ELF,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test only work for EMPTY_ELF? I think it should work for any ELF

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming it'd be more tedious to check every ELF that we have, since they might not all share (for eg.) similar run time args?

My understanding of this PR is that this ensures our loader is working as expected, and isn't necessarily a test for ELFs specifically, so an empty one would serve that purpose - we could have a followup PR if necessary?

&RuntimeArguments::new(&[], &[], &[]),
)
.unwrap()
.mozak_ro_memory
.unwrap();
let test_mozak_ro_memory = MozakMemory::default();
assert_eq!(mozak_ro_memory, test_mozak_ro_memory);
}
}