Mercurial > hg
annotate contrib/fuzz/revlog.cc @ 45095:8e04607023e5
procutil: ensure that procutil.std{out,err}.write() writes all bytes
Python 3 offers different kind of streams and it’s not guaranteed for all of
them that calling write() writes all bytes.
When Python is started in unbuffered mode, sys.std{out,err}.buffer are
instances of io.FileIO, whose write() can write less bytes for
platform-specific reasons (e.g. Linux has a 0x7ffff000 bytes maximum and could
write less if interrupted by a signal; when writing to Windows consoles, it’s
limited to 32767 bytes to avoid the "not enough space" error). This can lead to
silent loss of data, both when using sys.std{out,err}.buffer (which may in fact
not be a buffered stream) and when using the text streams sys.std{out,err}
(I’ve created a CPython bug report for that:
https://bugs.python.org/issue41221).
Python may fix the problem at some point. For now, we implement our own wrapper
for procutil.std{out,err} that calls the raw stream’s write() method until all
bytes have been written. We don’t use sys.std{out,err} for larger writes, so I
think it’s not worth the effort to patch them.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Fri, 10 Jul 2020 12:27:58 +0200 |
parents | 8766728dbce6 |
children | efbbc2f9121e |
rev | line source |
---|---|
41014
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
1 #include <Python.h> |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
2 #include <assert.h> |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
3 #include <stdlib.h> |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
4 #include <unistd.h> |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
5 |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
6 #include <string> |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
7 |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
8 #include "pyutil.h" |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
9 |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
10 extern "C" { |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
11 |
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
43151
diff
changeset
|
12 static PYCODETYPE *code; |
41014
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
13 |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
15 { |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
16 contrib::initpy(*argv[0]); |
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
43151
diff
changeset
|
17 code = (PYCODETYPE *)Py_CompileString(R"py( |
41014
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
18 for inline in (True, False): |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
19 try: |
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
43151
diff
changeset
|
20 index, cache = parsers.parse_index2(data, inline) |
41310
ebe51a2e75be
fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents:
41309
diff
changeset
|
21 index.slicechunktodensity(list(range(len(index))), 0.5, 262144) |
43151
36e386dbbd30
fuzz: exercise a little more revlog code
Augie Fackler <augie@google.com>
parents:
41310
diff
changeset
|
22 index.stats() |
36e386dbbd30
fuzz: exercise a little more revlog code
Augie Fackler <augie@google.com>
parents:
41310
diff
changeset
|
23 index.findsnapshots({}, 0) |
36e386dbbd30
fuzz: exercise a little more revlog code
Augie Fackler <augie@google.com>
parents:
41310
diff
changeset
|
24 10 in index |
41310
ebe51a2e75be
fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents:
41309
diff
changeset
|
25 for rev in range(len(index)): |
43151
36e386dbbd30
fuzz: exercise a little more revlog code
Augie Fackler <augie@google.com>
parents:
41310
diff
changeset
|
26 index.reachableroots(0, [len(index)-1], [rev]) |
41310
ebe51a2e75be
fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents:
41309
diff
changeset
|
27 node = index[rev][7] |
ebe51a2e75be
fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents:
41309
diff
changeset
|
28 partial = index.shortest(node) |
ebe51a2e75be
fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents:
41309
diff
changeset
|
29 index.partialmatch(node[:partial]) |
43151
36e386dbbd30
fuzz: exercise a little more revlog code
Augie Fackler <augie@google.com>
parents:
41310
diff
changeset
|
30 index.deltachain(rev, None, True) |
41014
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
31 except Exception as e: |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
32 pass |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
33 # uncomment this print if you're editing this Python code |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
34 # to debug failures. |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
35 # print e |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
36 )py", |
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
43151
diff
changeset
|
37 "fuzzer", Py_file_input); |
41014
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
38 return 0; |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
39 } |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
40 |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
41 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
42 { |
41309
afc33a5705b9
fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents:
41014
diff
changeset
|
43 // Don't allow fuzzer inputs larger than 60k, since we'll just bog |
afc33a5705b9
fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents:
41014
diff
changeset
|
44 // down and not accomplish much. |
afc33a5705b9
fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents:
41014
diff
changeset
|
45 if (Size > 60000) { |
afc33a5705b9
fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents:
41014
diff
changeset
|
46 return 0; |
afc33a5705b9
fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents:
41014
diff
changeset
|
47 } |
41014
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
48 PyObject *text = |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
49 PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size); |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
50 PyObject *locals = PyDict_New(); |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
51 PyDict_SetItemString(locals, "data", text); |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
52 PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
53 if (!res) { |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
54 PyErr_Print(); |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
55 } |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
56 Py_XDECREF(res); |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
57 Py_DECREF(locals); |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
58 Py_DECREF(text); |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
59 return 0; // Non-zero return values are reserved for future use. |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
60 } |
c06f0ef9a5ba
fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
61 } |