public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3, resend] Fix misaligned accesses to fields in HEADER struct defined in <arpa/nameser_compat.h>
@ 2022-03-15 13:42 John David Anglin
  2022-03-16 16:15 ` Andreas Schwab
  0 siblings, 1 reply; 3+ messages in thread
From: John David Anglin @ 2022-03-15 13:42 UTC (permalink / raw)
  To: libc-alpha

[-- Attachment #1: Type: text/plain, Size: 3964 bytes --]

This is a resend of the following message:
https://sourceware.org/legacy-ml/libc-alpha/2016-06/msg01020.html

I checked that the attached change still applies to the current
master tree and that it fixes the misaligned accesses on hppa.

This is BZ 20243.

Dave
---

diff --git a/resolv/res_mkquery.c b/resolv/res_mkquery.c
index 5bc5b41531..9b82c82157 100644
--- a/resolv/res_mkquery.c
+++ b/resolv/res_mkquery.c
@@ -193,6 +193,15 @@ context_mkquery_common (struct resolv_context *ctx,
   return result;
 }
 
+/* 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 on
    error.
 
diff --git a/resolv/res_query.c b/resolv/res_query.c
index 5d0a68dc81..1115cda26b 100644
--- a/resolv/res_query.c
+++ b/resolv/res_query.c
@@ -81,6 +81,14 @@
 #include <string.h>
 #include <shlib-compat.h>
 
+/* 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;
+
 #if PACKETSZ > 65536
 #define MAXPACKET	PACKETSZ
 #else
@@ -112,8 +120,8 @@ __res_context_query (struct resolv_context *ctx, const char *name,
 		     int *nanswerp2, int *resplen2, int *answerp2_malloced)
 {
 	struct __res_state *statp = ctx->resp;
-	HEADER *hp = (HEADER *) answer;
-	HEADER *hp2;
+	UHEADER *hp = (UHEADER *) answer;
+	UHEADER *hp2;
 	int n, use_malloc = 0;
 
 	size_t bufsize = (type == T_QUERY_A_AND_AAAA ? 2 : 1) * QUERYSIZE;
@@ -217,7 +225,7 @@ __res_context_query (struct resolv_context *ctx, const char *name,
 
 	if (answerp != NULL)
 	  /* __res_context_send 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
@@ -228,7 +236,7 @@ __res_context_query (struct resolv_context *ctx, const char *name,
 	  }
 	else
 	  {
-	    hp2 = (HEADER *) *answerp2;
+	    hp2 = (UHEADER *) *answerp2;
 	    if (n < (int) sizeof (HEADER))
 	      {
 	        hp = hp2;
@@ -338,7 +346,7 @@ __res_context_search (struct resolv_context *ctx,
 {
 	struct __res_state *statp = ctx->resp;
 	const char *cp;
-	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 5d6be4b82d..d549a9efbb 100644
--- a/resolv/res_send.c
+++ b/resolv/res_send.c
@@ -111,6 +111,15 @@
 #include <libc-diag.h>
 #include <random-bits.h>
 
+/* 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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3, resend] Fix misaligned accesses to fields in HEADER struct defined in <arpa/nameser_compat.h>
  2022-03-15 13:42 [PATCH v3, resend] Fix misaligned accesses to fields in HEADER struct defined in <arpa/nameser_compat.h> John David Anglin
@ 2022-03-16 16:15 ` Andreas Schwab
  2022-03-16 16:50   ` John David Anglin
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Schwab @ 2022-03-16 16:15 UTC (permalink / raw)
  To: John David Anglin; +Cc: libc-alpha

On Mär 15 2022, John David Anglin wrote:

> diff --git a/resolv/res_mkquery.c b/resolv/res_mkquery.c
> index 5bc5b41531..9b82c82157 100644
> --- a/resolv/res_mkquery.c
> +++ b/resolv/res_mkquery.c
> @@ -193,6 +193,15 @@ context_mkquery_common (struct resolv_context *ctx,
>    return result;
>  }
>  
> +/* 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
> +

The only use of HEADER below that point is in __res_nopt, and AFACS the
only caller __res_context_query always uses aligned memory.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3, resend] Fix misaligned accesses to fields in HEADER struct defined in <arpa/nameser_compat.h>
  2022-03-16 16:15 ` Andreas Schwab
@ 2022-03-16 16:50   ` John David Anglin
  0 siblings, 0 replies; 3+ messages in thread
From: John David Anglin @ 2022-03-16 16:50 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: libc-alpha

On 2022-03-16 12:15 p.m., Andreas Schwab wrote:
> On Mär 15 2022, John David Anglin wrote:
>
>> diff --git a/resolv/res_mkquery.c b/resolv/res_mkquery.c
>> index 5bc5b41531..9b82c82157 100644
>> --- a/resolv/res_mkquery.c
>> +++ b/resolv/res_mkquery.c
>> @@ -193,6 +193,15 @@ context_mkquery_common (struct resolv_context *ctx,
>>     return result;
>>   }
>>   
>> +/* 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
>> +
> The only use of HEADER below that point is in __res_nopt, and AFACS the
> only caller __res_context_query always uses aligned memory.
Yes, it appears the callers of __res_nopt always use aligned memory.  I'll check if this hunk can
be removed.

-- 
John David Anglin  dave.anglin@bell.net


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-03-16 16:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-15 13:42 [PATCH v3, resend] Fix misaligned accesses to fields in HEADER struct defined in <arpa/nameser_compat.h> John David Anglin
2022-03-16 16:15 ` Andreas Schwab
2022-03-16 16:50   ` John David Anglin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).