Skip to content

Commit 753f9f7

Browse files
postgresql: fix build (#13419)
Signed-off-by: David Korczynski <david@adalogics.com>
1 parent 9e98c59 commit 753f9f7

File tree

9 files changed

+92
-42
lines changed

9 files changed

+92
-42
lines changed

projects/postgresql/Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
FROM gcr.io/oss-fuzz-base/base-builder
1818

19-
RUN apt-get update && apt-get install -y make libreadline-dev zlib1g-dev bison flex
19+
RUN apt-get update && apt-get install -y make libreadline-dev zlib1g-dev bison\
20+
flex pkg-config libicu-dev
2021

21-
RUN git clone git://git.postgresql.org/git/postgresql.git
22-
RUN zip postgresql_fuzzer_seed_corpus.zip postgresql/src/test/regress/sql/*
22+
RUN git clone https://github.com/postgres/postgres
23+
RUN zip postgresql_fuzzer_seed_corpus.zip postgres/src/test/regress/sql/*
2324

24-
WORKDIR postgresql
25+
WORKDIR postgres
2526
RUN mkdir bld
2627

2728
COPY fuzzer $SRC/fuzzer
28-
COPY build.sh add_fuzzers.diff $SRC/
29+
COPY build.sh add_fuzzers.diff main.diff $SRC/

projects/postgresql/add_fuzzers.diff

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
2-
index 0775abe35d..f53b3580b3 100644
2+
index 2f8c3d5f918..d9774758413 100644
33
--- a/src/backend/tcop/postgres.c
44
+++ b/src/backend/tcop/postgres.c
5-
@@ -105,6 +105,11 @@ int PostAuthDelay = 0;
6-
/* Time between checks that the client is still connected. */
7-
int client_connection_check_interval = 0;
5+
@@ -104,6 +104,11 @@ int client_connection_check_interval = 0;
6+
/* flags for non-system relation kinds to restrict use */
7+
int restrict_nonsystem_relation_kind;
88

99
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1010
+bool fuzzer_first_run = true;
@@ -14,7 +14,7 @@ index 0775abe35d..f53b3580b3 100644
1414
/* ----------------
1515
* private typedefs etc
1616
* ----------------
17-
@@ -471,11 +476,14 @@ static int
17+
@@ -480,11 +485,14 @@ static int
1818
ReadCommand(StringInfo inBuf)
1919
{
2020
int result;
@@ -30,9 +30,9 @@ index 0775abe35d..f53b3580b3 100644
3030
return result;
3131
}
3232

33-
@@ -4021,6 +4029,11 @@ PostgresMain(const char *dbname, const char *username)
34-
bool idle_in_transaction_timeout_enabled = false;
35-
bool idle_session_timeout_enabled = false;
33+
@@ -4190,6 +4198,11 @@ PostgresMain(const char *dbname, const char *username)
34+
volatile bool idle_in_transaction_timeout_enabled = false;
35+
volatile bool idle_session_timeout_enabled = false;
3636

3737
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3838
+ if(fuzzer_first_run)
@@ -42,7 +42,7 @@ index 0775abe35d..f53b3580b3 100644
4242
Assert(dbname != NULL);
4343
Assert(username != NULL);
4444

45-
@@ -4312,6 +4325,11 @@ PostgresMain(const char *dbname, const char *username)
45+
@@ -4509,6 +4522,11 @@ PostgresMain(const char *dbname, const char *username)
4646
if (!ignore_till_sync)
4747
send_ready_for_query = true; /* initially, or after error */
4848

@@ -55,16 +55,15 @@ index 0775abe35d..f53b3580b3 100644
5555
* Non-error queries loop here.
5656
*/
5757
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
58-
index 2af87ee3bd..825bb70532 100644
58+
index 47af743990f..476e336d418 100644
5959
--- a/src/backend/utils/error/elog.c
6060
+++ b/src/backend/utils/error/elog.c
61-
@@ -594,7 +594,9 @@ errfinish(const char *filename, int lineno, const char *funcname)
61+
@@ -540,7 +540,7 @@ errfinish(const char *filename, int lineno, const char *funcname)
6262
}
6363

6464
/* Emit the message to the right places */
65-
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
66-
EmitErrorReport();
67-
+#endif
65+
- EmitErrorReport();
66+
+ //EmitErrorReport();
6867

6968
/* Now free up subsidiary data attached to stack entry, and release it */
70-
if (edata->message)
69+
FreeErrorDataContents(edata);

projects/postgresql/build.sh

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash -eu
1+
#!/bin/bash -eux
22
# Copyright 2020 Google Inc.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,16 +14,34 @@
1414
# limitations under the License.
1515
#
1616
################################################################################
17+
# Apply diff for fuzzers
1718
cp -r $SRC/fuzzer src/backend/
1819
git apply --ignore-space-change --ignore-whitespace ../add_fuzzers.diff
1920

