本文へ移動
Contact

TECHFUND
Media

Hi AUDIT
2024/9/10

Solidity Fuzzing with AI-Guided Genetic Algorithms:  

Insights from my talk at ETHTokyo 2024

Piyush Gururani, CTO, TECHFUND Inc.

Introduction

While Solidity provides the backbone for many blockchain applications, the applications written in it are not immune to bugs and vulnerabilities, some of which can lead to significant financial losses or security breaches.

To mitigate these risks, developers use fuzzing—a dynamic testing technique that involves feeding a program with random inputs to uncover potential vulnerabilities.

However it often struggles with issues like limited code coverage and inefficiency in detecting complex bugs. 

To address these challenges, I introduced an innovative approach at ETHTokyo 2024 that integrates Large Language Models (LLMs) with Genetic Algorithms (GAs) to enhance the effectiveness of Solidity fuzzers. This blog post delves into the details of this novel approach, exploring how it can revolutionize smart contract security.

Understanding Solidity Fuzzing

Fuzzing is a well-established technique in software testing, used to identify security vulnerabilities and bugs by providing random or semi-random data inputs to a program. In the context of Solidity, fuzzers work by executing smart contracts with various inputs to explore different execution paths. The goal is to identify abnormal behaviors, such as assertion failures, out-of-gas errors, or re-entrancy vulnerabilities, which could indicate a security flaw.

Traditional Fuzzing Techniques

Symbolic Execution and Z3 Solver

One of the primary methods used in Solidity fuzzing is symbolic execution, where the fuzzer treats inputs as symbolic variables rather than concrete values. By doing so, it explores all possible execution paths of a smart contract. The symbolic execution process involves:

  1. Compiling Contract Code to Bytecode: The Solidity code is compiled into Ethereum Virtual Machine (EVM) bytecode, which the fuzzer then analyzes.

  2. Constructing a Control Flow Graph (CFG): A CFG is generated from the bytecode, representing the different paths that can be taken during the execution of the contract. Each node in the CFG corresponds to a basic block of code, and the edges represent possible jumps between these blocks.

  3. Path Exploration: The fuzzer systematically explores each path in the CFG, generating mathematical constraints for each one. These constraints are then solved using a tool like the Z3 solver to determine if a particular path is feasible or impossible. If a path is deemed impossible, it is excluded from further exploration.

Challenges with Symbolic Execution

While symbolic execution is powerful, it comes with significant drawbacks:

  • Computational Expense: Exploring every possible path can be extremely time-consuming, especially for complex contracts. In some cases, the time taken to find a feasible solution can become practically infinite.

  • Limited Code Coverage: Due to the time constraints, not all paths can be explored, leading to reduced code coverage and potentially missed bugs.

  • Complex Bugs: Despite its thoroughness, symbolic execution may struggle to uncover deeply embedded logical bugs that require a more nuanced approach.

Introducing Genetic Algorithms (GAs) in Fuzzing

To overcome the limitations of symbolic execution, Genetic Algorithms (GAs) can be used as an alternative approach to fuzzing. GAs are inspired by the principles of natural evolution, where solutions evolve over successive iterations to optimize a particular outcome—in this case, identifying bugs in smart contracts.

How GAs Work

  • Initial Population Generation: The process begins by generating a random population of potential solutions (test cases). These test cases are then evaluated based on a fitness function that measures how effectively they explore different execution paths or trigger specific behaviors in the contract.

  • Selection: Individuals (test cases) are selected based on their fitness scores. The higher the score, the more likely a test case is to be selected for the next generation. This selection process mimics the survival of the fittest in natural evolution.

  • Crossover: Selected test cases undergo crossover, where parts of two or more test cases are combined to create new offspring. This process helps introduce variation and potentially more effective test cases.

  • Mutation: Occasionally, random mutations are applied to the test cases to introduce new variations. This helps the fuzzer avoid getting stuck in local optima and encourages exploration of different parts of the code.

Iterative Improvement: The process of selection, crossover, and mutation continues over multiple generations, with each new generation theoretically being better at finding bugs than the last.

Challenges with GAs in Fuzzing

GA was first used in Solidity Fuzzing by https://arxiv.org/pdf/2005.12156

