Skip to content

[k2] fix coro frame alignment#1631

Merged
PetrShumilov merged 4 commits into
masterfrom
pshumilov/fix_coro_frame_alignment
May 20, 2026
Merged

[k2] fix coro frame alignment#1631
PetrShumilov merged 4 commits into
masterfrom
pshumilov/fix_coro_frame_alignment

Conversation

@PetrShumilov
Copy link
Copy Markdown
Contributor

@PetrShumilov PetrShumilov commented May 20, 2026

With clang-18 and -O2 member initialization of record types may be lowered to SIMD instructions. For example, member zero initialization for promise_base (part of coroutine frame) may looks like:

;   void* m_next{};
  2bcb84: c5 f8 57 c0                  	vxorps	%xmm0, %xmm0, %xmm0
  2bcb88: c5 f8 29 43 50               	vmovaps	%xmm0, 0x50(%rbx)
  2bcb8d: c5 f8 29 43 40               	vmovaps	%xmm0, 0x40(%rbx)

According to the x86 specification, the destination operand of vmovaps must be aligned to 16, 32, or 64 bytes, depending on the register width. However,

  • the implementation of memory manager which is used for allocation of coroutine frame doesn't get any guarantees about alignment;
  • by default, without passing -fcoro-aligned-allocation option, clang doesn't invoke overload of operator new with alignment parameter;
  • such program:
<?php
/**
 * @kphp-required
 * @param int $a
 * @param int $b
 * @return int
 */
function cmp(int $a, int $b) {
  sched_yield();
  return $b - $a;
}
function main() {
  $arr = [3, 2, 1];
  uasort($arr, 'cmp');
}
main();

is failed with SIGSEGV when -O2 is enabled.

@PetrShumilov PetrShumilov added this to the next milestone May 20, 2026
@PetrShumilov PetrShumilov self-assigned this May 20, 2026
@PetrShumilov PetrShumilov added runtime Feature related to runtime k2 Affects compiler or runtime in K2 mode labels May 20, 2026
@PetrShumilov PetrShumilov force-pushed the pshumilov/fix_coro_frame_alignment branch 2 times, most recently from 7e42ece to 1f07398 Compare May 20, 2026 12:11
Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
@PetrShumilov PetrShumilov force-pushed the pshumilov/fix_coro_frame_alignment branch from 04e3b00 to 3219b77 Compare May 20, 2026 14:29
Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
@PetrShumilov PetrShumilov merged commit 767191e into master May 20, 2026
7 checks passed
@PetrShumilov PetrShumilov deleted the pshumilov/fix_coro_frame_alignment branch May 20, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

k2 Affects compiler or runtime in K2 mode runtime Feature related to runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants