Skip to content
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

Add fuzzer harness for ECSql fuzzer (do not merge into master) #968

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
48 changes: 48 additions & 0 deletions iModelCore/iModelPlatform/iModelConsole/iModelConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* See LICENSE.md in the repository root for full copyright notice.
*--------------------------------------------------------------------------------------------*/
#include "iModelConsole.h"
#include <fstream>
#include <Bentley/BeTimeUtilities.h>

USING_NAMESPACE_BENTLEY_SQLITE
Expand Down Expand Up @@ -246,6 +247,53 @@ int IModelConsole::Run(int argc, WCharCP argv[])
return WaitForUserInput();
}

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
int IModelConsole::ExecuteSampleQuery(char *sample_bytes)
{
//Initialize iModelConsole and print out banner
Setup();

// Helper lambda to run commands
auto runCommand = [this](const char* commandName, const char* args) {
IModelConsole::WriteLine(Utf8PrintfString("Executing: %s \n", commandName).c_str());
Command const* command = GetCommand(commandName);
BeAssert(command != nullptr);
command->Run(m_session, args);
};

// Helper function to remove files
auto removeFiles = [](const std::string& baseFilePath) {
std::remove((baseFilePath + "-journal").c_str());
std::remove((baseFilePath + "-wal").c_str());
std::remove((baseFilePath + "-shm").c_str());
std::remove(baseFilePath.c_str());
};

std::string filePath = "D:\\test.bim";

// 1. Close the bim file
runCommand(".close", filePath.c_str());

// 2. Delete the bim file if it exists
removeFiles(filePath);

// 3. Create a new bim file
runCommand(".create", ("ecdb " + filePath).c_str());

// 4. Run the sample command
runCommand(".parse", sample_bytes);

// 5. Close the bim file
runCommand(".close", filePath.c_str());

// 6. Delete the bim file
removeFiles(filePath);

return 0;
}

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions iModelCore/iModelPlatform/iModelConsole/iModelConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ struct IModelConsole final : Dgn::PlatformLib::Host

int Run(int argc, WCharCP argv[]);

int ExecuteSampleQuery(char *sample_bytes);

static size_t FindNextToken(Utf8String& token, WStringCR inputString, size_t startIndex, WChar delimiter, WChar delimiterEscapeChar = L'\0');

static void Write(Utf8CP format, ...);
Expand Down
53 changes: 52 additions & 1 deletion iModelCore/iModelPlatform/iModelConsole/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,31 @@
#include "iModelConsole.h"
#include <Bentley/Logging.h>

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>

#ifdef COMMENT_OUT_UNUSED_VARIABLE
static WCharCP s_configFileName = L"logging.config.xml";
#endif

USING_NAMESPACE_BENTLEY
USING_NAMESPACE_BENTLEY_SQLITE_EC

// Function declaration
void fuzz(char *name);

std::string convertWCharToString(WCharCP wstr) {
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &strTo[0], size_needed, NULL, NULL);
return strTo;
}

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
Expand All @@ -35,6 +53,34 @@ void InitLogging(BeFileNameCR exeDir)
{
}

// actual target function
void fuzz(char *name) {
std::cout << "Inside Fuzz function" << std::endl;
char *sample_bytes = NULL;
uint32_t sample_size = 0;

// read the sample either from file
FILE *fp = NULL;
if (fopen_s(&fp, name, "rb") != 0 || !fp) {
printf("Error opening %s\n", name);
return;
}
fseek(fp, 0, SEEK_END);
sample_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
sample_bytes = (char *)malloc(sample_size);
fread(sample_bytes, 1, sample_size, fp);
// Process the SQL input
printf("Sample Data: %s\n", sample_bytes);

IModelConsole::Singleton().ExecuteSampleQuery(sample_bytes);

fclose(fp);

if(sample_bytes)
free(sample_bytes);
}

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -71,7 +117,12 @@ int wmain(int argc, WCharCP argv[])
BeAssertFunctions::SetBeAssertHandler(IModelConsoleBeAssertHandler);

Dgn::PlatformLib::Initialize(IModelConsole::Singleton());
return IModelConsole::Singleton().Run(argc, argv);

std::string inputFileName = convertWCharToString(argv[1]);
fuzz(const_cast<char*>(inputFileName.c_str()));

return 0;
// return IModelConsole::Singleton().ExecuteSampleQuery(argc, argv);
}

#ifdef __unix__
Expand Down