aboutsummaryrefslogtreecommitdiff
path: root/projects/postgresql/fuzzer/fuzzer_initialize.c
blob: 0ab9d7dcdddc4cb902c8d4fc806db06c04fd526e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

#include "postgres.h"

#include "access/xlog.h"
#include "access/xact.h"
#include "common/username.h"
#include "executor/spi.h"
#include "jit/jit.h"
#include "libpq/libpq.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "optimizer/optimizer.h"
#include "parser/analyze.h"
#include "parser/parser.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/datetime.h"
#include "utils/memutils.h"
#include "utils/portal.h"
#include "utils/snapmgr.h"
#include "utils/timeout.h"

#include <libgen.h>

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){
  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; 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(av[0]);
  MemoryContextInit();

  InitStandaloneProcess(av[0]);
  SetProcessingMode(InitProcessing);
  InitializeGUCOptions();
  process_postgres_switches(4, av, PGC_POSTMASTER, NULL);

  SelectConfigFiles(arg_path, progname);

  checkDataDir();
  ChangeToDataDir();
  CreateDataDirLockFile(false);
  LocalProcessControlFile(false);
  InitializeMaxBackends();
		 
  BaseInit();
  InitProcess();
  PG_SETMASK(&UnBlockSig);
  InitPostgres("dbfuzz", InvalidOid, username, InvalidOid, NULL, false);
 
  SetProcessingMode(NormalProcessing);

  BeginReportingGUCOptions();
  process_session_preload_libraries();

  MessageContext = AllocSetContextCreate(TopMemoryContext,
					 "MessageContext",
					 ALLOCSET_DEFAULT_SIZES);
  row_description_context = AllocSetContextCreate(TopMemoryContext,
						  "RowDescriptionContext",
						  ALLOCSET_DEFAULT_SIZES);
  MemoryContextSwitchTo(row_description_context);
  initStringInfo(&row_description_buf);
  MemoryContextSwitchTo(TopMemoryContext);

  PgStartTime = GetCurrentTimestamp();
  whereToSendOutput = DestNone;
  Log_destination = 0;
  return 0;
}