1 /*
2  * Copyright (C) 2022 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 <CaptureResult.h>
18 #include <fuzzer/FuzzedDataProvider.h>
19 #include "camera2common.h"
20 
21 using namespace std;
22 using namespace android;
23 using namespace android::hardware::camera2::impl;
24 
25 constexpr int32_t kSizeMin = 0;
26 constexpr int32_t kSizeMax = 1000;
27 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)28 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
29     FuzzedDataProvider fdp = FuzzedDataProvider(data, size);
30     PhysicalCaptureResultInfo* physicalCaptureResultInfo = nullptr;
31 
32     if (fdp.ConsumeBool()) {
33         physicalCaptureResultInfo = new PhysicalCaptureResultInfo();
34     } else {
35         string cameraId = fdp.ConsumeRandomLengthString();
36         CameraMetadata cameraMetadata = CameraMetadata();
37         physicalCaptureResultInfo = new PhysicalCaptureResultInfo(cameraId, cameraMetadata);
38     }
39 
40     invokeReadWriteParcel<PhysicalCaptureResultInfo>(physicalCaptureResultInfo);
41 
42     CaptureResult* captureResult = new CaptureResult();
43 
44     if (fdp.ConsumeBool()) {
45         captureResult->mMetadata = CameraMetadata();
46     }
47     if (fdp.ConsumeBool()) {
48         captureResult->mResultExtras = CaptureResultExtras();
49         captureResult->mResultExtras.errorPhysicalCameraId = fdp.ConsumeRandomLengthString();
50         captureResult->mResultExtras.isValid();
51         invokeReadWriteNullParcel<CaptureResultExtras>(&(captureResult->mResultExtras));
52     }
53     if (fdp.ConsumeBool()) {
54         size_t physicalMetadatasSize = fdp.ConsumeIntegralInRange<size_t>(kSizeMin, kSizeMax);
55         for (size_t idx = 0; idx < physicalMetadatasSize; ++idx) {
56             captureResult->mPhysicalMetadatas.push_back(PhysicalCaptureResultInfo());
57         }
58     }
59 
60     invokeReadWriteNullParcel<CaptureResult>(captureResult);
61     invokeReadWriteParcel<CaptureResult>(captureResult);
62     CaptureResult captureResult2(*captureResult);
63     CaptureResult captureResult3(std::move(captureResult2));
64 
65     delete captureResult;
66     delete physicalCaptureResultInfo;
67     return 0;
68 }
69