1 /*
2  * Copyright (C) 2021 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 <camera2/ConcurrentCamera.h>
18 #include <CameraUtils.h>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #include "camera2common.h"
21 
22 using namespace std;
23 using namespace android;
24 using namespace android::hardware::camera2::utils;
25 
26 constexpr int32_t kRangeMin = 0;
27 constexpr int32_t kRangeMax = 1000;
28 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)29 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
30     FuzzedDataProvider fdp = FuzzedDataProvider(data, size);
31     ConcurrentCameraIdCombination camIdCombination;
32 
33     if (fdp.ConsumeBool()) {
34         size_t concurrentCameraIdSize = fdp.ConsumeIntegralInRange<size_t>(kRangeMin, kRangeMax);
35         for (size_t idx = 0; idx < concurrentCameraIdSize; ++idx) {
36             string concurrentCameraId = fdp.ConsumeRandomLengthString();
37             camIdCombination.mConcurrentCameraIdDeviceIdPairs.push_back(
38                     {concurrentCameraId, kDefaultDeviceId});
39         }
40     }
41 
42     invokeReadWriteNullParcel<ConcurrentCameraIdCombination>(&camIdCombination);
43     invokeReadWriteParcel<ConcurrentCameraIdCombination>(&camIdCombination);
44 
45     CameraIdAndSessionConfiguration camIdAndSessionConfig;
46 
47     if (fdp.ConsumeBool()) {
48         camIdAndSessionConfig.mCameraId = fdp.ConsumeRandomLengthString();
49         if (fdp.ConsumeBool()) {
50             camIdAndSessionConfig.mSessionConfiguration = SessionConfiguration();
51         } else {
52             int32_t inputWidth = fdp.ConsumeIntegral<int32_t>();
53             int32_t inputHeight = fdp.ConsumeIntegral<int32_t>();
54             int32_t inputFormat = fdp.ConsumeIntegral<int32_t>();
55             int32_t operatingMode = fdp.ConsumeIntegral<int32_t>();
56             camIdAndSessionConfig.mSessionConfiguration =
57                     SessionConfiguration(inputWidth, inputHeight, inputFormat, operatingMode);
58         }
59     }
60 
61     invokeReadWriteNullParcel<CameraIdAndSessionConfiguration>(&camIdAndSessionConfig);
62     invokeReadWriteParcel<CameraIdAndSessionConfiguration>(&camIdAndSessionConfig);
63     return 0;
64 }
65