Debian Codegraph @

rpython/rlib/_stacklet_shadowstack.py

raw symbols SCIP
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 # A GC wrapper around the C stacklet handles, with additionally a
13 # copy of the shadowstack (for all stacklets different than the main)
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 # Note: it's important that this is a light finalizer, otherwise
34 # the GC will call it but still expect the object to stay around for
35 # a while---and it can't stay around, because s_sscopy points to
36 # freed nonsense and customtrace() will crash
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 # This is a return that gave us a real handle. Store it.
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 # There is a fresh stacklet object waiting on the gcrootfinder,
117 # so populate it with data that represents the parent suspended
118 # stacklet and detach the stacklet object from gcrootfinder.
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 # Call the main function provided by the (RPython) user.
126 stacklet = gcrootfinder.runfn(stacklet, arg)
127 #
128 # Here, 'stacklet' points to the target stacklet to which we want
129 # to jump to next. Read the 'handle' and forget about the
130 # stacklet object.
131 gcrootfinder.sscopy = llmemory.NULL
132 return consume_stacklet(stacklet)
133
134 def _new(thread_handle, arg):
135 # No shadowstack manipulation here (no usage of gc references)
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 # No shadowstack manipulation here (no usage of gc references)
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 # 'stacklet' has a handle to target, i.e. where to switch to
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()