• arm函数栈帧(stackframe)结构和传参规则


    1. 概述和问题

    本文汇编代码的平台及编译器:arm/gcc。分析函数调用栈的规则对于理解程序运行基本原理很有帮助,汇编代码分析crash问题也大有裨益。本文示例代码通过C语言函数调用一个汇编函数,再从汇编函数跳转回C函数,分析该示例的汇编代码就可以stack frame的创建和arm函数调用的传参规则。

    问题:

    • arm32使用哪些寄存器传参,如果参数超过4个怎么传参?
    • arm32/gcc中函数调用stack frame的创建,以及函数返回stack frame的销毁过程是怎样的?

    2.arm32函数传参规则和stack frame基本结构

    2.1 传参规则

    • r0-r3传递第1-第4个参数;如果超过4个参数使用栈传递参数,且当前函数栈顶(sp指向的地址)放置第5个参数,sp+4处放置第6个参数。
    • r0存放返回值

    2.2 stack frame基本机构

    3.示例代码

    示例代码包括两个源文件:transferParam.c和transferParam.S

    transferParam.c:

    1. #include <stdlib.h>
    2. #include <stdio.h>
    3. #include <string.h>
    4. extern void bionic_clone(int flags, int* child_stack, int* parent_tid, int* tls, int* child_tid, int (*fn)(void*), int* arg);
    5. int child(void* v) {
    6. return 0;
    7. }
    8. void my_fork(int flags, int *child_stack, int *ptid, int *tls, int *child_tid, int *child, int *args) {
    9. printf("flags:%d sp:%p ptid:%p tls:%p child_tid:%p child:%p args:%d\n",
    10. flags, child_stack, ptid, tls, child_tid, child, *args);
    11. }
    12. int main(){
    13. int flags = 0;
    14. int *childStack = (int*)0x01;
    15. int *parent_tid = (int*)0x02;
    16. int *tls = (int*)0x03;
    17. int *child_tid = (int*)0x04;
    18. int arg = 5;
    19. printf("%s\n", "before bionic_clone");
    20. bionic_clone(flags, childStack, parent_tid, tls, child_tid, child, &arg);
    21. printf("%s\n", "after bionic_clone");
    22. return 0;
    23. }

    c代码中调用了汇编函数bionic_clone,且参数超过4个,需要使用栈传递参数。

    transferParam.S:

    1. .globl bionic_clone
    2. bionic_clone:
    3. push {fp, lr} @fp, lr入栈
    4. add fp, sp, #4 @fp = sp - 4
    5. @stmfd sp!, {r4, r5, r6}
    6. ldr r4, [fp, #4] @读取第五个参数到r4寄存器
    7. ldr r5, [fp, #8] @读取第六个参数到r5寄存器
    8. ldr r6, [fp, #12] @读取第7个参数到r6寄存器
    9. stmfd sp!, {r4, r5, r6} @r4, r5, r6入栈,以此给my_fork函数传参
    10. bl my_fork
    11. sub sp, fp, #4 @sp = fp - 4
    12. pop {fp, pc} @恢复fp, lr到fp和pc寄存器,实现函数返回
    13. .type bionic_clone,%function

    代码执行到ldr r6, [fp, #12] stack frame图示:

     正如main函数通过栈给bionic_clone函数传递第5,6,7三个参数,bionic_clone也将r4 r5 r6入栈给my_fork传递参数

    4. arm64栈帧结构

    1. void bar(int a , int b ) {
    2. printf("bar\n");
    3. a = a + b;
    4. printf("%d\n",a);
    5. }
    6. void foo() {
    7. int a = 0;
    8. int b = 1;
    9. bar(a, b);
    10. }
    11. int main(int argc, char *argv[]) {
    12. foo();
    13. }
    14. ~

    反汇编代码:

    1. 000000000040072c <bar>:
    2. 40072c: a9be7bfd stp x29, x30, [sp,#-32]!
    3. 400730: 910003fd mov x29, sp
    4. 400734: b9001fa0 str w0, [x29,#28]
    5. 400738: b9001ba1 str w1, [x29,#24]
    6. 40073c: 90000000 adrp x0, 400000 <_init-0x598>
    7. 400740: 91216000 add x0, x0, #0x858
    8. 400744: 97ffffaf bl 400600 <puts@plt>
    9. 400748: b9401fa1 ldr w1, [x29,#28]
    10. 40074c: b9401ba0 ldr w0, [x29,#24]
    11. 400750: 0b000020 add w0, w1, w0
    12. 400754: b9001fa0 str w0, [x29,#28]
    13. 400758: 90000000 adrp x0, 400000 <_init-0x598>
    14. 40075c: 91218000 add x0, x0, #0x860
    15. 400760: b9401fa1 ldr w1, [x29,#28]
    16. 400764: 97ffffab bl 400610 <printf@plt>
    17. 400768: d503201f nop
    18. 40076c: a8c27bfd ldp x29, x30, [sp],#32
    19. 400770: d65f03c0 ret
    20. 0000000000400774 <foo>:
    21. 400774: a9be7bfd stp x29, x30, [sp,#-32]!
    22. 400778: 910003fd mov x29, sp
    23. 40077c: b9001fbf str wzr, [x29,#28]
    24. 400780: 52800020 mov w0, #0x1 // #1
    25. 400784: b9001ba0 str w0, [x29,#24]
    26. 400788: b9401ba1 ldr w1, [x29,#24]
    27. 40078c: b9401fa0 ldr w0, [x29,#28]
    28. 400790: 97ffffe7 bl 40072c <bar>
    29. 400794: d503201f nop
    30. 400798: a8c27bfd ldp x29, x30, [sp],#32
    31. 40079c: d65f03c0 ret
    32. 00000000004007a0 <main>:
    33. 4007a0: a9be7bfd stp x29, x30, [sp,#-32]!
    34. 4007a4: 910003fd mov x29, sp
    35. 4007a8: b9001fa0 str w0, [x29,#28]
    36. 4007ac: f9000ba1 str x1, [x29,#16]
    37. 4007b0: 97fffff1 bl 400774 <foo>
    38. 4007b4: 52800000 mov w0, #0x0 // #0
    39. 4007b8: a8c27bfd ldp x29, x30, [sp],#32
    40. 4007bc: d65f03c0 ret

    arm64栈帧结构:

     5. 实战,内核如何dump bactrace

    为了加深stack frame的理解,可以分析arm64如何dump bactrace。内核配置CONFIG_FRAME_POINTER可以基于fp栈回溯。基本原理可以看栈帧结构中,比如arm64小节示例代码中,main调用foo,foo调用bar,我们从bar开始回溯栈帧,如果我们先得到bar的x29值,那么从x29 + 8处保存了x30,即为caller调用者的地址,bar x29又可以回溯到foo函数的栈帧结构,依次类推就可以回溯整个函数调用。

    kernel-4.14/arch/arm64/kernel/traps.c:

    1. void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
    2. {
    3. struct stackframe frame;
    4. int skip;
    5. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
    6. if (!tsk)
    7. tsk = current;
    8. if (!try_get_task_stack(tsk))
    9. return;
    10. //假设是dump当前task的backtrace
    11. if (tsk == current) {
    12. //__builtin_frame_address是编译内置函数,返回当前栈栈帧地址即x29.
    13. frame.fp = (unsigned long)__builtin_frame_address(0);
    14. frame.pc = (unsigned long)dump_backtrace;
    15. } else {
    16. /*
    17. * task blocked in __switch_to
    18. */
    19. frame.fp = thread_saved_fp(tsk);
    20. frame.pc = thread_saved_pc(tsk);
    21. }
    22. skip = !!regs;
    23. printk("Call trace:\n");
    24. while (1) {
    25. unsigned long stack;
    26. int ret;
    27. //dump_backtrace_entry打印frame.pc的值
    28. /* skip until specified stack frame */
    29. if (!skip) {
    30. dump_backtrace_entry(frame.pc);
    31. } else if (frame.fp == regs->regs[29]) {
    32. skip = 0;
    33. /*
    34. * Mostly, this is the case where this function is
    35. * called in panic/abort. As exception handler's
    36. * stack frame does not contain the corresponding pc
    37. * at which an exception has taken place, use regs->pc
    38. * instead.
    39. */
    40. dump_backtrace_entry(regs->pc);
    41. }
    42. ret = unwind_frame(tsk, &frame);
    43. if (ret < 0)
    44. break;
    45. if (in_entry_text(frame.pc)) {
    46. stack = frame.fp - offsetof(struct pt_regs, stackframe);
    47. if (on_accessible_stack(tsk, stack))
    48. dump_mem("", "Exception stack", stack,
    49. stack + sizeof(struct pt_regs));
    50. }
    51. }
    52. put_task_stack(tsk);
    53. }
    54. /*
    55. * AArch64 PCS assigns the frame pointer to x29.
    56. *
    57. * A simple function prologue looks like this:
    58. * sub sp, sp, #0x10
    59. * stp x29, x30, [sp]
    60. * mov x29, sp
    61. *
    62. * A simple function epilogue looks like this:
    63. * mov sp, x29
    64. * ldp x29, x30, [sp]
    65. * add sp, sp, #0x10
    66. */
    67. int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
    68. {
    69. unsigned long fp = frame->fp;
    70. if (fp & 0xf)
    71. return -EINVAL;
    72. if (!tsk)
    73. tsk = current;
    74. if (!on_accessible_stack(tsk, fp))
    75. return -EINVAL;
    76. //获取上一级(caller)的fp值,具体可以看arm64栈帧结构
    77. frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
    78. //fp+8存储的是caller调用之的地址(即返回地址),具体可以对着arm64栈帧结构看
    79. frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
    80. /*
    81. * Frames created upon entry from EL0 have NULL FP and PC values, so
    82. * don't bother reporting these. Frames created by __noreturn functions
    83. * might have a valid FP even if PC is bogus, so only terminate where
    84. * both are NULL.
    85. */
    86. if (!frame->fp && !frame->pc)
    87. return -EINVAL;
    88. return 0;
    89. }

  • 相关阅读:
    第13章 并发编程高阶(一)
    一般PMP证书会被认为是比较可靠的认证吗?Hr怎么看待?
    【Hack The Box】windows练习-- Scrambled
    Springboot音乐播放小程序的设计与实现毕业设计-附源码191730
    论文分享 | SpeechFormer: 利用语音信号的层次化特性提升Transformer在认知性语音信号处理领域中的性能
    (附源码)计算机毕业设计SSM基于的砂石矿山管理系统
    vue开发,如何结合chatgpt
    uniapp实现拍照涂鸦
    【数据结构之栈、队列、数组】
    生鲜行业B2B交易管理系统:助力企业一体化管理,促进生鲜企业线上线下融合
  • 原文地址:https://blog.csdn.net/GetNextWindow/article/details/126444049