0
   __asm__(
    "mov %%rsp, 0(%%rcx);"      // save stack
    "mov %%rcx, %%rsp;"         // switch stack
    "callq %%rax;"              // run coroutine
    "pop %%rsp;"                // recovery rsp
    "callq %%rbx;"              // coroutine_exit()
    ""::"a"(ready_coroutine->fun), "c"(ready_coroutine->esp3 - 8), "d"(current_coroutine), "b"(coroutine_exit):"memory"
);

I'm learning coroutine and try to implement it. this code was try to switch stack and run a function. I'm confused to this code can run in ubuntu18.04, but not in ubuntu 22.04.

it run success when only switch stack or only call function, but failed when both switch stack and call function

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
graydove
  • 133
  • 1
  • 5
  • 3
    How does it fail, and what does the compiler-generated asm around it look like? i.e. a [mcve]. If the function you call is compiler-generated, it will potentially modify registers you didn't declare clobbers on, which would be undefined behaviour. That might happen to work or not, depending on what registers GCC happened to expect. See [Calling printf in extended inline ASM](https://stackoverflow.com/a/37503773) re: getting the clobbers correct for x86-64 when making a function call from asm that you aren't telling the compiler about. – Peter Cordes Sep 04 '22 at 14:59

0 Answers0