Skip to content

Commit e053dfa

Browse files
committed
Add a compile-time error when oversized types are used
LLVM generates wrong code (which may be an instance of compile-time UB) when faced with types that take lots of memory - bigger than the address space. Make using such types a trans error. While trans errors are bad, overbig types are expected to be very rare.
1 parent 01d693b commit e053dfa

File tree

3 files changed

+142
-65
lines changed

3 files changed

+142
-65
lines changed

src/librustc/middle/trans/adt.rs

+87-43
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub fn represent_type(cx: &CrateContext, t: ty::t) -> Rc<Repr> {
163163
fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
164164
match ty::get(t).sty {
165165
ty::ty_tup(ref elems) => {
166-
return Univariant(mk_struct(cx, elems.as_slice(), false), false)
166+
return Univariant(mk_struct(cx, elems.as_slice(), false, t), false)
167167
}
168168
ty::ty_struct(def_id, ref substs) => {
169169
let fields = ty::lookup_struct_fields(cx.tcx(), def_id);
@@ -174,12 +174,12 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
174174
let dtor = ty::ty_dtor(cx.tcx(), def_id).has_drop_flag();
175175
if dtor { ftys.push(ty::mk_bool()); }
176176

177-
return Univariant(mk_struct(cx, ftys.as_slice(), packed), dtor)
177+
return Univariant(mk_struct(cx, ftys.as_slice(), packed, t), dtor)
178178
}
179179
ty::ty_unboxed_closure(def_id, _) => {
180180
let upvars = ty::unboxed_closure_upvars(cx.tcx(), def_id);
181181
let upvar_types = upvars.iter().map(|u| u.ty).collect::<Vec<_>>();
182-
return Univariant(mk_struct(cx, upvar_types.as_slice(), false),
182+
return Univariant(mk_struct(cx, upvar_types.as_slice(), false, t),
183183
false)
184184
}
185185
ty::ty_enum(def_id, ref substs) => {
@@ -194,7 +194,8 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
194194
// (Typechecking will reject discriminant-sizing attrs.)
195195
assert_eq!(hint, attr::ReprAny);
196196
let ftys = if dtor { vec!(ty::mk_bool()) } else { vec!() };
197-
return Univariant(mk_struct(cx, ftys.as_slice(), false), dtor);
197+
return Univariant(mk_struct(cx, ftys.as_slice(), false, t),
198+
dtor);
198199
}
199200

200201
if !dtor && cases.iter().all(|c| c.tys.len() == 0) {
@@ -225,15 +226,17 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
225226
assert_eq!(hint, attr::ReprAny);
226227
let mut ftys = cases.get(0).tys.clone();
227228
if dtor { ftys.push(ty::mk_bool()); }
228-
return Univariant(mk_struct(cx, ftys.as_slice(), false), dtor);
229+
return Univariant(mk_struct(cx, ftys.as_slice(), false, t),
230+
dtor);
229231
}
230232

231233
if !dtor && cases.len() == 2 && hint == attr::ReprAny {
232234
// Nullable pointer optimization
233235
let mut discr = 0;
234236
while discr < 2 {
235-
if cases.get(1 - discr).is_zerolen(cx) {
236-
let st = mk_struct(cx, cases.get(discr).tys.as_slice(), false);
237+
if cases.get(1 - discr).is_zerolen(cx, t) {
238+
let st = mk_struct(cx, cases.get(discr).tys.as_slice(),
239+
false, t);
237240
match cases.get(discr).find_ptr() {
238241
Some(ThinPointer(_)) if st.fields.len() == 1 => {
239242
return RawNullablePointer {
@@ -263,11 +266,15 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
263266
slo: 0, shi: (cases.len() - 1) as i64 };
264267
let ity = range_to_inttype(cx, hint, &bounds);
265268

266-
return General(ity, cases.iter().map(|c| {
269+
let fields : Vec<_> = cases.iter().map(|c| {
267270
let mut ftys = vec!(ty_of_inttype(ity)).append(c.tys.as_slice());
268271
if dtor { ftys.push(ty::mk_bool()); }
269-
mk_struct(cx, ftys.as_slice(), false)
270-
}).collect(), dtor);
272+
mk_struct(cx, ftys.as_slice(), false, t)
273+
}).collect();
274+
275+
ensure_enum_fits_in_address_space(cx, ity, fields.as_slice(), t);
276+
277+
General(ity, fields, dtor)
271278
}
272279
_ => cx.sess().bug(format!("adt::represent_type called on non-ADT type: {}",
273280
ty_to_string(cx.tcx(), t)).as_slice())
@@ -288,8 +295,8 @@ pub enum PointerField {
288295
}
289296

290297
impl Case {
291-
fn is_zerolen(&self, cx: &CrateContext) -> bool {
292-
mk_struct(cx, self.tys.as_slice(), false).size == 0
298+
fn is_zerolen(&self, cx: &CrateContext, scapegoat: ty::t) -> bool {
299+
mk_struct(cx, self.tys.as_slice(), false, scapegoat).size == 0
293300
}
294301

295302
fn find_ptr(&self) -> Option<PointerField> {
@@ -344,29 +351,25 @@ fn get_cases(tcx: &ty::ctxt, def_id: ast::DefId, substs: &subst::Substs) -> Vec<
344351
}).collect()
345352
}
346353

347-
fn mk_struct(cx: &CrateContext, tys: &[ty::t], packed: bool) -> Struct {
348-
if tys.iter().all(|&ty| ty::type_is_sized(cx.tcx(), ty)) {
349-
let lltys = tys.iter().map(|&ty| type_of::sizing_type_of(cx, ty)).collect::<Vec<_>>();
350-
let llty_rec = Type::struct_(cx, lltys.as_slice(), packed);
351-
Struct {
352-
size: machine::llsize_of_alloc(cx, llty_rec),
353-
align: machine::llalign_of_min(cx, llty_rec),
354-
sized: true,
355-
packed: packed,
356-
fields: Vec::from_slice(tys),
357-
}
354+
fn mk_struct(cx: &CrateContext, tys: &[ty::t], packed: bool, scapegoat: ty::t) -> Struct {
355+
let sized = tys.iter().all(|&ty| ty::type_is_sized(cx.tcx(), ty));
356+
let lltys : Vec<Type> = if sized {
357+
tys.iter()
358+
.map(|&ty| type_of::sizing_type_of(cx, ty)).collect()
358359
} else {
359-
// Ignore any dynamically sized fields.
360-
let lltys = tys.iter().filter(|&ty| ty::type_is_sized(cx.tcx(), *ty))
361-
.map(|&ty| type_of::sizing_type_of(cx, ty)).collect::<Vec<_>>();
362-
let llty_rec = Type::struct_(cx, lltys.as_slice(), packed);
363-
Struct {
364-
size: machine::llsize_of_alloc(cx, llty_rec),
365-
align: machine::llalign_of_min(cx, llty_rec),
366-
sized: false,
367-
packed: packed,
368-
fields: Vec::from_slice(tys),
369-
}
360+
tys.iter().filter(|&ty| ty::type_is_sized(cx.tcx(), *ty))
361+
.map(|&ty| type_of::sizing_type_of(cx, ty)).collect()
362+
};
363+
364+
ensure_struct_fits_in_address_space(cx, lltys.as_slice(), packed, scapegoat);
365+
366+
let llty_rec = Type::struct_(cx, lltys.as_slice(), packed);
367+
Struct {
368+
size: machine::llsize_of_alloc(cx, llty_rec),
369+
align: machine::llalign_of_min(cx, llty_rec),
370+
sized: sized,
371+
packed: packed,
372+
fields: Vec::from_slice(tys),
370373
}
371374
}
372375

@@ -461,6 +464,48 @@ pub fn ty_of_inttype(ity: IntType) -> ty::t {
461464
}
462465
}
463466

467+
// LLVM doesn't like types that don't fit in the address space
468+
fn ensure_struct_fits_in_address_space(ccx: &CrateContext,
469+
fields: &[Type],
470+
packed: bool,
471+
scapegoat: ty::t) {
472+
let mut offset = 0;
473+
for &llty in fields.iter() {
474+
if !packed {
475+
let type_align = machine::llalign_of_min(ccx, llty);
476+
offset = roundup(offset, type_align);
477+
}
478+
offset += machine::llsize_of_alloc(ccx, llty);
479+
480+
// We can get away with checking for overflow once per iteration,
481+
// because field sizes are less than 1<<60.
482+
if offset >= ccx.max_obj_size() {
483+
ccx.report_overbig_object(scapegoat);
484+
}
485+
}
486+
}
487+
488+
fn union_size_and_align(sts: &[Struct]) -> (machine::llsize, machine::llalign) {
489+
let size = sts.iter().map(|st| st.size).max().unwrap();
490+
let most_aligned = sts.iter().max_by(|st| st.align).unwrap();
491+
(size, most_aligned.align)
492+
}
493+
494+
fn ensure_enum_fits_in_address_space(ccx: &CrateContext,
495+
discr: IntType,
496+
fields: &[Struct],
497+
scapegoat: ty::t) {
498+
let discr_size = machine::llsize_of_alloc(ccx, ll_inttype(ccx, discr));
499+
let (field_size, field_align) = union_size_and_align(fields);
500+
501+
// This can't overflow because field_size, discr_size, field_align < 1<<60
502+
let total_size = roundup(discr_size, field_align) + field_size;
503+
504+
if total_size >= ccx.max_obj_size() {
505+
ccx.report_overbig_object(scapegoat);
506+
}
507+
}
508+
464509

465510
/**
466511
* LLVM-level types are a little complicated.
@@ -523,13 +568,12 @@ fn generic_type_of(cx: &CrateContext,
523568
// of the size.
524569
//
525570
// FIXME #10604: this breaks when vector types are present.
526-
let size = sts.iter().map(|st| st.size).max().unwrap();
527-
let most_aligned = sts.iter().max_by(|st| st.align).unwrap();
528-
let align = most_aligned.align;
571+
let (size, align) = union_size_and_align(sts.as_slice());
572+
let align_s = align as u64;
529573
let discr_ty = ll_inttype(cx, ity);
530-
let discr_size = machine::llsize_of_alloc(cx, discr_ty) as u64;
531-
let align_units = (size + align - 1) / align - 1;
532-
let pad_ty = match align {
574+
let discr_size = machine::llsize_of_alloc(cx, discr_ty);
575+
let align_units = (size + align_s - 1) / align_s - 1;
576+
let pad_ty = match align_s {
533577
1 => Type::array(&Type::i8(cx), align_units),
534578
2 => Type::array(&Type::i16(cx), align_units),
535579
4 => Type::array(&Type::i32(cx), align_units),
@@ -539,10 +583,10 @@ fn generic_type_of(cx: &CrateContext,
539583
align_units),
540584
_ => fail!("unsupported enum alignment: {:?}", align)
541585
};
542-
assert_eq!(machine::llalign_of_min(cx, pad_ty) as u64, align);
543-
assert_eq!(align % discr_size, 0);
586+
assert_eq!(machine::llalign_of_min(cx, pad_ty), align);
587+
assert_eq!(align_s % discr_size, 0);
544588
let fields = vec!(discr_ty,
545-
Type::array(&discr_ty, align / discr_size - 1),
589+
Type::array(&discr_ty, align_s / discr_size - 1),
546590
pad_ty);
547591
match name {
548592
None => Type::struct_(cx, fields.as_slice(), false),

src/librustc/middle/trans/context.rs

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use middle::trans::debuginfo;
2525
use middle::trans::monomorphize::MonoId;
2626
use middle::trans::type_::{Type, TypeNames};
2727
use middle::ty;
28+
use util::ppaux::Repr;
2829
use util::sha2::Sha256;
2930
use util::nodemap::{NodeMap, NodeSet, DefIdMap};
3031

@@ -717,6 +718,16 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
717718
pub fn trait_cache(&self) -> &RefCell<HashMap<Rc<ty::TraitRef>, traits::Vtable<()>>> {
718719
&self.local.trait_cache
719720
}
721+
722+
pub fn max_obj_size(&self) -> u64 {
723+
1<<31 /* FIXME: select based on architecture */
724+
}
725+
726+
pub fn report_overbig_object(&self, obj: ty::t) -> ! {
727+
self.sess().fatal(
728+
format!("Objects of type `{}` are too big for the current ABI",
729+
obj.repr(self.tcx())).as_slice())
730+
}
720731
}
721732

722733
fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {

src/librustc/middle/trans/type_of.rs

+44-22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ use middle::trans::type_::Type;
2424
use syntax::abi;
2525
use syntax::ast;
2626

27+
use std::num::CheckedMul;
28+
29+
// LLVM doesn't like objects that are too big. Issue #17913
30+
fn ensure_array_fits_in_address_space(ccx: &CrateContext,
31+
llet: Type,
32+
size: machine::llsize,
33+
scapegoat: ty::t) {
34+
let esz = machine::llsize_of_alloc(ccx, llet);
35+
match esz.checked_mul(&size) {
36+
Some(n) if n < ccx.max_obj_size() => {}
37+
_ => { ccx.report_overbig_object(scapegoat) }
38+
}
39+
}
40+
2741
pub fn arg_is_indirect(ccx: &CrateContext, arg_ty: ty::t) -> bool {
2842
!type_is_immediate(ccx, arg_ty)
2943
}
@@ -186,7 +200,10 @@ pub fn sizing_type_of(cx: &CrateContext, t: ty::t) -> Type {
186200
ty::ty_closure(..) => Type::struct_(cx, [Type::i8p(cx), Type::i8p(cx)], false),
187201

188202
ty::ty_vec(ty, Some(size)) => {
189-
Type::array(&sizing_type_of(cx, ty), size as u64)
203+
let llty = sizing_type_of(cx, ty);
204+
let size = size as u64;
205+
ensure_array_fits_in_address_space(cx, llty, size, t);
206+
Type::array(&llty, size)
190207
}
191208

192209
ty::ty_tup(..) | ty::ty_enum(..) | ty::ty_unboxed_closure(..) => {
@@ -196,9 +213,10 @@ pub fn sizing_type_of(cx: &CrateContext, t: ty::t) -> Type {
196213

197214
ty::ty_struct(..) => {
198215
if ty::type_is_simd(cx.tcx(), t) {
199-
let et = ty::simd_type(cx.tcx(), t);
200-
let n = ty::simd_size(cx.tcx(), t);
201-
Type::vector(&type_of(cx, et), n as u64)
216+
let llet = type_of(cx, ty::simd_type(cx.tcx(), t));
217+
let n = ty::simd_size(cx.tcx(), t) as u64;
218+
ensure_array_fits_in_address_space(cx, llet, n, t);
219+
Type::vector(&llet, n)
202220
} else {
203221
let repr = adt::represent_type(cx, t);
204222
adt::sizing_type_of(cx, &*repr, false)
@@ -282,21 +300,21 @@ pub fn type_of(cx: &CrateContext, t: ty::t) -> Type {
282300
ty::ty_uint(t) => Type::uint_from_ty(cx, t),
283301
ty::ty_float(t) => Type::float_from_ty(cx, t),
284302
ty::ty_enum(did, ref substs) => {
285-
// Only create the named struct, but don't fill it in. We
286-
// fill it in *after* placing it into the type cache. This
287-
// avoids creating more than one copy of the enum when one
288-
// of the enum's variants refers to the enum itself.
289-
let repr = adt::represent_type(cx, t);
290-
let tps = substs.types.get_slice(subst::TypeSpace);
291-
let name = llvm_type_name(cx, an_enum, did, tps);
292-
adt::incomplete_type_of(cx, &*repr, name.as_slice())
303+
// Only create the named struct, but don't fill it in. We
304+
// fill it in *after* placing it into the type cache. This
305+
// avoids creating more than one copy of the enum when one
306+
// of the enum's variants refers to the enum itself.
307+
let repr = adt::represent_type(cx, t);
308+
let tps = substs.types.get_slice(subst::TypeSpace);
309+
let name = llvm_type_name(cx, an_enum, did, tps);
310+
adt::incomplete_type_of(cx, &*repr, name.as_slice())
293311
}
294312
ty::ty_unboxed_closure(did, _) => {
295-
// Only create the named struct, but don't fill it in. We
296-
// fill it in *after* placing it into the type cache.
297-
let repr = adt::represent_type(cx, t);
298-
let name = llvm_type_name(cx, an_unboxed_closure, did, []);
299-
adt::incomplete_type_of(cx, &*repr, name.as_slice())
313+
// Only create the named struct, but don't fill it in. We
314+
// fill it in *after* placing it into the type cache.
315+
let repr = adt::represent_type(cx, t);
316+
let name = llvm_type_name(cx, an_unboxed_closure, did, []);
317+
adt::incomplete_type_of(cx, &*repr, name.as_slice())
300318
}
301319

302320
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) | ty::ty_ptr(ty::mt{ty, ..}) => {
@@ -315,8 +333,11 @@ pub fn type_of(cx: &CrateContext, t: ty::t) -> Type {
315333
}
316334
}
317335

318-
ty::ty_vec(ty, Some(n)) => {
319-
Type::array(&type_of(cx, ty), n as u64)
336+
ty::ty_vec(ty, Some(size)) => {
337+
let size = size as u64;
338+
let llty = type_of(cx, ty);
339+
ensure_array_fits_in_address_space(cx, llty, size, t);
340+
Type::array(&llty, size)
320341
}
321342
ty::ty_vec(ty, None) => {
322343
type_of(cx, ty)
@@ -341,9 +362,10 @@ pub fn type_of(cx: &CrateContext, t: ty::t) -> Type {
341362
}
342363
ty::ty_struct(did, ref substs) => {
343364
if ty::type_is_simd(cx.tcx(), t) {
344-
let et = ty::simd_type(cx.tcx(), t);
345-
let n = ty::simd_size(cx.tcx(), t);
346-
Type::vector(&type_of(cx, et), n as u64)
365+
let llet = type_of(cx, ty::simd_type(cx.tcx(), t));
366+
let n = ty::simd_size(cx.tcx(), t) as u64;
367+
ensure_array_fits_in_address_space(cx, llet, n, t);
368+
Type::vector(&llet, n)
347369
} else {
348370
// Only create the named struct, but don't fill it in. We fill it
349371
// in *after* placing it into the type cache. This prevents

0 commit comments

Comments
 (0)