/src/postgis/fuzzers/wkb_import_fuzzer.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: PostGIS |
4 | | * Purpose: Fuzzer |
5 | | * Author: Even Rouault, even.rouault at spatialys.com |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2017, Even Rouault <even.rouault at spatialys.com> |
9 | | * |
10 | | * Permission is hereby granted, free of charge, to any person obtaining a |
11 | | * copy of this software and associated documentation files (the "Software"), |
12 | | * to deal in the Software without restriction, including without limitation |
13 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
14 | | * and/or sell copies of the Software, and to permit persons to whom the |
15 | | * Software is furnished to do so, subject to the following conditions: |
16 | | * |
17 | | * The above copyright notice and this permission notice shall be included |
18 | | * in all copies or substantial portions of the Software. |
19 | | * |
20 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
21 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
23 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
25 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
26 | | * DEALINGS IN THE SOFTWARE. |
27 | | ****************************************************************************/ |
28 | | |
29 | | #include <assert.h> |
30 | | #include <stddef.h> |
31 | | #include <stdint.h> |
32 | | #include <stdlib.h> |
33 | | #include <string.h> |
34 | | #include <setjmp.h> |
35 | | |
36 | | #include <set> |
37 | | |
38 | | extern "C" |
39 | | { |
40 | | #include "liblwgeom.h" |
41 | | #include "geos_stub.h" |
42 | | #include "proj_stub.h" |
43 | | } |
44 | | |
45 | | extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv); |
46 | | |
47 | | // Keep active heap allocated memory corresponding to returns of allocator() |
48 | | // and reallocator() |
49 | | std::set<void*> oSetPointers; |
50 | | jmp_buf jmpBuf; |
51 | | |
52 | | extern "C" |
53 | | { |
54 | | static void * |
55 | | allocator(size_t size) |
56 | 0 | { |
57 | 0 | void *mem = malloc(size); |
58 | 0 | oSetPointers.insert(mem); |
59 | 0 | return mem; |
60 | 0 | } |
61 | | |
62 | | static void |
63 | | freeor(void *mem) |
64 | 0 | { |
65 | 0 | oSetPointers.erase(mem); |
66 | 0 | free(mem); |
67 | 0 | } |
68 | | |
69 | | static void * |
70 | | reallocator(void *mem, size_t size) |
71 | 0 | { |
72 | 0 | oSetPointers.erase(mem); |
73 | 0 | void *ret = realloc(mem, size); |
74 | 0 | oSetPointers.insert(ret); |
75 | 0 | return ret; |
76 | 0 | } |
77 | | |
78 | | static void |
79 | | noticereporter(const char *, va_list ) |
80 | 122k | { |
81 | 122k | } |
82 | | |
83 | | static void |
84 | | errorreporter(const char *, va_list ) |
85 | 0 | { |
86 | 0 | // Cleanup any heap-allocated memory still active |
87 | 0 | for(std::set<void*>::iterator oIter = oSetPointers.begin(); |
88 | 0 | oIter != oSetPointers.end(); ++oIter ) |
89 | 0 | { |
90 | 0 | free(*oIter); |
91 | 0 | } |
92 | 0 | oSetPointers.clear(); |
93 | 0 | // Abort everything to jump to setjmp() call |
94 | 0 | longjmp(jmpBuf, 1); |
95 | 0 | } |
96 | | |
97 | | static void |
98 | | debuglogger(int, const char *, va_list) |
99 | 0 | { |
100 | 0 | } |
101 | | |
102 | | } |
103 | | |
104 | | int LLVMFuzzerInitialize(int* /*argc*/, char*** /*argv*/) |
105 | 6 | { |
106 | 6 | lwgeom_set_handlers(malloc, realloc, free, noticereporter, noticereporter); |
107 | 6 | lwgeom_set_debuglogger(debuglogger); |
108 | 6 | return 0; |
109 | 6 | } |
110 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len); |
111 | | |
112 | | int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) |
113 | 1.77k | { |
114 | 1.77k | if( setjmp(jmpBuf) ) |
115 | 0 | return 0; |
116 | 1.77k | LWGEOM* lwgeom = lwgeom_from_wkb(buf, len, LW_PARSER_CHECK_NONE); |
117 | 1.77k | lwgeom_free(lwgeom); |
118 | | //assert( oSetPointers.empty() ); |
119 | 1.77k | return 0; |
120 | 1.77k | } |