Skip to content

Override std::ptr::align_offset #2396

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

tautschnig
Copy link
Member

@tautschnig tautschnig commented Apr 20, 2023

This hook intercepts calls to std::ptr::align_offset<T> as CBMC's memory model has no concept of alignment of allocations, so we would have to non-deterministically choose an alignment of the base pointer, add the pointer's offset to it, and then do the math that is done in library/core/src/ptr/mod.rs. Instead, we choose to always return either 0 when the pointer points to the beginning of an object (and, therefore, is necessarily aligned), or usize::MAX, per align_offset's documentation, which states: "It is permissible for the implementation to always return usize::MAX. Only your algorithm’s performance can depend on getting a usable offset here, not its correctness."

Fixes: #2363

@tautschnig
Copy link
Member Author

Perhaps @karkhaz:

  1. The alleged performance regression (https://github.com/model-checking/kani/actions/runs/4753944159/jobs/8446153448?pr=2396) is the same as previously reported upon a merge into main: https://github.com/model-checking/kani/actions/runs/4747428362/jobs/8432323295.
  2. Would benchcomp also tell us about any performance improvements?

Copy link
Contributor

@zhassan-aws zhassan-aws left a comment

Choose a reason for hiding this comment

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

Is there any potential unsoundness with this change?

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

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

I don't think this implementation is sound. The code should panic if the input is not a power of two.
For example, the following code panic in Rust:

use std::mem::align_of;

fn main() {
  let x = 10;
  let ptr = &x as *const i32;
  let _ = ptr.align_offset(5);
}

@tautschnig tautschnig self-assigned this Apr 27, 2023
@carolynzech carolynzech marked this pull request as draft April 7, 2025 17:06
@github-actions github-actions bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Jun 27, 2025
@tautschnig tautschnig dismissed celinval’s stale review June 27, 2025 12:39

Requested changes have been implemented.

@tautschnig tautschnig marked this pull request as ready for review June 27, 2025 13:16
@tautschnig tautschnig assigned zhassan-aws and unassigned tautschnig Jun 27, 2025
This hook intercepts calls to `std::ptr::align_offset<T>` as CBMC's memory
model has no concept of alignment of allocations, so we would have to
non-deterministically choose an alignment of the base pointer, add the
pointer's offset to it, and then do the math that is done in
`library/core/src/ptr/mod.rs`. Instead, we choose to always return
`usize::MAX`, per `align_offset`'s documentation, which states: "It is
permissible for the implementation to always return usize::MAX. Only your
algorithm’s performance can depend on getting a usable offset here, not its
correctness."

Fixes: model-checking#2363
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve symbolic execution performance when running against concrete values
3 participants