xref: /aosp_15_r20/tools/security/fuzzing/example_fuzzer/example_fuzzer.cpp (revision d9ecfb0f4d734c9ce41cde8ac4d585b094fd4222)
1*d9ecfb0fSAndroid Build Coastguard Worker /*
2*d9ecfb0fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d9ecfb0fSAndroid Build Coastguard Worker  *
4*d9ecfb0fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*d9ecfb0fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*d9ecfb0fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*d9ecfb0fSAndroid Build Coastguard Worker  *
8*d9ecfb0fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*d9ecfb0fSAndroid Build Coastguard Worker  *
10*d9ecfb0fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*d9ecfb0fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*d9ecfb0fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d9ecfb0fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*d9ecfb0fSAndroid Build Coastguard Worker  * limitations under the License.
15*d9ecfb0fSAndroid Build Coastguard Worker  */
16*d9ecfb0fSAndroid Build Coastguard Worker 
17*d9ecfb0fSAndroid Build Coastguard Worker #include <stddef.h>
18*d9ecfb0fSAndroid Build Coastguard Worker #include <stdint.h>
19*d9ecfb0fSAndroid Build Coastguard Worker #include <string.h>
20*d9ecfb0fSAndroid Build Coastguard Worker 
21*d9ecfb0fSAndroid Build Coastguard Worker #include <string>
22*d9ecfb0fSAndroid Build Coastguard Worker 
BuggyCode(const char * data)23*d9ecfb0fSAndroid Build Coastguard Worker void BuggyCode(const char *data) {
24*d9ecfb0fSAndroid Build Coastguard Worker   if (strcmp(data, "Hi!") == 0) {
25*d9ecfb0fSAndroid Build Coastguard Worker     abort();  // Boom!
26*d9ecfb0fSAndroid Build Coastguard Worker   }
27*d9ecfb0fSAndroid Build Coastguard Worker }
28*d9ecfb0fSAndroid Build Coastguard Worker 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)29*d9ecfb0fSAndroid Build Coastguard Worker extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
30*d9ecfb0fSAndroid Build Coastguard Worker   std::string null_terminated_string(reinterpret_cast<const char *>(data),
31*d9ecfb0fSAndroid Build Coastguard Worker                                      size);
32*d9ecfb0fSAndroid Build Coastguard Worker 
33*d9ecfb0fSAndroid Build Coastguard Worker   BuggyCode(null_terminated_string.c_str());
34*d9ecfb0fSAndroid Build Coastguard Worker   return 0;
35*d9ecfb0fSAndroid Build Coastguard Worker }
36