File: test_rawrefcount_boehm.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (308 lines) | stat: -rw-r--r-- 10,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import itertools, os, subprocess, py
from hypothesis import given, strategies
from rpython.tool.udir import udir
from rpython.rlib import rawrefcount, rgc
from rpython.rlib.rawrefcount import REFCNT_FROM_PYPY
from rpython.rlib.test.test_rawrefcount import W_Root, PyObject, PyObjectS
from rpython.rtyper.lltypesystem import lltype
from rpython.translator.c.test.test_standalone import StandaloneTests
from rpython.config.translationoption import get_combined_translation_config


def compile_test(basename):
    srcdir = os.path.dirname(os.path.dirname(
        os.path.abspath(os.path.join(__file__))))
    srcdir = os.path.join(srcdir, 'src')

    err = os.system("cd '%s' && gcc -Werror -lgc -I%s -o %s %s.c"
                    % (udir, srcdir, basename, basename))
    return err

def setup_module():
    filename = str(udir.join("test-rawrefcount-boehm-check.c"))
    with open(filename, "w") as f:
        print >> f, '#include "gc/gc_mark.h"'
        print >> f, '#include <stdio.h>'
        print >> f, 'int main(void) {'
        print >> f, '    printf("%p", &GC_set_start_callback);'
        print >> f, '    return 0;'
        print >> f, '}'

    if compile_test("test-rawrefcount-boehm-check") != 0:
        py.test.skip("Boehm GC not installed or too old version")



TEST_CODE = r"""
#define TEST_BOEHM_RAWREFCOUNT
#include "boehm-rawrefcount.c"

static gcobj_t *alloc_gcobj(void)   /* for tests */
{
    gcobj_t *g = GC_MALLOC(1000);
    printf("gc obj: %p\n", g);
    return g;
}

static pyobj_t *alloc_pyobj(void)   /* for tests */
{
    pyobj_t *p = malloc(1000);
    p->ob_refcnt = 1;
    p->ob_pypy_link = 0;
    printf("py obj: %p\n", p);
    return p;
}

static void decref(pyobj_t *p)      /* for tests */
{
    p->ob_refcnt--;
    if (p->ob_refcnt == 0) {
        printf("decref to zero: %p\n", p);
        free(p);
    }
    assert(p->ob_refcnt >= REFCNT_FROM_PYPY ||
           p->ob_refcnt < REFCNT_FROM_PYPY * 0.99);
}

void run_test(void);     /* forward declaration, produced by the test */

int main(void)
{
    run_test();
    while (gc_rawrefcount_next_dead() != NULL)
        ;
    return 0;
}
"""


operations = strategies.sampled_from([
    'new_pyobj',
    'new_gcobj',
    'create_link',
    'from_obj',
    'to_obj',
    'forget_pyobj',
    'forget_gcobj',
    'collect',
    'dead',
    ])


@strategies.composite
def make_code(draw):
    code = []
    pyobjs = []
    gcobjs = []
    num_gcobj = itertools.count()
    num_pyobj = itertools.count()
    links_g2p = {}
    links_p2g = {}

    def new_gcobj():
        varname = 'g%d' % next(num_gcobj)
        code.append('gcobj_t *volatile %s = alloc_gcobj();' % varname)
        gcobjs.append(varname)
        return varname

    def new_pyobj():
        varname = 'p%d' % next(num_pyobj)
        code.append('pyobj_t *%s = alloc_pyobj();' % varname)
        pyobjs.append(varname)
        return varname

    for op in draw(strategies.lists(operations, average_size=250)):
        if op == 'new_gcobj':
            new_gcobj()
        elif op == 'new_pyobj':
            new_pyobj()
        elif op == 'create_link':
            gvars = [varname for varname in gcobjs if varname not in links_g2p]
            if gvars == []:
                gvars.append(new_gcobj())
            pvars = [varname for varname in pyobjs if varname not in links_p2g]
            if pvars == []:
                pvars.append(new_pyobj())
            gvar = draw(strategies.sampled_from(gvars))
            pvar = draw(strategies.sampled_from(pvars))
            code.append(r'printf("create_link %%p-%%p\n", %s, %s); '
                            % (gvar, pvar) +
                        "%s->ob_refcnt += REFCNT_FROM_PYPY; " % pvar +
                        "gc_rawrefcount_create_link_pypy(%s, %s);"
                            % (gvar, pvar))
            links_g2p[gvar] = pvar
            links_p2g[pvar] = gvar
        elif op == 'from_obj':
            if gcobjs:
                prnt = False
                gvar = draw(strategies.sampled_from(gcobjs))
                if gvar not in links_g2p:
                    check = "== NULL"
                elif links_g2p[gvar] in pyobjs:
                    check = "== %s" % (links_g2p[gvar],)
                else:
                    check = "!= NULL"
                    prnt = True
                code.append("assert(gc_rawrefcount_from_obj(%s) %s);"
                            % (gvar, check))
                if prnt:
                    code.append(r'printf("link %%p-%%p\n", %s, '
                        'gc_rawrefcount_from_obj(%s));' % (gvar, gvar))
        elif op == 'to_obj':
            if pyobjs:
                prnt = False
                pvar = draw(strategies.sampled_from(pyobjs))
                if pvar not in links_p2g:
                    check = "== NULL"
                elif links_p2g[pvar] in gcobjs:
                    check = "== %s" % (links_p2g[pvar],)
                else:
                    check = "!= NULL"
                    prnt = True
                code.append("assert(gc_rawrefcount_to_obj(%s) %s);"
                            % (pvar, check))
                if prnt:
                    code.append(r'printf("link %%p-%%p\n", '
                        'gc_rawrefcount_to_obj(%s), %s);' % (pvar, pvar))
        elif op == 'forget_pyobj':
            if pyobjs:
                index = draw(strategies.sampled_from(range(len(pyobjs))))
                pvar = pyobjs.pop(index)
                code.append(r'printf("-p%%p\n", %s); ' % pvar +
                            "decref(%s); %s = NULL;" % (pvar, pvar))
        elif op == 'forget_gcobj':
            if gcobjs:
                index = draw(strategies.sampled_from(range(len(gcobjs))))
                gvar = gcobjs.pop(index)
                code.append(r'printf("-g%%p\n", %s); ' % gvar +
                            "%s = NULL;" % (gvar,))
        elif op == 'collect':
            code.append("GC_gcollect();")
        elif op == 'dead':
            code.append('gc_rawrefcount_next_dead();')
        else:
            assert False, op

    return '\n'.join(code)


