Skip to content
  • Alexander Richardson's avatar
    Fix InternalCallbackRec layout if pointers are bigger than 64 bits · 1d5bb760
    Alexander Richardson authored and Alan Coopersmith's avatar Alan Coopersmith committed
    
    
    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 that only a `InternalCallbackRec *` can be
    passed to the macro.
    
    Signed-off-by: default avatarAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
    1d5bb760