On Mon, Jan 15, 2018 at 7:02 AM, H.J. Lu <hjl.to...@gmail.com> wrote: > On Mon, Jan 15, 2018 at 1:42 AM, Jakub Jelinek <ja...@redhat.com> wrote: >> On Sun, Jan 14, 2018 at 06:29:54AM -0800, H.J. Lu wrote: >>> + if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field)) >>> + { >>> + tree field_type = TREE_TYPE (field); >>> + unsigned int type_align = TYPE_ALIGN (field_type); >>> + tree context = DECL_CONTEXT (field); >>> + unsigned int record_align = TYPE_ALIGN (context); >>> + if ((record_align % type_align) != 0) >>> + return context; >>> + type_align /= BITS_PER_UNIT; >>> + unsigned HOST_WIDE_INT field_off >>> + = (tree_to_uhwi (DECL_FIELD_OFFSET (field)) >>> + + (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)) >>> + / BITS_PER_UNIT)); >> >> This has the same bug I've just created PR83844 for, you can't assume >> DECL_FIELD_OFFSET is INTEGER_CST that fits into UHWI, and also we have >> byte_position wrapper that should be used to compute the offset from >> DECL_FIELD_*OFFSET. > > Here is the updated patch to use byte_position wrapper. OK for trunk? >
Here is the updated patch not to warn: struct pair_t { char c; __int128_t i; } __attribute__((packed)); typedef struct unaligned_int128_t_ { __int128_t value; } __attribute__((packed)) unaligned_int128_t; struct pair_t p = {0, 1}; unaligned_int128_t *addr = (unaligned_int128_t *)(&p.i); int main() { addr->value = ~(__int128_t)0; return (p.i != 1) ? 0 : 1; } by properly checking the expected alignment against the field alignment. -- H.J.
From 54b68f11c18971d1371d5bb5bde7b0c1d3e6ee7b Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.to...@gmail.com> Date: Fri, 12 Jan 2018 21:12:05 -0800 Subject: [PATCH] C/C++: Add -Waddress-of-packed-member When address of packed member of struct or union is taken, it may result in an unaligned pointer value. This patch adds -Waddress-of-packed-member to warn it: $ cat x.i struct pair_t { char c; int i; } __attribute__ ((packed)); extern struct pair_t p; int *addr = &p.i; $ gcc -O2 -S x.i x.i:8:13: warning: initialization of 'int *' from address of packed member of 'struct pair_t' may result in an unaligned pointer value [-Waddress-of-packed-member] int *addr = &p.i; ^ $ This warning is enabled by default. gcc/c/ PR c/51628 * doc/invoke.texi: Document -Wno-address-of-packed-member. gcc/c-family/ PR c/51628 * c-common.h (warn_for_address_of_packed_member): New. * c-warn.c (warn_for_address_of_packed_member): New function. * c.opt: Add -Wno-address-of-packed-member. gcc/c/ PR c/51628 * c-typeck.c (convert_for_assignment): Call warn_for_address_of_packed_member. Issue an warning if address of packed member is taken. gcc/cp/ PR c/51628 * call.c (convert_for_arg_passing): Call warn_for_address_of_packed_member. Issue an warning if address of packed member is taken. * typeck.c (convert_for_assignment): Likewise. gcc/testsuite/ PR c/51628 * c-c++-common/pr51628-1.c: New tests. * c-c++-common/pr51628-10.c: Likewise. * c-c++-common/pr51628-2.c: Likewise. * c-c++-common/pr51628-3.c: Likewise. * c-c++-common/pr51628-4.c: Likewise. * c-c++-common/pr51628-5.c: Likewise. * c-c++-common/pr51628-6.c: Likewise. * c-c++-common/pr51628-7.c: Likewise. * c-c++-common/pr51628-8.c: Likewise. * c-c++-common/pr51628-9.c: Likewise. * gcc.dg/pr51628-10.c: Likewise. * gcc.dg/pr51628-11.c: Likewise. * c-c++-common/ubsan/align-10.c: Add -Wno-address-of-packed-member. * c-c++-common/ubsan/align-2.c: Likewise. * c-c++-common/ubsan/align-4.c: Likewise. * c-c++-common/ubsan/align-6.c: Likewise. * c-c++-common/ubsan/align-7.c: Likewise. * c-c++-common/ubsan/align-8.c: Likewise. * g++.dg/ubsan/align-2.C: Likewise. * gcc.target/i386/avx512bw-vmovdqu16-2.c: Likewise. * gcc.target/i386/avx512f-vmovdqu32-2.c: Likewise. * gcc.target/i386/avx512f-vmovdqu64-2.c: Likewise. * gcc.target/i386/avx512vl-vmovdqu16-2.c: Likewise. * gcc.target/i386/avx512vl-vmovdqu32-2.c: Likewise. * gcc.target/i386/avx512vl-vmovdqu64-2.c: Likewise. --- gcc/c-family/c-common.h | 1 + gcc/c-family/c-warn.c | 56 ++++++++++++++++++++++ gcc/c-family/c.opt | 4 ++ gcc/c/c-typeck.c | 40 +++++++++++++++- gcc/cp/call.c | 8 ++++ gcc/cp/typeck.c | 41 ++++++++++++++++ gcc/doc/invoke.texi | 11 ++++- gcc/testsuite/c-c++-common/pr51628-1.c | 29 +++++++++++ gcc/testsuite/c-c++-common/pr51628-10.c | 24 ++++++++++ gcc/testsuite/c-c++-common/pr51628-2.c | 29 +++++++++++ gcc/testsuite/c-c++-common/pr51628-3.c | 35 ++++++++++++++ gcc/testsuite/c-c++-common/pr51628-4.c | 35 ++++++++++++++ gcc/testsuite/c-c++-common/pr51628-5.c | 35 ++++++++++++++ gcc/testsuite/c-c++-common/pr51628-6.c | 35 ++++++++++++++ gcc/testsuite/c-c++-common/pr51628-7.c | 29 +++++++++++ gcc/testsuite/c-c++-common/pr51628-8.c | 36 ++++++++++++++ gcc/testsuite/c-c++-common/pr51628-9.c | 36 ++++++++++++++ gcc/testsuite/c-c++-common/ubsan/align-10.c | 2 +- gcc/testsuite/c-c++-common/ubsan/align-2.c | 2 +- gcc/testsuite/c-c++-common/ubsan/align-4.c | 2 +- gcc/testsuite/c-c++-common/ubsan/align-6.c | 2 +- gcc/testsuite/c-c++-common/ubsan/align-7.c | 2 +- gcc/testsuite/c-c++-common/ubsan/align-8.c | 2 +- gcc/testsuite/g++.dg/ubsan/align-2.C | 2 +- gcc/testsuite/gcc.dg/pr51628-10.c | 23 +++++++++ gcc/testsuite/gcc.dg/pr51628-11.c | 26 ++++++++++ .../gcc.target/i386/avx512bw-vmovdqu16-2.c | 2 +- .../gcc.target/i386/avx512f-vmovdqu32-2.c | 2 +- .../gcc.target/i386/avx512f-vmovdqu64-2.c | 2 +- .../gcc.target/i386/avx512vl-vmovdqu16-2.c | 2 +- .../gcc.target/i386/avx512vl-vmovdqu32-2.c | 2 +- .../gcc.target/i386/avx512vl-vmovdqu64-2.c | 2 +- 32 files changed, 543 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/pr51628-1.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-10.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-2.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-3.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-4.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-5.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-6.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-7.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-8.c create mode 100644 gcc/testsuite/c-c++-common/pr51628-9.c create mode 100644 gcc/testsuite/gcc.dg/pr51628-10.c create mode 100644 gcc/testsuite/gcc.dg/pr51628-11.c diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index d090881e95d..ef31e4d0aa2 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -1384,6 +1384,7 @@ extern void c_do_switch_warnings (splay_tree, location_t, tree, tree, bool, bool); extern void warn_for_omitted_condop (location_t, tree); extern void warn_for_restrict (unsigned, tree *, unsigned); +extern tree warn_for_address_of_packed_member (tree type, tree rhs); /* Places where an lvalue, or modifiable lvalue, may be required. Used to select diagnostic messages in lvalue_error and diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c index 7d87c455ec0..c755df13ca1 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -2576,3 +2576,59 @@ warn_for_multistatement_macros (location_t body_loc, location_t next_loc, inform (guard_loc, "some parts of macro expansion are not guarded by " "this %qs clause", guard_tinfo_to_string (keyword)); } + +/* Return struct or union type if the right hand type, RHS, is the + address of packed member of struct or union when assigning to TYPE. + Otherwise, return NULL_TREE. */ + +tree +warn_for_address_of_packed_member (tree type, tree rhs) +{ + if (!warn_address_of_packed_member || !POINTER_TYPE_P (type)) + return NULL_TREE; + + if (TREE_CODE (rhs) == NOP_EXPR) + rhs = TREE_OPERAND (rhs, 0); + + tree base; + tree field = NULL_TREE; + tree rhs_type; + + switch (TREE_CODE (rhs)) + { + case ADDR_EXPR: + base = TREE_OPERAND (rhs, 0); + while (TREE_CODE (base) == ARRAY_REF) + base = TREE_OPERAND (base, 0); + if (TREE_CODE (base) != COMPONENT_REF) + return NULL_TREE; + field = TREE_OPERAND (base, 1); + break; + case COMPONENT_REF: + rhs_type = TREE_TYPE (rhs); + if (TREE_CODE (rhs_type) != ARRAY_TYPE + || (TYPE_MAIN_VARIANT (TREE_TYPE (type)) + != TYPE_MAIN_VARIANT (TREE_TYPE (rhs_type)))) + return NULL_TREE; + field = TREE_OPERAND (rhs, 1); + break; + default: + return NULL_TREE; + } + + if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field)) + { + /* Check the expected alignment against the field alignment. */ + unsigned int type_align = TYPE_ALIGN (TREE_TYPE (type)); + tree context = DECL_CONTEXT (field); + unsigned int record_align = TYPE_ALIGN (context); + if ((record_align % type_align) != 0) + return context; + tree field_off = byte_position (field); + if (!multiple_of_p (TREE_TYPE (field_off), field_off, + size_int (type_align / BITS_PER_UNIT))) + return context; + } + + return NULL_TREE; +} diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 16a515afd55..686d52a2055 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -599,6 +599,10 @@ Wincompatible-pointer-types C ObjC Var(warn_incompatible_pointer_types) Init(1) Warning Warn when there is a conversion between pointers that have incompatible types. +Waddress-of-packed-member +C ObjC C++ ObjC++ Var(warn_address_of_packed_member) Init(1) Warning +Warn when the address of packed member of struct or union is taken. + Winit-self C ObjC C++ ObjC++ Var(warn_init_self) Warning LangEnabledBy(C++ ObjC++,Wall) Warn about variables which are initialized to themselves. diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index e22bc740bca..8e31d6cc5fd 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -6495,7 +6495,45 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type, } if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) - return rhs; + { + tree context = warn_for_address_of_packed_member (type, orig_rhs); + if (context) + switch (errtype) + { + case ic_argpass: + warning_at (expr_loc, OPT_Waddress_of_packed_member, + "passing argument %d %qT of %qE from address " + "of packed member of %qT may result in an " + "unaligned pointer value", + parmnum, type, rname, context); + break; + case ic_assign: + warning_at (location, OPT_Waddress_of_packed_member, + "assignment to %qT from address of packed " + "member of %qT may result in an unaligned " + "pointer value", + type, context); + break; + case ic_init: + warning_at (location, OPT_Waddress_of_packed_member, + "initialization of %qT from address of packed " + "member of %qT may result in an unaligned " + "pointer value", + type, context); + break; + case ic_return: + warning_at (location, OPT_Waddress_of_packed_member, + "returning address of packed member of %qT " + "from a function with return type %qT may " + "result in an unaligned pointer value", + context, type); + break; + default: + gcc_unreachable (); + } + + return rhs; + } if (coder == VOID_TYPE) { diff --git a/gcc/cp/call.c b/gcc/cp/call.c index f5542850cea..8672ab0d58c 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -7416,6 +7416,14 @@ convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain) } maybe_warn_parm_abi (type, EXPR_LOC_OR_LOC (val, input_location)); } + + tree context = warn_for_address_of_packed_member (type, val); + if (context) + warning (OPT_Waddress_of_packed_member, + "passing argument of type %qT from address of member of " + "packed %qT may result in an unaligned pointer value", + type, context); + return val; } diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index d0adb798278..78ed0d61f8b 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -8887,6 +8887,47 @@ convert_for_assignment (tree type, tree rhs, TREE_NO_WARNING (rhs) = 1; } + tree context = warn_for_address_of_packed_member (type, rhs); + if (context) + switch (errtype) + { + case ICR_ARGPASS: + case ICR_DEFAULT_ARGUMENT: + warning (OPT_Waddress_of_packed_member, + "passing argument %d %qT of %qD from address of " + "packed member of %qT may result in an unaligned " + "pointer value", + parmnum, type, fndecl, context); + break; + case ICR_CONVERTING: + warning (OPT_Waddress_of_packed_member, + "converting to %qT from address of packed member of " + "%qT may result in an unaligned pointer value", + type, context); + break; + case ICR_ASSIGN: + warning (OPT_Waddress_of_packed_member, + "assignment to %qT from address of packed member of " + "%qT may result in an unaligned pointer value", + type, context); + break; + case ICR_INIT: + warning (OPT_Waddress_of_packed_member, + "initialization of %qT from address of packed member " + "of %qT may result in an unaligned pointer value", + type, context); + break; + case ICR_RETURN: + warning (OPT_Waddress_of_packed_member, + "returning address of packed member of %qT from a " + "function with return type %qT may result in an " + "unaligned pointer value", + context, type); + break; + default: + gcc_unreachable (); + } + return perform_implicit_conversion_flags (strip_top_quals (type), rhs, complain, flags); } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 98b73db17af..b83d113b76d 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -259,8 +259,8 @@ Objective-C and Objective-C++ Dialects}. @xref{Warning Options,,Options to Request or Suppress Warnings}. @gccoptlist{-fsyntax-only -fmax-errors=@var{n} -Wpedantic @gol -pedantic-errors @gol --w -Wextra -Wall -Waddress -Waggregate-return @gol --Walloc-zero -Walloc-size-larger-than=@var{n} +-w -Wextra -Wall -Waddress -Waddress-of-packed-member @gol +-Waggregate-return -Walloc-zero -Walloc-size-larger-than=@var{n} @gol -Walloca -Walloca-larger-than=@var{n} @gol -Wno-aggressive-loop-optimizations -Warray-bounds -Warray-bounds=@var{n} @gol -Wno-attributes -Wbool-compare -Wbool-operation @gol @@ -6379,6 +6379,13 @@ behavior and are not portable in C, so they usually indicate that the programmer intended to use @code{strcmp}. This warning is enabled by @option{-Wall}. +@item -Waddress-of-packed-member +@opindex Waddress-of-packed-member +@opindex Wno-address-of-packed-member +Warn when the address of packed member of struct or union is taken, +which usually results in an unaligned pointer value. This is +enabled by default. + @item -Wlogical-op @opindex Wlogical-op @opindex Wno-logical-op diff --git a/gcc/testsuite/c-c++-common/pr51628-1.c b/gcc/testsuite/c-c++-common/pr51628-1.c new file mode 100644 index 00000000000..5324f9cc964 --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-1.c @@ -0,0 +1,29 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + int x; + int i; +} __attribute__((packed, aligned (4))); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = &p.i; + +int * +foo (void) +{ + struct pair_t arr[2] = { { 1, 10 }, { 2, 20 } }; + int *p0, *p1; + p0 = &arr[0].i; + bar (p0); + p1 = &arr[1].i; + bar (p1); + bar (&p.i); + x = &p.i; + return &p.i; +} diff --git a/gcc/testsuite/c-c++-common/pr51628-10.c b/gcc/testsuite/c-c++-common/pr51628-10.c new file mode 100644 index 00000000000..085fe1608c4 --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-10.c @@ -0,0 +1,24 @@ +/* PR c/51628. */ +/* { dg-do run { target int128 } } */ +/* { dg-options "-O2" } */ + +struct pair_t +{ + char c; + __int128_t i; +} __attribute__ ((packed)); + +typedef struct unaligned_int128_t_ +{ + __int128_t value; +} __attribute__((packed)) unaligned_int128_t; + +struct pair_t p = {0, 1}; +unaligned_int128_t *addr = (unaligned_int128_t *) &p.i; + +int +main() +{ + addr->value = ~(__int128_t)0; + return (p.i != 1) ? 0 : 1; +} diff --git a/gcc/testsuite/c-c++-common/pr51628-2.c b/gcc/testsuite/c-c++-common/pr51628-2.c new file mode 100644 index 00000000000..abfb84ddd05 --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-2.c @@ -0,0 +1,29 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + int x; + int i; +} __attribute__((packed, aligned (8))); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = &p.i; + +int * +foo (void) +{ + struct pair_t arr[2] = { { 1, 10 }, { 2, 20 } }; + int *p0, *p1; + p0 = &arr[0].i; + bar (p0); + p1 = &arr[1].i; + bar (p1); + bar (&p.i); + x = &p.i; + return &p.i; +} diff --git a/gcc/testsuite/c-c++-common/pr51628-3.c b/gcc/testsuite/c-c++-common/pr51628-3.c new file mode 100644 index 00000000000..0ea94c845a0 --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-3.c @@ -0,0 +1,35 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + int x; + int i; +} __attribute__((packed, aligned (2))); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + +int * +foo (void) +{ + struct pair_t arr[2] = { { 1, 10 }, { 2, 20 } }; + int *p0, *p1; + p0 = &arr[0].i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p0); + p1 = &arr[1].i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p1); + bar (&p.i); +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + x = &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + return &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ +} diff --git a/gcc/testsuite/c-c++-common/pr51628-4.c b/gcc/testsuite/c-c++-common/pr51628-4.c new file mode 100644 index 00000000000..c4c1fb72d6d --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-4.c @@ -0,0 +1,35 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + int x; + int i; +} __attribute__((packed)); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + +int * +foo (void) +{ + struct pair_t arr[2] = { { 1, 10 }, { 2, 20 } }; + int *p0, *p1; + p0 = &arr[0].i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p0); + p1 = &arr[1].i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p1); + bar (&p.i); +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + x = &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + return &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ +} diff --git a/gcc/testsuite/c-c++-common/pr51628-5.c b/gcc/testsuite/c-c++-common/pr51628-5.c new file mode 100644 index 00000000000..9d7c309a0ef --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-5.c @@ -0,0 +1,35 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + char x; + int i; +} __attribute__((packed)); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + +int * +foo (void) +{ + struct pair_t arr[2] = { { 1, 10 }, { 2, 20 } }; + int *p0, *p1; + p0 = &arr[0].i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p0); + p1 = &arr[1].i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p1); + bar (&p.i); +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + x = &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + return &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ +} diff --git a/gcc/testsuite/c-c++-common/pr51628-6.c b/gcc/testsuite/c-c++-common/pr51628-6.c new file mode 100644 index 00000000000..52aa07a4cf3 --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-6.c @@ -0,0 +1,35 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + char x; + int i; +} __attribute__((packed, aligned (4))); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + +int * +foo (void) +{ + struct pair_t arr[2] = { { 1, 10 }, { 2, 20 } }; + int *p0, *p1; + p0 = &arr[0].i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p0); + p1 = &arr[1].i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p1); + bar (&p.i); +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + x = &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + return &p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ +} diff --git a/gcc/testsuite/c-c++-common/pr51628-7.c b/gcc/testsuite/c-c++-common/pr51628-7.c new file mode 100644 index 00000000000..ae4a681f966 --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-7.c @@ -0,0 +1,29 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + int x; + int i[4]; +} __attribute__((packed, aligned (4))); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = p.i; + +int * +foo (struct pair_t *p) +{ + int *p0, *p1; + p0 = p->i; + bar (p0); + p1 = &p->i[1]; + bar (p1); + bar (p->i); + bar (&p->i[2]); + x = p->i; + return &p->i[3]; +} diff --git a/gcc/testsuite/c-c++-common/pr51628-8.c b/gcc/testsuite/c-c++-common/pr51628-8.c new file mode 100644 index 00000000000..cc2dae096ae --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-8.c @@ -0,0 +1,36 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + char x; + int i[4]; +} __attribute__ ((packed, aligned (4))); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + +int * +foo (struct pair_t *p) +{ + int *p0, *p1; + p0 = p->i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p0); + p1 = &p->i[1]; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p1); + bar (p->i); +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (&p->i[2]); +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + x = p->i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + return &p->i[3]; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ +} diff --git a/gcc/testsuite/c-c++-common/pr51628-9.c b/gcc/testsuite/c-c++-common/pr51628-9.c new file mode 100644 index 00000000000..0470aa3b93d --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr51628-9.c @@ -0,0 +1,36 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +struct pair_t +{ + int x; + int i[4]; +} __attribute__ ((packed)); + +extern struct pair_t p; +extern int *x; +extern void bar (int *); + +int *addr = p.i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + +int * +foo (struct pair_t *p) +{ + int *p0, *p1; + p0 = p->i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p0); + p1 = &p->i[1]; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (p1); + bar (p->i); +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + bar (&p->i[2]); +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + x = p->i; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + return &p->i[3]; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ +} diff --git a/gcc/testsuite/c-c++-common/ubsan/align-10.c b/gcc/testsuite/c-c++-common/ubsan/align-10.c index 56ae9ebfe30..6210533173c 100644 --- a/gcc/testsuite/c-c++-common/ubsan/align-10.c +++ b/gcc/testsuite/c-c++-common/ubsan/align-10.c @@ -1,6 +1,6 @@ /* Limit this to known non-strict alignment targets. */ /* { dg-do run { target { i?86-*-linux* x86_64-*-linux* } } } */ -/* { dg-options "-O -fsanitize=alignment -fsanitize-recover=alignment" } */ +/* { dg-options "-O -fsanitize=alignment -fsanitize-recover=alignment -Wno-address-of-packed-member" } */ struct R { int a; } r; struct S { struct R a; char b; long long c; short d[10]; }; diff --git a/gcc/testsuite/c-c++-common/ubsan/align-2.c b/gcc/testsuite/c-c++-common/ubsan/align-2.c index 071de8c202a..336b1c3c907 100644 --- a/gcc/testsuite/c-c++-common/ubsan/align-2.c +++ b/gcc/testsuite/c-c++-common/ubsan/align-2.c @@ -1,6 +1,6 @@ /* Limit this to known non-strict alignment targets. */ /* { dg-do run { target { i?86-*-linux* x86_64-*-linux* } } } */ -/* { dg-options "-fsanitize=alignment" } */ +/* { dg-options "-fsanitize=alignment -Wno-address-of-packed-member" } */ struct S { int a; char b; long long c; short d[10]; }; struct T { char a; long long b; }; diff --git a/gcc/testsuite/c-c++-common/ubsan/align-4.c b/gcc/testsuite/c-c++-common/ubsan/align-4.c index 3252595d330..d5feeee29c6 100644 --- a/gcc/testsuite/c-c++-common/ubsan/align-4.c +++ b/gcc/testsuite/c-c++-common/ubsan/align-4.c @@ -1,6 +1,6 @@ /* Limit this to known non-strict alignment targets. */ /* { dg-do run { target { i?86-*-linux* x86_64-*-linux* } } } */ -/* { dg-options "-fsanitize=null,alignment" } */ +/* { dg-options "-fsanitize=null,alignment -Wno-address-of-packed-member" } */ #include "align-2.c" diff --git a/gcc/testsuite/c-c++-common/ubsan/align-6.c b/gcc/testsuite/c-c++-common/ubsan/align-6.c index 3364746fb27..0302b7b8894 100644 --- a/gcc/testsuite/c-c++-common/ubsan/align-6.c +++ b/gcc/testsuite/c-c++-common/ubsan/align-6.c @@ -1,6 +1,6 @@ /* Limit this to known non-strict alignment targets. */ /* { dg-do run { target { i?86-*-linux* x86_64-*-linux* } } } */ -/* { dg-options "-O -fsanitize=alignment -fsanitize-recover=alignment" } */ +/* { dg-options "-O -fsanitize=alignment -fsanitize-recover=alignment -Wno-address-of-packed-member" } */ struct S { int a; char b; long long c; short d[10]; }; struct T { char a; long long b; }; diff --git a/gcc/testsuite/c-c++-common/ubsan/align-7.c b/gcc/testsuite/c-c++-common/ubsan/align-7.c index ec4e87f56d5..dd1e8c91cef 100644 --- a/gcc/testsuite/c-c++-common/ubsan/align-7.c +++ b/gcc/testsuite/c-c++-common/ubsan/align-7.c @@ -1,6 +1,6 @@ /* Limit this to known non-strict alignment targets. */ /* { dg-do run { target { i?86-*-linux* x86_64-*-linux* } } } */ -/* { dg-options "-O -fsanitize=alignment -fno-sanitize-recover=alignment -fdump-tree-sanopt-details" } */ +/* { dg-options "-O -fsanitize=alignment -fno-sanitize-recover=alignment -Wno-address-of-packed-member -fdump-tree-sanopt-details" } */ /* { dg-skip-if "" { *-*-* } { "-flto -fno-fat-lto-objects" } } */ /* { dg-shouldfail "ubsan" } */ diff --git a/gcc/testsuite/c-c++-common/ubsan/align-8.c b/gcc/testsuite/c-c++-common/ubsan/align-8.c index 61c1ceb6682..5fe0e0fe931 100644 --- a/gcc/testsuite/c-c++-common/ubsan/align-8.c +++ b/gcc/testsuite/c-c++-common/ubsan/align-8.c @@ -1,6 +1,6 @@ /* Limit this to known non-strict alignment targets. */ /* { dg-do run { target { i?86-*-linux* x86_64-*-linux* } } } */ -/* { dg-options "-O -fsanitize=alignment -fsanitize-undefined-trap-on-error -fdump-tree-sanopt-details" } */ +/* { dg-options "-O -fsanitize=alignment -fsanitize-undefined-trap-on-error -Wno-address-of-packed-member -fdump-tree-sanopt-details" } */ /* { dg-skip-if "" { *-*-* } { "-flto -fno-fat-lto-objects" } } */ /* { dg-shouldfail "ubsan" } */ diff --git a/gcc/testsuite/g++.dg/ubsan/align-2.C b/gcc/testsuite/g++.dg/ubsan/align-2.C index 3e4f5485d02..c97ede88392 100644 --- a/gcc/testsuite/g++.dg/ubsan/align-2.C +++ b/gcc/testsuite/g++.dg/ubsan/align-2.C @@ -1,6 +1,6 @@ // Limit this to known non-strict alignment targets. // { dg-do run { target { i?86-*-linux* x86_64-*-linux* } } } -// { dg-options "-fsanitize=alignment -Wall -Wno-unused-variable -std=c++11" } +// { dg-options "-fsanitize=alignment -Wall -Wno-unused-variable -Wno-address-of-packed-member -std=c++11" } typedef const long int L; struct S { long int l; char buf[1 + sizeof (int) + sizeof (L)]; } s; diff --git a/gcc/testsuite/gcc.dg/pr51628-10.c b/gcc/testsuite/gcc.dg/pr51628-10.c new file mode 100644 index 00000000000..03a04eff75c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr51628-10.c @@ -0,0 +1,23 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +void foo (int *); + +int * +bar (int n, int k, void *ptr) +{ + struct A + { + int c[k]; + int x[n]; + } __attribute__ ((packed, aligned (4))); + struct A *p = (struct A *) ptr; + + int *p0, *p1; + p0 = p->x; + foo (p0); + p1 = &p->x[1]; + foo (p1); + return &p->x[1]; +} diff --git a/gcc/testsuite/gcc.dg/pr51628-11.c b/gcc/testsuite/gcc.dg/pr51628-11.c new file mode 100644 index 00000000000..7ff03e85cea --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr51628-11.c @@ -0,0 +1,26 @@ +/* PR c/51628. */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +void foo (int *); + +int * +bar (int n, int k, void *ptr) +{ + struct A + { + char c[k]; + int x[n]; + } __attribute__ ((packed)); + struct A *p = (struct A *) ptr; + + int *p0, *p1; + p0 = p->x; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + foo (p0); + p1 = &p->x[1]; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ + foo (p1); + return &p->x[1]; +/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */ +} diff --git a/gcc/testsuite/gcc.target/i386/avx512bw-vmovdqu16-2.c b/gcc/testsuite/gcc.target/i386/avx512bw-vmovdqu16-2.c index a61609c40d2..c6e3ebdc507 100644 --- a/gcc/testsuite/gcc.target/i386/avx512bw-vmovdqu16-2.c +++ b/gcc/testsuite/gcc.target/i386/avx512bw-vmovdqu16-2.c @@ -1,5 +1,5 @@ /* { dg-do run } */ -/* { dg-options "-O2 -mavx512bw" } */ +/* { dg-options "-O2 -mavx512bw -Wno-address-of-packed-member" } */ /* { dg-require-effective-target avx512bw } */ #define AVX512BW diff --git a/gcc/testsuite/gcc.target/i386/avx512f-vmovdqu32-2.c b/gcc/testsuite/gcc.target/i386/avx512f-vmovdqu32-2.c index f2edc3dff7b..95a657fc5ff 100644 --- a/gcc/testsuite/gcc.target/i386/avx512f-vmovdqu32-2.c +++ b/gcc/testsuite/gcc.target/i386/avx512f-vmovdqu32-2.c @@ -1,5 +1,5 @@ /* { dg-do run } */ -/* { dg-options "-O2 -mavx512f" } */ +/* { dg-options "-O2 -mavx512f -Wno-address-of-packed-member" } */ /* { dg-require-effective-target avx512f } */ #define AVX512F diff --git a/gcc/testsuite/gcc.target/i386/avx512f-vmovdqu64-2.c b/gcc/testsuite/gcc.target/i386/avx512f-vmovdqu64-2.c index 14176965ace..954b091d976 100644 --- a/gcc/testsuite/gcc.target/i386/avx512f-vmovdqu64-2.c +++ b/gcc/testsuite/gcc.target/i386/avx512f-vmovdqu64-2.c @@ -1,5 +1,5 @@ /* { dg-do run } */ -/* { dg-options "-O2 -mavx512f" } */ +/* { dg-options "-O2 -mavx512f -Wno-address-of-packed-member" } */ /* { dg-require-effective-target avx512f } */ #define AVX512F diff --git a/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu16-2.c b/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu16-2.c index 45ae83d4552..81465f8d9a0 100644 --- a/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu16-2.c +++ b/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu16-2.c @@ -1,5 +1,5 @@ /* { dg-do run } */ -/* { dg-options "-O2 -mavx512bw -mavx512vl" } */ +/* { dg-options "-O2 -mavx512bw -mavx512vl -Wno-address-of-packed-member" } */ /* { dg-require-effective-target avx512vl } */ /* { dg-require-effective-target avx512bw } */ diff --git a/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu32-2.c b/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu32-2.c index 4b928d0cd42..19390664bd0 100644 --- a/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu32-2.c +++ b/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu32-2.c @@ -1,5 +1,5 @@ /* { dg-do run } */ -/* { dg-options "-O2 -mavx512vl" } */ +/* { dg-options "-O2 -mavx512vl -Wno-address-of-packed-member" } */ /* { dg-require-effective-target avx512vl } */ #define AVX512VL diff --git a/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu64-2.c b/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu64-2.c index 1863ed3616f..aea0c12a5ff 100644 --- a/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu64-2.c +++ b/gcc/testsuite/gcc.target/i386/avx512vl-vmovdqu64-2.c @@ -1,5 +1,5 @@ /* { dg-do run } */ -/* { dg-options "-O2 -mavx512vl" } */ +/* { dg-options "-O2 -mavx512vl -Wno-address-of-packed-member" } */ /* { dg-require-effective-target avx512vl } */ #define AVX512VL -- 2.14.3