1 /*
2 * Copyright (c) 2024-2025 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 "verifymanagerhost_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "verify_manager_stub.h"
22 #include "securec.h"
23
24 using namespace OHOS::AppExecFwk;
25 namespace OHOS {
26 constexpr size_t U32_AT_SIZE = 4;
27 constexpr size_t MESSAGE_SIZE = 3;
28 constexpr size_t DCAMERA_SHIFT_24 = 24;
29 constexpr size_t DCAMERA_SHIFT_16 = 16;
30 constexpr size_t DCAMERA_SHIFT_8 = 8;
31
GetU32Data(const char * ptr)32 uint32_t GetU32Data(const char* ptr)
33 {
34 return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | (ptr[3]);
35 }
36 class MockVerifyManagerStub : public VerifyManagerStub {
37 public:
CallbackEnter(uint32_t code)38 int32_t CallbackEnter(uint32_t code) override
39 {
40 return 0;
41 }
42
CallbackExit(uint32_t code,int32_t result)43 int32_t CallbackExit(uint32_t code, int32_t result) override
44 {
45 return 0;
46 }
47
Verify(const std::vector<std::string> & abcPaths,int32_t & funcResult)48 ErrCode Verify(const std::vector<std::string>& abcPaths, int32_t& funcResult) override
49 {
50 return ERR_OK;
51 }
52
DeleteAbc(const std::string & path,int32_t & funcResult)53 ErrCode DeleteAbc(const std::string& path, int32_t& funcResult) override
54 {
55 return ERR_OK;
56 }
57 };
58
DoSomethingInterestingWithMyAPI(const char * data,size_t size)59 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
60 {
61 uint32_t code = (GetU32Data(data) % MESSAGE_SIZE);
62 MessageParcel datas;
63 std::u16string descriptor = MockVerifyManagerStub::GetDescriptor();
64 datas.WriteInterfaceToken(descriptor);
65 datas.WriteBuffer(data, size);
66 datas.RewindRead(0);
67 MessageParcel reply;
68 MessageOption option;
69 MockVerifyManagerStub verifyManagerStub;
70 verifyManagerStub.OnRemoteRequest(code, datas, reply, option);
71 return true;
72 }
73 }
74
75 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78 /* Run your code on data */
79 if (data == nullptr) {
80 return 0;
81 }
82
83 if (size < OHOS::U32_AT_SIZE) {
84 return 0;
85 }
86
87 char* ch = static_cast<char*>(malloc(size + 1));
88 if (ch == nullptr) {
89 return 0;
90 }
91
92 (void)memset_s(ch, size + 1, 0x00, size + 1);
93 if (memcpy_s(ch, size, data, size) != EOK) {
94 free(ch);
95 ch = nullptr;
96 return 0;
97 }
98 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
99 free(ch);
100 ch = nullptr;
101 return 0;
102 }