• 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 <iostream>
17 #include "aw_common.h"
18 #include "string_ex.h"
19 #include "media_errors.h"
20 #include "directory_ex.h"
21 #include "window_option.h"
22 #include "image_type.h"
23 #include "avmetadatasetsource_fuzzer.h"
24 
25 using namespace std;
26 using namespace OHOS;
27 using namespace Media;
28 using namespace PlayerTestParam;
29 
30 namespace OHOS {
31 namespace Media {
AVMetadataSetSourceFuzzer()32 AVMetadataSetSourceFuzzer::AVMetadataSetSourceFuzzer()
33 {
34 }
35 
~AVMetadataSetSourceFuzzer()36 AVMetadataSetSourceFuzzer::~AVMetadataSetSourceFuzzer()
37 {
38 }
39 
FuzzAVMetadataSetSource(uint8_t * data,size_t size)40 bool AVMetadataSetSourceFuzzer::FuzzAVMetadataSetSource(uint8_t *data, size_t size)
41 {
42     constexpr int32_t USAGE_LIST = 2;
43     std::shared_ptr<AVMetadataHelper> avmetadata = AVMetadataHelperFactory::CreateAVMetadataHelper();
44     if (avmetadata == nullptr) {
45         return false;
46     }
47 
48     const string path = "/data/test/media/H264_AAC.mp4";
49     int32_t setsourcefd = open(path.c_str(), O_RDONLY);
50     if (setsourcefd < 0) {
51         (void)close(setsourcefd);
52         avmetadata->Release();
53         return false;
54     }
55 
56     struct stat64 buffer;
57     if (fstat64(setsourcefd, &buffer) != 0) {
58         (void)close(setsourcefd);
59         avmetadata->Release();
60         return false;
61     }
62     int64_t setsourcesize = static_cast<int64_t>(buffer.st_size);
63     AVMetadataUsage usage[USAGE_LIST] {AVMetadataUsage::AV_META_USAGE_META_ONLY,
64                                     AVMetadataUsage::AV_META_USAGE_PIXEL_MAP};
65     int32_t reproducibleRandom = abs((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]));
66     int32_t setsourceusage = usage[reproducibleRandom % USAGE_LIST];
67     int32_t retSetsource = avmetadata->SetSource(setsourcefd,
68         *reinterpret_cast<int64_t *>(data), setsourcesize, setsourceusage);
69     if (retSetsource != 0) {
70         (void)close(setsourcefd);
71         avmetadata->Release();
72         return true;
73     }
74     (void)close(setsourcefd);
75     std::unordered_map<int32_t, std::string> retResolvenetadata = avmetadata->ResolveMetadata();
76 
77     if (retResolvenetadata.empty()) {
78         avmetadata->Release();
79         return true;
80     }
81 
82     avmetadata->Release();
83     return true;
84 }
85 }
86 
FuzzTestAVMetadataSetSource(uint8_t * data,size_t size)87 bool FuzzTestAVMetadataSetSource(uint8_t *data, size_t size)
88 {
89     if (data == nullptr) {
90         return 0;
91     }
92 
93     if (size < sizeof(int64_t)) {
94         return 0;
95     }
96     AVMetadataSetSourceFuzzer metadata;
97     return metadata.FuzzAVMetadataSetSource(data, size);
98 }
99 }
100 
101 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)102 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
103 {
104     /* Run your code on data */
105     OHOS::FuzzTestAVMetadataSetSource(data, size);
106     return 0;
107 }