1 /*
2 * Copyright (c) 2023 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 "codeccreatecomponent_fuzzer.h"
17 #include "codeccommon_fuzzer.h"
18
19 using namespace OHOS::HDI::Codec::V3_0;
20
21 namespace OHOS {
22 namespace Codec {
CodecCreateComponent(const uint8_t * data,size_t size)23 bool CodecCreateComponent(const uint8_t *data, size_t size)
24 {
25 if (data == nullptr) {
26 return false;
27 }
28
29 OHOS::sptr<OHOS::HDI::Codec::V3_0::ICodecComponent> client_ = nullptr;
30 OHOS::sptr<OHOS::HDI::Codec::V3_0::ICodecCallback> callback_ = nullptr;
31 OHOS::sptr<OHOS::HDI::Codec::V3_0::ICodecComponentManager> omxMgr_ = nullptr;
32 uint32_t g_componentId = 0;
33
34 omxMgr_ = ICodecComponentManager::Get(true);
35 if (omxMgr_ == nullptr) {
36 HDF_LOGE("%{public}s: ICodecComponentManager failed\n", __func__);
37 return false;
38 }
39
40 int32_t count = 0;
41 auto err = omxMgr_->GetComponentNum(count);
42 if (err != HDF_SUCCESS || count <= 0) {
43 HDF_LOGE("%{public}s GetComponentNum return %{public}d, count = %{public}d", __func__, err, count);
44 return false;
45 }
46
47 std::vector<CodecCompCapability> caps;
48 err = omxMgr_->GetComponentCapabilityList(caps, count);
49 if (err != HDF_SUCCESS) {
50 HDF_LOGE("%{public}s GetComponentCapabilityList return %{public}d", __func__, err);
51 return false;
52 }
53
54 callback_ = new CodecCallbackFuzz();
55 int32_t ret = omxMgr_->CreateComponent(client_, g_componentId, caps[0].compName,
56 static_cast<int64_t >(*data), callback_);
57 if (ret != HDF_SUCCESS) {
58 HDF_LOGE("%{public}s: CreateComponent failed\n", __func__);
59 return false;
60 }
61
62 int32_t result = omxMgr_->DestroyComponent(g_componentId);
63 if (result != HDF_SUCCESS) {
64 HDF_LOGE("%{public}s: DestroyComponent failed\n", __func__);
65 return false;
66 }
67
68 return true;
69 }
70 } // namespace codec
71 } // namespace OHOS
72
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
74 {
75 OHOS::Codec::CodecCreateComponent(data, size);
76 return 0;
77 }
78