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 #include "fileinfosharedmemory_fuzzer.h"
16
17 #include <cstring>
18 #include <memory>
19
20 #include "file_info_shared_memory.h"
21
22 namespace OHOS {
23 using namespace std;
24 using namespace FileAccessFwk;
25
26 template<class T>
TypeCast(const uint8_t * data,int * pos=nullptr)27 T TypeCast(const uint8_t *data, int *pos = nullptr)
28 {
29 if (pos) {
30 *pos += sizeof(T);
31 }
32 return *(reinterpret_cast<const T*>(data));
33 }
34
MarshallingFuzzTest(shared_ptr<SharedMemoryInfo> info,const uint8_t * data,size_t size)35 bool MarshallingFuzzTest(shared_ptr<SharedMemoryInfo> info, const uint8_t *data, size_t size)
36 {
37 if (data == nullptr || size < sizeof(int) + sizeof(uint64_t)) {
38 return true;
39 }
40
41 int pos = 0;
42 MessageParcel reply;
43 info->memFd = TypeCast<int>(data, &pos);
44 info->memSize = TypeCast<uint64_t>(data + pos);
45 info->Marshalling(reply);
46
47 return true;
48 }
49
UnmarshallingFuzzTest(shared_ptr<SharedMemoryInfo> info,const uint8_t * data,size_t size)50 bool UnmarshallingFuzzTest(shared_ptr<SharedMemoryInfo> info, const uint8_t *data, size_t size)
51 {
52 MessageParcel reply;
53 info->Unmarshalling(reply);
54
55 return true;
56 }
57
CreateSharedMemoryFuzzTest(const uint8_t * data,size_t size)58 bool CreateSharedMemoryFuzzTest(const uint8_t *data, size_t size)
59 {
60 if (data == nullptr || size < sizeof(uint64_t)) {
61 return true;
62 }
63
64 int pos = 0;
65 uint64_t memSize = TypeCast<uint64_t>(data, &pos);
66 string memName(reinterpret_cast<const char*>(data + pos), size - pos);
67 SharedMemoryInfo memInfo;
68 SharedMemoryOperation::CreateSharedMemory(memName.c_str(), memSize, memInfo);
69 SharedMemoryOperation::DestroySharedMemory(memInfo);
70
71 return true;
72 }
73
ExpandSharedMemoryFuzzTest(const uint8_t * data,size_t size)74 bool ExpandSharedMemoryFuzzTest(const uint8_t *data, size_t size)
75 {
76 if (data == nullptr || size < sizeof(uint64_t)) {
77 return true;
78 }
79
80 int pos = 0;
81 uint64_t memSize = TypeCast<uint64_t>(data, &pos);
82 string memName(reinterpret_cast<const char*>(data + pos), size - pos);
83 SharedMemoryInfo memInfo;
84 SharedMemoryOperation::CreateSharedMemory(memName.c_str(), memSize, memInfo);
85 SharedMemoryOperation::ExpandSharedMemory(memInfo);
86 SharedMemoryOperation::DestroySharedMemory(memInfo);
87
88 return true;
89 }
90
WriteFileInfosFuzzTest(const uint8_t * data,size_t size)91 bool WriteFileInfosFuzzTest(const uint8_t *data, size_t size)
92 {
93 FileInfo info;
94 vector<FileInfo> fileInfoVec;
95 fileInfoVec.emplace_back();
96 SharedMemoryInfo memInfo;
97 SharedMemoryOperation::WriteFileInfos(fileInfoVec, memInfo);
98 SharedMemoryOperation::ReadFileInfo(info, memInfo);
99
100 return true;
101 }
102
103 } // namespace OHOS
104
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
107 {
108 auto sharedMemoryInfo = std::make_shared<OHOS::FileAccessFwk::SharedMemoryInfo>();
109 if (sharedMemoryInfo == nullptr) {
110 return 0;
111 }
112
113 OHOS::MarshallingFuzzTest(sharedMemoryInfo, data, size);
114 OHOS::UnmarshallingFuzzTest(sharedMemoryInfo, data, size);
115
116 OHOS::CreateSharedMemoryFuzzTest(data, size);
117 OHOS::ExpandSharedMemoryFuzzTest(data, size);
118 OHOS::WriteFileInfosFuzzTest(data, size);
119
120 return 0;
121 }
122