8000 Add FuzzyDuck fuzzer - and move fuzzer CI to separate repo by Mytherin · Pull Request #7712 · duckdb/duckdb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add FuzzyDuck fuzzer - and move fuzzer CI to separate repo #7712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 0 additions & 97 deletions .github/workflows/cifuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,100 +45,3 @@ jobs:
name: artifacts
path: ./out/artifacts

sqlsmith-fuzzing:
name: SQLSmith Fuzzing
if: github.repository == 'duckdb/duckdb'
runs-on: ubuntu-latest
env:
BUILD_SQLSMITH: 1
BUILD_TPCH: 1
BUILD_TPCDS: 1
BUILD_PARQUET: 1
BUILD_JEMALLOC: 1
GEN: ninja
FUZZEROFDUCKSKEY: ${{ secrets.FUZZEROFDUCKSKEY }}

steps:
- name: Dependencies
shell: bash
run: sudo apt-get update -y -qq && sudo apt-get install -y -qq ninja-build ccache

- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Build
shell: bash
run: |
make debug

- name: Fuzz
shell: bash
run: |
python3 scripts/run_fuzzer.py --sqlsmith --alltypes --shell=build/debug/duckdb

sqlancer-fuzzing:
name: SQLancer Fuzzing
if: github.repository == 'duckdb/duckdb'
runs-on: ubuntu-latest
env:
BUILD_SQLSMITH: 1
BUILD_JDBC: 1
BUILD_JEMALLOC: 1
GEN: ninja
FUZZEROFDUCKSKEY: ${{ secrets.FUZZEROFDUCKSKEY }}

steps:
- name: Dependencies
shell: bash
run: sudo apt-get update -y -qq && sudo apt-get install -y -qq ninja-build ccache

- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Build SQLancer
shell: bash
run: |
git clone https://github.com/Mytherin/sqlancer
cd sqlancer
mvn package -q -DskipTests
cd ..

- name: Setup Ccache
uses: hendrikmuhs/ccache-action@main
with:
key: ${{ github.job }}
save: ${{ github.ref == 'refs/heads/master' || github.repository != 'duckdb/duckdb' }}

- name: Build JDBC Driver
shell: bash
run: |
mkdir -p build/jdbcdebug
cd build/jdbcdebug
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DJDBC_DRIVER=1 -DBUILD_UNITTESTS=FALSE -DENABLE_SANITIZER=FALSE ../..
cmake --build .
cd ../..
cp build/jdbcdebug/tools/jdbc/duckdb_jdbc.jar ./sqlancer/target/lib/duckdb_jdbc-*.jar

- name: Build Shell
shell: bash
run: |
make debug

- name: Build Python Client
shell: bash
run: |
pip install requests numpy
cd tools/pythonpkg
python3 setup.py install --user
cd ../..

- name: Fuzz
shell: bash
run: |
python3 scripts/run_sqlancer.py --sqlancer=`pwd`/sqlancer --shell=build/debug/duckdb
5 changes: 3 additions & 2 deletions extension/sqlsmith/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ include_directories(include)
include_directories(third_party/sqlsmith/include)
add_subdirectory(third_party)

set(SQLSMITH_SOURCES sqlsmith-extension.cpp statement_simplifier.cpp
${SQLSMITH_OBJECT_FILES})
set(SQLSMITH_SOURCES
sqlsmith-extension.cpp statement_generator.cpp statement_simplifier.cpp
fuzzyduck.cpp ${SQLSMITH_OBJECT_FILES})

add_library(sqlsmith_extension STATIC ${SQLSMITH_SOURCES})
set(PARAMETERS "-warnings")
Expand Down
112 changes: 112 additions & 0 deletions extension/sqlsmith/fuzzyduck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "fuzzyduck.hpp"
#include "duckdb/common/random_engine.hpp"
#include "statement_generator.hpp"

namespace duckdb {

FuzzyDuck::FuzzyDuck(ClientContext &context) : context(context) {
}

FuzzyDuck::~FuzzyDuck() {
}

void FuzzyDuck::Fuzz() {
auto &random_engine = RandomEngine::Get(context);
if (seed == 0) {
seed = random_engine.NextRandomInteger();
}
if (max_queries == 0) {
max_queries = NumericLimits<idx_t>::Maximum();
}
if (!complete_log.empty()) {
auto &fs = FileSystem::GetFileSystem(context);
TryRemoveFile(complete_log);
complete_log_handle =
fs.OpenFile(complete_log, FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE_NEW);
}
for (idx_t i = 0; i < max_queries; i++) {
LogMessage("Query " + to_string(i) + "\n");
auto query = GenerateQuery();
RunQuery(std::move(query));
}
if (complete_log_handle) {
complete_log_handle->Close();
}
}

string FuzzyDuck::GenerateQuery() {
LogTask("Generating query with seed " + to_string(seed));
auto &engine = RandomEngine::Get(context);
// set the seed
engine.SetSeed(seed);
// get the next seed
seed = engine.NextRandomInteger();

// generate the statement
StatementGenerator generator(context);
auto statement = generator.GenerateStatement();
return statement->ToString();
}

void FuzzyDuck::RunQuery(string query) {
LogQuery(query + ";");

Connection con(*context.db);
auto result = con.Query(query);
if (result->HasError()) {
LogMessage("EXECUTION ERROR: " + result->GetError());
} else {
LogMessage("EXECUTION SUCCESS!");
}
}

void FuzzyDuck::TryRemoveFile(const string &path) {
auto &fs = FileSystem::GetFileSystem(context);
if (fs.FileExists(path)) {
fs.RemoveFile(path);
}
}

void FuzzyDuck::LogMessage(const string &message) {
if (!verbose_output) {
return;
}
Printer::Print(message);
}

void FuzzyDuck::LogTask(const string &message) {
if (verbose_output) {
LogMessage(message + "\n");
}
LogToCurrent(message);
}

void FuzzyDuck::LogQuery(const string &message) {
if (verbose_output) {
LogMessage(message + "\n");
}
LogToCurrent(message);
LogToComplete(message);
}

void FuzzyDuck::LogToCurrent(const string &message) {
if (log.empty()) {
return;
}
auto &fs = FileSystem::GetFileSystem(context);
TryRemoveFile(log);
auto file = fs.OpenFile(log, FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE_NEW);
file->Write((void *)message.c_str(), message.size());
file->Sync();
file->Close();
}
void FuzzyDuck::LogToComplete(const string &message) {
if (!complete_log_handle) {
return;
}
complete_log_handle->Write((void *)message.c_str(), message.size());
complete_log_handle->Write((void *)"\n", 1);
complete_log_handle->Sync();
}

} // namespace duckdb
49 changes: 49 additions & 0 deletions extension/sqlsmith/include/fuzzyduck.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// fuzzyduck.hpp
//
//
//===----------------------------------------------------------------------===//

#pragma once

#include "duckdb.hpp"
#include "duckdb/parser/query_node.hpp"

namespace duckdb {
struct FileHandle;

class FuzzyDuck {
public:
FuzzyDuck(ClientContext &context);
~FuzzyDuck();

ClientContext &context;
uint32_t seed = 0;
idx_t max_queries = 0;
string complete_log;
string log;
bool verbose_output = false;

public:
void Fuzz();

private:
string GenerateQuery();
void RunQuery(string query);

void LogMessage(const string &message);
void LogTask(const string &message);
void LogQuery(const string &message);

void LogToCurrent(const string &message);
void LogToComplete(const string &message);

void TryRemoveFile(const string &path);

private:
unique_ptr<FileHandle> complete_log_handle;
};

} // namespace duckdb
Loading
0