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

Store and restore hash from a compiled module #4654

Merged
merged 18 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/cross-linux-riscv64/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RUN apt-get update && \
# install rust tools
RUN curl --proto "=https" --tlsv1.2 --retry 3 -sSfL https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
RUN rustup -v toolchain install 1.73
RUN rustup -v toolchain install 1.74
# add docker the manual way
COPY install_docker.sh /
RUN /install_docker.sh
Expand Down Expand Up @@ -61,7 +61,7 @@ ENV CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc
RUST_TEST_THREADS=1 \
PKG_CONFIG_PATH="/usr/lib/riscv64-linux-gnu/pkgconfig/:${PKG_CONFIG_PATH}"

RUN rustup target add riscv64gc-unknown-linux-gnu --toolchain 1.73-x86_64-unknown-linux-gnu
RUN rustup target add riscv64gc-unknown-linux-gnu --toolchain 1.74-x86_64-unknown-linux-gnu

#compile libssl-dev for riscv64!
COPY build_openssl.sh /
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

env:
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: git
MSRV: "1.73"
MSRV: "1.74"

jobs:
run_benchmark:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Builds
env:
RUST_BACKTRACE: 1
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: git
MSRV: "1.73"
MSRV: "1.74"

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cloudcompiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Release cloudcompiler.wasm

env:
RUST_BACKTRACE: 1
MSRV: "1.73"
MSRV: "1.74"

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- 'lib/**'

env:
MSRV: "1.73"
MSRV: "1.74"

jobs:
documentation:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ env:
# Rust, but it's not stable on 1.69 yet. By explicitly setting the protocol we
# can override that behaviour
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: git
MSRV: "1.73"
MSRV: "1.74"
NEXTEST_PROFILE: "ci"

jobs:
Expand Down
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: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ edition = "2021"
homepage = "https://wasmer.io/"
license = "MIT"
repository = "https://github.com/wasmerio/wasmer"
rust-version = "1.73"
rust-version = "1.74"
version = "4.3.0-beta.1"

[workspace.dependencies]
Expand Down
8 changes: 7 additions & 1 deletion lib/api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::sync::Arc;
#[allow(unused_imports)]
pub use wasmer_compiler::{Artifact, CompilerConfig, EngineInner, Features, Tunables};
#[cfg(feature = "sys")]
use wasmer_types::DeserializeError;
use wasmer_types::{DeserializeError, HashAlgorithm};

#[cfg(feature = "js")]
use crate::js::engine as engine_imp;
Expand All @@ -38,6 +38,12 @@ impl Engine {
self.0.deterministic_id()
}

/// Sets the hash algorithm
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
pub fn set_hash_algorithm(&mut self, hash_algorithm: Option<HashAlgorithm>) {
self.0.set_hash_algorithm(hash_algorithm)
}

