1 /*
2 * Copyright (c) 2022-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 <cstddef>
17 #include <cstdint>
18 #include <iostream>
19 #include "hdf_log.h"
20 #include "codec_fuzzer.h"
21 #include "codec_component_manager_service.h"
22
23 using namespace OHOS;
24
dlclose(void * handle)25 extern "C" __attribute__((visibility("default"))) int dlclose(void* handle)
26 {
27 return 0;
28 }
29
30 namespace OHOS {
31 constexpr size_t THRESHOLD = 10;
32 constexpr uint32_t OFFSET = 4;
33 const std::u16string CODEC_INTERFACE_TOKEN = u"ohos.hdi.codec_service";
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
CodecFuzzTest(const uint8_t * rawData,size_t size)47 bool CodecFuzzTest(const uint8_t* rawData, size_t size)
48 {
49 if (rawData == nullptr || size < OFFSET) {
50 HDF_LOGE("%{public}s: Failed to obtain rawData", __func__);
51 return false;
52 }
53 uint32_t code = Convert2Uint32(rawData);
54 rawData = rawData + OFFSET;
55 size = size - OFFSET;
56
57 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
58 if (data == nullptr) {
59 HDF_LOGE("%{public}s: Failed to obtain data", __func__);
60 return false;
61 }
62
63 HdfSbufWriteBuffer(data, CODEC_INTERFACE_TOKEN.c_str(), CODEC_INTERFACE_TOKEN.length());
64 HdfSbufWriteBuffer(data, rawData, size);
65
66 struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
67 if (reply == nullptr) {
68 HDF_LOGE("%{public}s: Failed to obtain reply", __func__);
69 HdfSbufRecycle(data);
70 return false;
71 }
72
73 CodecComponentManagerSerivce *service = nullptr;
74
75 service = CodecComponentManagerSerivceGet();
76 if (service == nullptr) {
77 HDF_LOGE("%{public}s:CodecComponentManagerSerivceGet failed.", __func__);
78 HdfSbufRecycle(data);
79 HdfSbufRecycle(reply);
80 return false;
81 }
82
83 service->stub.OnRemoteRequest((struct CodecComponentManager *)(&service->stub.interface), code, data, reply);
84
85 OmxComponentManagerSeriveRelease(service);
86 HdfSbufRecycle(data);
87 HdfSbufRecycle(reply);
88
89 return true;
90 }
91 }
92
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
94 {
95 if (size < OHOS::THRESHOLD) {
96 return 0;
97 }
98
99 OHOS::CodecFuzzTest(data, size);
100 return 0;
101 }
102