• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "openpandafile_fuzzer.h"
17 #include "libpandafile/file.h"
18 #include "libziparchive/zip_archive.h"
19 
20 namespace OHOS {
OpenPandaFileFuzzTest(const uint8_t * data,size_t size)21 void OpenPandaFileFuzzTest(const uint8_t *data, size_t size)
22 {
23     // Create zip file
24     const char *filename1 = panda::panda_file::ARCHIVE_FILENAME;
25     const char *filename2 = "classes1.abc";
26 
27     const char *zip_filename1 = "__OpenPandaFileFuzzTest.zip";
28     int ret1 =
29         panda::CreateOrAddFileIntoZip(zip_filename1, filename1, data, size, APPEND_STATUS_CREATE, Z_BEST_COMPRESSION);
30     int ret2 =
31         panda::CreateOrAddFileIntoZip(zip_filename1, filename2, data, size, APPEND_STATUS_ADDINZIP, Z_BEST_COMPRESSION);
32     if (ret1 != 0 || ret2 != 0) {
33         (void)remove(zip_filename1);
34         return;
35     }
36 
37     const char *zip_filename2 = "__OpenPandaFileFromZipNameAnonMem.zip";
38     int ret3 =
39         panda::CreateOrAddFileIntoZip(zip_filename2, filename1, data, size, APPEND_STATUS_CREATE, Z_BEST_COMPRESSION);
40     if (ret3 != 0) {
41         (void)remove(zip_filename2);
42         return;
43     }
44 
45     // Call OpenPandaFile
46     {
47         panda::panda_file::OpenPandaFile(zip_filename1);
48         panda::panda_file::OpenPandaFile(zip_filename2);
49     }
50     (void)remove(zip_filename1);
51     (void)remove(zip_filename2);
52 }
53 }  // namespace OHOS
54 
55 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
57 {
58     /* Run your code on data */
59     OHOS::OpenPandaFileFuzzTest(data, size);
60     return 0;
61 }
62