Skip to content

Commit

Permalink
fix regression related to bundled libraries
Browse files Browse the repository at this point in the history
 `componentize-py` tried to read the bundled libraries from the filesystem at
 runtime rather than actually bundling them.  That worked fine as long as the
 files still existed at the same path when you ran the binary, but stopped
 working if you deleted them or tried to run the binary on a different machine.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej committed Oct 28, 2024
1 parent 881cdb4 commit c5deb00
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 40 deletions.
2 changes: 1 addition & 1 deletion 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
@@ -1,6 +1,6 @@
[package]
name = "componentize-py"
version = "0.15.1"
version = "0.15.2"
edition = "2021"
exclude = ["cpython"]

Expand Down
4 changes: 2 additions & 2 deletions examples/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ run a Python-based component targetting the [wasi-cli] `command` world.
## Prerequisites

* `Wasmtime` 26.0.0 or later
* `componentize-py` 0.15.1
* `componentize-py` 0.15.2

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.

```
cargo install --version 26.0.0 wasmtime-cli
pip install componentize-py==0.15.1
pip install componentize-py==0.15.2
```

## Running the demo
Expand Down
4 changes: 2 additions & 2 deletions examples/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ run a Python-based component targetting the [wasi-http] `proxy` world.
## Prerequisites

* `Wasmtime` 26.0.0 or later
* `componentize-py` 0.15.1
* `componentize-py` 0.15.2

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.

```
cargo install --version 26.0.0 wasmtime-cli
pip install componentize-py==0.15.1
pip install componentize-py==0.15.2
```

## Running the demo
Expand Down
4 changes: 2 additions & 2 deletions examples/matrix-math/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ within a guest component.
## Prerequisites

* `wasmtime` 26.0.0 or later
* `componentize-py` 0.15.1
* `componentize-py` 0.15.2
* `NumPy`, built for WASI

Note that we use an unofficial build of NumPy since the upstream project does
Expand All @@ -23,7 +23,7 @@ https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.

```
cargo install --version 26.0.0 wasmtime-cli
pip install componentize-py==0.15.1
pip install componentize-py==0.15.2
curl -OL https://github.com/dicej/wasi-wheels/releases/download/v0.0.1/numpy-wasi.tar.gz
tar xf numpy-wasi.tar.gz
```
Expand Down
4 changes: 2 additions & 2 deletions examples/sandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ sandboxed Python code snippets from within a Python app.
## Prerequisites

* `wasmtime-py` 25.0.0 or later
* `componentize-py` 0.15.1
* `componentize-py` 0.15.2

```
pip install componentize-py==0.15.1 wasmtime==25.0.0
pip install componentize-py==0.15.2 wasmtime==25.0.0
```

## Running the demo
Expand Down
4 changes: 2 additions & 2 deletions examples/tcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ making an outbound TCP request using `wasi-sockets`.
## Prerequisites

* `Wasmtime` 26.0.0 or later
* `componentize-py` 0.15.1
* `componentize-py` 0.15.2

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.

```
cargo install --version 26.0.0 wasmtime-cli
pip install componentize-py==0.15.1
pip install componentize-py==0.15.2
```

## Running the demo
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ features = ["pyo3/extension-module"]

