Jim Meyering wrote:
> Improved patch.
> Ensuring alignment of the struct was not enough, since
> we're aliasing to an address (offset of "*read") into
> one of its buffers.
>
> The additional change below ensures that "read" is always
> a multiple of 8.
>
> As I type this, I realized that I can propose a more local (i.e., more
> maintainable) change that does not depend on the alignment of the buffer.
> but otherwise equivalent.
> Coming up...

Here's the smaller/better patch.
The difference is that it should work even when the
entire struct is misaligned.

>From 5c3db3a6fb7eedb837e3850756662938882745f8 Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email protected]>
Date: Mon, 23 Mar 2009 08:47:02 +0100
Subject: [PATCH] avoid bus error due to mis-aligned "shared_memory" access

* lib/coroipcc.c (ptr_align): Tiny new helper function.
(memcpy_swrap): Ensure that &src[*read] is always 8-byte-aligned.
---
 lib/coroipcc.c |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/lib/coroipcc.c b/lib/coroipcc.c
index 47937ee..d4faa66 100644
--- a/lib/coroipcc.c
+++ b/lib/coroipcc.c
@@ -424,6 +424,18 @@ coroipcc_fd_get (void *ipc_ctx)
        return (ipc_segment->fd);
 }

+/* Return PTR, aligned upward to the next multiple of ALIGNMENT.
+   ALIGNMENT must be nonzero.  The caller must arrange for ((char *)
+   PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
+   locations.  */
+static inline void *
+ptr_align (void const *ptr, size_t alignment)
+{
+  char const *p0 = ptr;
+  char const *p1 = p0 + alignment - 1;
+  return (void *) (p1 - (size_t) p1 % alignment);
+}
+
 static void memcpy_swrap (
        void *dest, void *src, int len, unsigned int *read)
 {
@@ -446,6 +458,14 @@ static void memcpy_swrap (
                        second_read);
        }
        *read = (*read + len) % (DISPATCH_SIZE);
+
+       /*
+        * Adjust *read so that &src[*read] is always 8-byte-aligned.
+        */
+       const char *p = &src[*read];
+       const char *p_aligned = ptr_align (p, 8);
+       *read += p_aligned - p;
+       *read %= DISPATCH_SIZE;
 }
 int original_flow = -1;

--
1.6.2.rc1.285.gc5f54
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to