Skip to content

Commit 14ba846

Browse files
committed
Guard runtime-array const reification against truncation and ZST tails
1 parent d14c956 commit 14ba846

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,33 @@ impl<'tcx> CodegenCx<'tcx> {
622622
}
623623
SpirvType::RuntimeArray { element } => {
624624
let stride = self.lookup_type(element).sizeof(self).unwrap();
625-
let count = (alloc.inner().size() - offset).bytes() / stride.bytes();
625+
if stride.bytes() == 0 {
626+
let result = self.undef(ty);
627+
self.zombie_no_span(
628+
result.def_cx(self),
629+
&format!(
630+
"unsupported unsized `{}` constant with zero-sized elements",
631+
self.debug_type(ty)
632+
),
633+
);
634+
return (result, Size::ZERO);
635+
}
636+
637+
let read_size = alloc.inner().size() - offset;
638+
let rem = read_size.bytes() % stride.bytes();
639+
if rem != 0 {
640+
let result = self.undef(ty);
641+
self.zombie_no_span(
642+
result.def_cx(self),
643+
&format!(
644+
"unsupported unsized `{}` constant with {rem} trailing bytes",
645+
self.debug_type(ty)
646+
),
647+
);
648+
return (result, read_size);
649+
}
650+
651+
let count = read_size.bytes() / stride.bytes();
626652
let sized_ty = self.type_array(element, count);
627653

628654
let result = self.constant_composite(

0 commit comments

Comments
 (0)