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 "codec_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include "hdf_log.h"
20 #include "v2_0/codec_component_manager_stub.h"
21
22 using namespace OHOS::HDI::Codec::V2_0;
23
24 namespace OHOS {
25 constexpr size_t THRESHOLD = 10;
26 constexpr int32_t OFFSET = 4;
27 const std::u16string CODEC_INTERFACE_TOKEN = u"ohos.hdi.codec.V2_0.ICodecComponentManager";
28 #define CMD_CODEC_COMPONENT_MANAGER_GREATE_COMPONENT 3
29
Convert2Uint32(const uint8_t * ptr)30 uint32_t Convert2Uint32(const uint8_t* ptr)
31 {
32 if (ptr == nullptr) {
33 return 0;
34 }
35 /*
36 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
37 * and the third digit no left
38 */
39 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
40 }
41
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)42 bool DoSomethingInterestingWithMyAPI(const uint8_t* rawData, size_t size)
43 {
44 if (rawData == nullptr) {
45 return false;
46 }
47 uint32_t code = Convert2Uint32(rawData);
48 rawData = rawData + OFFSET;
49 size = size - OFFSET;
50
51 MessageParcel data;
52 data.WriteInterfaceToken(CODEC_INTERFACE_TOKEN);
53 data.WriteBuffer(rawData, size);
54 data.RewindRead(0);
55 MessageParcel reply;
56 MessageOption option;
57 sptr<ICodecComponentManager> g_codecComponentManager = ICodecComponentManager::Get(true);
58 if (g_codecComponentManager == nullptr) {
59 HDF_LOGE("%{public}s:ICodecComponentManager::Get failed.", __func__);
60 return false;
61 }
62 sptr<CodecComponentManagerStub> codecComponentManager = new CodecComponentManagerStub(g_codecComponentManager);
63 if (codecComponentManager == nullptr) {
64 HDF_LOGE("%{public}s:new codecComponentManager failed.", __func__);
65 return false;
66 }
67 int32_t ret = codecComponentManager->OnRemoteRequest(code, data, reply, option);
68 if (ret != HDF_SUCCESS) {
69 return false;
70 }
71
72 if (code == CMD_CODEC_COMPONENT_MANAGER_GREATE_COMPONENT) {
73 uint32_t componentId = 0;
74 if (!reply.ReadUint32(componentId)) {
75 HDF_LOGE("%{public}s:read componentId failed!", __func__);
76 return false;
77 }
78 ret = g_codecComponentManager->DestroyComponent(componentId);
79 if (ret != HDF_SUCCESS) {
80 HDF_LOGE("%{public}s: DestroyComponent failed\n", __func__);
81 return false;
82 }
83 }
84
85 return true;
86 }
87 }
88
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
91 {
92 if (size < OHOS::THRESHOLD) {
93 return 0;
94 }
95
96 /* Run your code on data */
97 OHOS::DoSomethingInterestingWithMyAPI(data, size);
98 return 0;
99 }
100
101