#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
/// Deserializes a WebAssembly module which was previously serialized with
/// `Module::serialize`.
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/jsc/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ impl Module {
///
pub(crate) unsafe fn from_js_module(module: JSObject, binary: impl IntoBytes) -> Self {
let binary = binary.into_bytes();

// The module is now validated, so we can safely parse it's types
let info = crate::module_info_polyfill::translate_module(&binary[..])
let mut info = crate::module_info_polyfill::translate_module(&binary[..])
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
.unwrap()
.info;

Expand Down
23 changes: 19 additions & 4 deletions lib/api/src/sys/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use wasmer_compiler::{
};
#[cfg(feature = "compiler")]
use wasmer_types::Features;
use wasmer_types::{DeserializeError, Target};
use wasmer_types::{DeserializeError, HashAlgorithm, Target};

/// Get the default config for the sys Engine
#[allow(unreachable_code)]
Expand Down Expand Up @@ -55,7 +55,12 @@ pub(crate) fn default_engine() -> Engine {
pub trait NativeEngineExt {
/// Create a new `Engine` with the given config
#[cfg(feature = "compiler")]
fn new(compiler_config: Box<dyn CompilerConfig>, target: Target, features: Features) -> Self;
fn new(
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
compiler_config: Box<dyn CompilerConfig>,
target: Target,
features: Features,
hash_algorithm: Option<HashAlgorithm>,
) -> Self;

/// Create a headless `Engine`
///
Expand Down Expand Up @@ -104,8 +109,18 @@ pub trait NativeEngineExt {

impl NativeEngineExt for crate::engine::Engine {
#[cfg(feature = "compiler")]
fn new(compiler_config: Box<dyn CompilerConfig>, target: Target, features: Features) -> Self {
Self(Engine::new(compiler_config, target, features))
fn new(
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
compiler_config: Box<dyn CompilerConfig>,
target: Target,
features: Features,
hash_algorithm: Option<HashAlgorithm>,
) -> Self {
Self(Engine::new(
compiler_config,
target,
features,
hash_algorithm,
))
}

fn headless() -> Self {
Expand Down
14 changes: 12 additions & 2 deletions lib/api/src/sys/tunables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ mod tests {

let tunables = TinyTunables {};
#[allow(deprecated)]
let mut engine = Engine::new(compiler.into(), Default::default(), Default::default());
let mut engine = Engine::new(
compiler.into(),
Default::default(),
Default::default(),
None,
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
);
engine.set_tunables(tunables);
let mut store = Store::new(engine);
//let mut store = Store::new(compiler);
Expand Down Expand Up @@ -451,7 +456,12 @@ mod tests {

let tunables = TinyTunables {};
#[allow(deprecated)]
let mut engine = Engine::new(compiler.into(), Default::default(), Default::default());
let mut engine = Engine::new(
compiler.into(),
Default::default(),
Default::default(),
None,
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
);
engine.set_tunables(tunables);
let mut store = Store::new(engine);
let module = Module::new(&store, wasm_bytes)?;
Expand Down
5 changes: 4 additions & 1 deletion lib/cli-compiler/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ impl Compile {
})
.unwrap_or_default();
let (engine_builder, compiler_type) = self.store.get_engine_for_target(target.clone())?;
let engine = engine_builder.engine();
let engine = engine_builder
.set_hash_algorithm(Some(wasmer_types::HashAlgorithm::Sha256))
.engine();
let output_filename = self
.output
.file_stem()
Expand Down Expand Up @@ -105,6 +107,7 @@ impl Compile {
&target,
memory_styles,
table_styles,
engine.hash_algorithm(),
)?;
let serialized = artifact.serialize()?;
fs::write(output_filename, serialized)?;
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/binfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn seccheck(path: &Path) -> Result<()> {

impl Binfmt {
/// The filename used to register the wasmer CLI as a binfmt interpreter.
pub const FILENAME: &str = "wasmer-binfmt-interpreter";
pub const FILENAME: &'static str = "wasmer-binfmt-interpreter";

/// execute [Binfmt]
pub fn execute(&self) -> Result<()> {
Expand Down
11 changes: 10 additions & 1 deletion lib/cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use clap::Parser;
use wasmer::*;

use crate::{store::StoreOptions, warning};
use crate::{common::HashAlgorithm, store::StoreOptions, warning};

#[derive(Debug, Parser)]
/// The options for the `wasmer compile` subcommand
Expand All @@ -26,6 +26,10 @@ pub struct Compile {

#[clap(short = 'm')]
cpu_features: Vec<CpuFeature>,

/// Hashing algorithm to be used for module hash
#[clap(long, value_enum)]
hash_algorithm: Option<HashAlgorithm>,
}

impl Compile {
Expand Down Expand Up @@ -54,6 +58,11 @@ impl Compile {
})
.unwrap_or_default();
let (store, compiler_type) = self.store.get_store_for_target(target.clone())?;

let mut engine = store.engine().clone();
let hash_algorithm = self.hash_algorithm.unwrap_or_default().into();
engine.set_hash_algorithm(Some(hash_algorithm));

let output_filename = self
.output
.file_stem()
Expand Down
15 changes: 13 additions & 2 deletions lib/cli/src/commands/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use webc::{
};

use self::utils::normalize_atom_name;
use crate::{common::normalize_path, store::CompilerOptions};
use crate::{
common::{normalize_path, HashAlgorithm},
store::CompilerOptions,
};

const LINK_SYSTEM_LIBRARIES_WINDOWS: &[&str] = &["userenv", "Ws2_32", "advapi32", "bcrypt"];

Expand Down Expand Up @@ -91,6 +94,10 @@ pub struct CreateExe {

#[clap(flatten)]
compiler: CompilerOptions,

/// Hashing algorithm to be used for module hash
#[clap(long, value_enum)]
hash_algorithm: Option<HashAlgorithm>,
}

/// Url or version to download the release from
Expand Down Expand Up @@ -216,6 +223,10 @@ impl CreateExe {

let (store, compiler_type) = self.compiler.get_store_for_target(target.clone())?;

let mut engine = store.engine().clone();
let hash_algorithm = self.hash_algorithm.unwrap_or_default().into();
engine.set_hash_algorithm(Some(hash_algorithm));

println!("Compiler: {}", compiler_type.to_string());
println!("Target: {}", target.triple());
println!(
Expand Down Expand Up @@ -380,7 +391,7 @@ pub(super) fn compile_pirita_into_directory(
let volume_path = target_dir.join("volumes").join("volume.o");
write_volume_obj(&volume_bytes, volume_name, &volume_path, target)?;
let volume_path = volume_path.canonicalize()?;
let volume_path = pathdiff::diff_paths(&volume_path, &target_dir).unwrap();
let volume_path = pathdiff::diff_paths(volume_path, &target_dir).unwrap();

std::fs::create_dir_all(target_dir.join("atoms")).map_err(|e| {
anyhow::anyhow!("cannot create /atoms dir in {}: {e}", target_dir.display())
Expand Down
11 changes: 10 additions & 1 deletion lib/cli/src/commands/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bytesize::ByteSize;
use clap::Parser;
use wasmer::*;

use crate::store::StoreOptions;
use crate::{common::HashAlgorithm, store::StoreOptions};

#[derive(Debug, Parser)]
/// The options for the `wasmer validate` subcommand
Expand All @@ -16,6 +16,10 @@ pub struct Inspect {

#[clap(flatten)]
store: StoreOptions,

/// Hashing algorithm to be used for module hash
#[clap(long, value_enum)]
hash_algorithm: Option<HashAlgorithm>,
}

impl Inspect {
Expand All @@ -27,6 +31,11 @@ impl Inspect {

fn inner_execute(&self) -> Result<()> {
let (store, _compiler_type) = self.store.get_store()?;

let mut engine = store.engine().clone();
let hash_algorithm = self.hash_algorithm.unwrap_or_default().into();
engine.set_hash_algorithm(Some(hash_algorithm));

let module_contents = std::fs::read(&self.path)?;
let iswasm = is_wasm(&module_contents);
let module_len = module_contents.len();
Expand Down
Loading
Loading