/src/postgres/src/backend/fuzzer/json_parser_fuzzer.c
Line | Count | Source |
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 "common/jsonapi.h" |
20 | | #include "mb/pg_wchar.h" |
21 | | #include "utils/memutils.h" |
22 | | #include "utils/memdebug.h" |
23 | | |
24 | 2 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
25 | | //FuzzerInitialize("json_db", argv); |
26 | 2 | return 0; |
27 | 2 | } |
28 | | |
29 | | /* |
30 | | ** Main entry point. The fuzzer invokes this function with each |
31 | | ** fuzzed input. |
32 | | */ |
33 | 1.53k | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
34 | 1.53k | sigjmp_buf local_sigjmp_buf; |
35 | 1.53k | char *buffer; |
36 | 1.53k | JsonSemAction sem; |
37 | 1.53k | JsonLexContext *lex; |
38 | | |
39 | 1.53k | buffer = (char *) calloc(size+1, sizeof(char)); |
40 | 1.53k | memcpy(buffer, data, size); |
41 | | |
42 | 1.53k | MemoryContextInit(); |
43 | 1.53k | set_stack_base(); |
44 | 1.53k | sem = nullSemAction; |
45 | 1.53k | lex = makeJsonLexContextCstringLen(NULL, buffer, size+1, PG_UTF8, true); |
46 | | |
47 | 1.53k | if(!sigsetjmp(local_sigjmp_buf,0)){ |
48 | 1.53k | error_context_stack = NULL; |
49 | 1.53k | PG_exception_stack = &local_sigjmp_buf; |
50 | 1.53k | pg_parse_json(lex, &sem); |
51 | 1.53k | } |
52 | 1.53k | free(buffer); |
53 | 1.53k | FlushErrorState(); |
54 | 1.53k | MemoryContextReset(TopMemoryContext); |
55 | 1.53k | TopMemoryContext->ident = NULL; |
56 | 1.53k | TopMemoryContext->methods->delete_context(TopMemoryContext); |
57 | 1.53k | VALGRIND_DESTROY_MEMPOOL(TopMemoryContext); |
58 | 1.53k | return 0; |
59 | 1.53k | } |