1 /*
2 * Copyright (C) 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 <array>
18 #include <vector>
19
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "common/DepthPhotoProcessor.h"
23
24 using namespace android;
25 using namespace android::camera3;
26
27 static const size_t kTestBufferWidth = 640;
28 static const size_t kTestBufferHeight = 480;
29 static const size_t kTestBufferDepthSize (kTestBufferWidth * kTestBufferHeight);
30
generateDepth16Buffer(const uint8_t * data,size_t size,std::array<uint16_t,kTestBufferDepthSize> * depth16Buffer)31 void generateDepth16Buffer(const uint8_t* data, size_t size, std::array<uint16_t, kTestBufferDepthSize> *depth16Buffer /*out*/) {
32 FuzzedDataProvider dataProvider(data, size);
33 for (size_t i = 0; i < depth16Buffer->size(); i++) {
34 (*depth16Buffer)[i] = dataProvider.ConsumeIntegral<uint16_t>();
35 }
36 }
37
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
39 DepthPhotoInputFrame inputFrame;
40 // Worst case both depth and confidence maps have the same size as the main color image.
41 inputFrame.mMaxJpegSize = inputFrame.mMainJpegSize * 3;
42
43 std::vector<uint8_t> depthPhotoBuffer(inputFrame.mMaxJpegSize);
44 size_t actualDepthPhotoSize = 0;
45
46 std::array<uint16_t, kTestBufferDepthSize> depth16Buffer;
47 generateDepth16Buffer(data, size, &depth16Buffer);
48
49 inputFrame.mMainJpegBuffer = reinterpret_cast<const char*> (data);
50 inputFrame.mMainJpegSize = size;
51 inputFrame.mDepthMapBuffer = depth16Buffer.data();
52 inputFrame.mDepthMapStride = kTestBufferWidth;
53 inputFrame.mDepthMapWidth = kTestBufferWidth;
54 inputFrame.mDepthMapHeight = kTestBufferHeight;
55 processDepthPhotoFrame(
56 inputFrame,
57 depthPhotoBuffer.size(),
58 depthPhotoBuffer.data(),
59 &actualDepthPhotoSize);
60
61 return 0;
62 }
63