wasmer_wasix/bin_factory/
binary_package.rs

1use std::{path::Path, sync::Arc};
2
3use anyhow::Context;
4use once_cell::sync::OnceCell;
5use sha2::Digest;
6use virtual_fs::FileSystem;
7use wasmer_config::package::{
8    PackageHash, PackageId, PackageSource, SuggestedCompilerOptimizations,
9};
10use wasmer_package::package::Package;
11use webc::Container;
12use webc::compat::SharedBytes;
13
14use crate::{
15    Runtime,
16    runners::MappedDirectory,
17    runtime::resolver::{PackageInfo, ResolveError},
18};
19use wasmer_types::ModuleHash;
20
21#[derive(derive_more::Debug, Clone)]
22pub struct BinaryPackageCommand {
23    name: String,
24    metadata: webc::metadata::Command,
25    #[debug(ignore)]
26    pub(crate) atom: SharedBytes,
27    hash: ModuleHash,
28    features: Option<wasmer_types::Features>,
29    pub suggested_compiler_optimizations: SuggestedCompilerOptimizations,
30}
31
32impl BinaryPackageCommand {
33    pub fn new(
34        name: String,
35        metadata: webc::metadata::Command,
36        atom: SharedBytes,
37        hash: ModuleHash,
38        features: Option<wasmer_types::Features>,
39        suggested_compiler_optimizations: SuggestedCompilerOptimizations,
40    ) -> Self {
41        Self {
42            name,
43            metadata,
44            atom,
45            hash,
46            features,
47            suggested_compiler_optimizations,
48        }
49    }
50
51    pub fn name(&self) -> &str {
52        &self.name
53    }
54
55    pub fn metadata(&self) -> &webc::metadata::Command {
56        &self.metadata
57    }
58
59    /// Get a reference to this [`BinaryPackageCommand`]'s atom as a cheap
60    /// clone of the internal OwnedBuffer.
61    pub fn atom(&self) -> SharedBytes {
62        self.atom.clone()
63    }
64
65    pub fn hash(&self) -> &ModuleHash {
66        &self.hash
67    }
68
69    /// Get the WebAssembly features required by this command's module
70    pub fn wasm_features(&self) -> Option<wasmer_types::Features> {
71        // Return only the pre-computed features from the container manifest
72        if let Some(features) = &self.features {
73            return Some(features.clone());
74        }
75
76        // If no annotations were found, return None
77        None
78    }
79}
80
81/// A WebAssembly package that has been loaded into memory.
82#[derive(Debug, Clone)]
83pub struct BinaryPackage {
84    pub id: PackageId,
85    /// Includes the ids of all the packages in the tree
86    pub package_ids: Vec<PackageId>,
87
88    pub when_cached: Option<u128>,
89    /// The name of the [`BinaryPackageCommand`] which is this package's
90    /// entrypoint.
91    pub entrypoint_cmd: Option<String>,
92    pub hash: OnceCell<ModuleHash>,
93    pub webc_fs: Arc<dyn FileSystem + Send + Sync>,
94    pub commands: Vec<BinaryPackageCommand>,
95    pub uses: Vec<String>,
96    pub file_system_memory_footprint: u64,
97
98    pub additional_host_mapped_directories: Vec<MappedDirectory>,
99}
100
101impl BinaryPackage {
102    #[tracing::instrument(level = "debug", skip_all)]
103    pub async fn from_dir(
104        dir: &Path,
105        rt: &(dyn Runtime + Send + Sync),
106    ) -> Result<Self, anyhow::Error> {
107        let source = rt.source();
108
109        // since each package must be in its own directory, hash of the `dir` should provide a good enough
110        // unique identifier for the package
111        let hash = sha2::Sha256::digest(dir.display().to_string().as_bytes()).into();
112        let id = PackageId::Hash(PackageHash::from_sha256_bytes(hash));
113
114        let manifest_path = dir.join("wasmer.toml");
115        let webc = Package::from_manifest(&manifest_path)?;
116        let container = Container::from(webc);
117        let manifest = container.manifest();
118
119        let root = PackageInfo::from_manifest(id, manifest, container.version())?;
120        let root_id = root.id.clone();
121
122        let resolution = crate::runtime::resolver::resolve(&root_id, &root, &*source).await?;
123        let mut pkg = rt
124            .package_loader()
125            .load_package_tree(&container, &resolution, true)
126            .await
127            .map_err(|e| anyhow::anyhow!(e))?;
128
129        // HACK: webc has no way to return its deserialized manifest to us, so we need to do it again here
130        // We already read and parsed the manifest once, so it'll succeed again. Unwrapping is safe at this point.
131        let wasmer_toml = std::fs::read_to_string(&manifest_path).unwrap();
132        let wasmer_toml: wasmer_config::package::Manifest = toml::from_str(&wasmer_toml).unwrap();
133        pkg.additional_host_mapped_directories.extend(
134            wasmer_toml
135                .fs
136                .into_iter()
137                .map(|(guest, host)| {
138                    anyhow::Ok(MappedDirectory {
139                        host: dir.join(host).canonicalize()?,
140                        guest,
141                    })
142                })
143                .collect::<Result<Vec<_>, _>>()?
144                .into_iter(),
145        );
146
147        Ok(pkg)
148    }
149
150    /// Load a [`webc::Container`] and all its dependencies into a
151    /// [`BinaryPackage`].
152    #[tracing::instrument(level = "debug", skip_all)]
153    pub async fn from_webc(
154        container: &Container,
155        rt: &(dyn Runtime + Send + Sync),
156    ) -> Result<Self, anyhow::Error> {
157        let source = rt.source();
158
159        let manifest = container.manifest();
160        let id = PackageInfo::package_id_from_manifest(manifest)?
161            .or_else(|| {
162                container
163                    .webc_hash()
164                    .map(|hash| PackageId::Hash(PackageHash::from_sha256_bytes(hash)))
165            })
166            .ok_or_else(|| anyhow::Error::msg("webc file did not provide its hash"))?;
167
168        let root = PackageInfo::from_manifest(id, manifest, container.version())?;
169        let root_id = root.id.clone();
170
171        let resolution = crate::runtime::resolver::resolve(&root_id, &root, &*source).await?;
172        let pkg = rt
173            .package_loader()
174            .load_package_tree(container, &resolution, false)
175            .await
176            .map_err(|e| anyhow::anyhow!(e))?;
177
178        Ok(pkg)
179    }
180
181    /// Load a [`BinaryPackage`] and all its dependencies from a registry.
182    #[tracing::instrument(level = "debug", skip_all)]
183    pub async fn from_registry(
184        specifier: &PackageSource,
185        runtime: &(dyn Runtime + Send + Sync),
186    ) -> Result<Self, anyhow::Error> {
187        let source = runtime.source();
188        let root_summary =
189            source
190                .latest(specifier)
191                .await
192                .map_err(|error| ResolveError::Registry {
193                    package: specifier.clone(),
194                    error,
195                })?;
196        let root = runtime.package_loader().load(&root_summary).await?;
197        let id = root_summary.package_id();
198
199        let resolution = crate::runtime::resolver::resolve(&id, &root_summary.pkg, &source)
200            .await
201            .context("Dependency resolution failed")?;
202        let pkg = runtime
203            .package_loader()
204            .load_package_tree(&root, &resolution, false)
205            .await
206            .map_err(|e| anyhow::anyhow!(e))?;
207
208        Ok(pkg)
209    }
210
211    pub fn get_command(&self, name: &str) -> Option<&BinaryPackageCommand> {
212        self.commands.iter().find(|cmd| cmd.name() == name)
213    }
214
215    /// Resolve the entrypoint command name to a [`BinaryPackageCommand`].
216    pub fn get_entrypoint_command(&self) -> Option<&BinaryPackageCommand> {
217        self.entrypoint_cmd
218            .as_deref()
219            .and_then(|name| self.get_command(name))
220    }
221
222    /// Get the bytes for the entrypoint command.
223    #[deprecated(
224        note = "Use BinaryPackage::get_entrypoint_command instead",
225        since = "0.22.0"
226    )]
227    pub fn entrypoint_bytes(&self) -> Option<SharedBytes> {
228        self.get_entrypoint_command().map(|entry| entry.atom())
229    }
230
231    /// Get a hash for this binary package.
232    ///
233    /// Usually the hash of the entrypoint.
234    pub fn hash(&self) -> ModuleHash {
235        *self.hash.get_or_init(|| {
236            if let Some(cmd) = self.get_entrypoint_command() {
237                cmd.hash
238            } else {
239                ModuleHash::xxhash(self.id.to_string())
240            }
241        })
242    }
243
244    pub fn infer_entrypoint(&self) -> Result<&str, anyhow::Error> {
245        if let Some(entrypoint) = self.entrypoint_cmd.as_deref() {
246            return Ok(entrypoint);
247        }
248
249        match self.commands.as_slice() {
250            [] => anyhow::bail!("The package doesn't contain any executable commands"),
251            [one] => Ok(one.name()),
252            [..] => {
253                let mut commands: Vec<_> = self.commands.iter().map(|cmd| cmd.name()).collect();
254                commands.sort();
255                anyhow::bail!(
256                    "Unable to determine the package's entrypoint. Please choose one of {commands:?}"
257                );
258            }
259        }
260    }
261}
262
263#[cfg(test)]
264mod tests {
265    use sha2::Digest;
266    use tempfile::TempDir;
267    use virtual_fs::AsyncReadExt;
268    use wasmer_package::utils::from_disk;
269
270    use crate::{
271        PluggableRuntime,
272        runtime::{package_loader::BuiltinPackageLoader, task_manager::VirtualTaskManager},
273    };
274
275    use super::*;
276
277    fn task_manager() -> Arc<dyn VirtualTaskManager + Send + Sync> {
278        cfg_if::cfg_if! {
279            if #[cfg(feature = "sys-thread")] {
280                Arc::new(crate::runtime::task_manager::tokio::TokioTaskManager::new(tokio::runtime::Handle::current()))
281            } else {
282                unimplemented!("Unable to get the task manager")
283            }
284        }
285    }
286
287    #[tokio::test]
288    #[cfg_attr(
289        not(feature = "sys-thread"),
290        ignore = "The tokio task manager isn't available on this platform"
291    )]
292    async fn fs_table_can_map_directories_to_different_names() {
293        let temp = TempDir::new().unwrap();
294        let wasmer_toml = r#"
295            [package]
296            name = "some/package"
297            version = "0.0.0"
298            description = "a dummy package"
299
300            [fs]
301            "/public" = "./out"
302        "#;
303        let manifest = temp.path().join("wasmer.toml");
304        std::fs::write(&manifest, wasmer_toml).unwrap();
305        let out = temp.path().join("out");
306        std::fs::create_dir_all(&out).unwrap();
307        let file_txt = "Hello, World!";
308        std::fs::write(out.join("file.txt"), file_txt).unwrap();
309        let tasks = task_manager();
310        let mut runtime = PluggableRuntime::new(tasks);
311        runtime.set_package_loader(
312            BuiltinPackageLoader::new()
313                .with_shared_http_client(runtime.http_client().unwrap().clone()),
314        );
315
316        let pkg = Package::from_manifest(&manifest).unwrap();
317        let data = pkg.serialize().unwrap();
318        let webc_path = temp.path().join("package.webc");
319        std::fs::write(&webc_path, data).unwrap();
320
321        let pkg = BinaryPackage::from_webc(&from_disk(&webc_path).unwrap(), &runtime)
322            .await
323            .unwrap();
324
325        // We should have mapped "./out/file.txt" on the host to
326        // "/public/file.txt" on the guest.
327        let mut f = pkg
328            .webc_fs
329            .new_open_options()
330            .read(true)
331            .open("/public/file.txt")
332            .unwrap();
333        let mut buffer = String::new();
334        f.read_to_string(&mut buffer).await.unwrap();
335        assert_eq!(buffer, file_txt);
336    }
337
338    #[tokio::test]
339    #[cfg_attr(
340        not(feature = "sys-thread"),
341        ignore = "The tokio task manager isn't available on this platform"
342    )]
343    async fn commands_use_the_atom_signature() {
344        let temp = TempDir::new().unwrap();
345        let wasmer_toml = r#"
346            [package]
347            name = "some/package"
348            version = "0.0.0"
349            description = "a dummy package"
350
351            [[module]]
352            name = "foo"
353            source = "foo.wasm"
354            abi = "wasi"
355            
356            [[command]]
357            name = "cmd"
358            module = "foo"     
359        "#;
360        let manifest = temp.path().join("wasmer.toml");
361        std::fs::write(&manifest, wasmer_toml).unwrap();
362
363        let atom_path = temp.path().join("foo.wasm");
364        std::fs::write(&atom_path, b"").unwrap();
365
366        let webc: Container = Package::from_manifest(&manifest).unwrap().into();
367
368        let tasks = task_manager();
369        let mut runtime = PluggableRuntime::new(tasks);
370        runtime.set_package_loader(
371            BuiltinPackageLoader::new()
372                .with_shared_http_client(runtime.http_client().unwrap().clone()),
373        );
374
375        let pkg = BinaryPackage::from_dir(temp.path(), &runtime)
376            .await
377            .unwrap();
378
379        assert_eq!(pkg.commands.len(), 1);
380        let command = pkg.get_command("cmd").unwrap();
381        let atom_sha256_hash: [u8; 32] = sha2::Sha256::digest(webc.get_atom("foo").unwrap()).into();
382        let module_hash = ModuleHash::sha256_from_bytes(atom_sha256_hash);
383        assert_eq!(command.hash(), &module_hash);
384    }
385}