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 "transcoderservicestub_fuzzer.h"
17 #include <cmath>
18 #include <iostream>
19 #include "string_ex.h"
20 #include "directory_ex.h"
21 #include <unistd.h>
22 #include "media_server.h"
23 #include "media_parcel.h"
24 #include "i_standard_transcoder_service.h"
25 #include "stub_common.h"
26
27 using namespace std;
28 using namespace OHOS;
29 using namespace Media;
30
31 namespace OHOS {
32 namespace Media {
TranscoderServiceStubFuzzer()33 TranscoderServiceStubFuzzer::TranscoderServiceStubFuzzer()
34 {
35 }
36
~TranscoderServiceStubFuzzer()37 TranscoderServiceStubFuzzer::~TranscoderServiceStubFuzzer()
38 {
39 }
40
41 const int32_t SYSTEM_ABILITY_ID = 3002;
42 const bool RUN_ON_CREATE = false;
FuzzTranscoderOnRemoteRequest(uint8_t * data,size_t size)43 bool TranscoderServiceStubFuzzer::FuzzTranscoderOnRemoteRequest(uint8_t *data, size_t size)
44 {
45 if (data == nullptr || size < sizeof(int64_t)) {
46 return true;
47 }
48 std::shared_ptr<MediaServer> mediaServer =
49 std::make_shared<MediaServer>(SYSTEM_ABILITY_ID, RUN_ON_CREATE);
50 sptr<IRemoteObject> listener = new(std::nothrow) MediaListenerStubFuzzer();
51 sptr<IRemoteObject> transcoder = mediaServer->GetSubSystemAbility(
52 IStandardMediaService::MediaSystemAbility::MEDIA_TRANSCODER, listener);
53 if (transcoder == nullptr) {
54 return false;
55 }
56
57 sptr<IRemoteStub<IStandardTransCoderService>> transcoderStub =
58 iface_cast<IRemoteStub<IStandardTransCoderService>>(transcoder);
59 if (transcoderStub == nullptr) {
60 return false;
61 }
62
63 const int maxIpcNum = 20;
64 for (uint32_t code = 0; code <= maxIpcNum; code++) {
65 MessageParcel msg;
66 msg.WriteInterfaceToken(transcoderStub->GetDescriptor());
67 msg.WriteBuffer(data, size);
68 msg.RewindRead(0);
69 MessageParcel reply;
70 MessageOption option;
71 transcoderStub->OnRemoteRequest(code, msg, reply, option);
72 }
73
74 return true;
75 }
76 }
77
FuzzTestTranscoderOnRemoteRequest(uint8_t * data,size_t size)78 bool FuzzTestTranscoderOnRemoteRequest(uint8_t *data, size_t size)
79 {
80 if (data == nullptr) {
81 return true;
82 }
83
84 if (size < sizeof(int32_t)) {
85 return true;
86 }
87 TranscoderServiceStubFuzzer testTranscoder;
88 return testTranscoder.FuzzTranscoderOnRemoteRequest(data, size);
89 }
90 }
91
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
94 {
95 /* Run your code on data */
96 OHOS::FuzzTestTranscoderOnRemoteRequest(data, size);
97 return 0;
98 }