-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathtype-layout-fuzzer.py
97 lines (70 loc) · 2.36 KB
/
type-layout-fuzzer.py
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
#!/usr/bin/env python
# This script outputs a Swift source with randomly-generated type definitions,
# which can be used for ABI or layout algorithm fuzzing.
# TODO: generate types with generics, existentials, compositions
# TODO: prevent cycles in inline storage
import collections
import random
maxDepth = 5
maxMembers = 5
typesToDefine = collections.deque([0])
nextName = 1
random.seed()
def randomTypeList(depth):
count = random.randint(0, maxMembers)
result = "("
for i in xrange(count):
if i > 0:
result += ", "
result += randomTypeReference(depth + 1)
result += ")"
return result
def randomTypeReference(depth):
def nominal():
global nextName
global typesToDefine
which = random.randint(0,
nextName if depth < maxDepth else nextName - 1)
if which == nextName:
typesToDefine.append(which)
nextName += 1
return "T" + str(which)
def tuple():
return randomTypeList(depth + 1)
def metatype():
return "(" + randomTypeReference(depth + 1) + ").Type"
def leaf():
leaves = ["Int", "String", "Int8", "Int16", "Int32", "Int64"]
return random.choice(leaves)
if depth < maxDepth:
kinds = [nominal, tuple, metatype, leaf, leaf, leaf, leaf, leaf]
else:
kinds = [leaf]
return random.choice(kinds)()
def defineRandomProduct(kind, name, depth):
print(kind + " " + name + " {")
# Suppress errors about missing initializers
print(" init() { fatalError() }")
numMembers = random.randint(0, maxMembers)
for i in xrange(numMembers):
print(" var x" + str(i) + ": " + randomTypeReference(depth + 1))
print("}")
def defineRandomEnum(name, depth):
# TODO: indirect cases
print("enum " + name + " {")
numCases = random.randint(0, maxMembers)
for i in xrange(numCases):
print(" case x" + str(i) + randomTypeList(depth + 1))
print("}")
def defineRandomType(name, depth):
def struct():
defineRandomProduct("struct", name, depth)
def clas():
defineRandomProduct("class", name, depth)
def enum():
defineRandomEnum(name, depth)
kinds = [struct, clas, enum]
return random.choice(kinds)()
while len(typesToDefine) > 0:
ty = typesToDefine.popleft()
defineRandomType("T" + str(ty), 0)