From patchwork Sat Jun 25 15:31:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John David Anglin X-Patchwork-Id: 13375 Received: (qmail 41822 invoked by alias); 25 Jun 2016 15:32:06 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 41811 invoked by uid 89); 25 Jun 2016 15:32:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.0 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS, UNPARSEABLE_RELAY autolearn=ham version=3.3.2 spammy=__aligned__, 11015, substituted, vice X-HELO: mtlfep01.bell.net Subject: [PATCH v3] Fix misaligned accesses to fields in HEADER struct defined in Mime-Version: 1.0 (Apple Message framework v1085) From: John David Anglin In-Reply-To: <20160622105117.GC24532@vapier.lan> Date: Sat, 25 Jun 2016 11:31:46 -0400 Cc: GNU C Library , Carlos O'Donell Message-Id: References: <5E7CF115-219A-4260-BE53-19B5A8D1D7F3@bell.net> <20160622105117.GC24532@vapier.lan> To: Mike Frysinger X-Opwv-CommTouchExtSvcRefID: str=0001.0A020201.576EA3E6.014A, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 On 2016-06-22, at 6:51 AM, Mike Frysinger wrote: > On 17 Jun 2016 18:52, John David Anglin wrote: >> --- a/resolv/res_mkquery.c >> +++ b/resolv/res_mkquery.c >> @@ -83,6 +83,8 @@ >> # define RANDOM_BITS(Var) { uint64_t v64; HP_TIMING_NOW (v64); Var = v64; } >> #endif >> >> +typedef HEADER __attribute__ ((aligned(1))) HEADER1; > > could do with a comment above it explaining what this is all about > > should be __aligned__ > > bike shed: maybe "UHEADER" is better ? > > code-wise, should this file always be using this variant ? would > it be too ugly to do: > typedef ... > #define HEADER HEADER1 The attached patch implements the above suggestions except for res_query.c where is directly substituted as necessary. Tested on hppa-unknown-linux-gnu with a trunk build and check. Please install if okay. Dave --- John David Anglin dave.anglin@bell.net 2016-06-25 John David Anglin [BZ 20243] * res_mkquery.c (UHEADER): New typedef derived from HEADER typedef. (HEADER): Define. * res_send.c: Likewise. * res_query.c (UHEADER): New typedef derived from HEADER typedef. (__libc_res_nquery): Use UHEADER typedef instead of HEADER for pointers requiring byte access. (__libc_res_nsearch): Likewise. diff --git a/resolv/res_mkquery.c b/resolv/res_mkquery.c index 12f9730..1c4c0bc 100644 --- a/resolv/res_mkquery.c +++ b/resolv/res_mkquery.c @@ -83,6 +83,15 @@ # define RANDOM_BITS(Var) { uint64_t v64; HP_TIMING_NOW (v64); Var = v64; } #endif +/* The structure HEADER is normally aligned to a word boundary and its + fields are accessed using word loads and stores. We need to access + this structure when it is aligned on a byte boundary. This can cause + problems on machines with strict alignment. So, we create a new + typedef to reduce its alignment to one. This ensures the fields are + accessed with byte loads and stores. */ +typedef HEADER __attribute__ ((__aligned__(1))) UHEADER; +#define HEADER UHEADER + /* * Form all types of queries. * Returns the size of the result or -1. diff --git a/resolv/res_query.c b/resolv/res_query.c index 944d1a9..ebf0fb9 100644 --- a/resolv/res_query.c +++ b/resolv/res_query.c @@ -78,6 +78,14 @@ #include #include +/* The structure HEADER is normally aligned to a word boundary and its + fields are accessed using word loads and stores. We need to access + this structure when it is aligned on a byte boundary. This can cause + problems on machines with strict alignment. So, we create a new + typedef to reduce its alignment to one. This ensures the fields are + accessed with byte loads and stores. */ +typedef HEADER __attribute__ ((__aligned__(1))) UHEADER; + /* Options. Leave them on. */ /* #undef DEBUG */ @@ -117,8 +125,8 @@ __libc_res_nquery(res_state statp, int *resplen2, int *answerp2_malloced) { - HEADER *hp = (HEADER *) answer; - HEADER *hp2; + UHEADER *hp = (UHEADER *) answer; + UHEADER *hp2; int n, use_malloc = 0; u_int oflags = statp->_flags; @@ -235,7 +243,7 @@ __libc_res_nquery(res_state statp, if (answerp != NULL) /* __libc_res_nsend might have reallocated the buffer. */ - hp = (HEADER *) *answerp; + hp = (UHEADER *) *answerp; /* We simplify the following tests by assigning HP to HP2 or vice versa. It is easy to verify that this is the same as @@ -246,7 +254,7 @@ __libc_res_nquery(res_state statp, } else { - hp2 = (HEADER *) *answerp2; + hp2 = (UHEADER *) *answerp2; if (n < (int) sizeof (HEADER)) { hp = hp2; @@ -336,7 +344,7 @@ __libc_res_nsearch(res_state statp, int *answerp2_malloced) { const char *cp, * const *domain; - HEADER *hp = (HEADER *) answer; + UHEADER *hp = (UHEADER *) answer; char tmp[NS_MAXDNAME]; u_int dots; int trailing_dot, ret, saved_herrno; diff --git a/resolv/res_send.c b/resolv/res_send.c index 869294f..da075af 100644 --- a/resolv/res_send.c +++ b/resolv/res_send.c @@ -110,6 +110,15 @@ #include #include +/* The structure HEADER is normally aligned to a word boundary and its + fields are accessed using word loads and stores. We need to access + this structure when it is aligned on a byte boundary. This can cause + problems on machines with strict alignment. So, we create a new + typedef to reduce its alignment to one. This ensures the fields are + accessed with byte loads and stores. */ +typedef HEADER __attribute__ ((__aligned__(1))) UHEADER; +#define HEADER UHEADER + #if PACKETSZ > 65536 #define MAXPACKET PACKETSZ #else