Commit 1d5bb760 authored by Alexander Richardson's avatar Alexander Richardson Committed by Alan Coopersmith
Browse files

Fix InternalCallbackRec layout if pointers are bigger than 64 bits

When running `xeyes` via `SSH -X` on CHERI-RISC-V FreeBSD, I was getting
a Bus Error (unaligned store) in the `cl->callback = callback;` line of
_XtAddCallback. The `cl` variable (created using `ToList(icl)`) was only
aligned to 8 bytes, but for CHERI-RISC-V pointer-type loads and stores
require 16-byte alignment.

In order to fix this, I added a C99 flexible array member to
internalCallbackRec when compiling for C99 or newer. This ensures
that sizeof(InternalCallbackRec) is 16 (since it now includes the
required 12 byte padding up to the first XtCallbackRec). This also
ensures that alignof(InternalCallbackRec) is 16, but that doesn't
matter in this case since malloc() will always return a
sufficiently-aligned pointer.

I also changed ToList(p) to use the flexible array member directly
when compiling for C99. This is not a functional change since it
will evaluate to the same pointer, but it does add additional type
checking and ensures tha...
parent f8c7069a
......@@ -62,7 +62,11 @@ typedef struct internalCallbackRec {
unsigned short count;
char is_padded; /* contains NULL padding for external form */
char call_state; /* combination of _XtCB{FreeAfter}Calling */
#ifdef LONG64
#if __STDC_VERSION__ >= 199901L
/* When compiling with C99 or newer, use a flexible member to ensure
* padding and alignment for sizeof(). */
XtCallbackRec callbacks[];
#elif defined(LONG64)
unsigned int align_pad; /* padding to align callback list */
#endif
/* XtCallbackList */
......
......@@ -80,7 +80,11 @@ static _Xconst _XtString XtNxtRemoveAllCallback = "xtRemoveAllCallback";
static _Xconst _XtString XtNxtCallCallback = "xtCallCallback";
/* However it doesn't contain a final NULL record */
#if __STDC_VERSION__ >= 199901L
#define ToList(p) ((p)->callbacks)
#else
#define ToList(p) ((XtCallbackList) ((p)+1))
#endif
static InternalCallbackList *
FetchInternalList(Widget widget,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment