• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <fuzzer/FuzzedDataProvider.h>
18 #include <sys/time.h>
19 #include <iostream>
20 #include "Common.h"
21 #include "Enumerator.h"
22 #include "HalDisplay.h"
23 #include "MockHWDisplay.h"
24 
25 using DisplayDesc = ::android::hardware::automotive::evs::V1_0::DisplayDesc;
26 
27 namespace android {
28 namespace automotive {
29 namespace evs {
30 namespace V1_1 {
31 namespace implementation {
32 
33 namespace {
34 
35 enum EvsFuzzFuncs {
36     EVS_FUZZ_GET_HW_DISPLAY = 0,       // verify getHwDisplay
37     EVS_FUZZ_GET_DISPLAY_INFO,         // verify getDisplayInfo
38     EVS_FUZZ_SET_DISPLAY_STATE,        // verify setDisplayState
39     EVS_FUZZ_GET_DISPLAY_STATE,        // verify getDisplayState
40     EVS_FUZZ_GET_TARGET_BUFFER,        // verify getTargetBuffer
41     EVS_FUZZ_RTN_TGT_BUF_FOR_DISPLAY,  // verify returnTargetBufferForDisplay
42     EVS_FUZZ_GET_DISPLAY_INFO_1_1,     // verify getDisplayInfo_1_1
43     EVS_FUZZ_TO_STRING,                // verify toString
44     EVS_FUZZ_API_SUM
45 };
46 
47 const int kMaxFuzzerConsumedBytes = 12;
48 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)49 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
50     FuzzedDataProvider fdp(data, size);
51     sp<IEvsDisplay_1_0> mockHWDisplay = new MockHWDisplay();
52     sp<HalDisplay> halDisplay = new HalDisplay(mockHWDisplay);
53 
54     while (fdp.remaining_bytes() > kMaxFuzzerConsumedBytes) {
55         switch (fdp.ConsumeIntegralInRange<uint32_t>(0, EVS_FUZZ_API_SUM)) {
56             case EVS_FUZZ_GET_HW_DISPLAY: {
57                 LOG(DEBUG) << "EVS_FUZZ_GET_HW_DISPLAY";
58                 halDisplay->getHwDisplay();
59                 break;
60             }
61             case EVS_FUZZ_GET_DISPLAY_INFO: {
62                 LOG(DEBUG) << "EVS_FUZZ_GET_DISPLAY_INFO";
63                 halDisplay->getDisplayInfo([](DisplayDesc desc) {});
64                 break;
65             }
66             case EVS_FUZZ_SET_DISPLAY_STATE: {
67                 LOG(DEBUG) << "EVS_FUZZ_SET_DISPLAY_STATE";
68                 uint32_t state =
69                         fdp.ConsumeIntegralInRange<uint32_t>(0,
70                                                              static_cast<uint32_t>(
71                                                                      EvsDisplayState::NUM_STATES) -
72                                                                      1);
73                 halDisplay->setDisplayState(static_cast<EvsDisplayState>(state));
74                 break;
75             }
76             case EVS_FUZZ_GET_DISPLAY_STATE: {
77                 LOG(DEBUG) << "EVS_FUZZ_GET_DISPLAY_STATE";
78                 halDisplay->getDisplayState();
79                 break;
80             }
81             case EVS_FUZZ_GET_TARGET_BUFFER: {
82                 LOG(DEBUG) << "EVS_FUZZ_GET_TARGET_BUFFER";
83                 halDisplay->getTargetBuffer([](const BufferDesc_1_0& buff) {});
84                 break;
85             }
86             case EVS_FUZZ_RTN_TGT_BUF_FOR_DISPLAY: {
87                 LOG(DEBUG) << "EVS_FUZZ_RTN_TGT_BUF_FOR_DISPLAY";
88                 BufferDesc_1_0 buffer;
89                 buffer.bufferId = fdp.ConsumeIntegral<int32_t>();
90                 halDisplay->returnTargetBufferForDisplay(buffer);
91                 break;
92             }
93             case EVS_FUZZ_GET_DISPLAY_INFO_1_1: {
94                 LOG(DEBUG) << "EVS_FUZZ_GET_DISPLAY_INFO_1_1";
95                 halDisplay->getDisplayInfo_1_1([](const auto& config, const auto& state) {});
96                 break;
97             }
98             case EVS_FUZZ_TO_STRING: {
99                 LOG(DEBUG) << "EVS_FUZZ_TO_STRING";
100                 std::string indent = fdp.ConsumeRandomLengthString(kMaxFuzzerConsumedBytes);
101                 halDisplay->toString(indent.c_str());
102                 break;
103             }
104             default:
105                 LOG(ERROR) << "Unexpected option, aborting...";
106                 break;
107         }
108     }
109     return 0;
110 }
111 
112 }  // namespace
113 }  // namespace implementation
114 }  // namespace V1_1
115 }  // namespace evs
116 }  // namespace automotive
117 }  // namespace android
118