• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "video_output_fuzzer.h"
17 #include "camera_device.h"
18 #include "camera_log.h"
19 #include "camera_output_capability.h"
20 #include "capture_scene_const.h"
21 #include <cstdint>
22 #include "input/camera_manager.h"
23 #include "message_parcel.h"
24 #include <cstdint>
25 #include <memory>
26 #include "token_setproc.h"
27 #include "nativetoken_kit.h"
28 #include "accesstoken_kit.h"
29 #include "securec.h"
30 
31 using namespace std;
32 
33 namespace OHOS {
34 namespace CameraStandard {
35 static constexpr int32_t MAX_CODE_LEN  = 512;
36 static constexpr int32_t MIN_SIZE_NUM = 4;
37 static const uint8_t* RAW_DATA = nullptr;
38 const size_t THRESHOLD = 10;
39 static size_t g_dataSize = 0;
40 static size_t g_pos;
41 
42 /*
43 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
44 * tips: only support basic type
45 */
46 template<class T>
GetData()47 T GetData()
48 {
49     T object {};
50     size_t objectSize = sizeof(object);
51     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
52         return object;
53     }
54     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
55     if (ret != EOK) {
56         return {};
57     }
58     g_pos += objectSize;
59     return object;
60 }
61 
62 template<class T>
GetArrLength(T & arr)63 uint32_t GetArrLength(T& arr)
64 {
65     if (arr == nullptr) {
66         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
67         return 0;
68     }
69     return sizeof(arr) / sizeof(arr[0]);
70 }
71 
VideoOutputFuzzTest()72 void VideoOutputFuzzer::VideoOutputFuzzTest()
73 {
74     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
75         return;
76     }
77     auto manager = CameraManager::GetInstance();
78     auto cameras = manager->GetSupportedCameras();
79     CHECK_ERROR_RETURN_LOG(cameras.empty(), "GetSupportedCameras Error");
80     auto s = manager->CreateCaptureSession(SceneMode::VIDEO);
81     s->BeginConfig();
82     auto cap = s->GetCameraOutputCapabilities(cameras[0]);
83     CHECK_ERROR_RETURN_LOG(cap.empty(), "GetCameraOutputCapabilities Error");
84     auto vp = cap[0]->GetVideoProfiles();
85     CHECK_ERROR_RETURN_LOG(vp.empty(), "GetVideoProfiles Error");
86     sptr<Surface> surface = Surface::CreateSurfaceAsConsumer();
87     auto output = manager->CreateVideoOutput(vp[0], surface);
88     output->Start();
89     output->Stop();
90     output->Resume();
91     output->Pause();
92     output->CreateStream();
93     output->Release();
94     output->GetApplicationCallback();
95     output->GetFrameRateRange();
96     int32_t minFrameRate = GetData<int32_t>();
97     int32_t maxFrameRate = GetData<int32_t>();
98     output->SetFrameRateRange(minFrameRate, maxFrameRate);
99     output->SetOutputFormat(GetData<int32_t>());
100     output->SetFrameRate(minFrameRate, maxFrameRate);
101     output->GetSupportedFrameRates();
102     bool enabled = GetData<bool>();
103     output->enableMirror(enabled);
104     output->IsMirrorSupported();
105     output->GetSupportedVideoMetaTypes();
106     output->AttachMetaSurface(surface, VIDEO_META_MAKER_INFO);
107     int pid = GetData<int>();
108     output->CameraServerDied(pid);
109     output->canSetFrameRateRange(minFrameRate, maxFrameRate);
110     output->GetVideoRotation(GetData<int32_t>());
111     output->IsAutoDeferredVideoEnhancementSupported();
112     output->IsAutoDeferredVideoEnhancementEnabled();
113     output->EnableAutoDeferredVideoEnhancement(enabled);
114     output->IsVideoStarted();
115     output->IsRotationSupported(enabled);
116     output->SetRotation(GetData<int32_t>());
117     output->IsAutoVideoFrameRateSupported();
118     output->EnableAutoVideoFrameRate(enabled);
119     std::vector<int32_t> supportedRotations;
120     output->GetSupportedRotations(supportedRotations);
121 }
122 
Test()123 void Test()
124 {
125     auto videoOutput = std::make_unique<VideoOutputFuzzer>();
126     if (videoOutput == nullptr) {
127         MEDIA_INFO_LOG("videoPostProcessor is null");
128         return;
129     }
130     videoOutput->VideoOutputFuzzTest();
131 }
132 
133 typedef void (*TestFuncs[1])();
134 
135 TestFuncs g_testFuncs = {
136     Test,
137 };
138 
FuzzTest(const uint8_t * rawData,size_t size)139 bool FuzzTest(const uint8_t* rawData, size_t size)
140 {
141     // initialize data
142     RAW_DATA = rawData;
143     g_dataSize = size;
144     g_pos = 0;
145 
146     uint32_t code = GetData<uint32_t>();
147     uint32_t len = GetArrLength(g_testFuncs);
148     if (len > 0) {
149         g_testFuncs[code % len]();
150     } else {
151         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
152     }
153 
154     return true;
155 }
156 } // namespace CameraStandard
157 } // namespace OHOS
158 
159 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)160 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
161 {
162     if (size < OHOS::CameraStandard::THRESHOLD) {
163         return 0;
164     }
165 
166     OHOS::CameraStandard::FuzzTest(data, size);
167     return 0;
168 }