• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #include "postgres.h"
18 
19 #include "access/xlog.h"
20 #include "access/xact.h"
21 #include "common/username.h"
22 #include "executor/spi.h"
23 #include "jit/jit.h"
24 #include "libpq/libpq.h"
25 #include "libpq/pqsignal.h"
26 #include "miscadmin.h"
27 #include "optimizer/optimizer.h"
28 #include "parser/analyze.h"
29 #include "parser/parser.h"
30 #include "storage/proc.h"
31 #include "tcop/tcopprot.h"
32 #include "utils/datetime.h"
33 #include "utils/memutils.h"
34 #include "utils/portal.h"
35 #include "utils/snapmgr.h"
36 #include "utils/timeout.h"
37 
38 #include <libgen.h>
39 
40 const char *progname;
41 static MemoryContext row_description_context = NULL;
42 static StringInfoData row_description_buf;
43 static const char *username = "username";
44 
FuzzerInitialize(char * dbname,char *** argv)45 int FuzzerInitialize(char *dbname, char ***argv){
46   char *av[5];
47   char arg_path[50];
48   char path_to_db[50];
49   char untar[100];
50   char *exe_path = (*argv)[0];
51   //dirname() can modify its argument
52   char *exe_path_copy = strdup(exe_path);
53   char *dir = dirname(exe_path_copy);
54   chdir(dir);
55   free(exe_path_copy);
56 
57   snprintf(arg_path, sizeof(arg_path), "/tmp/%s/data", dbname);
58   snprintf(path_to_db, sizeof(path_to_db), "-D\"/tmp/%s/data\"", dbname);
59   snprintf(untar, sizeof(untar), "rm -rf /tmp/%s; mkdir /tmp/%s; cp -r data /tmp/%s", dbname, dbname, dbname);
60 
61   av[0] = "tmp_install/usr/local/pgsql/bin/postgres";
62   av[1] = path_to_db;
63   av[2] = "-F";
64   av[3] = "-k\"/tmp\"";
65   av[4] = NULL;
66 
67   system(untar);
68 
69   progname = get_progname(av[0]);
70   MemoryContextInit();
71 
72   InitStandaloneProcess(av[0]);
73   SetProcessingMode(InitProcessing);
74   InitializeGUCOptions();
75   process_postgres_switches(4, av, PGC_POSTMASTER, NULL);
76 
77   SelectConfigFiles(arg_path, progname);
78 
79   checkDataDir();
80   ChangeToDataDir();
81   CreateDataDirLockFile(false);
82   LocalProcessControlFile(false);
83   InitializeMaxBackends();
84 
85   BaseInit();
86   InitProcess();
87   PG_SETMASK(&UnBlockSig);
88   InitPostgres("dbfuzz", InvalidOid, username, InvalidOid, NULL, false);
89 
90   SetProcessingMode(NormalProcessing);
91 
92   BeginReportingGUCOptions();
93   process_session_preload_libraries();
94 
95   MessageContext = AllocSetContextCreate(TopMemoryContext,
96 					 "MessageContext",
97 					 ALLOCSET_DEFAULT_SIZES);
98   row_description_context = AllocSetContextCreate(TopMemoryContext,
99 						  "RowDescriptionContext",
100 						  ALLOCSET_DEFAULT_SIZES);
101   MemoryContextSwitchTo(row_description_context);
102   initStringInfo(&row_description_buf);
103   MemoryContextSwitchTo(TopMemoryContext);
104 
105   PgStartTime = GetCurrentTimestamp();
106   whereToSendOutput = DestNone;
107   Log_destination = 0;
108   return 0;
109 }
110