| 1 |
from rpython.rlib import _rffi_stacklet as _c |
| 2 |
from rpython.rlib.debug import ll_assert |
| 3 |
from rpython.rlib import rgc |
| 4 |
from rpython.rtyper.annlowlevel import llhelper, MixLevelHelperAnnotator |
| 5 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 6 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 7 |
from rpython.annotator import model as annmodel |
| 8 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
STACKLET = lltype.GcStruct('Stacklet', |
| 16 |
('s_handle', _c.handle), |
| 17 |
('s_sscopy', llmemory.Address), |
| 18 |
rtti=True) |
| 19 |
STACKLET_PTR = lltype.Ptr(STACKLET) |
| 20 |
NULL_STACKLET = lltype.nullptr(STACKLET) |
| 21 |
|
| 22 |
|
| 23 |
def complete_destrptr(gctransformer): |
| 24 |
translator = gctransformer.translator |
| 25 |
mixlevelannotator = MixLevelHelperAnnotator(translator.rtyper) |
| 26 |
args_s = [lltype_to_annotation(STACKLET_PTR)] |
| 27 |
s_result = annmodel.s_None |
| 28 |
destrptr = mixlevelannotator.delayedfunction(stacklet_destructor, |
| 29 |
args_s, s_result) |
| 30 |
mixlevelannotator.finish() |
| 31 |
lltype.attachRuntimeTypeInfo(STACKLET, destrptr=destrptr) |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
@rgc.must_be_light_finalizer |
| 38 |
def stacklet_destructor(stacklet): |
| 39 |
sscopy = stacklet.s_sscopy |
| 40 |
if sscopy: |
| 41 |
llmemory.raw_free(sscopy) |
| 42 |
h = stacklet.s_handle |
| 43 |
if h: |
| 44 |
_c.destroy(h) |
| 45 |
|
| 46 |
|
| 47 |
SIZEADDR = llmemory.sizeof(llmemory.Address) |
| 48 |
|
| 49 |
def customtrace(gc, obj, callback, arg1, arg2): |
| 50 |
from rpython.memory.gctransform.shadowstack import walk_stack_root |
| 51 |
|
| 52 |
stacklet = llmemory.cast_adr_to_ptr(obj, STACKLET_PTR) |
| 53 |
sscopy = stacklet.s_sscopy |
| 54 |
if sscopy: |
| 55 |
length_bytes = sscopy.signed[0] |
| 56 |
walk_stack_root(gc._trace_callback, callback, arg1, arg2, |
| 57 |
sscopy + SIZEADDR, sscopy + SIZEADDR + length_bytes, |
| 58 |
is_minor=False) |
| 59 |
lambda_customtrace = lambda: customtrace |
| 60 |
|
| 61 |
def sscopy_detach_shadow_stack(): |
| 62 |
base = llop.gc_adr_of_root_stack_base(llmemory.Address).address[0] |
| 63 |
top = llop.gc_adr_of_root_stack_top(llmemory.Address).address[0] |
| 64 |
length_bytes = top - base |
| 65 |
result = llmemory.raw_malloc(SIZEADDR + length_bytes) |
| 66 |
if result: |
| 67 |
result.signed[0] = length_bytes |
| 68 |
llmemory.raw_memcopy(base, result + SIZEADDR, length_bytes) |
| 69 |
llop.gc_adr_of_root_stack_top(llmemory.Address).address[0] = base |
| 70 |
return result |
| 71 |
|
| 72 |
def sscopy_attach_shadow_stack(sscopy): |
| 73 |
base = llop.gc_adr_of_root_stack_base(llmemory.Address).address[0] |
| 74 |
ll_assert(llop.gc_adr_of_root_stack_top(llmemory.Address).address[0]==base, |
| 75 |
"attach_shadow_stack: ss is not empty?") |
| 76 |
length_bytes = sscopy.signed[0] |
| 77 |
llmemory.raw_memcopy(sscopy + SIZEADDR, base, length_bytes) |
| 78 |
llop.gc_adr_of_root_stack_top(llmemory.Address).address[0] = ( |
| 79 |
base + length_bytes) |
| 80 |
llop.gc_modified_shadowstack(lltype.Void) |
| 81 |
llmemory.raw_free(sscopy) |
| 82 |
|
| 83 |
def alloc_stacklet(): |
| 84 |
new_stacklet = lltype.malloc(STACKLET) |
| 85 |
new_stacklet.s_handle = _c.null_handle |
| 86 |
new_stacklet.s_sscopy = llmemory.NULL |
| 87 |
return new_stacklet |
| 88 |
|
| 89 |
def attach_handle_on_stacklet(stacklet, h): |
| 90 |
ll_assert(stacklet.s_handle == _c.null_handle, "attach stacklet 1: garbage") |
| 91 |
ll_assert(stacklet.s_sscopy == llmemory.NULL, "attach stacklet 2: garbage") |
| 92 |
if not h: |
| 93 |
raise MemoryError |
| 94 |
elif _c.is_empty_handle(h): |
| 95 |
ll_assert(gcrootfinder.sscopy == llmemory.NULL, |
| 96 |
"empty_handle but sscopy != NULL") |
| 97 |
return NULL_STACKLET |
| 98 |
else: |
| 99 |
|
| 100 |
stacklet.s_handle = h |
| 101 |
stacklet.s_sscopy = gcrootfinder.sscopy |
| 102 |
ll_assert(gcrootfinder.sscopy != llmemory.NULL, |
| 103 |
"!empty_handle but sscopy == NULL") |
| 104 |
gcrootfinder.sscopy = llmemory.NULL |
| 105 |
llop.gc_writebarrier(lltype.Void, llmemory.cast_ptr_to_adr(stacklet)) |
| 106 |
return stacklet |
| 107 |
|
| 108 |
def consume_stacklet(stacklet): |
| 109 |
h = stacklet.s_handle |
| 110 |
ll_assert(bool(h), "consume_stacklet: null handle") |
| 111 |
stacklet.s_handle = _c.null_handle |
| 112 |
stacklet.s_sscopy = llmemory.NULL |
| 113 |
return h |
| 114 |
|
| 115 |
def _new_callback(h, arg): |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
stacklet = gcrootfinder.fresh_stacklet |
| 120 |
gcrootfinder.fresh_stacklet = NULL_STACKLET |
| 121 |
ll_assert(stacklet != NULL_STACKLET, "_new_callback: NULL #1") |
| 122 |
stacklet = attach_handle_on_stacklet(stacklet, h) |
| 123 |
ll_assert(stacklet != NULL_STACKLET, "_new_callback: NULL #2") |
| 124 |
|
| 125 |
|
| 126 |
stacklet = gcrootfinder.runfn(stacklet, arg) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
gcrootfinder.sscopy = llmemory.NULL |
| 132 |
return consume_stacklet(stacklet) |
| 133 |
|
| 134 |
def _new(thread_handle, arg): |
| 135 |
|
| 136 |
sscopy = sscopy_detach_shadow_stack() |
| 137 |
gcrootfinder.sscopy = sscopy |
| 138 |
if not sscopy: |
| 139 |
return _c.null_handle |
| 140 |
h = _c.new(thread_handle, llhelper(_c.run_fn, _new_callback), arg) |
| 141 |
sscopy_attach_shadow_stack(sscopy) |
| 142 |
return h |
| 143 |
_new._dont_inline_ = True |
| 144 |
|
| 145 |
def _switch(h): |
| 146 |
|
| 147 |
sscopy = sscopy_detach_shadow_stack() |
| 148 |
gcrootfinder.sscopy = sscopy |
| 149 |
if not sscopy: |
| 150 |
return _c.null_handle |
| 151 |
h = _c.switch(h) |
| 152 |
sscopy_attach_shadow_stack(sscopy) |
| 153 |
return h |
| 154 |
_switch._dont_inline_ = True |
| 155 |
|
| 156 |
|
| 157 |
class StackletGcRootFinder(object): |
| 158 |
fresh_stacklet = NULL_STACKLET |
| 159 |
|
| 160 |
@staticmethod |
| 161 |
def new(thrd, callback, arg): |
| 162 |
rgc.register_custom_trace_hook(STACKLET, lambda_customtrace) |
| 163 |
result_stacklet = alloc_stacklet() |
| 164 |
gcrootfinder.fresh_stacklet = alloc_stacklet() |
| 165 |
gcrootfinder.runfn = callback |
| 166 |
thread_handle = thrd._thrd |
| 167 |
h = _new(thread_handle, arg) |
| 168 |
return attach_handle_on_stacklet(result_stacklet, h) |
| 169 |
|
| 170 |
@staticmethod |
| 171 |
def switch(stacklet): |
| 172 |
|
| 173 |
h = consume_stacklet(stacklet) |
| 174 |
h = _switch(h) |
| 175 |
return attach_handle_on_stacklet(stacklet, h) |
| 176 |
|
| 177 |
@staticmethod |
| 178 |
def is_empty_handle(stacklet): |
| 179 |
return not stacklet |
| 180 |
|
| 181 |
@staticmethod |
| 182 |
def get_null_handle(): |
| 183 |
return NULL_STACKLET |
| 184 |
|
| 185 |
|
| 186 |
gcrootfinder = StackletGcRootFinder() |