|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <cstring> |
| 19 | +#include "brpc/details/http_parser.h" |
| 20 | +#include "brpc/http_method.h" |
| 21 | + |
| 22 | +#define kMinInputLength 5 |
| 23 | +#define kMaxInputLength 4096 |
| 24 | + |
| 25 | +static int on_url_cb(brpc::http_parser* p, const char* at, size_t length) { return 0; } |
| 26 | +static int on_header_field_cb(brpc::http_parser* p, const char* at, size_t length) { return 0; } |
| 27 | +static int on_header_value_cb(brpc::http_parser* p, const char* at, size_t length) { return 0; } |
| 28 | +static int on_body_cb(brpc::http_parser* p, const char* at, size_t length) { return 0; } |
| 29 | +static int on_message_begin_cb(brpc::http_parser* p) { return 0; } |
| 30 | +static int on_headers_complete_cb(brpc::http_parser* p) { return 0; } |
| 31 | +static int on_message_complete_cb(brpc::http_parser* p) { return 0; } |
| 32 | + |
| 33 | +extern "C" int |
| 34 | +LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
| 35 | +{ |
| 36 | + if (size < kMinInputLength || size > kMaxInputLength){ |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + // Use first byte to select mode |
| 41 | + uint8_t mode = data[0] % 4; |
| 42 | + const uint8_t *payload = data + 1; |
| 43 | + size_t payload_size = size - 1; |
| 44 | + |
| 45 | + switch (mode) { |
| 46 | + case 0: { |
| 47 | + // Fuzz low-level HTTP request parsing |
| 48 | + brpc::http_parser parser; |
| 49 | + brpc::http_parser_init(&parser, brpc::HTTP_REQUEST); |
| 50 | + brpc::http_parser_settings settings; |
| 51 | + memset(&settings, 0, sizeof(settings)); |
| 52 | + settings.on_url = on_url_cb; |
| 53 | + settings.on_header_field = on_header_field_cb; |
| 54 | + settings.on_header_value = on_header_value_cb; |
| 55 | + settings.on_body = on_body_cb; |
| 56 | + settings.on_message_begin = on_message_begin_cb; |
| 57 | + settings.on_headers_complete = on_headers_complete_cb; |
| 58 | + settings.on_message_complete = on_message_complete_cb; |
| 59 | + brpc::http_parser_execute(&parser, &settings, |
| 60 | + reinterpret_cast<const char*>(payload), payload_size); |
| 61 | + break; |
| 62 | + } |
| 63 | + case 1: { |
| 64 | + // Fuzz low-level HTTP response parsing |
| 65 | + brpc::http_parser parser; |
| 66 | + brpc::http_parser_init(&parser, brpc::HTTP_RESPONSE); |
| 67 | + brpc::http_parser_settings settings; |
| 68 | + memset(&settings, 0, sizeof(settings)); |
| 69 | + settings.on_url = on_url_cb; |
| 70 | + settings.on_header_field = on_header_field_cb; |
| 71 | + settings.on_header_value = on_header_value_cb; |
| 72 | + settings.on_body = on_body_cb; |
| 73 | + settings.on_message_begin = on_message_begin_cb; |
| 74 | + settings.on_headers_complete = on_headers_complete_cb; |
| 75 | + settings.on_message_complete = on_message_complete_cb; |
| 76 | + brpc::http_parser_execute(&parser, &settings, |
| 77 | + reinterpret_cast<const char*>(payload), payload_size); |
| 78 | + break; |
| 79 | + } |
| 80 | + case 2: { |
| 81 | + // Fuzz URL parsing (not connect) |
| 82 | + brpc::http_parser_url u; |
| 83 | + brpc::http_parser_parse_url(reinterpret_cast<const char*>(payload), |
| 84 | + payload_size, 0, &u); |
| 85 | + break; |
| 86 | + } |
| 87 | + case 3: { |
| 88 | + // Fuzz URL parsing (connect mode) |
| 89 | + brpc::http_parser_url u; |
| 90 | + brpc::http_parser_parse_url(reinterpret_cast<const char*>(payload), |
| 91 | + payload_size, 1, &u); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
0 commit comments