Skip to content

Commit 5b49cd3

Browse files
authored
Merge pull request #3 from tanin47/tanin-sqlite3
Port sqlite3 fuzzer.
2 parents 4d88838 + 1d74066 commit 5b49cd3

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

sqlite3/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Copyright 2016 Google Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
################################################################################
17+
18+
FROM ossfuzz/base-libfuzzer
19+
MAINTAINER tanin@google.com
20+
RUN apt-get install -y make autoconf automake libtool fossil tcl
21+
22+
CMD /src/oss-fuzz/sqlite3/build.sh

sqlite3/Jenkinsfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
// Copyright 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
////////////////////////////////////////////////////////////////////////////////
17+
18+
def libfuzzerBuild = fileLoader.fromGit('infra/libfuzzer-pipeline.groovy',
19+
'https://github.com/google/oss-fuzz.git',
20+
'master', null, '')
21+
22+
libfuzzerBuild {
23+
# We can't use git. We need to use fossil.
24+
# build.sh switches to fossil.
25+
# The below is just a dummy.
26+
# See: crbug.com/643224#c4
27+
git = "https://github.com/google/oss-fuzz"
28+
}

sqlite3/build.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
#!/bin/bash -eu
3+
# Copyright 2016 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
################################################################################
18+
19+
cd /src/sqlite3
20+
21+
rm -rf fossil
22+
mkdir fossil
23+
cd fossil
24+
25+
fossil clone https://www.sqlite.org/src sqlite --user `whoami`
26+
fossil open sqlite
27+
28+
mkdir bld
29+
cd bld
30+
31+
export ASAN_OPTIONS=detect_leaks=0
32+
../configure
33+
make
34+
make sqlite3.c
35+
36+
$CXX $CXXFLAGS -std=c++11 -I. \
37+
/src/oss-fuzz/sqlite3/sqlite3_fuzzer.cc -o /out/sqlite3_fuzzer \
38+
/work/libfuzzer/*.o ./sqlite3.o $LDFLAGS

sqlite3/sqlite3_fuzzer.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2015 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include <stddef.h>
6+
#include <stdint.h>
7+
8+
#include <algorithm>
9+
#include <array>
10+
#include <string>
11+
#include <vector>
12+
13+
#include "sqlite3.h"
14+
15+
16+
static const std::array<uint8_t, 6> kBadKeyword{{'R', 'E', 'G', 'E', 'X', 'P'}};
17+
18+
19+
bool checkForBadKeyword(const uint8_t* data, size_t size) {
20+
auto it = std::search(
21+
data, data + size, kBadKeyword.begin(), kBadKeyword.end(),
22+
[](char c1, char c2) { return std::toupper(c1) == std::toupper(c2); });
23+
24+
if (it != data + size)
25+
return true;
26+
27+
return false;
28+
}
29+
30+
31+
static int Progress(void *not_used_ptr) {
32+
return 1;
33+
}
34+
35+
36+
// Entry point for LibFuzzer.
37+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
38+
if (size < 2)
39+
return 0;
40+
41+
if (checkForBadKeyword(data, size))
42+
return 0;
43+
44+
sqlite3* db;
45+
int return_code = sqlite3_open_v2(
46+
"db.db",
47+
&db,
48+
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, 0);
49+
50+
51+
if (SQLITE_OK != return_code)
52+
return 0;
53+
54+
// Use first byte as random selector for other parameters.
55+
int selector = data[0];
56+
57+
// To cover both cases when progress_handler is used and isn't used.
58+
if (selector & 1)
59+
sqlite3_progress_handler(db, 4, &Progress, NULL);
60+
else
61+
sqlite3_progress_handler(db, 0, NULL, NULL);
62+
63+
// Remove least significant bit to make further usage of selector independent.
64+
selector >>= 1;
65+
66+
sqlite3_stmt* statement = NULL;
67+
int result = sqlite3_prepare_v2(db, reinterpret_cast<const char*>(data + 1),
68+
static_cast<int>(size - 1), &statement, NULL);
69+
if (result == SQLITE_OK) {
70+
// Use selector value to randomize number of iterations.
71+
for (int i = 0; i < selector; i++) {
72+
if (sqlite3_step(statement) != SQLITE_ROW)
73+
break;
74+
}
75+
76+
sqlite3_finalize(statement);
77+
}
78+
79+
sqlite3_close(db);
80+
return 0;
81+
}

0 commit comments

Comments
 (0)