Skip to content

Commit

Permalink
make webc hash required
Browse files Browse the repository at this point in the history
if a package does not have name, it falls back to it's webc hash.

This does mean that you need a webc hash to actually generate the PackageId.
  • Loading branch information
ayys committed Apr 2, 2024
1 parent 1e7a545 commit db767e1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
8 changes: 7 additions & 1 deletion lib/wasix/src/bin_factory/binary_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use webc::{compat::SharedBytes, Container};
use crate::{
runtime::{
module_cache::ModuleHash,
resolver::{PackageId, PackageInfo, PackageSpecifier, ResolveError},
resolver::{PackageId, PackageInfo, PackageSpecifier, ResolveError, WebcHash},
},
Runtime,
};
Expand Down Expand Up @@ -83,9 +83,15 @@ impl BinaryPackage {
) -> Result<Self, anyhow::Error> {
let source = rt.source();
let root = PackageInfo::from_manifest(container.manifest())?;
let hash = if let Some(hash) = container.webc_hash() {
WebcHash::from_bytes(hash)
} else {
return Err(anyhow::anyhow!("No hash found in the container"));
};
let root_id = PackageId {
package_name: root.name.clone(),
version: root.version.clone(),
hash,
};

let resolution = crate::runtime::resolver::resolve(&root_id, &root, &*source).await?;
Expand Down
12 changes: 8 additions & 4 deletions lib/wasix/src/runtime/package_loader/load_package_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ pub async fn load_package_tree(

let file_system_memory_footprint = count_file_system(&fs, Path::new("/"));

let package_name = if let Some(name) = &root.package_name {
name.clone()
} else {
tracing::warn!("The root package doesn't have a name. Falling back to the package ID");
root.hash.as_hex()
};

let loaded = BinaryPackage {
package_name: root
.package_name
.clone()
.context("Unnamed packages are not supported yet.")?,
package_name,
version: root.version.clone(),
when_cached: crate::syscalls::platform_clock_time_get(
wasmer_wasix_types::wasi::Snapshot0Clockid::Monotonic,
Expand Down
9 changes: 7 additions & 2 deletions lib/wasix/src/runtime/resolver/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub struct PackageSummary {

impl PackageSummary {
pub fn package_id(&self) -> PackageId {
self.pkg.id()
self.pkg.id(self.dist.webc_sha256)
}

pub fn from_webc_file(path: impl AsRef<Path>) -> Result<PackageSummary, Error> {
Expand Down Expand Up @@ -242,10 +242,11 @@ impl PackageInfo {
})
}

pub fn id(&self) -> PackageId {
pub fn id(&self, hash: WebcHash) -> PackageId {
PackageId {
package_name: self.name.clone(),
version: self.version.clone(),
hash,
}
}
}
Expand Down Expand Up @@ -403,6 +404,10 @@ impl WebcHash {
pub fn as_bytes(self) -> [u8; 32] {
self.0
}

pub fn as_hex(&self) -> String {
hex::encode(&self.0)
}
}

impl From<[u8; 32]> for WebcHash {
Expand Down
15 changes: 12 additions & 3 deletions lib/wasix/src/runtime/resolver/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use semver::Version;

use crate::runtime::resolver::{DistributionInfo, PackageInfo};

use super::WebcHash;

#[derive(Debug, Clone)]
pub struct Resolution {
pub package: ResolvedPackage,
Expand All @@ -32,18 +34,20 @@ pub struct ItemLocation {
pub struct PackageId {
pub package_name: Option<String>,
pub version: Version,
pub hash: WebcHash,
}

impl Display for PackageId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let PackageId {
package_name,
version,
hash,
} = self;
if let Some(package_name) = &self.package_name {
write!(f, "{package_name}@{version}")?;
if let Some(package_name) = &package_name {
return write!(f, "{package_name}@{version}");
}
write!(f, "@{version}")
write!(f, "{hash}")
}
}

Expand Down Expand Up @@ -86,6 +90,11 @@ impl DependencyGraph {
pkg
}

pub fn id(&self) -> &PackageId {
let Node { id, .. } = &self.graph[self.root];
id
}

pub fn root(&self) -> NodeIndex {
self.root
}
Expand Down
5 changes: 3 additions & 2 deletions lib/wasix/src/runtime/resolver/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ fn cycle_error(graph: &petgraph::Graph<Node, Edge>) -> ResolveError {
// Don't forget to make the cycle start and end with the same node
cycle.push(lowest_index_node);

let package_ids = cycle.into_iter().map(|ix| graph[ix].pkg.id()).collect();
let package_ids = cycle.into_iter().map(|ix| graph[ix].id.clone()).collect();
ResolveError::Cycle(package_ids)
}

Expand Down Expand Up @@ -264,6 +264,7 @@ where
for PackageId {
package_name,
version,
..
} in package_ids
{
if let Some(package_name) = package_name {
Expand Down Expand Up @@ -390,7 +391,7 @@ fn resolve_package(dependency_graph: &DependencyGraph) -> Result<ResolvedPackage
tracing::debug!("resolved filesystem: {:?}", &filesystem);

Ok(ResolvedPackage {
root_package: dependency_graph.root_info().id(),
root_package: dependency_graph.id().clone(),
commands,
entrypoint,
filesystem,
Expand Down

0 comments on commit db767e1

Please sign in to comment.