@given(make_code())
def test_random(code):
    filename = str(udir.join("test-rawrefcount-boehm.c"))
    with open(filename, "w") as f:
        print >> f, TEST_CODE
        print >> f, 'void run_test(void) {'
        print >> f, code
        print >> f, '}'

    err = compile_test("test-rawrefcount-boehm")
    if err != 0:
        raise OSError("gcc failed")
    p = subprocess.Popen("./test-rawrefcount-boehm", stdout=subprocess.PIPE,
                         cwd=str(udir))
    stdout, _ = p.communicate()
    assert p.wait() == 0

    gcobjs = {}
    pyobjs = {}
    links_p2g = {}
    links_g2p = {}
    for line in stdout.splitlines():
        if line.startswith('py obj: '):
            p = line[8:]
            assert not pyobjs.get(p)
            pyobjs[p] = True
            assert p not in links_p2g
        elif line.startswith('gc obj: '):
            g = line[8:]
            assert not gcobjs.get(g)
            gcobjs[g] = True
            if g in links_g2p: del links_g2p[g]
        elif line.startswith('-p'):
            p = line[2:]
            assert pyobjs[p] == True
            pyobjs[p] = False
        elif line.startswith('-g'):
            g = line[2:]
            assert gcobjs[g] == True
            gcobjs[g] = False
        elif line.startswith('decref to zero: '):
            p = line[16:]
            assert pyobjs[p] == False
            assert p not in links_p2g
            del pyobjs[p]
        elif line.startswith('create_link '):
            g, p = line[12:].split('-')
            assert g in gcobjs
            assert p in pyobjs
            assert g not in links_g2p
            assert p not in links_p2g
            links_g2p[g] = p
            links_p2g[p] = g
        elif line.startswith('link '):
            g, p = line[5:].split('-')
            assert g in gcobjs
            assert p in pyobjs
            assert links_g2p[g] == p
            assert links_p2g[p] == g
        elif line.startswith('plist['):
            pass
        elif line.startswith('next_dead: '):
            p = line[11:]
            assert pyobjs[p] == False
            del pyobjs[p]
            del links_p2g[p]
        else:
            assert False, repr(line)


class TestBoehmTranslated(StandaloneTests):

    def test_full_translation(self):

        def make_ob():
            p = W_Root(42)
            ob = lltype.malloc(PyObjectS, flavor='raw', zero=True)
            rawrefcount.create_link_pypy(p, ob)
            ob.c_ob_refcnt += REFCNT_FROM_PYPY
            assert rawrefcount.from_obj(PyObject, p) == ob
            assert rawrefcount.to_obj(W_Root, ob) == p
            return ob

        prebuilt_p = W_Root(-42)
        prebuilt_ob = lltype.malloc(PyObjectS, flavor='raw', zero=True,
                                    immortal=True)

        def entry_point(argv):
            rawrefcount.create_link_pypy(prebuilt_p, prebuilt_ob)
            prebuilt_ob.c_ob_refcnt += REFCNT_FROM_PYPY
            oblist = [make_ob() for i in range(50)]
            rgc.collect()
            deadlist = []
            while True:
                ob = rawrefcount.next_dead(PyObject)
                if not ob: break
                if ob.c_ob_refcnt != 1:
                    print "next_dead().ob_refcnt != 1"
                    return 1
                deadlist.append(ob)
            if len(deadlist) == 0:
                print "no dead object"
                return 1
            if len(deadlist) < 30:
                print "not enough dead objects"
                return 1
            for ob in deadlist:
                if ob not in oblist:
                    print "unexpected value for dead pointer"
                    return 1
                oblist.remove(ob)
            print "OK!"
            lltype.free(ob, flavor='raw')
            return 0

        self.config = get_combined_translation_config(translating=True)
        self.config.translation.gc = "boehm"
        t, cbuilder = self.compile(entry_point)
        data = cbuilder.cmdexec('hi there')
        assert data.startswith('OK!\n')