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 #include <cstddef>
16 #include <cstdint>
17 #include "mem_mgr_service.h"
18 #include "memmgrstub_fuzzer.h"
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace Memory {
23 #define MEMMGRSTUB_CODE_MIN 1
24 #define MEMMGRSTUB_CODE_MAX 10
25 #define MEMMGRSTUB_CODE_MIN_PURGEABLE 4
26 #define MEMMGRSTUB_CODE_MAX_PURGEABLE 9
27
28 const uint8_t *g_baseFuzzData = nullptr;
29 size_t g_baseFuzzSize = 0;
30 size_t g_baseFuzzPos;
31
GetData()32 template <class T> T GetData()
33 {
34 T object{};
35 size_t objectSize = sizeof(object);
36 if (g_baseFuzzData == nullptr || g_baseFuzzSize > g_baseFuzzSize - g_baseFuzzPos) {
37 return object;
38 }
39 errno_t ret = memcpy_s(&object, objectSize, g_baseFuzzData + g_baseFuzzPos, objectSize);
40 if (ret != EOK) {
41 return {};
42 }
43 g_baseFuzzPos += objectSize;
44 return object;
45 }
46
HandleGetBunldePriorityListFuzzTest(const uint8_t * data,size_t size)47 bool HandleGetBunldePriorityListFuzzTest(const uint8_t *data, size_t size)
48 {
49 g_baseFuzzData = data;
50 g_baseFuzzSize = size;
51 g_baseFuzzPos = 0;
52 if (size > sizeof(int) + sizeof(int)) {
53 MessageParcel data1 = MessageParcel();
54 Parcel parcel;
55 sptr<IRemoteObject> iremoteobject = IRemoteObject::Unmarshalling(parcel);
56 int intdata = GetData<int>();
57 void *voiddata = &intdata;
58 size_t size1 = sizeof(int);
59 data1.WriteRemoteObject(iremoteobject);
60 data1.WriteRawData(voiddata, size1);
61 data1.ReadRawData(size1);
62 MessageParcel reply = MessageParcel();
63 MessageOption option;
64 uint32_t code = GetData<int>() % (MEMMGRSTUB_CODE_MAX - MEMMGRSTUB_CODE_MIN + 1) + MEMMGRSTUB_CODE_MIN;
65 #ifndef USE_PURGEABLE_MEMORY
66 if (code <= MEMMGRSTUB_CODE_MAX_PURGEABLE || code >= MEMMGRSTUB_CODE_MIN_PURGEABLE) {
67 return false;
68 }
69 #endif
70 MemMgrService::GetInstance().OnRemoteRequest(code, data1, reply, option);
71 }
72 return true;
73 }
74 } // namespace Memory
75 } // namespace OHOS
76
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
79 {
80 /* Run your code on data */
81 OHOS::Memory::HandleGetBunldePriorityListFuzzTest(data, size);
82 return 0;
83 }
84