Skip to content

fix up #[cfg(...)] related to test code that uses dirty bitmaps #331

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

Merged
merged 6 commits into from
Jul 8, 2025

Conversation

roypat
Copy link
Member

@roypat roypat commented Jul 1, 2025

Currently, vm-memory doesn't compile on all feature combinations: cargo check --no-default-features --features backend-bitmap does not compile because in bitmap/backend/atomic_bitmap.rs we try to use the libc crate to determine page size at some point. libc is an optional dependency and backend-bitmap does not enable it. Fixing this for production code is, of course, a one-liner in Cargo.toml. However, nothing is ever that straightforward. Turns out our test code use some #[cfg(test)] directives to bypass cargo features, and always compile in the bitmap/backend/mod.rs module. This means cargo test --no-default-features is still broken. Instead of hacking around this by simply declaring libc an unconditional dev-dependency, ensure that tests that depend on bitmaps being available have the correct feature cfgs on them, and then drop the cfg(test) stuff.

There's a lot more cleanup that can be done here (all functions in bitmap/mod.rs probably do not belong there), but this is all I'm willing to do for today.

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@roypat roypat requested a review from andreeaflorescu July 1, 2025 15:16
@roypat roypat force-pushed the bitmap-test-cleanup branch 2 times, most recently from 34a4ded to 72c93af Compare July 1, 2025 15:28
andreeaflorescu
andreeaflorescu previously approved these changes Jul 3, 2025
ShadowCurse
ShadowCurse previously approved these changes Jul 3, 2025
roypat added 6 commits July 3, 2025 16:37
BytesHelper::test_access() returned a result that was always immediately
unwrapped. Just assert in the function directly instead.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
The usesite of this import was behind a cfg, so some feature combos
printed a warning about unused imports.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
backend-bitmap uses libc to determine host page size in
atomic_bitmap.rs. Currently, commands such as cargo build
--no-default-features --features backend-bitmap fail, because libc is an
optional dependency, and only enabled if we also enable backend-mmap or
rawfd (which there is no reasonable setup in which this wasnt the case,
explaining why no one ever ran into this).

Technically the libc dependency is only needed on target_family =
"unix", but there's no way to enable a feature only on a specific
target, and on non-unix systems the libc call is disabled via cfg-ery,
so won't cause any problems.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
This overly complex agglomeration of function pointers and closures was
used to... still write the same number of LoC in the end. So just
eliminate it and inline it into its one use site.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
The bitmap backend implementation in vm_memory::bitmap::backend is only
exported if the backend-bitmap feature is enabled. However, compiling
the crate in test mode (e.g. `cargo test`), also unconditionally enables
it. This is weird, and actually causes problems: The bitmap backend code
depends on the libc crate, and this weird "enable unconditionally for
tests" behavior means we have to go through more trouble in Cargo.toml
to get the code to compile in some feature combinations, as not only do
we need to add the libc crate as a dependency of the backend-bitmap
feature, we also have to add it as a dev-dependency.

Add a #[cfg(feature = "backend-bitmap")] to the test module in
src/bitmap/mod.rs, as otherwise some of the functions will be warned
about being unused in some feature configurations.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
Now that the relevant code is no longer unconditionally enabled in
cfg(test), explicitly enable the feature for miri

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
@roypat roypat dismissed stale reviews from ShadowCurse and andreeaflorescu via c7d9c3d July 3, 2025 15:40
@roypat roypat force-pushed the bitmap-test-cleanup branch from 72c93af to c7d9c3d Compare July 3, 2025 15:40
@roypat
Copy link
Member Author

roypat commented Jul 3, 2025

@andreeaflorescu @ShadowCurse sorry, this didnt cleanly rebase on top of main after merging #312, could you reapprove?

@roypat roypat requested a review from stefano-garzarella July 3, 2025 16:18
@roypat roypat merged commit 5eb996a into rust-vmm:main Jul 8, 2025
2 checks passed
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