Giter Club home page Giter Club logo

pruner's Introduction

The Gauntlet Pruner

The Gauntlet pruner is a tool designed to reduce the size of P4 programs that cause crashes or abnormal behavior in P4 compilers. This tool is intended to be used in combination with the Gauntlet tool suite. The pruner currently supports the pruning of programs with crash (exit code 1) or semantic translation bugs (exit code 20, as defined here).

Usage

Note: A sample configuration file is provided here.

Configuration file Format

The pruner can automatically load some basic configuration parameters from a JSON configuration file. The file has the following fields:

 # Was the validation bug found when undefined behavior was allowed?
"allow_undef": false,
 # What was the compiler used to compile the P4 program?
"compiler": "gauntlet/modules/p4c/build/p4test",
 # The exit code of the program
"exit_code": 1,
 # the input P4 program
"input_file": "program_with_validation_bug.p4",
 # the target directory of the binary that compiled the program
"out_dir": "gauntlet/random/validation_bugs",
# the binary to the P4-to-Z3 interpreter
"p4z3_bin": "",
# if we encountered a validation bug, this describes the incorrect program
"prog_after": "",
# the program that was still correct, before a miscompilation occurred
"prog_before": "",
# the path to the validation tool that found the bug
"validation_bin": "",
# the error message that was recorded when the binary exited
"err_string": ""

Pruning with a Configuration File

If a configuration file is provided only the P4 program is necessary:

p4pruner --config [P4_CONFIG] [P4_PROG]

Pruning without a Configuration File

If no configuration file is present, the pruner requires the path to the validation binary that was used, as well as the compiler that was used to translate the P4 program:

p4pruner --compiler-bin [PATH_TO_COMPILER_BIN] --validation-bin [PATH_TO_VALIDATION_BIN] [P4_PROG] --bug-type [VALIDATION/CRASH]

For crash bugs, the validation binary can be omitted:

p4pruner --compiler-bin [PATH_TO_COMPILER_BIN] [P4_PROG] --bug-type [VALIDATION/CRASH]

The Pruning Stages

The pruning passes build on top of each other. The current order of execution is as follows:

Statement pruning // randomly remove statements
        V
Expression pruning // randomly remove expressions
        V
Boolean expression pruning // simplify boolean expressions
        V
Generic passes // reuse initialization passes from P4C
        V
Replace variables // replace variables with constants
        V
Extended remove unused declarations // remove any unused declarations

The following passes to prune a P4 program are currently implemented:

Statement Pruning

Required Preceding Passes

  • None

Parameters

  • SIZE_BANK_RATIO : The ratio between the initial size of the bank of statements and the size of the program.
  • PRUNE_ITERS : The number of times the statement pruner would run through the program.
  • NO_CHNG_ITERS : If the program remains the same for this number of iterations then we assume the phase is completed and move to the next pass.
  • AIDM_INCREASE : The additive increasing factor for the bank of statements.
  • AIDM_DECREASE : The multiplicative decreasing factor for the bank of statements.

Description

This pass tries to prune statements from the program. A certain number of statements are chosen at random by a Collector which is a subclass of an Inspector. Then these are fed to a Pruner which is a subclass of a Transform, which actually prunes the statements from the program tree. We start with a very large number of statements to prune, and then follow an additive increase/multiplicative decrease (AIMD) scheme. If we were successful in our attempt, i.e. the exit code of the program remained the same and the bug still exists in the program, we increase the bank by 2 statements otherwise we half the size of the bank.

Expression Pruning

Required Preceding Passes

  • None

Parameters

  • PRUNE_ITERS : The number of times the expressions pruner would run through the program.
  • NO_CHNG_ITERS : If the program remains the same for this number of iterations then we assume the phase is completed and move to the next pass.

Description

This pass operators similarly as the statement pruner but prunes the expressions in the statements that remain. We explicitly define how the pass would handle and prune different types of expressions. For example, given an addition operator, we try to randomly pick a side and see if the bug remains. In other cases, for example shifts, we just pick the value that is to be shifted. Only few expressions are implemented so far, for example Range or a Slice are still missing.

Boolean Pruning

Required Preceding Passes

  • None

Parameters

  • PRUNE_ITERS : The number of times the boolean pruner would run through the program.
  • NO_CHNG_ITERS : If the program remains the same for this number of iterations then we assume the phase is completed and move to the next pass.

Description

This pass now tries to prune and simplify boolean expressions. Given a boolean expression we try to replace it with a random boolean literal and see if the bug remains.

Compiler Pruning

The passes after this stage require some generic passes from P4C. We execute these passes to retrieve the reference map and infer all types in the program. Note, that because type inference requires constant folding, we may not always be able to apply these passes.

ResolveReferences,
ConstantFolding,
InstantiateDirectCalls,
TypeInference

Replace Variables

Required Preceding Passes

  • Generic Passes once
  • ResolveReferences and TypeInference every pass

Parameters

  • None

Description

This pass tries to replace each variable with a literal and checks if the bug remained. For example, it might turn the expressions aVar + bVar to 16w0 + 10w0 depending upon the bit width of the variables. We do this to aid the subsequent pass (i.e Extended unused declarations) which tries to remove all unused declarations from the program. This pass frees up more expressions and declarations, that can then be removed by the next pass.

Extended Remove Unused Declarations

Required Preceding Passes

  • Generic Passes once
  • Replace variables

Parameters

  • None

Description

This is a subclass of P4::RemoveUnusedDeclarations where we try to aggressively remove all unused declarations as opposed to the conservative approach of P4C. We do not care about maintaining the functionality of the program only about the bug that exists in it.

=====

Testing

To reproduce pruning of old bugs, we maintain a folder of binaries with bugs in the tests folder.

We use check_prog.p4 for testing. The program takes a P4 file along with a P4C binary ( and optionally a validation binary) as input, prunes the provided program with a fixed seed, and then compares the output to a reference file. If the output and reference file match, the test passes.

Note that the provided P4 program must have a reference present in the references directory.

Usage

check_prog.py --pruner_path [PATH_TO_PRUNER_BIN] --compiler [PATH_TO_COMPILER_BIN] --validation [PATH_TO_VALIDATION_BIN] --p4prog [P4_PROG] --type [V/C]

Also, there is a folder called p4c_bins which will house various versions of the P4C compiler each named after the commit hash of the time it was compiled.

pruner's People

Contributors

fruffy avatar robotrobo avatar

Stargazers

Anagh Kanungo avatar

Watchers

James Cloos avatar Tao Wang avatar  avatar

pruner's Issues

Dynamic statement count

The starting size of the bank of statements that we choose to prune should be a function of the size of the program.

Add Github workflow that builds the pruner extension

We should add a Github workflow that builds the pruner extension on every commit and pull request to ensure that the latest version can compiler.
This is a good practice to get familiar with continuous integration. However, this has low priority for now.

Add better diagnostics

After we have applied a pass we should be able to distinguish whether the program could not compiled, had a validation error, a compiler bug, or still the same error class.
This requires that we actually record the output of the binary we call

Fix compiler passes

The compiler passes from p4c are too complex and we can not simply apply them. However, some of these passes are still very useful. For example, RemoveUnusedDeclarations. We should figure out how to use them.

Remove declarations that are not used

Our pruner currently does a decent job of removing expressions and statements but leaves declarations even though they may not be used. We should remove those, including function, control, and header declarations.

We can try to apply a compiler pass or try to roll our own pass that uses the ReferenceMap.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.