-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ICE due to mishandling of Aggregate rvalue for raw pointers to tr…
…ait objects (#3636) Add a match arm for the `AggregateKind::RawPtr(TyKind::RigidTy(RigidTy::Dynamic(..)))` case. Pointers to trait objects [are fat](https://github.com/rust-lang/rust/blob/master/library/core/src/ptr/metadata.rs#L20-#L27), so generate a fat pointer for the rvalue. Resolves #3631 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
- Loading branch information
1 parent
8c9ee58
commit 325c9e4
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// Test that Kani can verify code that produces a aggregate raw pointer to trait objects | ||
// c.f. https://github.com/model-checking/kani/issues/3631 | ||
|
||
#![feature(ptr_metadata)] | ||
|
||
use std::ptr::NonNull; | ||
|
||
trait SampleTrait { | ||
fn get_value(&self) -> i32; | ||
} | ||
|
||
struct SampleStruct { | ||
value: i32, | ||
} | ||
|
||
impl SampleTrait for SampleStruct { | ||
fn get_value(&self) -> i32 { | ||
self.value | ||
} | ||
} | ||
|
||
#[cfg(kani)] | ||
#[kani::proof] | ||
fn check_nonnull_dyn_from_raw_parts() { | ||
// Create a SampleTrait object from SampleStruct | ||
let sample_struct = SampleStruct { value: kani::any() }; | ||
let trait_object: &dyn SampleTrait = &sample_struct; | ||
|
||
// Get the raw data pointer and metadata for the trait object | ||
let trait_ptr = NonNull::new(trait_object as *const dyn SampleTrait as *mut ()).unwrap(); | ||
let metadata = std::ptr::metadata(trait_object); | ||
|
||
// Create NonNull<dyn SampleTrait> from the data pointer and metadata | ||
let nonnull_trait_object: NonNull<dyn SampleTrait> = | ||
NonNull::from_raw_parts(trait_ptr, metadata); | ||
|
||
unsafe { | ||
// Ensure trait method and member is preserved | ||
kani::assert( | ||
trait_object.get_value() == nonnull_trait_object.as_ref().get_value(), | ||
"trait method and member must correctly preserve", | ||
); | ||
} | ||
} |