21+
# Change permission for fuzzers
2022
useradd fuzzuser
2123
chown -R fuzzuser .
24+
2225
cd bld
2326

27+
# Build icu 66 for postgres
28+
wget https://github.com/unicode-org/icu/releases/download/release-66-1/icu4c-66_1-src.tgz
29+
tar -xzf icu4c-66_1-src.tgz
30+
pushd icu/source
31+
./configure --prefix=/opt/icu66 --enable-renaming CC=clang CXX=clang++ CFLAGS="" CXXFLAGS=""
32+
make -j$(nproc)
33+
make install
34+
popd
35+
36+
# Add environment flags for icu 66
37+
export PKG_CONFIG_PATH=/opt/icu66/lib/pkgconfig
38+
export LD_LIBRARY_PATH=/opt/icu66/lib
39+
export ICU_CFLAGS="-I/opt/icu66/include"
40+
export ICU_LIBS="-L/opt/icu66/lib -licui18n -licuuc -licudata"
41+
2442
CC="" CXX="" CFLAGS="" CXXFLAGS="" su fuzzuser -c ../configure
2543
cd src/backend/fuzzer
26-
su fuzzuser -c "make createdb"
44+
su fuzzuser -c "make -j10 createdb"
2745
chown -R root .
2846
mv temp/data .
2947
cp -r data $OUT/
@@ -32,12 +50,24 @@ cp -r tmp_install $OUT/
3250
make clean
3351

3452
../configure
35-
make
36-
cd src/backend/fuzzer
37-
make fuzzer
53+
make -j$(nproc)
54+
55+
# Manually remove main function from main.c and recompile it
56+
cd ../
57+
git apply --ignore-space-change --ignore-whitespace $SRC/main.diff
58+
cd bld
59+
$CC -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -I./src/include -I./src/include/port -I../src/include -fPIC -c ../src/backend/main/main.c -o ./src/backend/main/main.o
60+
61+
# Package static library
62+
cd src/backend
63+
ar rcs libpostgres.a $(find . -name '*.o' | grep -v '^./fuzzer/')
64+
65+
cd fuzzer
66+
make -j$(nproc) fuzzer
3867
#if [ "$FUZZING_ENGINE" = "afl" ]
3968
#then
4069
rm protocol_fuzzer
70+
rm simple_query_fuzzer
4171
#fi
4272
cp *_fuzzer $OUT/
4373
cp $SRC/postgresql_fuzzer_seed_corpus.zip $OUT/

projects/postgresql/fuzzer/Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ OBJS = \
3131
$(SUBDIROBJS) \
3232
$(top_builddir)/src/common/libpgcommon_srv.a \
3333
$(top_builddir)/src/port/libpgport_srv.a \
34+
$(top_builddir)/src/backend/libpostgres.a \
3435

3536
OBJS_FUZZERS = $(filter-out ../main/objfiles.txt, $(OBJS))
3637

@@ -41,19 +42,19 @@ fuzzer: simple_query_fuzzer \
4142
protocol_fuzzer
4243

4344
simple_query_fuzzer json_parser_fuzzer: %: %.o fuzzer_initialize.o $(OBJS_FUZZERS)
44-
$(CXX) $(CFLAGS) $(call expand_subsys,$^) -o $@ $(LIB_FUZZING_ENGINE) -lz
45+
$(CXX) $(CFLAGS) $(call expand_subsys,$^) -o $@ -l:libicui18n.a -l:libicuuc.a -l:libicudata.a $(LIB_FUZZING_ENGINE) -lz -lpthread -ldl -lrt
4546

4647
simple_query_fuzzer.o json_parser_fuzzer.o protocol_fuzzer.o fuzzer_initialize.o: %.o: %.c
47-
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $^
48+
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $^
4849

4950
protocol_fuzzer: %: %.o $(OBJS_FUZZERS)
50-
$(CXX) $(CFLAGS) $(call expand_subsys,$^) -o $@ $(LIB_FUZZING_ENGINE) -Wl,--wrap=exit -Wl,--wrap=pq_getbyte -lz
51+
$(CXX) $(CFLAGS) $(call expand_subsys,$^) -o $@ -l:libicui18n.a -l:libicuuc.a -l:libicudata.a $(LIB_FUZZING_ENGINE) -Wl,--wrap=exit -Wl,--wrap=pq_getbyte -lz -lpthread -ldl -lrt
5152

5253
dbfuzz: dbfuzz.o | submake-libpgport temp-install
5354
$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@ \
5455
&& PATH="$(abs_top_builddir)/tmp_install$(bindir):$$PATH" LD_LIBRARY_PATH="$(abs_top_builddir)/tmp_install/usr/local/pgsql/lib" ./dbfuzz
5556

