jmp_sp.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. # see: https://github.com/ossrs/srs/wiki/v1_CN_SrsLinuxArm
  3. g++ -g -O0 -o jmp_sp jmp_sp.cpp
  4. arm-linux-gnueabi-g++ -g -o jmp_sp jmp_sp.cpp -static
  5. arm-linux-gnueabi-strip jmp_sp
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <setjmp.h>
  10. jmp_buf context;
  11. void do_longjmp()
  12. {
  13. /**
  14. the definition of jmp_buf:
  15. typedef struct __jmp_buf_tag jmp_buf[1];
  16. struct __jmp_buf_tag {
  17. __jmp_buf __jmpbuf;
  18. int __mask_was_saved;
  19. __sigset_t __saved_mask;
  20. };
  21. */
  22. #if defined(__amd64__) || defined(__x86_64__)
  23. // http://ftp.gnu.org/gnu/glibc/glibc-2.12.2.tar.xz
  24. // http://ftp.gnu.org/gnu/glibc/glibc-2.12.1.tar.gz
  25. /*
  26. * Starting with glibc 2.4, JB_SP definitions are not public anymore.
  27. * They, however, can still be found in glibc source tree in
  28. * architecture-specific "jmpbuf-offsets.h" files.
  29. * Most importantly, the content of jmp_buf is mangled by setjmp to make
  30. * it completely opaque (the mangling can be disabled by setting the
  31. * LD_POINTER_GUARD environment variable before application execution).
  32. * Therefore we will use built-in _st_md_cxt_save/_st_md_cxt_restore
  33. * functions as a setjmp/longjmp replacement wherever they are available
  34. * unless USE_LIBC_SETJMP is defined.
  35. */
  36. // for glibc 2.4+, it's not possible to get and set the sp in jmp_buf
  37. /**
  38. for example, the following is show the jmp_buf when setjmp:
  39. (gdb) x /64xb context[0].__jmpbuf
  40. 0x600ca0 <context>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  41. 0x600ca8 <context+8>: 0xf8 0xc1 0x71 0xe5 0xa8 0x88 0xb4 0x15
  42. 0x600cb0 <context+16>: 0xa0 0x05 0x40 0x00 0x00 0x00 0x00 0x00
  43. 0x600cb8 <context+24>: 0x90 0xe4 0xff 0xff 0xff 0x7f 0x00 0x00
  44. 0x600cc0 <context+32>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  45. 0x600cc8 <context+40>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  46. 0x600cd0 <context+48>: 0xf8 0xc1 0x51 0xe5 0xa8 0x88 0xb4 0x15
  47. 0x600cd8 <context+56>: 0xf8 0xc1 0xd9 0x2f 0xd7 0x77 0x4b 0xea
  48. (gdb) p /x $sp
  49. $4 = 0x7fffffffe380
  50. we cannot finger the sp out.
  51. where the glibc is 2.12.
  52. */
  53. register long int rsp0 asm("rsp");
  54. int ret = setjmp(context);
  55. printf("setjmp func0 ret=%d, rsp=%#lx\n", ret, rsp0);
  56. printf("after setjmp: ");
  57. for (int i = 0; i < 8; i++) {
  58. printf("env[%d]=%#x, ", i, (int)context[0].__jmpbuf[i]);
  59. }
  60. printf("\n");
  61. #endif
  62. #if defined(__arm__)
  63. /**
  64. /usr/arm-linux-gnueabi/include/bits/setjmp.h
  65. #ifndef _ASM
  66. The exact set of registers saved may depend on the particular core
  67. in use, as some coprocessor registers may need to be saved. The C
  68. Library ABI requires that the buffer be 8-byte aligned, and
  69. recommends that the buffer contain 64 words. The first 28 words
  70. are occupied by v1-v6, sl, fp, sp, pc, d8-d15, and fpscr. (Note
  71. that d8-15 require 17 words, due to the use of fstmx.)
  72. typedef int __jmp_buf[64] __attribute__((__aligned__ (8)));
  73. the layout of setjmp for arm:
  74. 0-5: v1-v6
  75. 6: sl
  76. 7: fp
  77. 8: sp
  78. 9: pc
  79. 10-26: d8-d15 17words
  80. 27: fpscr
  81. */
  82. /**
  83. For example, on raspberry-pi, armv6 cpu:
  84. (gdb) x /64xb (char*)context[0].__jmpbuf
  85. v1, 0: 0x00 0x00 0x00 0x00
  86. v2, 1: 0x00 0x00 0x00 0x00
  87. v3, 2: 0x2c 0x84 0x00 0x00
  88. v4, 3: 0x00 0x00 0x00 0x00
  89. v5, 4: 0x00 0x00 0x00 0x00
  90. v6, 5: 0x00 0x00 0x00 0x00
  91. sl, 6: 0x00 0xf0 0xff 0xb6
  92. fp, 7: 0x9c 0xfb 0xff 0xbe
  93. sp, 8: 0x88 0xfb 0xff 0xbe
  94. pc, 9: 0x08 0x85 0x00 0x00
  95. (gdb) p /x $sp
  96. $5 = 0xbefffb88
  97. (gdb) p /x $pc
  98. $4 = 0x850c
  99. */
  100. int ret = setjmp(context);
  101. printf("setjmp func1 ret=%d\n", ret);
  102. printf("after setjmp: ");
  103. for (int i = 0; i < 64; i++) {
  104. printf("env[%d]=%#x, ", i, (int)context[0].__jmpbuf[i]);
  105. }
  106. printf("func0 terminated\n");
  107. #endif
  108. }
  109. int main(int argc, char** argv)
  110. {
  111. #if defined(__amd64__) || defined(__x86_64__)
  112. printf("x86_64 sizeof(long int)=%d, sizeof(long)=%d, "
  113. "sizeof(int)=%d, __WORDSIZE=%d, __GLIBC__=%d, __GLIBC_MINOR__=%d\n",
  114. (int)sizeof(long int), (int)sizeof(long), (int)sizeof(int),
  115. (int)__WORDSIZE, (int)__GLIBC__, (int)__GLIBC_MINOR__);
  116. #else
  117. printf("arm sizeof(long int)=%d, sizeof(long)=%d, "
  118. "sizeof(int)=%d, __GLIBC__=%d,__GLIBC_MINOR__=%d\n",
  119. (int)sizeof(long int), (int)sizeof(long), (int)sizeof(int),
  120. (int)__GLIBC__, (int)__GLIBC_MINOR__);
  121. #endif
  122. do_longjmp();
  123. printf("terminated\n");
  124. return 0;
  125. }