Debian Codegraph @

rpython/rtyper/lltypesystem/test/test_llarena.py

raw symbols SCIP
1 import py, os
2
3 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi, llarena
4 from rpython.rtyper.lltypesystem.llarena import (arena_malloc, arena_reset,
5 arena_reserve, arena_free, round_up_for_allocation, ArenaError,
6 arena_new_view, arena_shrink_obj, arena_protect, has_protect)
7 from rpython.rtyper.lltypesystem.llmemory import cast_adr_to_ptr
8 from rpython.translator.c.test import test_genc, test_standalone
9
10
11 def test_arena():
12 S = lltype.Struct('S', ('x', lltype.Signed))
13 SPTR = lltype.Ptr(S)
14 ssize = llmemory.raw_malloc_usage(llmemory.sizeof(S))
15 myarenasize = 2*ssize+1
16 a = arena_malloc(myarenasize, False)
17 assert a != llmemory.NULL
18 assert a + 3 != llmemory.NULL
19
20 arena_reserve(a, llmemory.sizeof(S))
21 s1_ptr1 = cast_adr_to_ptr(a, SPTR)
22 s1_ptr1.x = 1
23 s1_ptr2 = cast_adr_to_ptr(a, SPTR)
24 assert s1_ptr2.x == 1
25 assert s1_ptr1 == s1_ptr2
26
27 py.test.raises(ArenaError, arena_reserve, a + ssize + 1, # misaligned
28 llmemory.sizeof(S))
29 arena_reserve(a + ssize + 1, llmemory.sizeof(S), check_alignment=False)
30 s2_ptr1 = cast_adr_to_ptr(a + ssize + 1, SPTR)
31 py.test.raises(lltype.UninitializedMemoryAccess, 's2_ptr1.x')
32 s2_ptr1.x = 2
33 s2_ptr2 = cast_adr_to_ptr(a + ssize + 1, SPTR)
34 assert s2_ptr2.x == 2
35 assert s2_ptr1 == s2_ptr2
36 assert s1_ptr1 != s2_ptr1
37 assert not (s2_ptr2 == s1_ptr2)
38 assert s1_ptr1 == cast_adr_to_ptr(a, SPTR)
39
40 S2 = lltype.Struct('S2', ('y',lltype.Char))
41 S2PTR = lltype.Ptr(S2)
42 py.test.raises(lltype.InvalidCast, cast_adr_to_ptr, a, S2PTR)
43 py.test.raises(ArenaError, cast_adr_to_ptr, a+1, SPTR)
44 py.test.raises(ArenaError, cast_adr_to_ptr, a+ssize, SPTR)
45 py.test.raises(ArenaError, cast_adr_to_ptr, a+2*ssize, SPTR)
46 py.test.raises(ArenaError, cast_adr_to_ptr, a+2*ssize+1, SPTR)
47 py.test.raises(ArenaError, arena_reserve, a+1, llmemory.sizeof(S),
48 False)
49 py.test.raises(ArenaError, arena_reserve, a+ssize, llmemory.sizeof(S),
50 False)
51 py.test.raises(ArenaError, arena_reserve, a+2*ssize, llmemory.sizeof(S),
52 False)
53 py.test.raises(ArenaError, arena_reserve, a+2*ssize+1, llmemory.sizeof(S),
54 False)
55
56 arena_reset(a, myarenasize, True)
57 py.test.raises(ArenaError, cast_adr_to_ptr, a, SPTR)
58 arena_reserve(a, llmemory.sizeof(S))
59 s1_ptr1 = cast_adr_to_ptr(a, SPTR)
60 assert s1_ptr1.x == 0
61 s1_ptr1.x = 5
62
63 arena_reserve(a + ssize, llmemory.sizeof(S2), check_alignment=False)
64 s2_ptr1 = cast_adr_to_ptr(a + ssize, S2PTR)
65 assert s2_ptr1.y == '\x00'
66 s2_ptr1.y = 'X'
67
68 assert cast_adr_to_ptr(a + 0, SPTR).x == 5
69 assert cast_adr_to_ptr((a + ssize + 1) - 1, S2PTR).y == 'X'
70
71 assert (a + 4) - (a + 1) == 3
72
73
74 def lt(x, y):
75 if x < y:
76 assert (x < y) and (y > x)
77 assert (x <= y) and (y >= x)
78 assert not (x == y) and not (y == x)
79 assert (x != y) and (y != x)
80 assert not (x > y) and not (y < x)
81 assert not (x >= y) and not (y <= x)
82 return True
83 else:
84 assert (x >= y) and (y <= x)
85 assert (x == y) == (not (x != y)) == (y == x) == (not (y != x))
86 assert (x > y) == (not (x == y)) == (y < x)
87 return False
88
89 def eq(x, y):
90 if x == y:
91 assert not (x != y) and not (y != x)
92 assert not (x < y) and not (y > x)
93 assert not (x > y) and not (y < x)
94 assert (x <= y) and (y >= x)
95 assert (x >= y) and (y <= x)
96 return True
97 else:
98 assert (x != y) and (y != x)
99 assert ((x < y) == (x <= y) == (not (x > y)) == (not (x >= y)) ==
100 (y > x) == (y >= x) == (not (y < x)) == (not (y <= x)))
101 return False
102
103
104 def test_address_order():
105 a = arena_malloc(24, False)
106 assert eq(a, a)
107 assert lt(a, a+1)
108 assert lt(a+5, a+20)
109
110 b = arena_malloc(24, False)
111 if a > b:
112 a, b = b, a
113 assert lt(a, b)
114 assert lt(a+19, b)
115 assert lt(a, b+19)
116
117 c = b + round_up_for_allocation(llmemory.sizeof(lltype.Char))
118 arena_reserve(c, precomputed_size)
119 assert lt(b, c)
120 assert lt(a, c)
121 assert lt(llmemory.NULL, c)
122 d = c + llmemory.offsetof(SX, 'x')
123 assert lt(c, d)
124 assert lt(b, d)
125 assert lt(a, d)
126 assert lt(llmemory.NULL, d)
127 e = c + precomputed_size
128 assert lt(d, e)
129 assert lt(c, e)
130 assert lt(b, e)
131 assert lt(a, e)
132 assert lt(llmemory.NULL, e)
133
134
135 SX = lltype.Struct('S', ('foo',lltype.Signed), ('x',lltype.Signed))
136 SPTR = lltype.Ptr(SX)
137 precomputed_size = round_up_for_allocation(llmemory.sizeof(SX))
138
139 def test_look_inside_object():
140 # this code is also used in translation tests below
141 myarenasize = 50
142 a = arena_malloc(myarenasize, False)
143 b = a + round_up_for_allocation(llmemory.sizeof(lltype.Char))
144 arena_reserve(b, precomputed_size)
145 (b + llmemory.offsetof(SX, 'x')).signed[0] = 123
146 assert llmemory.cast_adr_to_ptr(b, SPTR).x == 123
147 llmemory.cast_adr_to_ptr(b, SPTR).x += 1
148 assert (b + llmemory.offsetof(SX, 'x')).signed[0] == 124
149 arena_reset(a, myarenasize, True)
150 arena_reserve(b, round_up_for_allocation(llmemory.sizeof(SX)))
151 assert llmemory.cast_adr_to_ptr(b, SPTR).x == 0
152 arena_free(a)
153 return 42
154
155 def test_arena_new_view():
156 a = arena_malloc(50, False)
157 arena_reserve(a, precomputed_size)
158 # we can now allocate the same space in new view
159 b = arena_new_view(a)
160 arena_reserve(b, precomputed_size)
161
162 def test_partial_arena_reset():
163 a = arena_malloc(72, False)
164 def reserve(i):
165 b = a + i * llmemory.raw_malloc_usage(precomputed_size)
166 arena_reserve(b, precomputed_size)
167 return b
168 blist = []
169 plist = []
170 for i in range(4):
171 b = reserve(i)
172 (b + llmemory.offsetof(SX, 'x')).signed[0] = 100 + i
173 blist.append(b)
174 plist.append(llmemory.cast_adr_to_ptr(b, SPTR))
175 # clear blist[1] and blist[2] but not blist[0] nor blist[3]
176 arena_reset(blist[1], llmemory.raw_malloc_usage(precomputed_size)*2, False)
177 py.test.raises(RuntimeError, "plist[1].x") # marked as freed
178 py.test.raises(RuntimeError, "plist[2].x") # marked as freed
179 # re-reserve object at index 1 and 2
180 blist[1] = reserve(1)
181 blist[2] = reserve(2)
182 # check via object pointers
183 assert plist[0].x == 100
184 assert plist[3].x == 103
185 py.test.raises(RuntimeError, "plist[1].x") # marked as freed
186 py.test.raises(RuntimeError, "plist[2].x") # marked as freed
187 # but we can still cast the old ptrs to addresses, which compare equal
188 # to the new ones we gotq
189 assert llmemory.cast_ptr_to_adr(plist[1]) == blist[1]
190 assert llmemory.cast_ptr_to_adr(plist[2]) == blist[2]
191 # check via addresses
192 assert (blist[0] + llmemory.offsetof(SX, 'x')).signed[0] == 100
193 assert (blist[3] + llmemory.offsetof(SX, 'x')).signed[0] == 103
194 py.test.raises(lltype.UninitializedMemoryAccess,
195 "(blist[1] + llmemory.offsetof(SX, 'x')).signed[0]")
196 py.test.raises(lltype.UninitializedMemoryAccess,
197 "(blist[2] + llmemory.offsetof(SX, 'x')).signed[0]")
198 # clear and zero-fill the area over blist[0] and blist[1]
199 arena_reset(blist[0], llmemory.raw_malloc_usage(precomputed_size)*2, True)
200 # re-reserve and check it's zero
201 blist[0] = reserve(0)
202 blist[1] = reserve(1)
203 assert (blist[0] + llmemory.offsetof(SX, 'x')).signed[0] == 0
204 assert (blist[1] + llmemory.offsetof(SX, 'x')).signed[0] == 0
205 assert (blist[3] + llmemory.offsetof(SX, 'x')).signed[0] == 103
206 py.test.raises(lltype.UninitializedMemoryAccess,
207 "(blist[2] + llmemory.offsetof(SX, 'x')).signed[0]")
208
209 def test_address_eq_as_int():
210 a = arena_malloc(50, False)
211 arena_reserve(a, precomputed_size)
212 p = llmemory.cast_adr_to_ptr(a, SPTR)
213 a1 = llmemory.cast_ptr_to_adr(p)
214 assert a == a1
215 assert not (a != a1)
216 assert (a+1) != a1
217 assert not ((a+1) == a1)
218 py.test.skip("cast_adr_to_int() is hard to get consistent")
219 assert llmemory.cast_adr_to_int(a) == llmemory.cast_adr_to_int(a1)
220 assert llmemory.cast_adr_to_int(a+1) == llmemory.cast_adr_to_int(a1) + 1
221
222 def test_replace_object_with_stub():
223 from rpython.memory.gcheader import GCHeaderBuilder
224 HDR = lltype.Struct('HDR', ('x', lltype.Signed))
225 S = lltype.GcStruct('S', ('y', lltype.Signed), ('z', lltype.Signed))
226 STUB = lltype.GcStruct('STUB', ('t', lltype.Char))
227 gcheaderbuilder = GCHeaderBuilder(HDR)
228 size_gc_header = gcheaderbuilder.size_gc_header
229 ssize = llmemory.raw_malloc_usage(llmemory.sizeof(S))
230
231 a = arena_malloc(13*ssize, True)
232 hdraddr = a + 3*ssize
233 arena_reserve(hdraddr, size_gc_header + llmemory.sizeof(S))
234 hdr = llmemory.cast_adr_to_ptr(hdraddr, lltype.Ptr(HDR))
235 hdr.x = 42
236 obj = llmemory.cast_adr_to_ptr(hdraddr + size_gc_header, lltype.Ptr(S))
237 obj.y = -5
238 obj.z = -6
239
240 hdraddr = llmemory.cast_ptr_to_adr(obj) - size_gc_header
241 arena_reset(hdraddr, size_gc_header + llmemory.sizeof(S), False)
242 arena_reserve(hdraddr, size_gc_header + llmemory.sizeof(STUB))
243
244 # check that it possible to reach the newly reserved HDR+STUB
245 # via the header of the old 'obj' pointer, both via the existing
246 # 'hdraddr':
247 hdr = llmemory.cast_adr_to_ptr(hdraddr, lltype.Ptr(HDR))
248 hdr.x = 46
249 stub = llmemory.cast_adr_to_ptr(hdraddr + size_gc_header, lltype.Ptr(STUB))
250 stub.t = '!'
251
252 # and via a (now-invalid) pointer to the old 'obj': (this is needed
253 # because during a garbage collection there are still pointers to
254 # the old 'obj' around to be fixed)
255 hdraddr = llmemory.cast_ptr_to_adr(obj) - size_gc_header
256 hdr = llmemory.cast_adr_to_ptr(hdraddr, lltype.Ptr(HDR))
257 assert hdr.x == 46
258 stub = llmemory.cast_adr_to_ptr(hdraddr + size_gc_header,
259 lltype.Ptr(STUB))
260 assert stub.t == '!'
261
262
263 def test_llinterpreted():
264 from rpython.rtyper.test.test_llinterp import interpret
265 res = interpret(test_look_inside_object, [])
266 assert res == 42
267
268 def test_compiled():
269 fn = test_genc.compile(test_look_inside_object, [])
270 res = fn()
271 assert res == 42
272
273 def test_shrink_obj():
274 from rpython.memory.gcheader import GCHeaderBuilder
275 HDR = lltype.Struct('HDR', ('h', lltype.Signed))
276 gcheaderbuilder = GCHeaderBuilder(HDR)
277 size_gc_header = gcheaderbuilder.size_gc_header
278 S = lltype.GcStruct('S', ('x', lltype.Signed),
279 ('a', lltype.Array(lltype.Signed)))
280 myarenasize = 200
281 a = arena_malloc(myarenasize, False)
282 arena_reserve(a, size_gc_header + llmemory.sizeof(S, 10))
283 arena_shrink_obj(a, size_gc_header + llmemory.sizeof(S, 5))
284 arena_reset(a, size_gc_header + llmemory.sizeof(S, 5), False)
285
286 def test_arena_protect():
287 a = arena_malloc(100, False)
288 S = lltype.Struct('S', ('x', lltype.Signed))
289 arena_reserve(a, llmemory.sizeof(S))
290 p = llmemory.cast_adr_to_ptr(a, lltype.Ptr(S))
291 p.x = 123
292 assert p.x == 123
293 arena_protect(a, 100, True)
294 py.test.raises(ArenaError, arena_reserve, a + 48, llmemory.sizeof(S))
295 py.test.raises(RuntimeError, "p.x")
296 py.test.raises(RuntimeError, "p.x = 124")
297 arena_protect(a, 100, False)
298 assert p.x == 123
299 p.x = 125
300 assert p.x == 125
301
302 def test_madvise_arena_free():
303 from rpython.rlib import rmmap
304
305 if os.name != 'posix':
306 py.test.skip("posix only")
307 pagesize = llarena.posixpagesize.get()
308 prev = rmmap.madvise_free
309 try:
310 seen = []
311 def my_madvise_free(addr, size):
312 assert lltype.typeOf(addr) == rmmap.PTR
313 seen.append((addr, size))
314 rmmap.madvise_free = my_madvise_free
315 llarena.madvise_arena_free(
316 rffi.cast(llmemory.Address, 123 * pagesize + 1),
317 pagesize * 7 - 2)
318 finally:
319 rmmap.madvise_free = prev
320 assert len(seen) == 1
321 addr, size = seen[0]
322 assert rffi.cast(lltype.Signed, addr) == 124 * pagesize
323 assert size == pagesize * 5
324
325
326 class TestStandalone(test_standalone.StandaloneTests):
327 def test_compiled_arena_protect(self):
328 import sys
329 S = lltype.Struct('S', ('x', lltype.Signed))
330 #
331 def fn(argv):
332 testrun = int(argv[1])
333 a = arena_malloc(65536, False)
334 arena_reserve(a, llmemory.sizeof(S))
335 p = llmemory.cast_adr_to_ptr(a + 23432, lltype.Ptr(S))
336 p.x = 123
337 assert p.x == 123
338 arena_protect(a, 65536, True)
339 result = 0
340 if testrun == 1:
341 print p.x # segfault
342 if testrun == 2:
343 p.x = 124 # segfault
344 arena_protect(a, 65536, False)
345 p.x += 10
346 print p.x
347 return 0
348 #
349 t, cbuilder = self.compile(fn)
350 data = cbuilder.cmdexec('0')
351 assert data == '133\n'
352 if has_protect:
353 if sys.platform.startswith('win'):
354 # Do not open error dialog box
355 import ctypes
356 SEM_NOGPFAULTERRORBOX = 0x0002 # From MSDN
357 old_err_mode = ctypes.windll.kernel32.GetErrorMode()
358 new_err_mode = old_err_mode | SEM_NOGPFAULTERRORBOX
359 ctypes.windll.kernel32.SetErrorMode(new_err_mode)
360 cbuilder.cmdexec('1', expect_crash=True)
361 cbuilder.cmdexec('2', expect_crash=True)
362 if sys.platform.startswith('win'):
363 ctypes.windll.kernel32.SetErrorMode(old_err_mode)