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 <camera2/OutputConfiguration.h>
18 #include <camera2/SessionConfiguration.h>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #include <gui/IGraphicBufferProducer.h>
21 #include <gui/Surface.h>
22 #include <gui/SurfaceComposerClient.h>
23 #include "camera2common.h"
24
25 using namespace std;
26 using namespace android;
27 using namespace android::hardware::camera2::params;
28
29 constexpr int8_t kMaxLoopIterations = 100;
30 constexpr int32_t kSizeMin = 0;
31 constexpr int32_t kSizeMax = 1000;
32
33 class C2OutputConfigurationFuzzer {
34 public:
35 void process(const uint8_t* data, size_t size);
36
37 private:
38 void invokeC2OutputConfigFuzzer();
39 unique_ptr<OutputConfiguration> getC2OutputConfig();
40 sp<IGraphicBufferProducer> createIGraphicBufferProducer();
41 FuzzedDataProvider* mFDP = nullptr;
42 };
43
createIGraphicBufferProducer()44 sp<IGraphicBufferProducer> C2OutputConfigurationFuzzer::createIGraphicBufferProducer() {
45 sp<SurfaceComposerClient> composerClient = new SurfaceComposerClient;
46 sp<SurfaceControl> surfaceControl = composerClient->createSurface(
47 static_cast<String8>(mFDP->ConsumeRandomLengthString(kMaxBytes).c_str()) /* name */,
48 mFDP->ConsumeIntegral<uint32_t>() /* width */,
49 mFDP->ConsumeIntegral<uint32_t>() /* height */,
50 mFDP->ConsumeIntegral<int32_t>() /* format */,
51 mFDP->ConsumeIntegral<int32_t>() /* flags */);
52 if (surfaceControl) {
53 sp<Surface> surface = surfaceControl->getSurface();
54 return surface->getIGraphicBufferProducer();
55 } else {
56 sp<IGraphicBufferProducer> gbp;
57 return gbp;
58 }
59 }
60
getC2OutputConfig()61 unique_ptr<OutputConfiguration> C2OutputConfigurationFuzzer::getC2OutputConfig() {
62 unique_ptr<OutputConfiguration> outputConfiguration = nullptr;
63 auto selectOutputConfigurationConstructor =
64 mFDP->PickValueInArray<const std::function<void()>>({
65 [&]() { outputConfiguration = make_unique<OutputConfiguration>(); },
66
67 [&]() {
68 int32_t rotation = mFDP->ConsumeIntegral<int32_t>();
69 string physicalCameraId = mFDP->ConsumeRandomLengthString(kMaxBytes);
70 int32_t surfaceSetID = mFDP->ConsumeIntegral<int32_t>();
71 bool isShared = mFDP->ConsumeBool();
72 sp<IGraphicBufferProducer> iGBP = createIGraphicBufferProducer();
73 outputConfiguration = make_unique<OutputConfiguration>(
74 iGBP, rotation, physicalCameraId, surfaceSetID, isShared);
75 },
76
77 [&]() {
78 int32_t rotation = mFDP->ConsumeIntegral<int32_t>();
79 string physicalCameraId = mFDP->ConsumeRandomLengthString(kMaxBytes);
80 int32_t surfaceSetID = mFDP->ConsumeIntegral<int32_t>();
81 bool isShared = mFDP->ConsumeBool();
82 size_t iGBPSize = mFDP->ConsumeIntegralInRange<size_t>(kSizeMin, kSizeMax);
83 vector<sp<IGraphicBufferProducer>> iGBPs;
84 for (size_t idx = 0; idx < iGBPSize; ++idx) {
85 sp<IGraphicBufferProducer> iGBP = createIGraphicBufferProducer();
86 iGBPs.push_back(iGBP);
87 }
88 outputConfiguration = make_unique<OutputConfiguration>(
89 iGBPs, rotation, physicalCameraId, surfaceSetID, isShared);
90 },
91 });
92 selectOutputConfigurationConstructor();
93 return outputConfiguration;
94 }
95
invokeC2OutputConfigFuzzer()96 void C2OutputConfigurationFuzzer::invokeC2OutputConfigFuzzer() {
97 unique_ptr<OutputConfiguration> outputConfiguration = getC2OutputConfig();
98 int8_t count = kMaxLoopIterations;
99 while (--count > 0) {
100 unique_ptr<OutputConfiguration> outputConfiguration2 = getC2OutputConfig();
101 auto callC2OutputConfAPIs = mFDP->PickValueInArray<const std::function<void()>>({
102 [&]() { outputConfiguration->getRotation(); },
103 [&]() { outputConfiguration->getSurfaceSetID(); },
104 [&]() { outputConfiguration->getSurfaceType(); },
105 [&]() { outputConfiguration->getWidth(); },
106 [&]() { outputConfiguration->getHeight(); },
107 [&]() { outputConfiguration->isDeferred(); },
108 [&]() { outputConfiguration->isShared(); },
109 [&]() { outputConfiguration->getPhysicalCameraId(); },
110 [&]() { outputConfiguration->gbpsEqual(*outputConfiguration2); },
111 [&]() { outputConfiguration->sensorPixelModesUsedEqual(*outputConfiguration2); },
112 [&]() { outputConfiguration->gbpsLessThan(*outputConfiguration2); },
113 [&]() { outputConfiguration->sensorPixelModesUsedLessThan(*outputConfiguration2); },
114 [&]() { outputConfiguration->getGraphicBufferProducers(); },
115 [&]() {
116 sp<IGraphicBufferProducer> gbp = createIGraphicBufferProducer();
117 outputConfiguration->addGraphicProducer(gbp);
118 },
119 [&]() { outputConfiguration->isMultiResolution(); },
120 [&]() { outputConfiguration->getColorSpace(); },
121 [&]() { outputConfiguration->getStreamUseCase(); },
122 [&]() { outputConfiguration->getTimestampBase(); },
123 [&]() { outputConfiguration->getMirrorMode(); },
124 [&]() { outputConfiguration->useReadoutTimestamp(); },
125 });
126 callC2OutputConfAPIs();
127 }
128 // Not keeping invokeReadWrite() APIs in while loop to avoid possible OOM.
129 invokeReadWriteNullParcel<OutputConfiguration>(outputConfiguration.get());
130 if (mFDP->ConsumeBool()) {
131 invokeReadWriteParcel<OutputConfiguration>(outputConfiguration.get());
132 } else {
133 invokeNewReadWriteParcel<OutputConfiguration>(outputConfiguration.get(), *mFDP);
134 }
135 }
136
process(const uint8_t * data,size_t size)137 void C2OutputConfigurationFuzzer::process(const uint8_t* data, size_t size) {
138 mFDP = new FuzzedDataProvider(data, size);
139 invokeC2OutputConfigFuzzer();
140 delete mFDP;
141 }
142
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)143 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
144 C2OutputConfigurationFuzzer c2OutputConfigurationFuzzer;
145 c2OutputConfigurationFuzzer.process(data, size);
146 return 0;
147 }
148