contrib/fuzz/pyutil.cc
author Martin von Zweigbergk <martinvonz@google.com>
Wed, 03 Apr 2019 16:03:41 -0700
changeset 42093 edbcf5b239f9
parent 41013 ef103c96ed33
child 43807 c78f8f0720cc
permissions -rw-r--r--
config: read configs from directories in lexicographical order Mercurial currently reads the .rc files specified in HGRCPATH (and the system-default paths) in directory order, which is unspecified. My team at work maintains a set of .rc files. So far there has been no overlap between them, so we had not noticed this behavior. However, we would now like to release some common .rc files and then have another one per plaform with platform-specific overrides. It would be nice if we can determine the load order by choosing names carefully. This patch enables that by loading the .rc files in lexicographical order. Before this patch, the added test case would consistently say "30" on my file system (whatever I have -- some Linux FS). Differential Revision: https://phab.mercurial-scm.org/D6193
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41013
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
#include "pyutil.h"
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
#include <string>
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
namespace contrib
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
{
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
static char cpypath[8192] = "\0";
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
static PyObject *mainmod;
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
static PyObject *globals;
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
/* TODO: use Python 3 for this fuzzing? */
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
PyMODINIT_FUNC initparsers(void);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
void initpy(const char *cselfpath)
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
{
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
	const std::string subdir = "/sanpy/lib/python2.7";
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
	/* HACK ALERT: we need a full Python installation built without
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
	   pymalloc and with ASAN, so we dump one in
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
	   $OUT/sanpy/lib/python2.7. This helps us wire that up. */
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
	std::string selfpath(cselfpath);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
	std::string pypath;
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
	auto pos = selfpath.rfind("/");
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
	if (pos == std::string::npos) {
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
		char wd[8192];
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
		getcwd(wd, 8192);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
		pypath = std::string(wd) + subdir;
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
	} else {
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
		pypath = selfpath.substr(0, pos) + subdir;
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
	}
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
	strncpy(cpypath, pypath.c_str(), pypath.size());
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
	setenv("PYTHONPATH", cpypath, 1);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
	setenv("PYTHONNOUSERSITE", "1", 1);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
	/* prevent Python from looking up users in the fuzz environment */
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
	setenv("PYTHONUSERBASE", cpypath, 1);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
	Py_SetPythonHome(cpypath);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
	Py_InitializeEx(0);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
	mainmod = PyImport_AddModule("__main__");
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
	globals = PyModule_GetDict(mainmod);
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
	initparsers();
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
}
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
PyObject *pyglobals()
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
{
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
	return globals;
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
}
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
ef103c96ed33 fuzz: extract Python initialization to utility package
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
} // namespace contrib