2
0

jmp_sp.cpp 4.8 KB

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