Skip to content

Commit 070307a

Browse files
committed
work a bit on vmmap reader and add a demo
1 parent ad77c77 commit 070307a

File tree

2 files changed

+111
-4
lines changed

2 files changed

+111
-4
lines changed

vmprof/reader.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,28 @@ def read_ranges(data):
7676
data = data.decode('latin1')
7777
ranges = []
7878
lines = data.splitlines()
79-
if lines[0].endswith('PATH'):
79+
if 'Virtual Memory Map' in lines[0]:
80+
mode = 'vmmap'
81+
end = 3
82+
while lines[end]:
83+
end += 1
84+
lines = lines[4:end]
85+
elif lines[0].endswith('PATH'):
8086
lines = lines[1:]
81-
procstat = True
87+
mode = 'procstat'
8288
else:
83-
procstat = False
89+
mode = 'proc'
8490
for line in lines:
8591
parts = re.split("\s+", line)
8692
name = parts[-1]
87-
if procstat:
93+
if mode == 'procstat':
8894
start, end = parts[1], parts[2]
95+
elif mode == 'vmmap':
96+
k = 1
97+
while not parts[k].startswith('000'):
98+
k += 1
99+
continue
100+
start, end = parts[k].split('-')
89101
else:
90102
start, end = parts[0].split('-')
91103
start = int(start, 16)

vmprofdemo.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
import random, sys
3+
4+
class Node(object):
5+
def __init__(self, right, left):
6+
self.left = left
7+
self.right = right
8+
9+
class Digit(object):
10+
def __init__(self, v):
11+
self.v = v
12+
13+
def eval(self):
14+
return ord(self.v) - ord('0')
15+
16+
class Plus(Node):
17+
def eval(self):
18+
return self.left.eval() + self.right.eval()
19+
20+
class Minus(Node):
21+
def eval(self):
22+
return self.left.eval() - self.right.eval()
23+
24+
def parse_pn(text):
25+
stack = []
26+
for c in text:
27+
if c == '+':
28+
stack.append(Plus(stack.pop(), stack.pop()))
29+
elif c == '-':
30+
stack.append(Minus(stack.pop(), stack.pop()))
31+
elif ord('0') <= ord(c) <= ord('9'):
32+
stack.append(Digit(c))
33+
else:
34+
assert False
35+
assert len(stack) == 1
36+
return stack[0]
37+
38+
def oracle(expr):
39+
return parse_pn(expr).eval() > 0
40+
41+
def find(expr):
42+
cur_expr = expr
43+
if oracle(expr):
44+
cur = 0
45+
while oracle(cur_expr):
46+
cur += 1
47+
cur_expr = cur_expr + "1-"
48+
else:
49+
cur = 0
50+
while not oracle(cur_expr):
51+
cur += 1
52+
cur_expr = cur_expr + "1+"
53+
cur = -cur + 1
54+
return cur
55+
56+
DIGITS = [chr(ord('0') + i) for i in range(10)]
57+
58+
def gen_exp(lgt):
59+
stack_depth = 0
60+
exp = ''
61+
for i in range(lgt):
62+
if stack_depth > 1:
63+
c = random.choice(DIGITS + ['-', '+'] * 4)
64+
else:
65+
c = random.choice(DIGITS)
66+
if c in DIGITS:
67+
stack_depth += 1
68+
else:
69+
stack_depth -= 1
70+
exp += c
71+
while stack_depth > 1:
72+
exp += random.choice(['-', '+'])
73+
stack_depth -= 1
74+
return exp
75+
76+
def fuzzer(count):
77+
for i in range(count):
78+
exp = gen_exp(10)
79+
assert parse_pn(exp).eval() == find(exp)
80+
81+
if __name__ == '__main__':
82+
if len(sys.argv) == 2 and sys.argv[1] == 'demo':
83+
import time
84+
for k in range(30):
85+
t0 = time.time()
86+
fuzzer(100)
87+
print "%.1f ms" % ((time.time() - t0) * 1000)
88+
sys.exit(0)
89+
if len(sys.argv) > 1:
90+
count = int(sys.argv[1])
91+
else:
92+
count = 10000
93+
if len(sys.argv) > 2:
94+
random.seed(int(sys.argv[2]))
95+
fuzzer(count)

0 commit comments

Comments
 (0)