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