Skip to content

Commit

Permalink
polkavm-test-data
Browse files Browse the repository at this point in the history
Signed-off-by: Jarkko Sakkinen <[email protected]>
  • Loading branch information
jarkkojs committed Dec 15, 2024
1 parent 965ff40 commit df084cf
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 37 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"crates/polkavm-common",
"crates/polkavm-linux-raw",
"crates/polkavm",
"crates/polkavm-test-data",

"crates/simplealloc",

Expand Down Expand Up @@ -39,6 +40,7 @@ polkavm-derive-impl = { version = "0.18.0", path = "crates/polkavm-derive-
polkavm-derive-impl-macro = { version = "0.18.0", path = "crates/polkavm-derive-impl-macro" }
polkavm-linker = { version = "0.18.0", path = "crates/polkavm-linker" }
polkavm-linux-raw = { version = "0.18.0", path = "crates/polkavm-linux-raw" }
polkavm-test-data = { version = "0.18.0", path = "crates/polkavm-test-data" }

blake3 = { version = "1.5.4", default-features = false }
clap = "4.4.6"
Expand Down
14 changes: 14 additions & 0 deletions crates/polkavm-test-data/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "polkavm-test-data"
version.workspace = true
authors.workspace = true
license.workspace = true
edition.workspace = true
rust-version.workspace = true
repository.workspace = true

[build-dependencies]
polkavm-linker = { workspace = true }

[lints]
workspace = true
64 changes: 64 additions & 0 deletions crates/polkavm-test-data/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::path::Path;
use std::process::Command;

fn build(project: &str, profile: &str, target: &str) -> Result<(), String> {
let polkavm = Path::new(std::env!("CARGO_MANIFEST_DIR")).join("../..");
let project_path = polkavm.join("guest-programs").join(project).into_os_string().into_string().unwrap();

let remap = std::format!(
"--remap-path-prefix={}= --remap-path-prefix={}=",
polkavm.into_os_string().into_string().unwrap(),
std::env!("HOME"),
);

let mut cmd = Command::new("cargo");

cmd.current_dir(project_path)
.env_clear()
.env("PATH", std::env!("PATH"))
.env("RUSTFLAGS", remap)
.env("RUSTUP_HOME", std::env!("RUSTUP_HOME"))
.arg("build")
.arg("-q")
.arg("--profile")
.arg(profile)
.arg("--bin")
.arg(project)
.arg("-p")
.arg(project)
.arg("--target")
.arg(target)
.arg("-Zunstable-options")
.arg("-Zbuild-std=core,alloc");

let res = cmd.output().unwrap();

if res.status.success() {
return Ok(());
}

return Err(String::from_utf8_lossy(&res.stderr).to_string());
}

fn main() -> Result<(), String> {
println!("cargo:rerun-if-env-changed=OUT_DIR");

let target_32 = polkavm_linker::target_json_32_path()
.unwrap()
.into_os_string()
.into_string()
.unwrap();

let target_64 = polkavm_linker::target_json_64_path()
.unwrap()
.into_os_string()
.into_string()
.unwrap();

build("test-blob", "no-lto", &target_32)?;
build("test-blob", "no-lto", &target_64)?;
build("bench-pinky", "release", &target_32)?;
build("bench-pinky", "release", &target_64)?;

Ok(())
}
4 changes: 4 additions & 0 deletions crates/polkavm-test-data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const BENCH_PINKY_32: &[u8] = include_bytes!("../../../guest-programs/target/riscv64emac-unknown-none-polkavm/release/bench-pinky");
pub const BENCH_PINKY_64: &[u8] = include_bytes!("../../../guest-programs/target/riscv64emac-unknown-none-polkavm/release/bench-pinky");
pub const TEST_BLOB_32: &[u8] = include_bytes!("../../../guest-programs/target/riscv32emac-unknown-none-polkavm/no-lto/test-blob");
pub const TEST_BLOB_64: &[u8] = include_bytes!("../../../guest-programs/target/riscv64emac-unknown-none-polkavm/no-lto/test-blob");
1 change: 1 addition & 0 deletions crates/polkavm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ libc = { workspace = true }
env_logger = { workspace = true }
polkavm-common = { workspace = true, features = ["export-internals-for-testing"] }
polkavm-linker = { workspace = true }
polkavm-test-data = { workspace = true }
image = { workspace = true, features = ["tga"] }
ruzstd = { workspace = true }
paste = { workspace = true }
Expand Down
33 changes: 17 additions & 16 deletions crates/polkavm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,18 +1319,18 @@ fn decompress_zstd(mut bytes: &[u8]) -> Vec<u8> {

static BLOB_MAP: Mutex<Option<BTreeMap<(bool, bool, &'static [u8]), ProgramBlob>>> = Mutex::new(None);

fn get_blob(elf: &'static [u8]) -> ProgramBlob {
get_blob_impl(true, false, elf)
fn get_blob(elf: &'static [u8], decompress: bool) -> ProgramBlob {
get_blob_impl(true, false, decompress, elf)
}

fn get_blob_impl(optimize: bool, strip: bool, elf: &'static [u8]) -> ProgramBlob {
fn get_blob_impl(optimize: bool, strip: bool, decompress: bool, elf: &'static [u8]) -> ProgramBlob {
let mut blob_map = BLOB_MAP.lock();
let blob_map = blob_map.get_or_insert_with(BTreeMap::new);
blob_map
.entry((optimize, strip, elf))
.or_insert_with(|| {
// This is slow, so cache it.
let elf = decompress_zstd(elf);
let elf = if decompress { decompress_zstd(elf) } else { elf.to_vec() };
let mut config = polkavm_linker::Config::default();
config.set_optimize(optimize);
config.set_strip(strip);
Expand All @@ -1355,7 +1355,7 @@ fn doom(config: Config, elf: &'static [u8]) {
const DOOM_WAD: &[u8] = include_bytes!("../../../examples/doom/roms/doom1.wad");

let _ = env_logger::try_init();
let blob = get_blob(elf);
let blob = get_blob(elf, true /* decompress */);
let engine = Engine::new(&config).unwrap();
let mut module_config = ModuleConfig::default();
module_config.set_page_size(16 * 1024); // TODO: Also test with other page sizes.
Expand Down Expand Up @@ -1507,9 +1507,9 @@ fn pinky_impl(config: Config, is_64_bit: bool) {

let _ = env_logger::try_init();
let blob = if !is_64_bit {
get_blob(include_bytes!("../../../test-data/bench-pinky_32.elf.zst"))
get_blob(polkavm_test_data::BENCH_PINKY_32, false /* no decompress */)
} else {
get_blob(include_bytes!("../../../test-data/bench-pinky_64.elf.zst"))
get_blob(polkavm_test_data::BENCH_PINKY_64, false /* no decompress */)
};

let engine = Engine::new(&config).unwrap();
Expand Down Expand Up @@ -1998,14 +1998,15 @@ struct TestInstance {
instance: crate::Instance,
}

const TEST_BLOB_32_ELF_ZST: &[u8] = include_bytes!("../../../test-data/test-blob_32.elf.zst");
const TEST_BLOB_64_ELF_ZST: &[u8] = include_bytes!("../../../test-data/test-blob_64.elf.zst");

impl TestInstance {
fn new(config: &Config, optimize: bool, is_64_bit: bool) -> Self {
let _ = env_logger::try_init();
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let blob = get_blob_impl(optimize, is_64_bit, blob);
let blob = if is_64_bit {
polkavm_test_data::TEST_BLOB_64
} else {
polkavm_test_data::TEST_BLOB_32
};
let blob = get_blob_impl(optimize, is_64_bit, false, blob);

let engine = Engine::new(config).unwrap();
let module = Module::from_blob(&engine, &Default::default(), blob).unwrap();
Expand Down Expand Up @@ -2591,7 +2592,7 @@ fn gas_metering_with_implicit_trap(config: Config) {

fn test_basic_debug_info(raw_blob: &'static [u8]) {
let _ = env_logger::try_init();
let program = get_blob(raw_blob);
let program = get_blob(raw_blob, false /* no decompress */);
let entry_point = program.exports().find(|export| export == "read_u32").unwrap().program_counter();
let mut line_program = program.get_debug_line_program_at(entry_point).unwrap().unwrap();
let info = line_program.run().unwrap().unwrap();
Expand All @@ -2615,13 +2616,13 @@ fn test_basic_debug_info(raw_blob: &'static [u8]) {
#[ignore]
#[test]
fn test_basic_debug_info_32() {
test_basic_debug_info(TEST_BLOB_32_ELF_ZST);
test_basic_debug_info(polkavm_test_data::TEST_BLOB_32);
}

#[ignore]
#[test]
fn test_basic_debug_info_64() {
test_basic_debug_info(TEST_BLOB_64_ELF_ZST);
test_basic_debug_info(polkavm_test_data::TEST_BLOB_64);
}

#[test]
Expand Down Expand Up @@ -2688,7 +2689,7 @@ fn module_cache(_config: Config) {}
#[cfg(feature = "module-cache")]
fn module_cache(mut config: Config) {
let _ = env_logger::try_init();
let blob = get_blob(TEST_BLOB_32_ELF_ZST);
let blob = get_blob(polkavm_test_data::TEST_BLOB_32, false /* no decompress */);

config.set_worker_count(0);

Expand Down
21 changes: 0 additions & 21 deletions guest-programs/build-test-data.sh

This file was deleted.

Binary file removed test-data/bench-pinky_32.elf.zst
Binary file not shown.
Binary file removed test-data/bench-pinky_64.elf.zst
Binary file not shown.
Binary file removed test-data/test-blob_32.elf.zst
Binary file not shown.
Binary file removed test-data/test-blob_64.elf.zst
Binary file not shown.

0 comments on commit df084cf

Please sign in to comment.