Skip to content

Commit ddaa43c

Browse files
authored
Code clean up and improvement. (s3team#28)
* Add clang tidy * Fix cmake command * Change comment to todo * Convert more todo * remove useless files * Create CODEOWNERS * Fix memory leak. * Suppress warning from third parties. * Create lint.yml * Update lint.yml * Update README.md * Seperate database in Cmake * Done * Fix github actions * Remove useless files * Fix clang-format * Avoid duplicated lint * Update README.md * Update README.md
1 parent c0c45db commit ddaa43c

32 files changed

+431
-1444
lines changed

.clang-format

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ ReflowComments: true
171171
RemoveBracesLLVM: false
172172
SeparateDefinitionBlocks: Leave
173173
ShortNamespaceLines: 1
174-
SortIncludes: CaseSensitive
174+
#SortIncludes: CaseSensitive
175175
SortJavaStaticImport: Before
176176
SortUsingDeclarations: true
177177
SpaceAfterCStyleCast: false

.clang-tidy

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
Checks: 'clang-diagnostic-*,clang-analyzer-*,performance-*,-performance-no-int-to-ptr'
3+
WarningsAsErrors: '*'
4+
HeaderFilterRegex: ''
5+
AnalyzeTemporaryDtors: false
6+
FormatStyle: file
7+
CheckOptions:
8+
- key: llvm-else-after-return.WarnOnConditionVariables
9+
value: 'false'
10+
- key: modernize-loop-convert.MinConfidence
11+
value: reasonable
12+
- key: modernize-replace-auto-ptr.IncludeStyle
13+
value: llvm
14+
- key: cert-str34-c.DiagnoseSignedUnsignedCharComparisons
15+
value: 'false'
16+
- key: google-readability-namespace-comments.ShortNamespaceLines
17+
value: '10'
18+
- key: cert-err33-c.CheckedFunctions
19+
value: '::aligned_alloc;::asctime_s;::at_quick_exit;::atexit;::bsearch;::bsearch_s;::btowc;::c16rtomb;::c32rtomb;::calloc;::clock;::cnd_broadcast;::cnd_init;::cnd_signal;::cnd_timedwait;::cnd_wait;::ctime_s;::fclose;::fflush;::fgetc;::fgetpos;::fgets;::fgetwc;::fopen;::fopen_s;::fprintf;::fprintf_s;::fputc;::fputs;::fputwc;::fputws;::fread;::freopen;::freopen_s;::fscanf;::fscanf_s;::fseek;::fsetpos;::ftell;::fwprintf;::fwprintf_s;::fwrite;::fwscanf;::fwscanf_s;::getc;::getchar;::getenv;::getenv_s;::gets_s;::getwc;::getwchar;::gmtime;::gmtime_s;::localtime;::localtime_s;::malloc;::mbrtoc16;::mbrtoc32;::mbsrtowcs;::mbsrtowcs_s;::mbstowcs;::mbstowcs_s;::memchr;::mktime;::mtx_init;::mtx_lock;::mtx_timedlock;::mtx_trylock;::mtx_unlock;::printf_s;::putc;::putwc;::raise;::realloc;::remove;::rename;::scanf;::scanf_s;::setlocale;::setvbuf;::signal;::snprintf;::snprintf_s;::sprintf;::sprintf_s;::sscanf;::sscanf_s;::strchr;::strerror_s;::strftime;::strpbrk;::strrchr;::strstr;::strtod;::strtof;::strtoimax;::strtok;::strtok_s;::strtol;::strtold;::strtoll;::strtoul;::strtoull;::strtoumax;::strxfrm;::swprintf;::swprintf_s;::swscanf;::swscanf_s;::thrd_create;::thrd_detach;::thrd_join;::thrd_sleep;::time;::timespec_get;::tmpfile;::tmpfile_s;::tmpnam;::tmpnam_s;::tss_create;::tss_get;::tss_set;::ungetc;::ungetwc;::vfprintf;::vfprintf_s;::vfscanf;::vfscanf_s;::vfwprintf;::vfwprintf_s;::vfwscanf;::vfwscanf_s;::vprintf_s;::vscanf;::vscanf_s;::vsnprintf;::vsnprintf_s;::vsprintf;::vsprintf_s;::vsscanf;::vsscanf_s;::vswprintf;::vswprintf_s;::vswscanf;::vswscanf_s;::vwprintf_s;::vwscanf;::vwscanf_s;::wcrtomb;::wcschr;::wcsftime;::wcspbrk;::wcsrchr;::wcsrtombs;::wcsrtombs_s;::wcsstr;::wcstod;::wcstof;::wcstoimax;::wcstok;::wcstok_s;::wcstol;::wcstold;::wcstoll;::wcstombs;::wcstombs_s;::wcstoul;::wcstoull;::wcstoumax;::wcsxfrm;::wctob;::wctrans;::wctype;::wmemchr;::wprintf_s;::wscanf;::wscanf_s;'
20+
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
21+
value: 'false'
22+
- key: cert-dcl16-c.NewSuffixes
23+
value: 'L;LL;LU;LLU'
24+
- key: google-readability-braces-around-statements.ShortStatementLines
25+
value: '1'
26+
- key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
27+
value: 'true'
28+
- key: google-readability-namespace-comments.SpacesBeforeComments
29+
value: '2'
30+
- key: modernize-loop-convert.MaxCopySize
31+
value: '16'
32+
- key: modernize-pass-by-value.IncludeStyle
33+
value: llvm
34+
- key: modernize-use-nullptr.NullMacros
35+
value: 'NULL'
36+
- key: llvm-qualified-auto.AddConstToQualified
37+
value: 'false'
38+
- key: modernize-loop-convert.NamingStyle
39+
value: CamelCase
40+
- key: llvm-else-after-return.WarnOnUnfixable
41+
value: 'false'
42+
- key: google-readability-function-size.StatementThreshold
43+
value: '800'
44+
...
45+

.github/workflows/build.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ jobs:
2323
- name: update
2424
run: sudo apt-get update && sudo apt-get upgrade -y
2525
- name: install packages
26-
run: sudo apt-get install -y -m -f --install-suggests build-essential git libtool libtool-bin automake bison libglib2.0-0 clang llvm-dev libc++-dev findutils libcmocka-dev python3-dev python3-setuptools ninja-build
26+
run: sudo apt-get install -y -m -f --install-suggests build-essential git libtool libtool-bin automake bison libglib2.0-0 clang llvm-dev libc++-dev findutils libcmocka-dev python3-dev python3-setuptools ninja-build clang-tidy clang-format libyaml-cpp-dev
2727
- name: Run cmake
28-
run: cmake -S . -B build
28+
run: cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
2929
- name: Build
3030
run: cmake --build build -j
31+
- name: Clang format
32+
run: cmake --build build --target formatcheck
33+
- name: Clang tidy
34+
run: cmake --build build --target lint
35+
- name: Tests
36+
run: cd build/tests && ctest

.github/workflows/lint.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
#################################
3+
#################################
4+
## Super Linter GitHub Actions ##
5+
#################################
6+
#################################
7+
name: Lint Code Base
8+
9+
#############################
10+
# Start the job on all push #
11+
#############################
12+
on:
13+
push:
14+
branches-ignore: [master, main]
15+
# Remove the line above to run when pushing to master
16+
#pull_request:
17+
#branches: [master, main]
18+
19+
###############
20+
# Set the Job #
21+
###############
22+
jobs:
23+
lint:
24+
# Name the Job
25+
name: Lint Code Base
26+
# Set the agent to run on
27+
runs-on: ubuntu-latest
28+
29+
##################
30+
# Load all steps #
31+
##################
32+
steps:
33+
##########################
34+
# Checkout the code base #
35+
##########################
36+
- name: Checkout Code
37+
uses: actions/checkout@v3
38+
with:
39+
# Full git history is needed to get a proper
40+
# list of changed files within `super-linter`
41+
fetch-depth: 0
42+
43+
################################
44+
# Run Linter against code base #
45+
################################
46+
- name: Lint Code Base
47+
uses: github/super-linter@v4
48+
env:
49+
VALIDATE_ALL_CODEBASE: false
50+
DEFAULT_BRANCH: master
51+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52+
FILTER_REGEX_EXCLUDE: .*data/fuzz_root.*

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build
22
output
3-
.cache
3+
.cache
4+
Testing

CMakeLists.txt

+85-53
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,80 @@ project(Squirrel)
33

44
set(BUILD_SHARED_LIBS ON)
55

6-
# GoogleTest requires at least C++ 14
76
set(CMAKE_CXX_STANDARD 17)
87

98
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O2 -fsanitize=address")
109
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3")
1110
set(LINK_FLAGS "${LINK_FLAGS} -fsanitize=address")
12-
1311
find_package(PkgConfig)
14-
pkg_check_modules(MySQL REQUIRED mysqlclient>=5.7)
1512

16-
find_package(PostgreSQL REQUIRED)
13+
option(ALL "Build all types of database" ON)
14+
option(SQLITE "Build sqlite" OFF)
15+
option(MYSQL "Build mysql" OFF)
16+
option(POSTGRESQL "Build postgresql" OFF)
17+
18+
if(SQLITE
19+
OR MYSQL
20+
OR POSTGRESQL)
21+
set(ALL OFF)
22+
endif()
23+
24+
if(ALL)
25+
set(SQLITE ON)
26+
set(MYSQL ON)
27+
set(POSTGRESQL ON)
28+
endif()
29+
30+
if(SQLITE)
31+
list(APPEND DBMS sqlite)
32+
endif()
33+
34+
if(MYSQL)
35+
list(APPEND DBMS mysql)
36+
pkg_check_modules(MySQL REQUIRED mysqlclient>=5.7)
37+
add_library(mysql_client OBJECT srcs/internal/client/client_mysql.cc)
38+
target_include_directories(mysql_client PUBLIC ${MySQL_INCLUDE_DIRS}
39+
srcs/internal/client)
40+
target_link_libraries(mysql_client PUBLIC ${MySQL_LIBRARIES}
41+
${YAML_CPP_LIBRARIES})
42+
target_compile_options(mysql_client PRIVATE -fPIC)
43+
list(APPEND LINK_CLIENT mysql_client)
44+
list(APPEND CLIENT_DEFINITION __SQUIRREL_MYSQL__)
45+
endif()
46+
47+
if(POSTGRESQL)
48+
list(APPEND DBMS postgresql)
49+
find_package(PostgreSQL REQUIRED)
50+
add_library(postgresql_client OBJECT
51+
srcs/internal/client/client_postgresql.cc)
52+
target_include_directories(postgresql_client PUBLIC ${PostgreSQL_INCLUDE_DIRS}
53+
srcs/internal/client)
54+
target_link_libraries(
55+
postgresql_client PUBLIC ${PostgreSQL_LIBRARIES} ${YAML_CPP_LIBRARIES}
56+
absl::strings absl::str_format)
57+
target_compile_options(postgresql_client PRIVATE -fPIC)
58+
list(APPEND LINK_CLIENT postgresql_client)
59+
list(APPEND CLIENT_DEFINITION __SQUIRREL_POSTGRESQL__)
60+
endif()
1761

1862
include(FetchContent)
19-
FetchContent_Declare(
20-
googletest
21-
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
22-
)
23-
# For Windows : Prevent overriding the parent project's compiler/linker settings
24-
set(gtest_force_shared_crt
25-
ON
26-
CACHE BOOL "" FORCE)
27-
FetchContent_MakeAvailable(googletest)
28-
29-
FetchContent_Declare(
30-
yaml-cpp
31-
URL https://github.com/jbeder/yaml-cpp/archive/refs/tags/yaml-cpp-0.7.0.zip)
32-
FetchContent_MakeAvailable(yaml-cpp)
63+
find_package(yaml-cpp REQUIRED)
64+
# FetchContent_Declare( ${YAML_CPP_LIBRARIES} URL
65+
# https://github.com/jbeder/${YAML_CPP_LIBRARIES}/archive/refs/tags/${YAML_CPP_LIBRARIES}-0.7.0.zip)
66+
# FetchContent_MakeAvailable(${YAML_CPP_LIBRARIES})
67+
include_directories(${YAML_INCLUDE_DIRS})
68+
69+
# set_target_properties(${YAML_CPP_LIBRARIES} PROPERTIES COMPILE_FLAGS "-w")
3370

71+
set(ABSL_PROPAGATE_CXX_STD ON)
72+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")
3473
add_subdirectory(third_party/abseil-cpp)
35-
enable_testing()
74+
string(REPLACE " -w" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
3675

3776
set(AFLPP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/AFLplusplus/include)
3877

3978
include_directories(${AFLPP_DIR})
4079

41-
list(APPEND DBMS sqlite mysql postgresql)
42-
4380
foreach(dbms IN LISTS DBMS)
4481
add_library(
4582
${dbms}_impl OBJECT
@@ -53,43 +90,38 @@ foreach(dbms IN LISTS DBMS)
5390
target_include_directories(${dbms}_impl PRIVATE srcs/internal/${dbms}/include
5491
srcs)
5592
target_compile_options(${dbms}_impl PRIVATE -fPIC)
56-
target_link_libraries(${dbms}_impl yaml-cpp)
93+
target_link_libraries(${dbms}_impl ${YAML_CPP_LIBRARIES} absl::strings
94+
absl::str_format)
5795

5896
string(TOUPPER ${dbms} UPPER_CASE_DBMS)
5997
add_library(${dbms}_mutator SHARED srcs/custom_mutator.cc srcs/db_factory.cc)
60-
target_link_libraries(${dbms}_mutator ${dbms}_impl)
98+
target_link_libraries(${dbms}_mutator ${dbms}_impl config_validator)
6199
target_include_directories(${dbms}_mutator PRIVATE srcs/internal/${dbms} srcs)
62-
#target_compile_options(${dbms}_mutator PRIVATE -fPIC)
63-
target_compile_definitions(${dbms}_mutator PRIVATE __SQUIRREL_${UPPER_CASE_DBMS}__)
100+
# target_compile_options(${dbms}_mutator PRIVATE -fPIC)
101+
target_compile_definitions(${dbms}_mutator
102+
PRIVATE __SQUIRREL_${UPPER_CASE_DBMS}__)
64103
endforeach()
65104

66-
add_executable(db_driver srcs/db_driver.cc)
67-
target_link_libraries(db_driver yaml-cpp all_client absl::strings
68-
absl::str_format)
69-
70-
add_executable(test_client srcs/internal/client/test_client.cc)
71-
target_link_libraries(test_client all_client)
72-
target_include_directories(test_client PUBLIC srcs/internal/client)
73-
74-
add_library(all_client SHARED srcs/internal/client/client.cc)
75-
target_include_directories(all_client PUBLIC srcs/internal/client)
76-
target_link_libraries(all_client PUBLIC mysql_client postgresql_client)
77-
78-
# MySQL client
79-
add_library(mysql_client OBJECT srcs/internal/client/client_mysql.cc)
80-
target_include_directories(mysql_client PUBLIC ${MySQL_INCLUDE_DIRS}
81-
srcs/internal/client)
82-
target_link_libraries(mysql_client PUBLIC ${MySQL_LIBRARIES} yaml-cpp)
83-
target_compile_options(mysql_client PRIVATE -fPIC)
84-
85-
add_library(postgresql_client OBJECT srcs/internal/client/client_postgresql.cc)
86-
target_include_directories(postgresql_client PUBLIC ${PostgreSQL_INCLUDE_DIRS}
87-
srcs/internal/client)
88-
target_link_libraries(postgresql_client PUBLIC ${PostgreSQL_LIBRARIES} yaml-cpp
89-
absl::strings absl::str_format)
90-
target_compile_options(postgresql_client PRIVATE -fPIC)
105+
if(MYSQL OR POSTGRESQL)
106+
add_executable(db_driver srcs/db_driver.cc)
107+
target_link_libraries(db_driver ${YAML_CPP_LIBRARIES} all_client
108+
absl::strings absl::str_format)
91109

92-
include(GoogleTest)
93-
# gtest_discover_tests(variable_check_test)
110+
add_executable(test_client srcs/internal/client/test_client.cc)
111+
target_link_libraries(test_client all_client ${YAML_CPP_LIBRARIES})
112+
target_include_directories(test_client PUBLIC srcs/internal/client)
113+
114+
add_library(all_client SHARED srcs/internal/client/client.cc)
115+
target_include_directories(all_client PUBLIC srcs/internal/client)
116+
target_link_libraries(all_client PUBLIC ${LINK_CLIENT})
117+
target_compile_definitions(all_client PRIVATE ${CLIENT_DEFINITION})
118+
endif()
119+
120+
add_library(config_validator OBJECT srcs/utils/config_validate.cc)
121+
target_link_libraries(config_validator PRIVATE ${YAML_CPP_LIBRARIES}
122+
absl::strings absl::str_format)
123+
target_include_directories(config_validator PUBLIC srcs/utils)
124+
target_compile_options(config_validator PRIVATE -fPIC)
94125

95-
include(clang-format.cmake)
126+
include(lint.cmake)
127+
add_subdirectory(tests)

CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @Changochen @zr950624

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717

1818
For ubuntu 22.04:
1919
```
20-
sudo apt install libmysqlclient-dev cmake ninja-build clang pkg-config clang-format libpq-dev
20+
sudo apt install libmysqlclient-dev cmake ninja-build clang pkg-config clang-format libpq-dev libyaml-cpp-dev
2121
```
2222

2323
### Build Squirrel
2424
1. Clone this repo and run `git submodule update --init`.
25-
2. `cmake -S . -B build -DCMAKE_BUILD_TYPE=Release`.
25+
2. `cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -Wno-dev`. If you want to compile only the mutator for the specific databases, add `-DXXXXX=ON`, `XXXXX` can be `SQLITE`, `MYSQL` and `POSTGRESQL`. `Mariadb` share the same interface with `MySQL`.
2626
3. `cmake --build build -j`, the binaries are in `build/`.
2727

2828

2929
### Build AFLplusplus and DBMSs
30-
1. Build aflplusplus.
30+
1. Build aflplusplus: `cd AFLplusplus && make -j && cd ..`.
3131
2. Use `afl-cc` and `afl-c++` to instrument your database.
3232

3333

@@ -72,3 +72,7 @@ SQUIRREL: Testing Database Management Systems with Language Validity and Coverag
7272
address = {Orlando, USA},
7373
}
7474
```
75+
76+
## Special Thanks
77+
1. Roel Van de Paar ([@mariadb-RoelVandePaar](https://github.com/mariadb-RoelVandePaar)): For his helpful feedback for improving Squirrel.
78+

data/config_mysql.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
required: ["init_lib", "data_lib", "db", "passwd", "user_name", "host", "sock_path", "db_prefix"]
22
init_lib: ./data/fuzz_root/mysql_init_lib
33
data_lib: ./data/fuzz_root/global_data_lib_mysql
44
db: mysql

data/config_pqsql.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
---
1+
required: ["init_lib", "data_lib", "db", "passwd", "user_name", "host"]
22
init_lib: ./data/fuzz_root/pqsql_init_lib
33
data_lib: ./data/fuzz_root/pqsql_data_lib
44
db: postgresql
55
passwd: ''
66
user_name: yongheng
77
host: localhost
88
port: 5432
9-
sock_path: /tmp/mysql.sock
10-
db_prefix: test

data/config_sqlite.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
required: ["init_lib", "pragma"]
22
init_lib: /home/yongheng/Squirrel/squirrel_srcs/SQLite/fuzz_root/init_lib
33
pragma: /home/yongheng/Squirrel/squirrel_srcs/SQLite/fuzz_root/pragma
44
db: sqlite

clang-format.cmake lint.cmake

+16-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ file(GLOB_RECURSE ALL_SOURCE_FILES srcs/*.cc srcs/*.cpp srcs/*.h)
77
# ${PROJECT_TRDPARTY_DIR_FOUND} EQUAL -1) list(REMOVE_ITEM ALL_SOURCE_FILES
88
# ${SOURCE_FILE}) endif() endforeach()
99

10-
#set(clangformat_style "{BasedOnStyle: Google, IndentWidth: 2}")
10+
# set(clangformat_style "{BasedOnStyle: Google, IndentWidth: 2}")
1111
add_custom_target(clangformat COMMAND clang-format -style=file -i
12-
${ALL_SOURCE_FILES})
12+
${ALL_SOURCE_FILES})
13+
14+
add_custom_target(formatcheck COMMAND clang-format -style=file --Werror
15+
${ALL_SOURCE_FILES})
16+
17+
file(GLOB ALL_NEW_FILES srcs/*.cc srcs/*.h srcs/internal/*/*.h
18+
srcs/internal/*/*.cc
19+
srcs/utils/*
20+
)
21+
add_custom_target(
22+
lint
23+
COMMAND
24+
clang-tidy
25+
# -p build)
26+
${ALL_NEW_FILES} -p build)

0 commit comments

Comments
 (0)