Skip to content
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

Add support for anonymous nested statics #3953

Merged
merged 5 commits into from
Mar 22, 2025

Conversation

carolynzech
Copy link
Contributor

@carolynzech carolynzech commented Mar 21, 2025

rust-lang/rust#121644 added support for anonymous nested allocations to statics. This PR adds support for such statics to Kani.

The idea is to treat an anonymous GlobalAlloc::Static the same as we would treat a GlobalAlloc::Memory, since an anonymous static is a nested memory allocation. To frame this change in terms of the tests:

pointer_to_const_alloc.rs contains a test for the GlobalAlloc::Memory case, which we could already handle prior to this PR. The MIR looks like:

alloc3 (size: 4, align: 4) {
    2a 00 00 00                                     │ *...
}

alloc1 (static: FOO, size: 16, align: 8) {
    ╾─────alloc3<imm>─────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........
}

meaning that FOO contains a pointer to the immutable allocation alloc3 (note the alloc3<imm>, imm standing for "immutable").

anon_static.rs tests the code introduced in this PR. The MIR from example_1 looks almost identical:

alloc2 (static: FOO::{constant#0}, size: 4, align: 4) {
    2a 00 00 00                                     │ *...
}

alloc1 (static: FOO, size: 16, align: 8) {
    ╾───────alloc2────────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........
}

Note, however, that alloc2 is mutable, and is thus an anonymous nested static rather than a constant allocation.
But we can just call codegen_const_allocation anyway, since it ends up checking if the allocation is indeed constant before declaring the global variable in the symbol table:

alloc.mutability == Mutability::Not && !has_interior_mutabity,

Resolves #3904

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@github-actions github-actions bot added the Z-BenchCI Tag a PR to run benchmark CI label Mar 21, 2025
@carolynzech carolynzech marked this pull request as ready for review March 21, 2025 19:15
@carolynzech carolynzech requested a review from a team as a code owner March 21, 2025 19:15
This isn't related to issue 3904. In codegen_alloc_pointer, the GlobalAlloc::from call returns GlobalAlloc::Memory, so we already codegened it as a constant allocation prior to this PR.
But I thought it would be good to have a test that exercises this case explicitly, and that demonstrates the difference between this case and the nested statics case, so I added it in this PR.
@carolynzech carolynzech enabled auto-merge March 21, 2025 22:34
@carolynzech carolynzech added this pull request to the merge queue Mar 22, 2025
Merged via the queue into model-checking:main with commit 2c972fb Mar 22, 2025
24 of 25 checks passed
@carolynzech carolynzech deleted the issue-3904 branch March 22, 2025 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Z-BenchCI Tag a PR to run benchmark CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash in Kani compiler on chrono crate
3 participants