From 762ec93d3b333493d475b6885ff4fd83a83dc343 Mon Sep 17 00:00:00 2001 From: Yunshu Ouyang <61579667+yooyoo9@users.noreply.github.com> Date: Fri, 11 Sep 2020 08:21:59 +0200 Subject: [PATCH] [PostgreSQL] Fix startup crashes (#4430) --- projects/postgresql/build.sh | 3 +- .../postgresql/fuzzer/fuzzer_initialize.c | 33 ++++++++++++------- .../postgresql/fuzzer/json_parser_fuzzer.c | 6 ++-- .../postgresql/fuzzer/simple_query_fuzzer.c | 7 ++-- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/projects/postgresql/build.sh b/projects/postgresql/build.sh index dd6dd8cc5..0cc580065 100644 --- a/projects/postgresql/build.sh +++ b/projects/postgresql/build.sh @@ -25,8 +25,7 @@ cd src/backend/fuzzer su fuzzuser -c "make createdb" chown -R root . mv temp/data . -tar -czvf data.tar.gz data/ -cp data.tar.gz $OUT/ +cp -r data $OUT/ cd ../../.. cp -r tmp_install $OUT/ make clean diff --git a/projects/postgresql/fuzzer/fuzzer_initialize.c b/projects/postgresql/fuzzer/fuzzer_initialize.c index c3bada22d..0ab9d7dcd 100644 --- a/projects/postgresql/fuzzer/fuzzer_initialize.c +++ b/projects/postgresql/fuzzer/fuzzer_initialize.c @@ -35,35 +35,44 @@ #include "utils/snapmgr.h" #include "utils/timeout.h" +#include + const char *progname; static MemoryContext row_description_context = NULL; static StringInfoData row_description_buf; static const char *username = "username"; -int FuzzerInitialize(char *dbname){ - char *argv[5]; +int FuzzerInitialize(char *dbname, char ***argv){ + char *av[5]; char arg_path[50]; char path_to_db[50]; char untar[100]; + char *exe_path = (*argv)[0]; + //dirname() can modify its argument + char *exe_path_copy = strdup(exe_path); + char *dir = dirname(exe_path_copy); + chdir(dir); + free(exe_path_copy); + snprintf(arg_path, sizeof(arg_path), "/tmp/%s/data", dbname); snprintf(path_to_db, sizeof(path_to_db), "-D\"/tmp/%s/data\"", dbname); - snprintf(untar, sizeof(untar), "rm -rf /tmp/%s && mkdir /tmp/%s && tar -xvf data.tar.gz -C /tmp/%s", dbname, dbname, dbname); - - argv[0] = "tmp_install/usr/local/pgsql/bin/postgres"; - argv[1] = path_to_db; - argv[2] = "-F"; - argv[3] = "-k\"/tmp/pg_dbfuzz\""; - argv[4] = NULL; + snprintf(untar, sizeof(untar), "rm -rf /tmp/%s; mkdir /tmp/%s; cp -r data /tmp/%s", dbname, dbname, dbname); + + av[0] = "tmp_install/usr/local/pgsql/bin/postgres"; + av[1] = path_to_db; + av[2] = "-F"; + av[3] = "-k\"/tmp\""; + av[4] = NULL; system(untar); - progname = get_progname(argv[0]); + progname = get_progname(av[0]); MemoryContextInit(); - InitStandaloneProcess(argv[0]); + InitStandaloneProcess(av[0]); SetProcessingMode(InitProcessing); InitializeGUCOptions(); - process_postgres_switches(4, argv, PGC_POSTMASTER, NULL); + process_postgres_switches(4, av, PGC_POSTMASTER, NULL); SelectConfigFiles(arg_path, progname); diff --git a/projects/postgresql/fuzzer/json_parser_fuzzer.c b/projects/postgresql/fuzzer/json_parser_fuzzer.c index 411cfdd70..08a96fd7c 100644 --- a/projects/postgresql/fuzzer/json_parser_fuzzer.c +++ b/projects/postgresql/fuzzer/json_parser_fuzzer.c @@ -21,9 +21,9 @@ #include "utils/memutils.h" #include "utils/memdebug.h" -int __attribute__((constructor)) Initialize(void) { - FuzzerInitialize("json_db"); - return 0; +int LLVMFuzzerInitialize(int *argc, char ***argv) { + FuzzerInitialize("json_db", argv); + return 0; } /* diff --git a/projects/postgresql/fuzzer/simple_query_fuzzer.c b/projects/postgresql/fuzzer/simple_query_fuzzer.c index 1749f0893..3ba6b50ed 100644 --- a/projects/postgresql/fuzzer/simple_query_fuzzer.c +++ b/projects/postgresql/fuzzer/simple_query_fuzzer.c @@ -35,7 +35,6 @@ #include "utils/snapmgr.h" #include "utils/timeout.h" - static void exec_simple_query(const char *query_string) { @@ -94,9 +93,9 @@ exec_simple_query(const char *query_string) } -int __attribute__((constructor)) Initialize(void) { - FuzzerInitialize("query_db"); - return 0; +int LLVMFuzzerInitialize(int *argc, char ***argv) { + FuzzerInitialize("query_db", argv); + return 0; }