foundationdb/fdbrpc/actorFuzz.py

457 lines
12 KiB
Python
Raw Normal View History

2017-05-26 04:48:44 +08:00
#
# actorFuzz.py
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
2017-05-26 04:48:44 +08:00
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
2017-05-26 04:48:44 +08:00
# http://www.apache.org/licenses/LICENSE-2.0
#
2017-05-26 04:48:44 +08:00
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import random
import copy
2017-05-26 04:48:44 +08:00
class Context:
tok = 0
inLoop = False
indent = 0
2017-05-26 04:48:44 +08:00
def __init__(self):
self.random = random.Random()
2017-05-26 04:48:44 +08:00
def uniqueID(self):
return self.random.randint(100000, 999999)
2017-05-26 04:48:44 +08:00
class InfiniteLoop (Exception):
pass
2017-05-26 04:48:44 +08:00
class ExecContext:
iterationsLeft = 1000
ifstate = 0
2017-05-26 04:48:44 +08:00
def __init__(self, inputSeq):
self.input = iter(inputSeq)
self.output = []
def inp(self):
return next(self.input)
def out(self, x):
self.output.append(x)
2017-05-26 04:48:44 +08:00
def infinityCheck(self):
self.iterationsLeft -= 1
if self.iterationsLeft <= 0:
raise InfiniteLoop()
2017-05-26 04:48:44 +08:00
OK = 1
BREAK = 2
THROW = 3
RETURN = 4
CONTINUE = 5
2017-05-26 04:48:44 +08:00
def indent(cx):
return "\t" * cx.indent
2017-05-26 04:48:44 +08:00
class F (object):
def unreachable(self):
return False
2017-05-26 04:48:44 +08:00
def containsbreak(self):
return False
2017-05-26 04:48:44 +08:00
class hashF (F):
def __init__(self, cx):
self.cx = cx
self.uniqueID = cx.uniqueID()
2017-05-26 04:48:44 +08:00
def __str__(self):
return indent(self.cx) + "outputStream.send( %d );\n" % self.uniqueID
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
ecx.infinityCheck()
ecx.out(self.uniqueID)
2017-05-26 04:48:44 +08:00
return OK
2017-05-26 04:48:44 +08:00
class compoundF(F):
def __init__(self, cx, children):
self.cx = cx
self.children = []
for c in children:
self.children.append(c)
if c.unreachable():
self.unreachable = lambda: 1
break
2017-05-26 04:48:44 +08:00
def __str__(self):
return "".join(str(c) for c in self.children)
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
for c in self.children:
ecx.infinityCheck()
result = c.eval(ecx)
2017-05-26 04:48:44 +08:00
if result != OK:
break
return result
2017-05-26 04:48:44 +08:00
def containsbreak(self):
return any(c.containsbreak() for c in self.children)
2017-05-26 04:48:44 +08:00
class loopF (F):
def __init__(self, cx):
self.cx = cx
ccx = copy.copy(cx)
ccx.indent += 1
ccx.inLoop = True
self.body = compoundF(ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)])
2017-05-26 04:48:44 +08:00
self.uniqueID = cx.uniqueID()
self.forever = cx.random.random() < 0.1
def __str__(self):
if self.forever:
return (indent(self.cx) + "loop {\n" +
str(self.body) +
2017-05-26 04:48:44 +08:00
indent(self.cx) + "}\n")
else:
return (indent(self.cx) + "state int i%d; for(i%d = 0; i%d < 5; i%d++) {\n" % ((self.uniqueID,) * 4) +
str(self.body) +
2017-05-26 04:48:44 +08:00
indent(self.cx) + "}\n")
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
if self.forever:
while True:
ecx.infinityCheck()
result = self.body.eval(ecx)
2017-05-26 04:48:44 +08:00
if result == BREAK:
break
elif result not in (OK, CONTINUE):
2017-05-26 04:48:44 +08:00
return result
else:
for i in range(5):
ecx.infinityCheck()
result = self.body.eval(ecx)
2017-05-26 04:48:44 +08:00
if result == BREAK:
break
elif result not in (OK, CONTINUE):
2017-05-26 04:48:44 +08:00
return result
return OK
2017-05-26 04:48:44 +08:00
def unreachable(self):
return self.forever and not self.body.containsbreak()
2017-05-26 04:48:44 +08:00
class rangeForF (F):
def __init__(self, cx):
self.cx = cx
ccx = copy.copy(cx)
ccx.indent += 1
ccx.inLoop = True
self.body = compoundF(ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)])
2017-05-26 04:48:44 +08:00
self.uniqueID = cx.uniqueID()
def __str__(self):
return (
indent(self.cx) +
("\n" + indent(self.cx)).join([
2017-05-26 04:48:44 +08:00
"state std::vector<int> V;",
"V.push_back(1);",
"V.push_back(2);",
"V.push_back(3);",
"for( auto i : V ) {\n",
]).replace("V", "list%d" % self.uniqueID) +
2019-04-10 02:16:45 +08:00
indent(self.cx) + "\t(void)i;\n" + # Suppress -Wunused-variable warning in generated code
2017-05-26 04:48:44 +08:00
str(self.body) +
indent(self.cx) + "}\n")
def eval(self, ecx):
for i in range(1, 4):
2017-05-26 04:48:44 +08:00
ecx.infinityCheck()
result = self.body.eval(ecx)
2017-05-26 04:48:44 +08:00
if result == BREAK:
break
elif result not in (OK, CONTINUE):
2017-05-26 04:48:44 +08:00
return result
return OK
def unreachable(self):
return False
2017-05-26 04:48:44 +08:00
class ifF (F):
def __init__(self, cx):
self.cx = cx
ccx = copy.copy(cx)
ccx.indent += 1
self.toggle = cx.random.randint(0, 1)
self.ifbody = compoundF(ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)])
2017-05-26 04:48:44 +08:00
if cx.random.random() < 0.5:
ccx = copy.copy(cx)
ccx.indent += 1
self.elsebody = compoundF(ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)])
2017-05-26 04:48:44 +08:00
else:
self.elsebody = None
2017-05-26 04:48:44 +08:00
def __str__(self):
s = (indent(self.cx) + "if ( (++ifstate&1) == %d ) {\n" % self.toggle +
str(self.ifbody))
2017-05-26 04:48:44 +08:00
if self.elsebody:
s += (indent(self.cx) + "} else {\n" +
str(self.elsebody))
2017-05-26 04:48:44 +08:00
s += indent(self.cx) + "}\n"
return s
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
ecx.infinityCheck()
ecx.ifstate = ecx.ifstate + 1
if (ecx.ifstate & 1) == self.toggle:
2017-05-26 04:48:44 +08:00
return self.ifbody.eval(ecx)
elif self.elsebody:
return self.elsebody.eval(ecx)
else:
return OK
2017-05-26 04:48:44 +08:00
def unreachable(self):
return self.elsebody and self.ifbody.unreachable() and self.elsebody.unreachable()
2017-05-26 04:48:44 +08:00
def containsbreak(self):
return self.ifbody.containsbreak() or (self.elsebody and self.elsebody.containsbreak())
2017-05-26 04:48:44 +08:00
class tryF (F):
def __init__(self, cx):
self.cx = cx
ccx = copy.copy(cx)
ccx.indent += 1
self.body = compoundF(ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)])
2017-05-26 04:48:44 +08:00
ccx = copy.copy(cx)
ccx.indent += 1
self.catch = compoundF(ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)])
2017-05-26 04:48:44 +08:00
def __str__(self):
return (indent(self.cx) + "try {\n" +
str(self.body) +
2017-05-26 04:48:44 +08:00
indent(self.cx) + "} catch (...) {\n" +
str(self.catch) +
2017-05-26 04:48:44 +08:00
indent(self.cx) + "}\n"
)
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
ecx.infinityCheck()
result = self.body.eval(ecx)
if result != THROW:
return result
2017-05-26 04:48:44 +08:00
return self.catch.eval(ecx)
2017-05-26 04:48:44 +08:00
def unreachable(self):
return self.body.unreachable() and self.catch.unreachable()
2017-05-26 04:48:44 +08:00
def containsbreak(self):
return self.body.containsbreak() or self.catch.containsbreak()
def doubleF(cx):
return compoundF(cx, [fuzzCode(cx)(cx)] + [hashF(cx)] + [fuzzCode(cx)(cx)])
2017-05-26 04:48:44 +08:00
class breakF(F):
def __init__(self, cx):
self.cx = cx
2017-05-26 04:48:44 +08:00
def __str__(self):
return indent(self.cx) + "break;\n"
2017-05-26 04:48:44 +08:00
def unreachable(self):
return True
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
ecx.infinityCheck()
return BREAK
2017-05-26 04:48:44 +08:00
def containsbreak(self):
return True
2017-05-26 04:48:44 +08:00
class continueF(F):
def __init__(self, cx):
self.cx = cx
2017-05-26 04:48:44 +08:00
def __str__(self):
return indent(self.cx) + "continue;\n"
2017-05-26 04:48:44 +08:00
def unreachable(self):
return True
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
ecx.infinityCheck()
return CONTINUE
class waitF(F):
2017-05-26 04:48:44 +08:00
def __init__(self, cx):
self.cx = cx
self.uniqueID = cx.uniqueID()
2017-05-26 04:48:44 +08:00
def __str__(self):
return (
indent(self.cx) + "int input = waitNext( inputStream );\n" +
indent(self.cx) + "outputStream.send( input + %d );\n" % self.uniqueID
)
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
ecx.infinityCheck()
input = ecx.inp()
ecx.out((input + self.uniqueID) & 0xffffffff)
2017-05-26 04:48:44 +08:00
return OK
class throwF(F):
2017-05-26 04:48:44 +08:00
def __init__(self, cx):
self.cx = cx
2017-05-26 04:48:44 +08:00
def __str__(self):
return indent(self.cx) + "throw operation_failed();\n"
2017-05-26 04:48:44 +08:00
def unreachable(self):
return True
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
ecx.infinityCheck()
return THROW
class throwF2(throwF):
2017-05-26 04:48:44 +08:00
def __str__(self):
return indent(self.cx) + "throw_operation_failed();\n"
2017-05-26 04:48:44 +08:00
def unreachable(self):
return False # The actor compiler doesn't know the function never returns
class throwF3(throwF):
2017-05-26 04:48:44 +08:00
def __str__(self):
return indent(self.cx) + "wait( error ); // throw operation_failed()\n"
2017-05-26 04:48:44 +08:00
def unreachable(self):
return False # The actor compiler doesn't know that 'error' always contains an error
class returnF(F):
2017-05-26 04:48:44 +08:00
def __init__(self, cx):
self.cx = cx
self.uniqueID = cx.uniqueID()
2017-05-26 04:48:44 +08:00
def __str__(self):
return indent(self.cx) + "return %d;\n" % self.uniqueID
2017-05-26 04:48:44 +08:00
def unreachable(self):
return True
2017-05-26 04:48:44 +08:00
def eval(self, ecx):
ecx.infinityCheck()
ecx.returnValue = self.uniqueID
return RETURN
2017-05-26 04:48:44 +08:00
def fuzzCode(cx):
choices = [loopF, rangeForF, tryF, doubleF, ifF]
if (cx.indent < 2):
choices = choices * 2
2017-05-26 04:48:44 +08:00
choices += [waitF, returnF]
if (cx.inLoop):
choices += [breakF, continueF]
choices = choices * 3 + [throwF, throwF2, throwF3]
2017-05-26 04:48:44 +08:00
return cx.random.choice(choices)
2017-05-26 04:48:44 +08:00
def randomActor(index):
while 1:
cx = Context()
cx.indent += 1
actor = fuzzCode(cx)(cx)
actor = compoundF(cx, [actor, returnF(cx)]) # Add a return at the end if the end is reachable
2017-05-26 04:48:44 +08:00
name = "actorFuzz%d" % index
text = ("ACTOR Future<int> %s( FutureStream<int> inputStream, PromiseStream<int> outputStream, Future<Void> error ) {\n" % name
+ "\tstate int ifstate = 0;\n"
+ str(actor)
+ "}")
ecx = actor.ecx = ExecContext((i + 1) * 1000 for i in range(1000000))
2017-05-26 04:48:44 +08:00
try:
result = actor.eval(ecx)
except InfiniteLoop:
print("Infinite loop for actor %s" % name)
2017-05-26 04:48:44 +08:00
continue
if result == RETURN:
ecx.out(ecx.returnValue)
2017-05-26 04:48:44 +08:00
elif result == THROW:
ecx.out(1000)
2017-05-26 04:48:44 +08:00
else:
print(text)
raise Exception("Invalid eval result: " + str(result))
2017-05-26 04:48:44 +08:00
actor.name = name
actor.text = text
return actor
2019-04-10 02:16:45 +08:00
header='''
/*
* ActorFuzz.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'''
2017-05-26 04:48:44 +08:00
testCaseCount = 30
outputFile = open("ActorFuzz.actor.cpp", "wt")
2019-04-10 02:16:45 +08:00
print(header, file=outputFile)
2017-05-26 04:48:44 +08:00
print('// THIS FILE WAS GENERATED BY actorFuzz.py; DO NOT MODIFY IT DIRECTLY\n', file=outputFile)
print('#include "ActorFuzz.h"\n', file=outputFile)
print('#ifndef WIN32\n', file=outputFile)
actors = [randomActor(i) for i in range(testCaseCount)]
for actor in actors:
print(actor.text + "\n", file=outputFile)
print("std::pair<int,int> actorFuzzTests() {\n\tint testsOK = 0;", file=outputFile)
for actor in actors:
2020-05-23 00:25:32 +08:00
print('\ttestsOK += testFuzzActor( &%s, "%s", {%s} );' % (actor.name, actor.name, ','.join(str(e) for e in actor.ecx.output)),
2017-05-26 04:48:44 +08:00
file=outputFile)
print("\treturn std::make_pair(testsOK, %d);\n}" % len(actors), file=outputFile)
print('#endif // WIN32\n', file=outputFile)
outputFile.close()