Skip to content

stack handler alignment in fiber.c #11779

@damiendoligez

Description

@damiendoligez

Lines 165-168 in fiber.c (in function alloc_size_class_stack_noexc):

    /* Ensure 16-byte alignment because some architectures require it */
    hand = (struct stack_handler*)
     (((uintnat)stack + sizeof(struct stack_info) + sizeof(value) * wosize + 8)
      & ((uintnat)-1 << 4));

This works on 64-bit because the size of struct stack_info is a multiple of 8, so you can round it up by adding 8 and then masking. On 32-bit architectures it works because the size happens to be 32 bytes (8 words) so adding and masking gives the original number. But if the size was 4 modulo 16 (for example, if we ever add one pointer field to struct stack_info) we would be subtracting 4 from the pointer instead of rounding it up to the next multiple of 16 and then probably overwriting something at a later time.

Also, this magical constant 8 is strongly linked to the one seen in lines 99-102 of the same file (see below). I suggest using a #define or adding a comment at both points.

  size_t len = sizeof(struct stack_info) +
               sizeof(value) * wosize +
               8 /* for alignment to 16-bytes, needed for arm64 */ +
               sizeof(struct stack_handler);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions