Skip to content

Instantly share code, notes, and snippets.

@k-tsj
Created December 10, 2012 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-tsj/4249318 to your computer and use it in GitHub Desktop.
Save k-tsj/4249318 to your computer and use it in GitHub Desktop.
CRuby stack overflow detection

前提

  • Ubuntu 12.04 x64(pthread_attr_getstack利用可)
  • 図の上部側が下位アドレス
  • [stack]とあるのは/proc/[PID]/mapsで確認できる[stack]の先頭、末尾アドレス

pthread_attr_getstack由来の値

                                             +--------+ -----------------------------------
                                             |  4KB   |  |                               |
[stack] -----------------------------------> +========+  |                               |
                                             |        | 4.th->machine_stack_maxsize      |
                                             |        |  (3.size - 2.space)              |
                                             |        |  |                         3.size(pthread_attr_getstack)
                                             +--------+ ---                              |
                                             |        |  |                               |
                                             |        | 2.space(1.stackaddr - &local)    |
                                             |        |  |                               |
[stack]     1.stackaddr -------------------> +========+  -----------------------------------
        (pthread_attr_getstack)

STACK_END_ADDRESS由来の値

[stack] -----------------------------------> +========+
                                             |        |
                                             |        |
                                             |        |
  5.th->machine_stack_start ---------------> +--------+
   (STACK_END_ADDRESS)                       |argv,   |
                                             |env,etc |
[stack] -----------------------------------> +========+

ruby_stack_overflowed_pが真になる条件

A < si_addr <= B であること

                                     A   --- +--------+ ---
                                          |  |        |  |
                                   si_addr-->|        | 6.MIN(th->machine_stack_maxsize / 5, 1MB)
                                          |  |        |  |
                                     B   --- +--------+ ---
                                             |        |  |
[stack] -----------------------------------> +========+  |
                                             |        |  |
                                             |        | 4.th->machine_stack_maxsize
                                             |        |  |
                                             |        |  |
                                             |        |  |
  5.th->machine_stack_start ---------------> +--------+ ---
                                             |        | 
[stack] -----------------------------------> +========+

#953のnest.rb実行時にSystemStackErrorとなった際の例

0x7fffff6ff134:                              +--------+                                    ---
                                             |        |                                     |
0x7fffff7feff8: si_addr -------------------> |        |                                     |
                                             |        |                                   6.MIN(th->machine_stack_maxsize/ 5, 1MB)
0x7fffff7ff000:                              +--------+ --------------------------------    |
                                             |        |  |                            |     |
0x7fffff7ff134:                              +--------+  |                            |    --- [base(in ruby_stack_overflowed_p)]
                                             |        |  |                            |     |
0x7fffff800000: [stack]                      +========+  |                            |     |
                                             |        | 4.th->machine_stack_maxsize   |     |
                                             |        |  |                            |   4.th->machine_stack_maxsize
                                             |        |  |                            |     |
0x7fffffffdedc:                              +--------+ ---                        3.size   |
                                             |        |  |                            |     |
                                             |        | 2.space                       |     |
                                             |        |  |                            |     |
0x7fffffffe010: 5.th->machine_stack_start -> +--------+  |                            |    ---
                                             |        |  |                            |
0x7ffffffff000: [stack]                      +========+ -------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment