1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <fuzzer/FuzzedDataProvider.h>
17
18 #include "osi/include/compat.h"
19
20 #define MAX_BUFFER_SIZE 4096
21
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)22 extern "C" int LLVMFuzzerTestOneInput([[maybe_unused]] const uint8_t* Data,
23 [[maybe_unused]] size_t Size) {
24 // Our functions are only defined with __GLIBC__
25 #if __GLIBC__
26 // Init our wrapper
27 FuzzedDataProvider dataProvider(Data, Size);
28
29 size_t buf_size = dataProvider.ConsumeIntegralInRange<size_t>(0, MAX_BUFFER_SIZE);
30 if (buf_size == 0) {
31 return 0;
32 }
33
34 // Set up our buffers
35 // NOTE: If the src buffer is not NULL-terminated, the strlcpy will
36 // overread regardless of the len arg. Force null-term for now.
37 std::vector<char> bytes = dataProvider.ConsumeBytesWithTerminator<char>(buf_size, '\0');
38 if (bytes.empty()) {
39 return 0;
40 }
41 buf_size = bytes.size();
42 void* dst_buf = malloc(buf_size);
43 if (dst_buf == nullptr) {
44 return 0;
45 }
46
47 // Call the getId fn just to ensure things don't crash
48 gettid();
49
50 // Copy, then concat
51 size_t len_to_cpy = dataProvider.ConsumeIntegralInRange<size_t>(0, buf_size);
52 osi_strlcpy(reinterpret_cast<char*>(dst_buf), reinterpret_cast<char*>(bytes.data()), len_to_cpy);
53
54 // Clear out our dest buffer
55 free(dst_buf);
56 #endif
57
58 return 0;
59 }
60