| 1 | import itertools, os, subprocess, py |
| 2 | from hypothesis import given, strategies |
| 3 | from rpython.tool.udir import udir |
| 4 | from rpython.rlib import rawrefcount, rgc |
| 5 | from rpython.rlib.rawrefcount import REFCNT_FROM_PYPY |
| 6 | from rpython.rlib.test.test_rawrefcount import W_Root, PyObject, PyObjectS |
| 7 | from rpython.rtyper.lltypesystem import lltype |
| 8 | from rpython.translator.c.test.test_standalone import StandaloneTests |
| 9 | from rpython.config.translationoption import get_combined_translation_config |
| 10 | |
| 11 | |
| 12 | def compile_test(basename): |
| 13 | srcdir = os.path.dirname(os.path.dirname( |
| 14 | os.path.abspath(os.path.join(__file__)))) |
| 15 | srcdir = os.path.join(srcdir, 'src') |
| 16 | |
| 17 | err = os.system("cd '%s' && gcc -Werror -lgc -I%s -o %s %s.c" |
| 18 | % (udir, srcdir, basename, basename)) |
| 19 | return err |
| 20 | |
| 21 | def setup_module(): |
| 22 | filename = str(udir.join("test-rawrefcount-boehm-check.c")) |
| 23 | with open(filename, "w") as f: |
| 24 | print >> f, '#include "gc/gc_mark.h"' |
| 25 | print >> f, '#include <stdio.h>' |
| 26 | print >> f, 'int main(void) {' |
| 27 | print >> f, ' printf("%p", &GC_set_start_callback);' |
| 28 | print >> f, ' return 0;' |
| 29 | print >> f, '}' |
| 30 | |
| 31 | if compile_test("test-rawrefcount-boehm-check") != 0: |
| 32 | py.test.skip("Boehm GC not installed or too old version") |
| 33 | |
| 34 | |
| 35 | |
| 36 | TEST_CODE = r""" |
| 37 | #define TEST_BOEHM_RAWREFCOUNT |
| 38 | #include "boehm-rawrefcount.c" |
| 39 | |
| 40 | static gcobj_t *alloc_gcobj(void) /* for tests */ |
| 41 | { |
| 42 | gcobj_t *g = GC_MALLOC(1000); |
| 43 | printf("gc obj: %p\n", g); |
| 44 | return g; |
| 45 | } |
| 46 | |
| 47 | static pyobj_t *alloc_pyobj(void) /* for tests */ |
| 48 | { |
| 49 | pyobj_t *p = malloc(1000); |
| 50 | p->ob_refcnt = 1; |
| 51 | p->ob_pypy_link = 0; |
| 52 | printf("py obj: %p\n", p); |
| 53 | return p; |
| 54 | } |
| 55 | |
| 56 | static void decref(pyobj_t *p) /* for tests */ |
| 57 | { |
| 58 | p->ob_refcnt--; |
| 59 | if (p->ob_refcnt == 0) { |
| 60 | printf("decref to zero: %p\n", p); |
| 61 | free(p); |
| 62 | } |
| 63 | assert(p->ob_refcnt >= REFCNT_FROM_PYPY || |
| 64 | p->ob_refcnt < REFCNT_FROM_PYPY * 0.99); |
| 65 | } |
| 66 | |
| 67 | void run_test(void); /* forward declaration, produced by the test */ |
| 68 | |
| 69 | int main(void) |
| 70 | { |
| 71 | run_test(); |
| 72 | while (gc_rawrefcount_next_dead() != NULL) |
| 73 | ; |
| 74 | return 0; |
| 75 | } |
| 76 | """ |
| 77 | |
| 78 | |
| 79 | operations = strategies.sampled_from([ |
| 80 | 'new_pyobj', |
| 81 | 'new_gcobj', |
| 82 | 'create_link', |
| 83 | 'from_obj', |
| 84 | 'to_obj', |
| 85 | 'forget_pyobj', |
| 86 | 'forget_gcobj', |
| 87 | 'collect', |
| 88 | 'dead', |
| 89 | ]) |
| 90 | |
| 91 | |
| 92 | @strategies.composite |
| 93 | def make_code(draw): |
| 94 | code = [] |
| 95 | pyobjs = [] |
| 96 | gcobjs = [] |
| 97 | num_gcobj = itertools.count() |
| 98 | num_pyobj = itertools.count() |
| 99 | links_g2p = {} |
| 100 | links_p2g = {} |
| 101 | |
| 102 | def new_gcobj(): |
| 103 | varname = 'g%d' % next(num_gcobj) |
| 104 | code.append('gcobj_t *volatile %s = alloc_gcobj();' % varname) |
| 105 | gcobjs.append(varname) |
| 106 | return varname |
| 107 | |
| 108 | def new_pyobj(): |
| 109 | varname = 'p%d' % next(num_pyobj) |
| 110 | code.append('pyobj_t *%s = alloc_pyobj();' % varname) |
| 111 | pyobjs.append(varname) |
| 112 | return varname |
| 113 | |
| 114 | for op in draw(strategies.lists(operations)): |
| 115 | if op == 'new_gcobj': |
| 116 | new_gcobj() |
| 117 | elif op == 'new_pyobj': |
| 118 | new_pyobj() |
| 119 | elif op == 'create_link': |
| 120 | gvars = [varname for varname in gcobjs if varname not in links_g2p] |
| 121 | if gvars == []: |
| 122 | gvars.append(new_gcobj()) |
| 123 | pvars = [varname for varname in pyobjs if varname not in links_p2g] |
| 124 | if pvars == []: |
| 125 | pvars.append(new_pyobj()) |
| 126 | gvar = draw(strategies.sampled_from(gvars)) |
| 127 | pvar = draw(strategies.sampled_from(pvars)) |
| 128 | code.append(r'printf("create_link %%p-%%p\n", %s, %s); ' |
| 129 | % (gvar, pvar) + |
| 130 | "%s->ob_refcnt += REFCNT_FROM_PYPY; " % pvar + |
| 131 | "gc_rawrefcount_create_link_pypy(%s, %s);" |
| 132 | % (gvar, pvar)) |
| 133 | links_g2p[gvar] = pvar |
| 134 | links_p2g[pvar] = gvar |
| 135 | elif op == 'from_obj': |
| 136 | if gcobjs: |
| 137 | prnt = False |
| 138 | gvar = draw(strategies.sampled_from(gcobjs)) |
| 139 | if gvar not in links_g2p: |
| 140 | check = "== NULL" |
| 141 | elif links_g2p[gvar] in pyobjs: |
| 142 | check = "== %s" % (links_g2p[gvar],) |
| 143 | else: |
| 144 | check = "!= NULL" |
| 145 | prnt = True |
| 146 | code.append("assert(gc_rawrefcount_from_obj(%s) %s);" |
| 147 | % (gvar, check)) |
| 148 | if prnt: |
| 149 | code.append(r'printf("link %%p-%%p\n", %s, ' |
| 150 | 'gc_rawrefcount_from_obj(%s));' % (gvar, gvar)) |
| 151 | elif op == 'to_obj': |
| 152 | if pyobjs: |
| 153 | prnt = False |
| 154 | pvar = draw(strategies.sampled_from(pyobjs)) |
| 155 | if pvar not in links_p2g: |
| 156 | check = "== NULL" |
| 157 | elif links_p2g[pvar] in gcobjs: |
| 158 | check = "== %s" % (links_p2g[pvar],) |
| 159 | else: |
| 160 | check = "!= NULL" |
| 161 | prnt = True |
| 162 | code.append("assert(gc_rawrefcount_to_obj(%s) %s);" |
| 163 | % (pvar, check)) |
| 164 | if prnt: |
| 165 | code.append(r'printf("link %%p-%%p\n", ' |
| 166 | 'gc_rawrefcount_to_obj(%s), %s);' % (pvar, pvar)) |
| 167 | elif op == 'forget_pyobj': |
| 168 | if pyobjs: |
| 169 | index = draw(strategies.sampled_from(range(len(pyobjs)))) |
| 170 | pvar = pyobjs.pop(index) |
| 171 | code.append(r'printf("-p%%p\n", %s); ' % pvar + |
| 172 | "decref(%s); %s = NULL;" % (pvar, pvar)) |
| 173 | elif op == 'forget_gcobj': |
| 174 | if gcobjs: |
| 175 | index = draw(strategies.sampled_from(range(len(gcobjs)))) |
| 176 | gvar = gcobjs.pop(index) |
| 177 | code.append(r'printf("-g%%p\n", %s); ' % gvar + |
| 178 | "%s = NULL;" % (gvar,)) |
| 179 | elif op == 'collect': |
| 180 | code.append("GC_gcollect();") |
| 181 | elif op == 'dead': |
| 182 | code.append('gc_rawrefcount_next_dead();') |
| 183 | else: |
| 184 | assert False, op |
| 185 | |
| 186 | return '\n'.join(code) |
| 187 | |
| 188 | |
| 189 | @given(make_code()) |
| 190 | def test_random(code): |
| 191 | filename = str(udir.join("test-rawrefcount-boehm.c")) |
| 192 | with open(filename, "w") as f: |
| 193 | print >> f, TEST_CODE |
| 194 | print >> f, 'void run_test(void) {' |
| 195 | print >> f, code |
| 196 | print >> f, '}' |
| 197 | |
| 198 | err = compile_test("test-rawrefcount-boehm") |
| 199 | if err != 0: |
| 200 | raise OSError("gcc failed") |
| 201 | p = subprocess.Popen("./test-rawrefcount-boehm", stdout=subprocess.PIPE, |
| 202 | cwd=str(udir)) |
| 203 | stdout, _ = p.communicate() |
| 204 | assert p.wait() == 0 |
| 205 | |
| 206 | gcobjs = {} |
| 207 | pyobjs = {} |
| 208 | links_p2g = {} |
| 209 | links_g2p = {} |
| 210 | for line in stdout.splitlines(): |
| 211 | if line.startswith('py obj: '): |
| 212 | p = line[8:] |
| 213 | assert not pyobjs.get(p) |
| 214 | pyobjs[p] = True |
| 215 | assert p not in links_p2g |
| 216 | elif line.startswith('gc obj: '): |
| 217 | g = line[8:] |
| 218 | assert not gcobjs.get(g) |
| 219 | gcobjs[g] = True |
| 220 | if g in links_g2p: del links_g2p[g] |
| 221 | elif line.startswith('-p'): |
| 222 | p = line[2:] |
| 223 | assert pyobjs[p] == True |
| 224 | pyobjs[p] = False |
| 225 | elif line.startswith('-g'): |
| 226 | g = line[2:] |
| 227 | assert gcobjs[g] == True |
| 228 | gcobjs[g] = False |
| 229 | elif line.startswith('decref to zero: '): |
| 230 | p = line[16:] |
| 231 | assert pyobjs[p] == False |
| 232 | assert p not in links_p2g |
| 233 | del pyobjs[p] |
| 234 | elif line.startswith('create_link '): |
| 235 | g, p = line[12:].split('-') |
| 236 | assert g in gcobjs |
| 237 | assert p in pyobjs |
| 238 | assert g not in links_g2p |
| 239 | assert p not in links_p2g |
| 240 | links_g2p[g] = p |
| 241 | links_p2g[p] = g |
| 242 | elif line.startswith('link '): |
| 243 | g, p = line[5:].split('-') |
| 244 | assert g in gcobjs |
| 245 | assert p in pyobjs |
| 246 | assert links_g2p[g] == p |
| 247 | assert links_p2g[p] == g |
| 248 | elif line.startswith('plist['): |
| 249 | pass |
| 250 | elif line.startswith('next_dead: '): |
| 251 | p = line[11:] |
| 252 | assert pyobjs[p] == False |
| 253 | del pyobjs[p] |
| 254 | del links_p2g[p] |
| 255 | else: |
| 256 | assert False, repr(line) |
| 257 | |
| 258 | |
| 259 | class TestBoehmTranslated(StandaloneTests): |
| 260 | |
| 261 | def test_full_translation(self): |
| 262 | |
| 263 | def make_ob(): |
| 264 | p = W_Root(42) |
| 265 | ob = lltype.malloc(PyObjectS, flavor='raw', zero=True) |
| 266 | rawrefcount.create_link_pypy(p, ob) |
| 267 | ob.c_ob_refcnt += REFCNT_FROM_PYPY |
| 268 | assert rawrefcount.from_obj(PyObject, p) == ob |
| 269 | assert rawrefcount.to_obj(W_Root, ob) == p |
| 270 | return ob |
| 271 | |
| 272 | prebuilt_p = W_Root(-42) |
| 273 | prebuilt_ob = lltype.malloc(PyObjectS, flavor='raw', zero=True, |
| 274 | immortal=True) |
| 275 | |
| 276 | def entry_point(argv): |
| 277 | rawrefcount.create_link_pypy(prebuilt_p, prebuilt_ob) |
| 278 | prebuilt_ob.c_ob_refcnt += REFCNT_FROM_PYPY |
| 279 | oblist = [make_ob() for i in range(50)] |
| 280 | rgc.collect() |
| 281 | deadlist = [] |
| 282 | while True: |
| 283 | ob = rawrefcount.next_dead(PyObject) |
| 284 | if not ob: break |
| 285 | if ob.c_ob_refcnt != 1: |
| 286 | print "next_dead().ob_refcnt != 1" |
| 287 | return 1 |
| 288 | deadlist.append(ob) |
| 289 | if len(deadlist) == 0: |
| 290 | print "no dead object" |
| 291 | return 1 |
| 292 | if len(deadlist) < 30: |
| 293 | print "not enough dead objects" |
| 294 | return 1 |
| 295 | for ob in deadlist: |
| 296 | if ob not in oblist: |
| 297 | print "unexpected value for dead pointer" |
| 298 | return 1 |
| 299 | oblist.remove(ob) |
| 300 | print "OK!" |
| 301 | lltype.free(ob, flavor='raw') |
| 302 | return 0 |
| 303 | |
| 304 | self.config = get_combined_translation_config(translating=True) |
| 305 | self.config.translation.gc = "boehm" |
| 306 | t, cbuilder = self.compile(entry_point) |
| 307 | data = cbuilder.cmdexec('hi there') |
| 308 | assert data.startswith('OK!\n') |