]> granicus.if.org Git - sysstat/commitdiff
Fix #148: ARM: sadc crashes because of unaligned memory accesses
authorSebastien GODARD <sysstat@users.noreply.github.com>
Mon, 8 May 2017 08:10:22 +0000 (10:10 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Mon, 8 May 2017 08:10:22 +0000 (10:10 +0200)
Use posix_memalign() with 16 bytes alignment request instead of
realloc() to guarantee 16 byte alignment requested by several
sysstat's structures.

Note: 16 byte alignment is no longer needed by sysstat's
structures. Yet removing the corresponding attributes
(__attribute__ ((aligned (16)))) for example in rd_stats.h
would imply a format change for sar/sadc binary data files.
These attributes will be removed when work is done to make sa binary
datafiles portable (see #135).

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
common.h

index 70736f75a93d3ea3810e81dc73bb03695617ec05..aeacc9cbad7624ebdc343f366cc7d3105c2634ec 100644 (file)
--- a/common.h
+++ b/common.h
@@ -11,6 +11,7 @@
 
 #include <time.h>
 #include <sched.h>     /* For __CPU_SETSIZE */
+#include <stdlib.h>
 #include <limits.h>
 
 #ifdef HAVE_SYS_SYSMACROS_H
 
 /* Allocate and init structure */
 #define SREALLOC(S, TYPE, SIZE)        do {                                                             \
-                                       TYPE *_p_;                                               \
-                                       _p_ = S;                                                 \
+                                       TYPE *_p_ = S;                                           \
                                        if (SIZE) {                                              \
-                                               if ((S = (TYPE *) realloc(S, (SIZE))) == NULL) { \
+                                               void *_ptr = NULL;                               \
+                                               int error = posix_memalign(&_ptr, 16, (SIZE));   \
+                                               if (error || (_ptr == NULL)) {                   \
                                                        perror("realloc");                       \
                                                        exit(4);                                 \
                                                }                                                \
+                                               S = (TYPE *)_ptr;                                \
                                                /* If the ptr was null, then it's a malloc() */  \
                                                if (!_p_) {                                      \
                                                        memset(S, 0, (SIZE));                    \
                                                }                                                \
+                                               else {                                           \
+                                                       memcpy(S, _p_, (SIZE));                  \
+                                               }                                                \
                                        }                                                        \
                                        if (!S) {                                                \
                                                /* Should never happen */                        \