While GAs offer a more dynamic and efficient approach compared to symbolic execution, they are not without their challenges:

  • Random Initial Population: The initial generation of test cases is typically random, which may result in many irrelevant or unproductive test cases in start.

  • Premature Convergence: There is a risk that the GA may converge too quickly on a suboptimal solution, missing deeper, more complex bugs.

  • Static Crossover and Mutation Rates: Traditional GAs use fixed crossover and mutation rates, which may not be ideal for all situations, leading to inefficiencies.

LLM-Guided Enhancements: A New Era for Solidity Fuzzing

Recognising the limitations of both symbolic execution and traditional GAs, I presented a novel approach that integrates LLMs with GAs to significantly improve the fuzzing process. This approach introduces three key enhancements:

  • Smart Initial Population Generation via LLMs

    Traditionally, the initial population of test cases in a GA is generated randomly or based on simple heuristics. This can lead to inefficient exploration of the contract’s code. My approach uses LLMs to analyse the smart contract code and generate an initial population of test cases that are more likely to trigger meaningful behaviours.

    Increased Relevance: By leveraging LLMs, the initial test cases are more focused on critical functions or areas of the contract that are more likely to contain vulnerabilities.

    Enhanced Code Coverage: This targeted approach increases the likelihood of exploring deeper, more complex paths within the contract, thereby improving overall code coverage from the outset.

  • Adaptive Crossover Strategies with GPT Evaluation

    In traditional GAs, the crossover probability is typically set to a high static value (e.g., 0.9). However, setting up a static value can lead to issues such as premature convergence or extended search times.

    Dynamic Adjustment: My method proposes reducing the crossover probability to 0.5, thereby encouraging a more balanced exploration of the input space.

    GPT-Enhanced Pairing: Using GPT models, the crossover process becomes more intelligent. GPT evaluates the potential of test case pairs based on their relevance and fitness. If a pair is deemed promising, the crossover probability is increased, allowing for a more focused exploration of valuable combinations.

    Balancing Exploration and Exploitation: This adaptive approach reduces the risk of the GA converging too early on a suboptimal solution, while also avoiding excessive search times.

    The calculate interestingness function uses LLM to calculate a value based on outputs from slither and surya ( a path is generated and fed to LLM to score, if two parents are potentially interesting pair in an exploit )

  1. Enhanced Mutation Pools with LLM-Guided Values

    Mutation is a critical aspect of GAs, introducing diversity into the population and preventing stagnation. However, traditional mutation strategies may not always introduce beneficial variations.

    LLM-Guided Mutation Pools: Instead of relying on random mutations, my approach uses LLMs to populate mutation pools with “interesting” values. These values are selected based on real-time feedback from the contract’s execution state and previous generations’ outcomes.

    Improved Constraint Solving: By using LLMs to guide the mutation process, the fuzzer is more likely to generate test cases that solve constraints that Z3 or other solvers might struggle with, leading to the discovery of paths that would otherwise remain unexplored.

Results and Implications

The integration of LLMs with GA-based fuzzing represents a significant advancement in smart contract security testing. Early results from the application of these methods in tools like Confuzzius demonstrate:

  • Higher Code Coverage: By generating more relevant initial test cases and intelligently guiding the crossover and mutation processes, the fuzzer explores a broader range of execution paths.

  • Faster Bug Discovery: The targeted approach reduces the number of generations needed to find critical bugs, speeding up the overall testing process.

  • Detection of Complex Bugs: The LLM-enhanced strategies are more effective at uncovering deep, logical bugs that traditional methods might miss.

  • We saw an increase of about 12% in code coverage of smaller contracts, we still need to test it on larger contracts to see the effectiveness.

The future of Solidity fuzzing is poised for transformation, thanks to the innovative use of AI-guided Genetic Algorithms. By integrating LLMs into the fuzzing process, developers can achieve higher code coverage, faster bug discovery, and improved detection of complex vulnerabilities.

My work at ETHTokyo 2024 highlights the potential of combining AI with evolutionary algorithms, setting a new standard for smart contract security testing. As these methods become more widely adopted, we can expect to see a significant reduction in the number of vulnerabilities that slip through the cracks, making the blockchain space safer for developers and users alike.

Hi AUDIT

Hi AUDIT is a high speed, high quality, best price security audit service, providing automated audit SaaS and consulting for web3 startups.
View More