• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (data == nullptr || size < sizeof(uint64_t)) {
53         return true;
54     }
55 
56     MessageParcel reply;
57     reply.WriteUint64(*reinterpret_cast<const uint64_t*>(data));
58     info->Unmarshalling(reply);
59 
60     return true;
61 }
62 
CreateSharedMemoryFuzzTest(const uint8_t * data,size_t size)63 bool CreateSharedMemoryFuzzTest(const uint8_t *data, size_t size)
64 {
65     if (data == nullptr || size < sizeof(uint64_t)) {
66         return true;
67     }
68 
69     int pos = 0;
70     uint64_t memSize = TypeCast<uint64_t>(data, &pos);
71     string memName(reinterpret_cast<const char*>(data + pos), size - pos);
72     SharedMemoryInfo memInfo;
73     SharedMemoryOperation::CreateSharedMemory(memName.c_str(), memSize, memInfo);
74     SharedMemoryOperation::DestroySharedMemory(memInfo);
75 
76     return true;
77 }
78 
ExpandSharedMemoryFuzzTest(const uint8_t * data,size_t size)79 bool ExpandSharedMemoryFuzzTest(const uint8_t *data, size_t size)
80 {
81     if (data == nullptr || size < sizeof(uint64_t)) {
82         return true;
83     }
84 
85     int pos = 0;
86     uint64_t memSize = TypeCast<uint64_t>(data, &pos);
87     string memName(reinterpret_cast<const char*>(data + pos), size - pos);
88     SharedMemoryInfo memInfo;
89     SharedMemoryOperation::CreateSharedMemory(memName.c_str(), memSize, memInfo);
90     SharedMemoryOperation::ExpandSharedMemory(memInfo);
91     SharedMemoryOperation::DestroySharedMemory(memInfo);
92 
93     return true;
94 }
95 
WriteFileInfosFuzzTest(const uint8_t * data,size_t size)96 bool WriteFileInfosFuzzTest(const uint8_t *data, size_t size)
97 {
98     if (data == nullptr || size < sizeof(int32_t) + sizeof(int64_t) + sizeof(int64_t)) {
99         return true;
100     }
101 
102     int pos = 0;
103     int32_t mode = TypeCast<int32_t>(data, &pos);
104     int64_t mtime = TypeCast<int64_t>(data + pos, &pos);
105     int len = (size - pos) >> 2;
106     std::string uri(std::string(*reinterpret_cast<const char*>(data + pos), len));
107     std::string relativePath(*reinterpret_cast<const char*>(data + pos + len), len);
108     std::string fileName(*reinterpret_cast<const char*>(data + pos + len + len), len);
109     std::string mimeType(*reinterpret_cast<const char*>(data + pos + len + len + len), len);
110 
111     FileInfo info(uri, relativePath, fileName, mode, mimeType);
112     info.mtime = mtime;
113     vector<FileInfo> fileInfoVec;
114     fileInfoVec.emplace_back(info);
115 
116     SharedMemoryInfo memInfo;
117     SharedMemoryOperation::WriteFileInfos(fileInfoVec, memInfo);
118     SharedMemoryOperation::ReadFileInfo(info, memInfo);
119     return true;
120 }
121 
122 } // namespace OHOS
123 
124 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)125 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
126 {
127     auto sharedMemoryInfo = std::make_shared<OHOS::FileAccessFwk::SharedMemoryInfo>();
128     if (sharedMemoryInfo == nullptr) {
129         return 0;
130     }
131 
132     OHOS::MarshallingFuzzTest(sharedMemoryInfo, data, size);
133     OHOS::UnmarshallingFuzzTest(sharedMemoryInfo, data, size);
134 
135     OHOS::CreateSharedMemoryFuzzTest(data, size);
136     OHOS::ExpandSharedMemoryFuzzTest(data, size);
137     OHOS::WriteFileInfosFuzzTest(data, size);
138 
139     return 0;
140 }
141