56-
dbfuzz.o: dbfuzz.c $(top_builddir)/src/port/pg_config_paths.h
57+
dbfuzz.o: dbfuzz.c $(top_builddir)/src/port/pg_config_paths.h
5758
dbfuzz.o: override CPPFLAGS := $(CPPFLAGS) -I$(top_builddir)/src/port -I$(top_builddir)/../src/test/regress '-DSHELLPROG="$(SHELL)"'
5859

5960
$(top_builddir)/src/port/pg_config_paths.h: | submake-libpgport

projects/postgresql/fuzzer/fuzzer_initialize.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
#include <libgen.h>
3939

40-
const char *progname;
40+
extern const char *progname;
4141
static MemoryContext row_description_context = NULL;
4242
static StringInfoData row_description_buf;
4343
static const char *username = "username";
@@ -65,7 +65,7 @@ int FuzzerInitialize(char *dbname, char ***argv){
6565
av[4] = NULL;
6666

6767
system(untar);
68-
68+
6969
progname = get_progname(av[0]);
7070
MemoryContextInit();
7171

@@ -81,13 +81,13 @@ int FuzzerInitialize(char *dbname, char ***argv){
8181
CreateDataDirLockFile(false);
8282
LocalProcessControlFile(false);
8383
InitializeMaxBackends();
84-
85-
CreateSharedMemoryAndSemaphores();
84+
85+
// CreateSharedMemoryAndSemaphores();
8686
InitProcess();
8787
BaseInit();
88-
PG_SETMASK(&UnBlockSig);
89-
InitPostgres("dbfuzz", InvalidOid, username, InvalidOid, false, false, NULL);
90-
88+
sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
89+
InitPostgres("dbfuzz", InvalidOid, username, InvalidOid, false, false);
90+
9191
SetProcessingMode(NormalProcessing);
9292

9393
BeginReportingGUCOptions();

projects/postgresql/fuzzer/json_parser_fuzzer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "utils/memdebug.h"
2323

2424
int LLVMFuzzerInitialize(int *argc, char ***argv) {
25-
FuzzerInitialize("json_db", argv);
25+
//FuzzerInitialize("json_db", argv);
2626
return 0;
2727
}
2828

@@ -42,7 +42,7 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
4242
MemoryContextInit();
4343
set_stack_base();
4444
sem = nullSemAction;
45-
lex = makeJsonLexContextCstringLen(buffer, size+1, PG_UTF8, true);
45+
lex = makeJsonLexContextCstringLen(NULL, buffer, size+1, PG_UTF8, true);
4646

4747
if(!sigsetjmp(local_sigjmp_buf,0)){
4848
error_context_stack = NULL;

projects/postgresql/fuzzer/protocol_fuzzer.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#include <unistd.h>
4646
#include <libgen.h>
4747

48-
const char *progname = "progname";
4948
static sigjmp_buf postgre_exit;
5049
static bool postgre_started;
5150
static char *buffer;

projects/postgresql/fuzzer/simple_query_fuzzer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ exec_simple_query(const char *query_string)
5757
bool snapshot_set = false;
5858
MemoryContext per_parsetree_context = NULL;
5959
List *querytree_list,
60-
*plantree_list;
60+
*plantree_list;
6161

6262
if (use_implicit_block)
6363
BeginImplicitTransactionBlock();
@@ -81,7 +81,7 @@ exec_simple_query(const char *query_string)
8181

8282
querytree_list = pg_analyze_and_rewrite_fixedparams(parsetree, query_string,
8383
NULL, 0, NULL);
84-
84+
8585
plantree_list = pg_plan_queries(querytree_list, query_string,
8686
CURSOR_OPT_PARALLEL_OK, NULL);
8787

@@ -131,7 +131,7 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
131131
FlushErrorState();
132132

133133
MemoryContextSwitchTo(MessageContext);
134-
MemoryContextResetAndDeleteChildren(MessageContext);
134+
MemoryContextReset(MessageContext);
135135

136136
InvalidateCatalogSnapshotConditionally();
137137

projects/postgresql/main.diff

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
diff --git a/src/backend/main/main.c b/src/backend/main/main.c
2+
index 7d63cf94a6b..d10f721dadc 100644
3+
--- a/src/backend/main/main.c
4+
+++ b/src/backend/main/main.c
5+
@@ -64,6 +64,7 @@ static void help(const char *progname);
6+
static void check_root(const char *progname);
7+
8+
9+
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
10+
/*
11+
* Any Postgres server process begins execution here.
12+
*/
13+
@@ -231,6 +232,7 @@ main(int argc, char *argv[])
14+
/* the functions above should not return */
15+
abort();
16+
}
17+
+#endif
18+
19+
/*
20+
* Returns the matching DispatchOption value for the given option name. If no

0 commit comments

Comments
 (0)