LinuxDev 板


LINE

※ 引述《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)
1F:推 yueayase:真的是太厲害了,要碰到assembly等級的 01/26 01:53







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP