@@ -6,7 +6,7 @@ use tracing::debug;
66/// Returns `true` if this place is allowed to be less aligned
77/// than its containing struct (because it is within a packed
88/// struct).
9- pub fn is_potentially_disaligned < ' tcx , L > (
9+ pub fn is_potentially_misaligned < ' tcx , L > (
1010 tcx : TyCtxt < ' tcx > ,
1111 local_decls : & L ,
1212 typing_env : ty:: TypingEnv < ' tcx > ,
@@ -15,25 +15,16 @@ pub fn is_potentially_disaligned<'tcx, L>(
1515where
1616 L : HasLocalDecls < ' tcx > ,
1717{
18- debug ! ( "is_disaligned ({:?})" , place) ;
18+ debug ! ( "is_potentially_misaligned ({:?})" , place) ;
1919 let Some ( pack) = is_within_packed ( tcx, local_decls, place) else {
20- debug ! ( "is_disaligned ({:?}) - not within packed" , place) ;
20+ debug ! ( "is_potentially_misaligned ({:?}) - not within packed" , place) ;
2121 return false ;
2222 } ;
2323
2424 let ty = place. ty ( local_decls, tcx) . ty ;
2525 let unsized_tail = || tcx. struct_tail_for_codegen ( ty, typing_env) ;
2626
27- // Try to normalize the type to resolve any generic parameters
28- let normalized_ty = match tcx. try_normalize_erasing_regions ( typing_env, ty) {
29- Ok ( normalized) => normalized,
30- Err ( _) => {
31- // If normalization fails, fall back to the original type
32- ty
33- }
34- } ;
35-
36- match tcx. layout_of ( typing_env. as_query_input ( normalized_ty) ) {
27+ match tcx. layout_of ( typing_env. as_query_input ( ty) ) {
3728 Ok ( layout) => {
3829 if layout. align . abi <= pack
3930 && ( layout. is_sized ( ) || matches ! ( unsized_tail( ) . kind( ) , ty:: Slice ( ..) | ty:: Str ) )
4435 // just an approximation -- except when the unsized tail is a slice, where the alignment
4536 // is fully determined by the type.
4637 debug ! (
47- "is_disaligned ({:?}) - align = {}, packed = {}; not disaligned" ,
38+ "is_potentially_misaligned ({:?}) - align = {}, packed = {}; not disaligned" ,
4839 place,
4940 layout. align. abi. bytes( ) ,
5041 pack. bytes( )
@@ -55,12 +46,12 @@ where
5546 }
5647 }
5748 Err ( _) => {
58- // We cannot figure out the layout. This often happens with generic types.
59- // For const generic arrays like [u8; CAP], we can make a reasonable assumption
60- // about their alignment based on the element type .
49+ // Soundness-critical: this may return false positives (reporting potential misalignment),
50+ // but must not return false negatives. When layout is unavailable, we stay conservative
51+ // except for arrays of u8/i8 whose ABI alignment is provably 1 .
6152
6253 // Try to determine alignment from the type structure
63- if let Some ( element_align) = get_element_alignment ( tcx, normalized_ty ) {
54+ if let Some ( element_align) = get_element_alignment ( tcx, ty ) {
6455 element_align > pack
6556 } else {
6657 // If we still can't determine alignment, conservatively assume disaligned
@@ -71,16 +62,21 @@ where
7162}
7263
7364/// Try to determine the alignment of an array element type
74- fn get_element_alignment < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Option < Align > {
65+ fn get_element_alignment < ' tcx > ( _tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Option < Align > {
7566 match ty. kind ( ) {
7667 ty:: Array ( element_ty, _) | ty:: Slice ( element_ty) => {
77- // For arrays and slices, the alignment is the same as the element type
78- let param_env = ty:: ParamEnv :: empty ( ) ;
79- let typing_env =
80- ty:: TypingEnv { typing_mode : ty:: TypingMode :: non_body_analysis ( ) , param_env } ;
81- match tcx. layout_of ( typing_env. as_query_input ( * element_ty) ) {
82- Ok ( layout) => Some ( layout. align . abi ) ,
83- Err ( _) => None ,
68+ // Only allow u8 and i8 arrays when layout computation fails
69+ // Other types are conservatively assumed to be misaligned
70+ match element_ty. kind ( ) {
71+ ty:: Uint ( ty:: UintTy :: U8 ) | ty:: Int ( ty:: IntTy :: I8 ) => {
72+ // For u8 and i8, we know their alignment is 1
73+ Some ( Align :: from_bytes ( 1 ) . unwrap ( ) )
74+ }
75+ _ => {
76+ // For other types, we cannot safely determine alignment
77+ // Conservatively return None to indicate potential misalignment
78+ None
79+ }
8480 }
8581 }
8682 _ => None ,
0 commit comments