Skip to content

libkrun: Produce a proper Rust library#588

Open
d-e-s-o wants to merge 2 commits intocontainers:mainfrom
d-e-s-o:topic/rust-lib
Open

libkrun: Produce a proper Rust library#588
d-e-s-o wants to merge 2 commits intocontainers:mainfrom
d-e-s-o:topic/rust-lib

Conversation

@d-e-s-o
Copy link
Copy Markdown
Contributor

@d-e-s-o d-e-s-o commented Mar 14, 2026

Produce a proper Rust library to avoid the need for C library
installation as well as binding generation/definition. Basically, we can
now reference the crate via Cargo.toml:

[dependencies]
libkrun = ...

instead of having to rely on constructs like:

#[link(name = "krun")]
extern "C" {
  pub fn krun_create_ctx() -> i32;
  ...
}

For a brief discussion of the topic, see #494 (comment)

@d-e-s-o
Copy link
Copy Markdown
Contributor Author

d-e-s-o commented Mar 14, 2026

Still blocked on rust-vmm/linux-loader#223 or rust-vmm/linux-loader#229 and it being included in a new release.

@d-e-s-o d-e-s-o force-pushed the topic/rust-lib branch 4 times, most recently from 5987285 to c834676 Compare March 14, 2026 18:21
@ggoodman
Copy link
Copy Markdown
Contributor

I had my 🤖 help me put together a PR describing something I hope we can land before introducing the rust api: #593.

The main intent is that we not couple the init into the crate and instead have it as an optional sibling crate.

The existing version specification for the vm-memory dependency is
wrong: it includes incompatible versions that, when used, will break the
build. 0.18, for example does not build. Neither does 0.17. We cannot
go to 0.18, because linux-loader folks are reluctant to update to 0.18.
Hence, move the entire workspace to using vm-memory 0.17.

Signed-off-by: Daniel Müller <deso@posteo.net>
@d-e-s-o
Copy link
Copy Markdown
Contributor Author

d-e-s-o commented Mar 22, 2026

Still blocked on rust-vmm/linux-loader#223 or rust-vmm/linux-loader#229 and it being included in a new release.

Doesn't seem as if one of those is going to get merged anytime soon. So we have to stick with vm-memory 0.17. Updated PR accordingly.

@d-e-s-o d-e-s-o force-pushed the topic/rust-lib branch 4 times, most recently from 8c40869 to ac1e901 Compare March 22, 2026 13:51
@d-e-s-o d-e-s-o marked this pull request as ready for review March 22, 2026 13:56
@d-e-s-o
Copy link
Copy Markdown
Contributor Author

d-e-s-o commented Mar 30, 2026

Could somebody help review the change please?

Comment on lines +93 to +100
for slice in desc.mem.get_slices(desc.addr, desc.len as usize) {
let mut slice = slice?;
match input.read_volatile(&mut slice) {
Ok(0) => {
*eof = true;
break;
}
})
Ok(n) => total += n,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, going from try_access() to get_slices() changes the read behavior when guest memory is fragmented. The previous code called read() once, and the new code may call it multiple times (once per slice) until WouldBlock. This is probably fine, but worth noting since it's not mentioned in the PR description and should probably be tested with fragmented guest memory.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dorindabassey It should be the same in this regard actually, the old code would call it multiple times too - try_access invoked the closure for each slice.

Not sure if there is actually a functional difference here, so far haven't noticed any, but this did look suspicious it might break something.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks for clarifying! But separately, looks like there might be an issue with partial reads within a single slice as per gemini.

Copy link
Copy Markdown
Contributor Author

@d-e-s-o d-e-s-o Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it's best if we leave the code as it was for now? I only adjusted it reluctantly because try_access() has been deprecated. But it hasn't been removed yet and as per semver that can only happen with the next minor bump. So I've just #[allow]ed the deprecation now. Let me know what you think.

Copy link
Copy Markdown
Collaborator

@dorindabassey dorindabassey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code LGTM, just a few comments.

@dorindabassey
Copy link
Copy Markdown
Collaborator

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request performs extensive dependency updates across the project, most notably upgrading vm-memory to version 0.17 and pinning several VMM-related crates to specific versions. It also refactors error handling in the microVM builder and updates the virtio console's I/O logic to utilize get_slices. Feedback identifies critical issues in the new console I/O implementation, specifically that it fails to correctly handle partial reads and writes, which could lead to data loss or inefficient buffer utilization.

Produce a proper Rust library to avoid the need for C library
installation as well as binding generation/definition. Basically, we can
now reference the crate via Cargo.toml:

  [dependencies]
  libkrun = ...

instead of having to rely on constructs like:

  #[link(name = "krun")]
  extern "C" {
    pub fn krun_create_ctx() -> i32;
    ...
  }

Signed-off-by: Daniel Müller <deso@posteo.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants