@@ -511,7 +511,10 @@ static unsigned convert_struct_offset(Type *lty, unsigned byte_offset)
511
511
const DataLayout &DL = jl_data_layout;
512
512
const StructLayout *SL = DL.getStructLayout (cast<StructType>(lty));
513
513
unsigned idx = SL->getElementContainingOffset (byte_offset);
514
- assert (SL->getElementOffset (idx) == byte_offset);
514
+ if (SL->getElementOffset (idx) != byte_offset) {
515
+ // sentinel error value
516
+ return (unsigned )(-1 );
517
+ }
515
518
return idx;
516
519
}
517
520
@@ -1692,9 +1695,15 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
1692
1695
else if (!jl_field_isptr (jt, idx) && jl_is_uniontype (jfty)) {
1693
1696
int fsz = jl_field_size (jt, idx) - 1 ;
1694
1697
unsigned ptindex = convert_struct_offset (ctx, T, byte_offset + fsz);
1698
+ if (ptindex == (unsigned )(-1 )) {
1699
+ jl_errorf (" Cannot generate accessor code because of incompatible alignment requirements" );
1700
+ }
1695
1701
AllocaInst *lv = NULL ;
1696
1702
if (fsz > 0 ) {
1697
1703
unsigned st_idx = convert_struct_offset (ctx, T, byte_offset);
1704
+ if (ptindex == (unsigned )(-1 )) {
1705
+ jl_errorf (" Cannot generate accessor code because of incompatible alignment requirements" );
1706
+ }
1698
1707
IntegerType *ET = cast<IntegerType>(T->getStructElementType (st_idx));
1699
1708
unsigned align = (ET->getBitWidth () + 7 ) / 8 ;
1700
1709
lv = emit_static_alloca (ctx, ET);
@@ -1724,12 +1733,16 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
1724
1733
}
1725
1734
else {
1726
1735
unsigned st_idx;
1727
- if (isa<ArrayType>(T))
1736
+ if (isa<ArrayType>(T)) {
1728
1737
st_idx = idx;
1729
- else if (isa<StructType>(T))
1738
+ } else if (isa<StructType>(T)) {
1730
1739
st_idx = convert_struct_offset (ctx, T, byte_offset);
1731
- else
1740
+ if (st_idx == (unsigned )(-1 )) {
1741
+ jl_errorf (" Cannot generate accessor code because of incompatible alignment requirements" );
1742
+ }
1743
+ } else {
1732
1744
llvm_unreachable (" encountered incompatible type for a struct" );
1745
+ }
1733
1746
fldv = ctx.builder .CreateExtractValue (obj, makeArrayRef (st_idx));
1734
1747
}
1735
1748
if (maybe_null && jl_field_isptr (jt, idx))
@@ -2107,8 +2120,12 @@ static jl_value_t *static_constant_instance(Constant *constant, jl_value_t *jt)
2107
2120
return NULL ; // TODO: handle this?
2108
2121
}
2109
2122
unsigned llvm_idx = i;
2110
- if (i > 0 && isa<StructType>(constant->getType ()))
2123
+ if (i > 0 && isa<StructType>(constant->getType ())) {
2111
2124
llvm_idx = convert_struct_offset (constant->getType (), jl_field_offset (jst, i));
2125
+ if (llvm_idx == (unsigned )(-1 )) {
2126
+ jl_errorf (" Cannot create an static instance of %s because of incompatible alignment requirements" , jl_symbol_name (jst->name ->name ));
2127
+ }
2128
+ }
2112
2129
Constant *fld = constant->getAggregateElement (llvm_idx);
2113
2130
flds[i] = static_constant_instance (fld, ft);
2114
2131
if (flds[i] == NULL ) {
@@ -2617,6 +2634,10 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
2617
2634
Value *dest = NULL ;
2618
2635
unsigned offs = jl_field_offset (sty, i);
2619
2636
unsigned llvm_idx = (i > 0 && isa<StructType>(lt)) ? convert_struct_offset (ctx, lt, offs) : i;
2637
+ if (llvm_idx == (unsigned )(-1 )) {
2638
+ jl_errorf (" Cannot create an instance of %s because of incompatible alignment requirements" , jl_symbol_name (sty->name ->name ));
2639
+ }
2640
+
2620
2641
if (!init_as_value) {
2621
2642
// avoid unboxing the argument explicitly
2622
2643
// and use memcpy instead
@@ -2640,6 +2661,9 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
2640
2661
// then load it and combine with the tindex.
2641
2662
// But more efficient to just store it directly.
2642
2663
unsigned ptindex = convert_struct_offset (ctx, lt, offs + fsz);
2664
+ if (ptindex == (unsigned )(-1 )) {
2665
+ jl_errorf (" Cannot create an instance of %s because of incompatible alignment requirements" , jl_symbol_name (sty->name ->name ));
2666
+ }
2643
2667
if (fsz > 0 && !fval_info.isghost ) {
2644
2668
Type *ET = IntegerType::get (jl_LLVMContext, 8 * al);
2645
2669
assert (lt->getStructElementType (llvm_idx) == ET);
@@ -2694,6 +2718,9 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
2694
2718
unsigned offs = jl_field_offset (sty, i);
2695
2719
int fsz = jl_field_size (sty, i) - 1 ;
2696
2720
unsigned llvm_idx = convert_struct_offset (ctx, cast<StructType>(lt), offs + fsz);
2721
+ if (llvm_idx == (unsigned )(-1 )) {
2722
+ jl_errorf (" Cannot create an instance of %s because of incompatible alignment requirements" , jl_symbol_name (sty->name ->name ));
2723
+ }
2697
2724
if (init_as_value)
2698
2725
strct = ctx.builder .CreateInsertValue (strct, ConstantInt::get (T_int8, 0 ), makeArrayRef (llvm_idx));
2699
2726
else
0 commit comments