|
75 | 75 | "metadata": {}, |
76 | 76 | "outputs": [], |
77 | 77 | "source": [ |
78 | | - "from typing import List, Tuple, Dict, Any" |
| 78 | + "from typing import List, Tuple, Dict, Any, Optional" |
79 | 79 | ] |
80 | 80 | }, |
81 | 81 | { |
|
153 | 153 | "metadata": {}, |
154 | 154 | "outputs": [], |
155 | 155 | "source": [ |
156 | | - "from fuzzingbook.Grammars import Grammar" |
| 156 | + " from fuzzingbook.Grammars import Grammar, EXPR_GRAMMAR, reachable_nonterminals, is_valid_grammar, START_SYMBOL\n", |
| 157 | + " from fuzzingbook.GrammarFuzzer import GrammarFuzzer, expansion_to_children, DerivationTree, tree_to_string, display_tree, is_nonterminal\n", |
| 158 | + " from fuzzingbook.Parser import EarleyParser" |
157 | 159 | ] |
158 | 160 | }, |
159 | 161 | { |
|
180 | 182 | "\n", |
181 | 183 | " \"<digit>\":\n", |
182 | 184 | " [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"]\n", |
183 | | - "}\n", |
184 | | - "START_SYMBOL = \"<start>\"" |
| 185 | + "}" |
185 | 186 | ] |
186 | 187 | }, |
187 | 188 | { |
|
248 | 249 | "metadata": {}, |
249 | 250 | "outputs": [], |
250 | 251 | "source": [ |
251 | | - "from enum import Enum\n", |
252 | | - "\n", |
| 252 | + "from enum import Enum" |
| 253 | + ] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "code", |
| 257 | + "execution_count": null, |
| 258 | + "metadata": {}, |
| 259 | + "outputs": [], |
| 260 | + "source": [ |
253 | 261 | "class OracleResult(Enum):\n", |
254 | 262 | " BUG = \"BUG\"\n", |
255 | 263 | " NO_BUG = \"NO_BUG\"\n", |
|
617 | 625 | "metadata": {}, |
618 | 626 | "outputs": [], |
619 | 627 | "source": [ |
620 | | - "from fuzzingbook.GrammarFuzzer import expansion_to_children, DerivationTree\n", |
621 | | - "\n", |
622 | 628 | "class ExistenceFeature(Feature):\n", |
623 | 629 | " '''\n", |
624 | 630 | " This class represents existence features of a grammar. Existence features indicate\n", |
|
680 | 686 | "metadata": {}, |
681 | 687 | "outputs": [], |
682 | 688 | "source": [ |
683 | | - "from fuzzingbook.GrammarFuzzer import tree_to_string\n", |
684 | 689 | "from numpy import nanmax, isnan\n", |
685 | 690 | "\n", |
686 | 691 | "class NumericInterpretation(Feature):\n", |
|
762 | 767 | "metadata": {}, |
763 | 768 | "outputs": [], |
764 | 769 | "source": [ |
765 | | - "from fuzzingbook.Grammars import reachable_nonterminals\n", |
766 | 770 | "from collections import defaultdict\n", |
767 | 771 | "import re\n", |
768 | 772 | "\n", |
|
855 | 859 | "metadata": {}, |
856 | 860 | "outputs": [], |
857 | 861 | "source": [ |
858 | | - "from fuzzingbook.Parser import EarleyParser\n", |
859 | | - "from fuzzingbook.Grammars import Grammar\n", |
860 | | - "import pandas\n", |
861 | 862 | "from pandas import DataFrame\n", |
862 | 863 | "\n", |
863 | 864 | "def collect_features(sample_list: List[str],\n", |
|
905 | 906 | "outputs": [], |
906 | 907 | "source": [ |
907 | 908 | "# TODO: handle multiple trees\n", |
908 | | - "from fuzzingbook.Parser import EarleyParser\n", |
909 | | - "\n", |
910 | 909 | "def compute_feature_values(sample: str, grammar: Grammar, features: List[Feature]) -> Dict[str, float]:\n", |
911 | 910 | " '''\n", |
912 | 911 | " Extracts all feature values from an input.\n", |
|
952 | 951 | "metadata": {}, |
953 | 952 | "outputs": [], |
954 | 953 | "source": [ |
955 | | - "from fuzzingbook.Grammars import EXPR_GRAMMAR\n", |
956 | | - "\n", |
957 | | - "from fuzzingbook.GrammarFuzzer import GrammarFuzzer\n", |
958 | | - "from fuzzingbook.GrammarFuzzer import tree_to_string, display_tree\n", |
959 | 954 | "import random\n", |
960 | 955 | "\n", |
961 | 956 | "# For this example, fix the random seed so that the produced output is deterministic\n", |
|
1023 | 1018 | "metadata": {}, |
1024 | 1019 | "outputs": [], |
1025 | 1020 | "source": [ |
1026 | | - "from fuzzingbook.Grammars import is_nonterminal\n", |
1027 | | - "from fuzzingbook.GrammarFuzzer import tree_to_string\n", |
1028 | | - "\n", |
1029 | 1021 | "# Then, recursively iterate through the derivation tree and for each non-terminal,\n", |
1030 | 1022 | "# add the derived word to the grammar\n", |
1031 | 1023 | "\n", |
|
1050 | 1042 | "metadata": {}, |
1051 | 1043 | "outputs": [], |
1052 | 1044 | "source": [ |
1053 | | - "from fuzzingbook.GrammarFuzzer import GrammarFuzzer\n", |
1054 | | - "from fuzzingbook.GrammarFuzzer import display_tree, tree_to_string\n", |
1055 | | - "from fuzzingbook.Grammars import EXPR_GRAMMAR, Grammar\n", |
1056 | | - "\n", |
1057 | | - "import random\n", |
1058 | 1045 | "import copy\n", |
1059 | 1046 | "\n", |
1060 | | - "from fuzzingbook.Parser import EarleyParser\n", |
1061 | | - "from fuzzingbook.GrammarFuzzer import display_tree, tree_to_string\n", |
1062 | | - "\n", |
1063 | 1047 | "START_SYMBOL = \"<start>\"\n", |
1064 | 1048 | "\n", |
1065 | 1049 | "def transform_grammar(sample: str,\n", |
|
1115 | 1099 | "metadata": {}, |
1116 | 1100 | "outputs": [], |
1117 | 1101 | "source": [ |
| 1102 | + "import sklearn\n", |
1118 | 1103 | "from sklearn.tree import DecisionTreeClassifier\n", |
1119 | | - "from sklearn.feature_extraction import DictVectorizer\n", |
1120 | | - "\n", |
1121 | | - "import graphviz" |
| 1104 | + "from sklearn.feature_extraction import DictVectorizer" |
1122 | 1105 | ] |
1123 | 1106 | }, |
1124 | 1107 | { |
|
1223 | 1206 | "metadata": {}, |
1224 | 1207 | "outputs": [], |
1225 | 1208 | "source": [ |
1226 | | - "import graphviz\n", |
1227 | | - "import sklearn\n", |
1228 | | - "\n", |
| 1209 | + "import graphviz" |
| 1210 | + ] |
| 1211 | + }, |
| 1212 | + { |
| 1213 | + "cell_type": "code", |
| 1214 | + "execution_count": null, |
| 1215 | + "metadata": {}, |
| 1216 | + "outputs": [], |
| 1217 | + "source": [ |
1229 | 1218 | "def show_decision_tree(clf, feature_names):\n", |
1230 | 1219 | " dot_data = sklearn.tree.export_graphviz(clf, out_file=None, \n", |
1231 | 1220 | " feature_names=feature_names,\n", |
|
1351 | 1340 | "metadata": {}, |
1352 | 1341 | "outputs": [], |
1353 | 1342 | "source": [ |
1354 | | - "from sklearn.feature_extraction import DictVectorizer\n", |
1355 | | - "import pandas\n", |
1356 | | - "\n", |
1357 | 1343 | "# Features for each input, one dict per input\n", |
1358 | 1344 | "features = [\n", |
1359 | 1345 | " {'function-sqrt': 1, 'function-cos': 0, 'function-sin': 0, 'number': -900},\n", |
|
1387 | 1373 | "feature_names = ['function-sqrt', 'function-cos', 'function-sin', 'number']\n", |
1388 | 1374 | "X_data = pandas.DataFrame.from_records(features)\n", |
1389 | 1375 | "\n", |
1390 | | - "from sklearn.tree import DecisionTreeClassifier\n", |
1391 | | - "from sklearn import tree\n", |
1392 | | - "\n", |
1393 | 1376 | "# Fix the random state to produce a deterministic result (for illustration purposes only)\n", |
1394 | 1377 | "clf = DecisionTreeClassifier(random_state=10)\n", |
1395 | 1378 | "\n", |
|
1399 | 1382 | "# Train with Pandas Dataframe\n", |
1400 | 1383 | "clf = clf.fit(X_data, oracle)\n", |
1401 | 1384 | "\n", |
1402 | | - "import graphviz\n", |
1403 | 1385 | "dot_data = sklearn.tree.export_graphviz(clf, out_file=None, \n", |
1404 | 1386 | " feature_names=feature_names,\n", |
1405 | 1387 | " class_names=[\"BUG\", \"NO BUG\"], \n", |
|
1688 | 1670 | "source": [ |
1689 | 1671 | "import logging\n", |
1690 | 1672 | "from pathlib import Path\n", |
1691 | | - "\n", |
1692 | 1673 | "import numpy as np\n", |
1693 | | - "from typing import List, Optional\n", |
1694 | 1674 | "\n", |
1695 | 1675 | "def tree_to_paths(tree, features: List[Feature]):\n", |
1696 | 1676 | " logging.info(\"Extracting requirements from tree ...\")\n", |
|
1894 | 1874 | "metadata": {}, |
1895 | 1875 | "outputs": [], |
1896 | 1876 | "source": [ |
1897 | | - "import pandas\n", |
1898 | 1877 | "x = pandas.DataFrame.from_records(features)\n", |
1899 | 1878 | "bounds = pandas.DataFrame([{'feature': c, 'min': x[c].min(), 'max': x[c].max()}\n", |
1900 | 1879 | " for c in feature_names],\n", |
|
1955 | 1934 | "metadata": {}, |
1956 | 1935 | "outputs": [], |
1957 | 1936 | "source": [ |
1958 | | - "import pandas\n", |
1959 | | - "\n", |
1960 | 1937 | "def extracting_prediction_paths(clf, feature_names, data):\n", |
1961 | 1938 | " \n", |
1962 | 1939 | " # determine the bounds\n", |
|
2032 | 2009 | "outputs": [], |
2033 | 2010 | "source": [ |
2034 | 2011 | "import string\n", |
2035 | | - "from fuzzingbook.Grammars import Grammar, is_valid_grammar\n", |
2036 | | - "START_SYMBOL = \"<start>\"\n", |
2037 | 2012 | "\n", |
2038 | 2013 | "SPECIFICATION: Grammar = {\n", |
2039 | 2014 | " \"<start>\":\n", |
2040 | 2015 | " [\"<req_list>\"],\n", |
2041 | | - " \n", |
| 2016 | + "\n", |
2042 | 2017 | " \"<req_list>\": \n", |
2043 | 2018 | " [\"<req>\", \"<req>\"\", \"\"<req_list>\"],\n", |
2044 | 2019 | "\n", |
2045 | 2020 | " \"<req>\":\n", |
2046 | 2021 | " [\"<feature>\"\" \"\"<quant>\"\" \"\"<num>\"],\n", |
2047 | | - " \n", |
| 2022 | + "\n", |
2048 | 2023 | " \"<feature>\": [\"exists(<string>)\",\n", |
2049 | 2024 | " \"num(<string>)\",\n", |
2050 | 2025 | " # currently not used\n", |
2051 | 2026 | " \"char(<string>)\",\n", |
2052 | 2027 | " \"length(<string>)\"], \n", |
2053 | | - " \n", |
| 2028 | + "\n", |
2054 | 2029 | " \"<quant>\":\n", |
2055 | 2030 | " [\"<\", \">\", \"<=\", \">=\"],\n", |
2056 | | - " \n", |
| 2031 | + "\n", |
2057 | 2032 | " \"<num>\": [\"-<value>\", \"<value>\"],\n", |
2058 | | - " \n", |
| 2033 | + "\n", |
2059 | 2034 | " \"<value>\":\n", |
2060 | 2035 | " [\"<integer>.<integer>\",\n", |
2061 | 2036 | " \"<integer>\"],\n", |
|
2065 | 2040 | "\n", |
2066 | 2041 | " \"<digit>\":\n", |
2067 | 2042 | " [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"],\n", |
2068 | | - " \n", |
| 2043 | + "\n", |
2069 | 2044 | " '<string>': ['<letters>'],\n", |
2070 | 2045 | " '<letters>': ['<letter><letters>', '<letter>'],\n", |
2071 | 2046 | " '<letter>': list(string.ascii_letters + string.digits + string.punctuation)\n", |
2072 | 2047 | "}\n", |
2073 | | - " \n", |
| 2048 | + "\n", |
2074 | 2049 | "assert is_valid_grammar(SPECIFICATION, START_SYMBOL) == True" |
2075 | 2050 | ] |
2076 | 2051 | }, |
|
2094 | 2069 | "metadata": {}, |
2095 | 2070 | "outputs": [], |
2096 | 2071 | "source": [ |
2097 | | - "from fuzzingbook.GrammarFuzzer import GrammarFuzzer\n", |
2098 | | - "from fuzzingbook.Grammars import EXPR_GRAMMAR, Expansion\n", |
2099 | | - "from fuzzingbook.Parser import EarleyParser, tree_to_string\n", |
2100 | | - "\n", |
2101 | 2072 | "g = GrammarFuzzer(SPECIFICATION, START_SYMBOL ,max_nonterminals= 100)\n", |
2102 | 2073 | "earley = EarleyParser(SPECIFICATION)\n", |
2103 | 2074 | "for i in range(10):\n", |
|
2163 | 2134 | "metadata": {}, |
2164 | 2135 | "outputs": [], |
2165 | 2136 | "source": [ |
2166 | | - "from typing import List\n", |
2167 | | - "from fuzzingbook.GrammarFuzzer import DerivationTree\n", |
2168 | | - "\n", |
2169 | 2137 | "class SpecRequirement:\n", |
2170 | 2138 | " '''\n", |
2171 | 2139 | " This class represents a requirement for a new input sample that should be generated.\n", |
|
2383 | 2351 | "from typing import List\n", |
2384 | 2352 | "from itertools import chain\n", |
2385 | 2353 | "\n", |
2386 | | - "from fuzzingbook.Parser import EarleyParser\n", |
2387 | | - "from fuzzingbook.GrammarFuzzer import DerivationTree, all_terminals, Grammar, tree_to_string\n", |
2388 | | - "from fuzzingbook.Grammars import Grammar, nonterminals, opts, is_valid_grammar\n", |
2389 | | - "from fuzzingbook.Grammars import reachable_nonterminals, unreachable_nonterminals\n", |
2390 | | - "\n", |
2391 | 2354 | "\n", |
2392 | 2355 | "def best_trees(forest, spec):\n", |
2393 | 2356 | " samples = [tree_to_string(tree) for tree in forest]\n", |
|
2531 | 2494 | "metadata": {}, |
2532 | 2495 | "outputs": [], |
2533 | 2496 | "source": [ |
2534 | | - "from fuzzingbook.GrammarFuzzer import GrammarFuzzer\n", |
2535 | | - "\n", |
2536 | 2497 | "def generate_samples_random(grammar, new_input_specifications, num):\n", |
2537 | 2498 | " f = GrammarFuzzer(grammar ,max_nonterminals=50, log=False)\n", |
2538 | 2499 | " data = []\n", |
|
2584 | 2545 | "metadata": {}, |
2585 | 2546 | "outputs": [], |
2586 | 2547 | "source": [ |
2587 | | - "from typing import List\n", |
2588 | | - "import pandas\n", |
2589 | | - "\n", |
2590 | 2548 | "GENERATOR_TIMEOUT = 10 # timeout in seconds\n", |
2591 | 2549 | "\n", |
2592 | 2550 | "class Alhazen:\n", |
|
0 commit comments