Skip to content

Commit 519abc5

Browse files
committed
Fix amd64 restore callback stack pointer
Restore the sysv_amd64 callback stack-pointer fix so STACKMAN_OP_RESTORE receives the active switched stack pointer, not the pre-switch value. Update both the assembly and inline GCC x86_64 paths for consistent callback behavior. Add a regression assertion in tests/test.c and document the fix in CHANGELOG.md under Unreleased.
1 parent d136f89 commit 519abc5

4 files changed

Lines changed: 10 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
- Fixed amd64 (sysv_x86_64 GCC) restore callback stack-pointer argument to pass the active switched stack pointer
12+
- Added assertion coverage in `tests/test.c` to detect incorrect restore callback stack pointer on amd64
13+
814
## [1.2.0] - 2025-11-16
915

1016
### Added

stackman/platforms/switch_x86_64_gcc.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ LABEL(stackman_switch)
9393
addq %rax, %rbp
9494
# 0 "" 2
9595
#NO_APP
96-
movq %rbx, %rdx
96+
movq %rsp, %rdx
9797
movl $1, %esi
9898
movq %r15, %rdi
9999
call *%r14

stackman/platforms/switch_x86_64_gcc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void *STACKMAN_SWITCH_INASM_NAME(stackman_cb_t callback, void *context)
6767
__asm__ ("movq %[result], %%rsp" :: [result] "r" (stack_pointer2));
6868
__asm__ ("addq %[arg], %%rbp" :: [arg] "r" (diff));
6969

70-
stack_pointer = callback(context, STACKMAN_OP_RESTORE, stack_pointer);
70+
stack_pointer = callback(context, STACKMAN_OP_RESTORE, stack_pointer2);
7171
/* restore non-volatile registers from stack */
7272
__asm__ volatile (
7373
"ldmxcsr %[sr]\n\t"

tests/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ void *jmp_cb(void* context, int opcode, void *sp)
102102
if (opcode == (int)STACKMAN_OP_SAVE) {
103103
return c->stack_near;
104104
} else {
105+
/* RESTORE must receive the active switched stack pointer. */
106+
assert(sp == c->stack_near);
105107
restore_stack(c->stack_near, c->buf, c->size);
106108
return sp;
107109
}

0 commit comments

Comments
 (0)