Skip to content

Commit

Permalink
Add missing padding needded for alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
zhassan-aws committed Jul 31, 2024
1 parent 7cd31aa commit b126001
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
18 changes: 13 additions & 5 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ impl<'tcx> GotocCtx<'tcx> {

/// Mapping enums to CBMC types is rather complicated. There are a few cases to consider:
/// 1. When there is only 0 or 1 variant, this is straightforward as the code shows
/// 2. When there are more variants, rust might decides to apply the typical encoding which
/// 2. When there are more variants, rust might decide to apply the typical encoding which
/// regard enums as tagged union, or an optimized form, called niche encoding.
///
/// The direct encoding is straightforward. Enums are just mapped to C as a struct of union of structs.
Expand Down Expand Up @@ -1229,8 +1229,7 @@ impl<'tcx> GotocCtx<'tcx> {
}
let union_name = format!("{name}-union");
let union_pretty_name = format!("{pretty_name}-union");
fields.push(DatatypeComponent::field(
"cases",
let cases =
gcx.ensure_union(&union_name, &union_pretty_name, |ctx, name| {
ctx.codegen_enum_cases(
name,
Expand All @@ -1240,8 +1239,17 @@ impl<'tcx> GotocCtx<'tcx> {
variants,
initial_offset,
)
}),
));
});
let cases_size = Size::from_bytes(cases.sizeof(&gcx.symbol_table));
fields.push(DatatypeComponent::field("cases", cases));
// Check if any padding is needed for alignment. This is needed for
// https://github.com/model-checking/kani/issues/2857 for example.
let bytes_so_far = initial_offset + cases_size;
if let Some(padding) =
gcx.codegen_alignment_padding(bytes_so_far, &layout, fields.len())
{
fields.push(padding);
}
fields
})
}
Expand Down
7 changes: 7 additions & 0 deletions tests/cargo-kani/iss2857/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "iss2857"
version = "0.1.0"
edition = "2021"

[dependencies]
sec1 = "0.7.3"
1 change: 1 addition & 0 deletions tests/cargo-kani/iss2857/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERIFICATION:- SUCCESSFUL
13 changes: 13 additions & 0 deletions tests/cargo-kani/iss2857/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

// This test checks that https://github.com/model-checking/kani/issues/2857 is
// fixed

#[kani::proof]
fn check_der_error() {
let e = sec1::der::Error::incomplete(sec1::der::Length::ZERO);
let _ = format!("{e:?}");
}

fn main() {}

0 comments on commit b126001

Please sign in to comment.