[project]
name = "componentize-py"
version = "0.15.1"
version = "0.15.2"
description = "Tool to package Python applications as WebAssembly components"
readme = "README.md"
license = { file = "LICENSE" }
Expand Down
111 changes: 84 additions & 27 deletions src/prelink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,80 @@ pub fn embedded_helper_utils() -> Result<TempDir, io::Error> {

pub fn bundle_libraries(
library_path: Vec<(&str, Vec<std::path::PathBuf>)>,
) -> Result<Vec<Library>, io::Error> {
) -> Result<Vec<Library>, anyhow::Error> {
let mut libraries = vec![
library_from_so("libcomponentize_py_runtime.so")?,
library_from_so("libpython3.12.so")?,
library_from_so("libc.so")?,
library_from_so("libwasi-emulated-mman.so")?,
library_from_so("libwasi-emulated-process-clocks.so")?,
library_from_so("libwasi-emulated-getpid.so")?,
library_from_so("libwasi-emulated-signal.so")?,
library_from_so("libc++.so")?,
library_from_so("libc++abi.so")?,
Library {
name: "libcomponentize_py_runtime.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libcomponentize_py_runtime.so.zst"
))))?,
dl_openable: false,
},
Library {
name: "libpython3.12.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libpython3.12.so.zst"
))))?,
dl_openable: false,
},
Library {
name: "libc.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libc.so.zst"
))))?,
dl_openable: false,
},
Library {
name: "libwasi-emulated-mman.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libwasi-emulated-mman.so.zst"
))))?,
dl_openable: false,
},
Library {
name: "libwasi-emulated-process-clocks.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libwasi-emulated-process-clocks.so.zst"
))))?,
dl_openable: false,
},
Library {
name: "libwasi-emulated-getpid.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libwasi-emulated-getpid.so.zst"
))))?,
dl_openable: false,
},
Library {
name: "libwasi-emulated-signal.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libwasi-emulated-signal.so.zst"
))))?,
dl_openable: false,
},
Library {
name: "libc++.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libc++.so.zst"
))))?,
dl_openable: false,
},
Library {
name: "libc++abi.so".into(),
module: zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/libc++abi.so.zst"
))))?,
dl_openable: false,
},
];

for (index, (path, libs)) in library_path.iter().enumerate() {
Expand All @@ -76,7 +139,7 @@ pub fn bundle_libraries(

libraries.push(Library {
name: format!("/{index}/{path}"),
module: fs::read(library).unwrap(),
module: fs::read(library).with_context(|| library.display().to_string())?,
dl_openable: true,
});
}
Expand All @@ -85,18 +148,6 @@ pub fn bundle_libraries(
Ok(libraries)
}

fn library_from_so(library_name: &str) -> Result<Library, io::Error> {
let path = env!("OUT_DIR").to_owned();
let filepath = path + "/" + library_name + ".zst";
let bytes = fs::read(filepath)?;

Ok(Library {
name: library_name.into(),
module: zstd::decode_all(Cursor::new(bytes))?,
dl_openable: false,
})
}

pub fn search_for_libraries_and_configs<'a>(
python_path: &'a Vec<&'a str>,
module_worlds: &'a [(&'a str, &'a str)],
Expand Down Expand Up @@ -171,15 +222,19 @@ fn search_directory(
modules_seen: &mut HashSet<String>,
) -> Result<(), anyhow::Error> {
if path.is_dir() {
for entry in fs::read_dir(path)? {
for entry in fs::read_dir(path).with_context(|| path.display().to_string())? {
search_directory(root, &entry?.path(), libraries, configs, modules_seen)?;
}
} else if let Some(name) = path.file_name().and_then(|name| name.to_str()) {
if name.ends_with(NATIVE_EXTENSION_SUFFIX) {
libraries.push(path.to_owned());
} else if name == "componentize-py.toml" {
let root = root.canonicalize()?;
let path = path.canonicalize()?;
let root = root
.canonicalize()
.with_context(|| root.display().to_string())?;
let path = path
.canonicalize()
.with_context(|| path.display().to_string())?;

let module = module_name(&root, &path)
.ok_or_else(|| anyhow!("unable to determine module name for {}", path.display()))?;
Expand Down Expand Up @@ -224,7 +279,9 @@ fn search_directory(
module,
root: root.to_owned(),
path: path.parent().unwrap().to_owned(),
config: toml::from_str::<RawComponentizePyConfig>(&fs::read_to_string(path)?)?,
config: toml::from_str::<RawComponentizePyConfig>(
&fs::read_to_string(&path).with_context(|| path.display().to_string())?,
)?,
});
}
}
Expand Down

0 comments on commit c5deb00

Please sign in to comment.