Re: [問題]swapcontext
※ 引述《yueayase (scrya)》之銘言:
: static ucontext_t uctx_main, uctx_func1, uctx_func2;
static int exit_addr;
: static void
: func1(void)
: {
: printf("func1: started\n");
: printf("func1: swapcontext(&uctx_func1, &uctx_func2)\n");
: if (swapcontext(&uctx_func1, &uctx_func2) == -1)
: die("swapcontext");
: /* 有趣的地方 */
: printf("test...\n");
((int *)uctx_func2.uc_stack.ss_sp)[16384-5] = exit_addr;
: if (swapcontext(&uctx_func1, &uctx_func2) == -1)
: die("swapcontext");
: printf("func1: returning\n");
: }
: static void
: func2(void)
: {
: printf("func2: started\n");
: printf("func2: swapcontext(&uctx_func2, &uctx_func1)\n");
: if (swapcontext(&uctx_func2, &uctx_func1) == -1)
: die("swapcontext");
exit_addr = ((int *)uctx_func2.uc_stack.ss_sp)[16384-5];
: printf("func2: returning\n");
: }
: int
: main(int argc, char *argv[])
: {
: char func1_stack[16384];
: char func2_stack[16384];
/* 配合stack_t的型態宣告成int, 上面兩行的char宣告要拿掉 */
int func1_stack[16384];
int func2_stack[16384];
: if (getcontext(&uctx_func1) == -1)
: die("getcontext");
: 我很好奇,怎麼把這種"func2()已用完"的狀態抓出來?
: 希望有一些強者可以幫我解答
稍微trace了一下, 把上面的修改加到原本的code裡的話, 就可以return到f1和main
原因是函式return時會去呼叫一個exitcode func, exitcode func的address會被放
在stack[stacksize-5] (不同架構的機器可能會不一樣, 我的exitcode func address
是7243995 )
當函式return時, exitcode address會被修改成另一個位置(7244021), 所以之後切換
回f2時就不會return到f1, 而是直接終止程式
把glibc makecontext的source code挖出來看, 可以知道7243995就是下面L(exitcode)這
個label的位置, 7244021應該是 'call HIDDEN_JUMPTARGET(exit)' 的上一個指令的位置
, 當f2結束時, exitcode func address會被改寫成7244021, 所以之後切回f2, 當f2結束
時程式就會終止
回到原po的問題, 要偵測f2是否結束, 理論上是可以從f2 stack裡面的
exitcode func address來判斷, 但比較好的做法應該是f2結束時要把
uctx_func2的flag設成代表結束的值(define by yourself)
ENTRY(__makecontext)
.
.
.
/* 把exitcode address放到context的stack上 */
movl $L(exitcode), (%edx)
.
.
.
/* This is the helper code which gets called if a function which
is registered with 'makecontext' returns. In this case we
have to install the context listed in the uc_link element of
the context 'makecontext' manipulated at the time of the
'makecontext' call. If the pointer is NULL the process must
terminate. */
cfi_endproc
L(exitcode):
/* This removes the parameters passed to the function given to
'makecontext' from the stack. EBX contains the number of
parameters (see above). */
leal (%esp,%ebx,4), %esp
#ifdef PIC
call 1f
1: popl %ebx
addl $_GLOBAL_OFFSET_TABLE_+[.-1b], %ebx
#endif
cmpl $0, (%esp) /* Check the next context. */
je 2f /* If it is zero exit. */
call JUMPTARGET(__setcontext)
/* If this returns (which can happen if the syscall fails) we'll
exit the program with the return error value (-1). */
movl %eax, (%esp)
2: call HIDDEN_JUMPTARGET(exit)
/* The 'exit' call should never return. In case it does cause
the process to terminate. */
hlt
cfi_startproc
END(__makecontext)
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.166.226.207
※ 編輯: nith 來自: 118.166.226.207 (01/23 20:16)
※ 編輯: nith 來自: 118.166.226.207 (01/23 20:25)
推
01/26 01:53, , 1F
01/26 01:53, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章