| 1 |
""" |
| 2 |
This file defines utilities for manipulating the stack in an |
| 3 |
RPython-compliant way. It is mainly about the stack_check() function. |
| 4 |
""" |
| 5 |
|
| 6 |
import py |
| 7 |
|
| 8 |
from rpython.rlib.objectmodel import we_are_translated, fetch_translated_config |
| 9 |
from rpython.rlib.rarithmetic import r_uint |
| 10 |
from rpython.rlib import rgc |
| 11 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 12 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
def llexternal(name, args, res, _callable=None): |
| 17 |
return rffi.llexternal(name, args, res, |
| 18 |
sandboxsafe=True, _nowrapper=True, |
| 19 |
_callable=_callable) |
| 20 |
|
| 21 |
_stack_get_end = llexternal('LL_stack_get_end', [], lltype.Signed, |
| 22 |
lambda: 0) |
| 23 |
_stack_get_length = llexternal('LL_stack_get_length', [], lltype.Signed, |
| 24 |
lambda: 1) |
| 25 |
_stack_set_length_fraction = llexternal('LL_stack_set_length_fraction', |
| 26 |
[lltype.Float], lltype.Void, |
| 27 |
lambda frac: None) |
| 28 |
_stack_too_big_slowpath = llexternal('LL_stack_too_big_slowpath', |
| 29 |
[lltype.Signed], lltype.Char, |
| 30 |
lambda cur: '\x00') |
| 31 |
|
| 32 |
_stack_get_end_adr = llexternal('LL_stack_get_end_adr', [], lltype.Signed) |
| 33 |
_stack_get_length_adr= llexternal('LL_stack_get_length_adr',[], lltype.Signed) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
_stack_criticalcode_start = llexternal('LL_stack_criticalcode_start', [], |
| 38 |
lltype.Void, lambda: None) |
| 39 |
_stack_criticalcode_stop = llexternal('LL_stack_criticalcode_stop', [], |
| 40 |
lltype.Void, lambda: None) |
| 41 |
|
| 42 |
def stack_check(): |
| 43 |
if not we_are_translated(): |
| 44 |
return |
| 45 |
if fetch_translated_config().translation.reverse_debugger: |
| 46 |
return |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
current = llop.stack_current(lltype.Signed) |
| 51 |
|
| 52 |
|
| 53 |
end = _stack_get_end() |
| 54 |
length = _stack_get_length() |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
ofs = r_uint(end - current) |
| 59 |
if ofs <= r_uint(length): |
| 60 |
return |
| 61 |
|
| 62 |
|
| 63 |
stack_check_slowpath(current) |
| 64 |
stack_check._always_inline_ = True |
| 65 |
stack_check._dont_insert_stackcheck_ = True |
| 66 |
|
| 67 |
@rgc.no_collect |
| 68 |
def stack_check_slowpath(current): |
| 69 |
if ord(_stack_too_big_slowpath(current)): |
| 70 |
from rpython.rlib.rstackovf import _StackOverflow |
| 71 |
raise _StackOverflow |
| 72 |
stack_check_slowpath._dont_inline_ = True |
| 73 |
stack_check_slowpath._dont_insert_stackcheck_ = True |
| 74 |
|
| 75 |
def stack_almost_full(): |
| 76 |
"""Return True if the stack is more than 15/16th full.""" |
| 77 |
if not we_are_translated(): |
| 78 |
return False |
| 79 |
|
| 80 |
current = llop.stack_current(lltype.Signed) |
| 81 |
end = _stack_get_end() |
| 82 |
length = 15 * (r_uint(_stack_get_length()) >> 4) |
| 83 |
ofs = r_uint(end - current) |
| 84 |
if ofs <= length: |
| 85 |
return False |
| 86 |
else: |
| 87 |
_stack_too_big_slowpath(current) |
| 88 |
end = _stack_get_end() |
| 89 |
ofs = r_uint(end - current) |
| 90 |
return ofs > length |
| 91 |
stack_almost_full._dont_insert_stackcheck_ = True |
| 92 |
stack_almost_full._jit_look_inside_ = False |