#!/usr/bin/env python # coding: utf-8 # # Evolutionary Grammar Fuzzing # # In this chapter, we introduce how to implement [search-based test generation](SearchBasedFuzzing.ipynb) on grammars, using _genetic improvement_ operators such as mutation and cross-over on derivation trees. # **Prerequisites** # # * You should have read the [chapter on search-based test generation](SearchBasedFuzzer.ipynb). # * You should have read the [chapter on recombining inputs](LangFuzzer.ipynb). # # In[1]: import fuzzingbook_utils # In[2]: import SearchBasedFuzzer import LangFuzzer # ## Grammar-Based Mutation # # General idea: Take a derivation tree and a matching grammar; apply a random mutation. # In[3]: from Grammars import EXPR_GRAMMAR from GrammarFuzzer import display_tree from Parser import EarleyParser # In[4]: parser = EarleyParser(EXPR_GRAMMAR) tree, *_ = parser.parse("1 + 2 * 3") display_tree(tree) # 1. Pick any node in the tree # 2. Produce a new expansion. # # We have seen this for `LangFuzzer` already, right? # # How about we factor this out (from the Parser notebook), and have two notebook on mutational (and genetic fuzzing): # # 1. `LangFuzzer` – a chapter on # * Mutating trees (randomly) # * Mutating trees from a given population (the LangFuzz approach) # * Tree recombination (and crossover) # # 2. `EvoGrammarFuzzer` – a chapter on # * Genetic improvement (using coverage only) # * Genetic improvement (using a fitness function from search-based fuzzing) # In[5]: def mutate_tree(tree, grammar): pass # ## Grammar-Based Crossover # ## Lessons Learned # # * _Lesson one_ # * _Lesson two_ # * _Lesson three_ # ## Next Steps # # _Link to subsequent chapters (notebooks) here, as in:_ # # * [use _mutations_ on existing inputs to get more valid inputs](MutationFuzzer.ipynb) # * [use _grammars_ (i.e., a specification of the input format) to get even more valid inputs](Grammars.ipynb) # * [reduce _failing inputs_ for efficient debugging](Reducer.ipynb) # # ## Background # # _Cite relevant works in the literature and put them into context, as in:_ # # The idea of ensuring that each expansion in the grammar is used at least once goes back to Burkhardt \cite{Burkhardt1967}, to be later rediscovered by Paul Purdom \cite{Purdom1972}. # ## Exercises # # _Close the chapter with a few exercises such that people have things to do. To make the solutions hidden (to be revealed by the user), have them start with_ # # ```markdown # **Solution.** # ``` # # _Your solution can then extend up to the next title (i.e., any markdown cell starting with `#`)._ # # _Running `make metadata` will automatically add metadata to the cells such that the cells will be hidden by default, and can be uncovered by the user. The button will be introduced above the solution._ # ### Exercise 1: _Title_ # # _Text of the exercise_ # In[6]: # Some code that is part of the exercise pass # _Some more text for the exercise_ # **Solution.** _Some text for the solution_ # In[7]: # Some code for the solution 2 + 2 # _Some more text for the solution_ # ### Exercise 2: _Title_ # # _Text of the exercise_ # **Solution.** _Solution for the exercise_