File: fuzzbidi.py

package info (click to toggle)
graphite2 1.3.14-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,588 kB
  • sloc: cpp: 14,738; cs: 1,998; python: 1,737; ansic: 1,673; perl: 184; xml: 123; sh: 104; makefile: 62
file content (163 lines) | stat: -rwxr-xr-x 5,023 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python3

import graphite2
import itertools
import math
import os
import random
import resource
import sys
import time
from multiprocessing import Pool, current_process
from argparse import ArgumentParser


class Estimator:
    def __init__(self, max):
        self.__base = time.time()
        self.__max = float(max)
        self.__m = 0
        self.__t = 1
        self.__st = self.__base
        self.__ct = 0
        self.modulus = 10

    def sample(self, current):
        ct = time.time()
        if (ct - self.__st <= opts.status):
            return None
        else:
            self.modulus = int(round(math.pow(
                                10,
                                math.floor(math.log10(current - self.__ct)))))
        self.__ct = current
        x = current / (ct-self.__base)
        k = (ct-self.__base)/opts.status+1
        m = self.__m + (x - self.__m)/k
        s = (k-1) and (self.__t + (x - self.__m)*(x - m)) / (k-1)
        m = self.bayes(x, s, self.__m, self.__t)
        self.__t = s
        self.__m = m
        self.__st = ct
        return (x, time.ctime(ct + (self.__max - current)/m) if m else 'never')

    @staticmethod
    def bayes(x, s, m, t):
        st = s + t
        return m*s/st + x*t/st


class MultiProc(object):
    chunksize = 10

    def __init__(self, doclass, *args):
        self.pool = Pool(initializer=self.initialize,
                         initargs=[doclass, args],
                         processes=opts.cpus)

    def initialize(self, doclass, args):
        proc = current_process()
        proc.inst = doclass(*args)

    def execute(self, top):
        if top == 0:
            myiter = itertools.repeat(1)
        else:
            myiter = range(top)
        e = Estimator(top)
        try:
            results = self.pool.imap_unordered(multiproctest, myiter,
                                               self.chunksize)
            for count, res in enumerate(results):
                if count % e.modulus == 0:
                    sam = e.sample(count)
                    if sam:
                        sys.stdout.write("{0} - {1:8.2f}: {2}\r".format(
                                    count,
                                    sam[0],
                                    (sam[1] if top else "")))
                        sys.stdout.flush()
                yield (count, res)
        except KeyboardInterrupt:
            sys.stdout.write("\n")
            sys.exit(0)


def multiproctest(val):
    proc = current_process()
    return proc.inst.test()


class Test(object):
    chars = " %()-.3=[]A{}\u0627\u064b\u0663\u08f1" \
            "\u200b\u202a\u202b\u202c\u202d\u202e\u2066\u2067\u2068\u2069"
    seed = None

    def __init__(self, grface, grfont):
        self.random = random.Random()
        self.random.seed(self.seed)
        Test.seed = self.random.random()
        self.grface = grface
        self.font = grfont
        self.rtl = 1
        self.feats = {}

    def test(self):
        length = int(self.random.betavariate(2, 50) * 1000)
        lenchars = len(self.chars) - 1

        def g(x): return self.chars[self.random.randint(0, lenchars)]
        text = "".join(map(g, range(length)))
        pid = os.fork()
        if pid < 0:
            print("Failed to fork")
            return (-1, text)
        elif pid > 0:
            (respid, status) = os.waitpid(pid, 0)
            if (status & 0xFF):
                return (status & 0x7F, text)  # don't care abut the core
            else:
                return (0, text)
        else:
            self.subtest(text)

    def subtest(self, text):
        if opts.timeout:
            resource.setrlimit(resource.RLIMIT_CPU,
                               (opts.timeout, opts.timeout))
        if opts.memory:
            mem = opts.memory * 1024 * 1024
            resource.setrlimit(resource.RLIMIT_AS, (mem, mem))
        graphite2.Segment(self.font, self.grface, "", text, self.rtl,
                          self.feats)
        sys.exit(0)


p = ArgumentParser()
p.add_argument('fontname', help='font file')
p.add_argument('-m', '--max', type=int, default=0,
               help="Number of tests to run [infinite]")
p.add_argument('--timeout', type=int,
               help="limit subprocess time in seconds")
p.add_argument('--memory', type=int,
               help="memory limit for subprocesses in MB")
p.add_argument('-s', '--status', type=int, default=10,
               help="Update status every n seconds")
p.add_argument('-j', '--cpus', type=int,
               help="Number of threads to run in parallel [num cpus]")
opts = p.parse_args()

try:
    grface = graphite2.Face(opts.fontname)
except Exception as err:
    print("Failed to load font {}: {}".format(opts.fontname, err),
          file=sys.stderr, flush=True)
    sys.exit(1)
grfont = graphite2.Font(grface, 16)

m = MultiProc(Test, grface, grfont)

for (count, res) in m.execute(opts.max):
    if res[0]: print("{0},{1}".format(res[0], repr(res[1])))

print("\n")