Skip to content

Commit baa8dc7

Browse files
Fix bug with IL lowering on CFront ABI where padding was not correctly accounted for [GH #479]
1 parent b6a58cb commit baa8dc7

9 files changed

Lines changed: 69 additions & 10 deletions

File tree

‎src/lower_il.c‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,20 +1182,19 @@ offset for the field.
11821182
/* Store the base class name. */
11831183
(void)strcpy(name_ptr+prefix_length, temp_name);
11841184
/* Create the field. */
1185-
#if IA64_ABI
11861185
field_ptr = add_field(name_ptr, field_type, field_offset, struct_type,
11871186
/*for_base_class=*/TRUE);
1188-
if (targ_reuse_tail_padding && is_immediate_class_type(field_type) &&
1187+
if (
1188+
#if IA64_ABI
1189+
targ_reuse_tail_padding &&
1190+
#endif /* IA64_ABI */
1191+
is_immediate_class_type(field_type) &&
11891192
class_type_supp(field_type)->compiler_generated &&
11901193
(field_type->size % field_type->alignment) != 0) {
11911194
/* There is padding at the end of the base class subobject in which
11921195
derived class members may be allocated. */
11931196
field_ptr->class_subobject_with_tail_padding = TRUE;
11941197
} /* if */
1195-
#else /* !IA64_ABI */
1196-
field_ptr = add_field(name_ptr, field_type, field_offset, struct_type,
1197-
/*for_base_class=*/TRUE);
1198-
#endif /* IA64_ABI */
11991198
field_ptr->is_lowered_base_class = TRUE;
12001199
} /* add_base_class_dummy_field */
12011200

@@ -8534,13 +8533,21 @@ this routine to do a relatively simple copy of all the fields.
85348533

85358534
ctsp = class_type->variant.class_struct_union.extra_info;
85368535
if ((!class_type->variant.class_struct_union.any_virtual_base_classes
8536+
#if !IA64_ABI
8537+
/* The size as a base can still be smaller than the size of the class
8538+
(in Microsoft mode, which ignores some alignment requirements when
8539+
computing the size of a base class). */
8540+
&& (type_is(class_type, tk_union) ||
8541+
ctsp->size_without_virtual_base_classes == class_type->size)
8542+
#endif /* !IA64_ABI */
85378543
#if CFRONT_OBJECT_CODE_COMPATIBILITY
8538-
|| !class_has_independently_allocated_virtual_base_classes(class_type)
8544+
|| (class_type->variant.class_struct_union.any_virtual_base_classes &&
8545+
!class_has_independently_allocated_virtual_base_classes(class_type))
85398546
#endif /* CFRONT_OBJECT_CODE_COMPATIBILITY */
85408547
)
85418548
#if IA64_ABI
85428549
&& (!targ_reuse_tail_padding ||
8543-
class_type->kind == (a_type_kind)tk_union ||
8550+
type_is(class_type, tk_union) ||
85448551
ctsp->size_without_virtual_base_classes == class_type->size)
85458552
#endif /* IA64_ABI */
85468553
) {
@@ -10020,7 +10027,7 @@ Do IL lowering of the indicated field and everything under it.
1002010027
mark_as_visited(field);
1002110028
lower_source_correspondence(&field->source_corresp);
1002210029
lower_os_type(field->type);
10023-
#if BACK_END_IS_C_GEN_BE && IA64_ABI && MAINTAIN_NEEDED_FLAGS
10030+
#if BACK_END_IS_C_GEN_BE && MAINTAIN_NEEDED_FLAGS
1002410031
if (field->class_subobject_with_tail_padding) {
1002510032
/* Ensure that the corresponding subobject type definition is not
1002610033
removed from the IL, as it will be needed by the C-generating back
@@ -10031,7 +10038,7 @@ Do IL lowering of the indicated field and everything under it.
1003110038
class_type_supp(ftp)->subobject_partner->
1003210039
variant.class_struct_union.keep_definition_in_il = TRUE;
1003310040
} /* if */
10034-
#endif /* BACK_END_IS_C_GEN_BE && IA64_ABI && MAINTAIN_NEEDED_FLAGS */
10041+
#endif /* BACK_END_IS_C_GEN_BE && MAINTAIN_NEEDED_FLAGS */
1003510042
} /* if */
1003610043
} /* lower_field */
1003710044

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[GH #479]
2+
Lowered layout of Microsoft-mode base classes without the IA-64 ABI
3+
4+
In front ends configured with IA64_ABI set to FALSE, IL lowering did not
5+
account for the padding at the end of a base class subobject. In Microsoft
6+
mode, a base class whose alignment is stricter than the platform default
7+
can occupy less space as a base class subobject than its full size, so
8+
later members of the derived class are allocated in what would otherwise be
9+
its tail padding. IL lowering nonetheless emitted such a base class as a
10+
field of the complete base class type, so the C compiler placed the derived
11+
class members at larger offsets than the front end had computed (e.g., than
12+
those used for sizeof and offsetof). Such a base class is now
13+
lowered to a type-as-subobject whose members are emitted inline, as is done
14+
for IA-64 ABI configurations. For example, with --ms_c++20:
15+
16+
struct B { alignas(16) char arr[2]; };
17+
struct D : B { char d; }; // d is at offset 8.
18+
void f(D *p) { p->d = 'x'; } // Previously accessed offset 16.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eccp -DTEST_NUMBER=1 --c++11 --g++ --gnu_version=150200 Test_name.c
2+
"Test_name.c", line 13: warning: offsetof applied to a type other than a standard-layout class
3+
long fe_offset = __builtin_offsetof(D, d); // Front end's layout.
4+
^
5+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./a.out
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eccp -DTEST_NUMBER=2 --c++20 --clang --clang_version=210000 Test_name.c
2+
"Test_name.c", line 13: warning: offsetof applied to a type other than a standard-layout class
3+
long fe_offset = __builtin_offsetof(D, d); // Front end's layout.
4+
^
5+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./a.out
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eccp -DTEST_NUMBER=3 --ms_c++20 --microsoft_version 1950 Test_name.c
2+
"Test_name.c", line 13: warning: offsetof applied to a type other than a standard-layout class
3+
long fe_offset = __builtin_offsetof(D, d); // Front end's layout.
4+
^
5+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./a.out
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//type:rp
2+
//options:--c++11 --g++ --gnu_version=150200:--c++20 --clang --clang_version=210000:--ms_c++20 --microsoft_version 1950
3+
// A base class whose alignment requirement leaves padding at the end of the
4+
// base class subobject must have that padding accounted for when IL lowering
5+
// places the members of a derived class. The offset of "d" in the lowered
6+
// code must match the offset computed by the front end.
7+
8+
struct B { alignas(16) char c; };
9+
struct D : B { char d; };
10+
11+
int main() {
12+
D x;
13+
long fe_offset = __builtin_offsetof(D, d); // Front end's layout.
14+
long be_offset = (char *)&x.d - (char *)&x; // Lowered C's layout.
15+
return fe_offset != be_offset;
16+
}

0 commit comments

Comments
 (0)