• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <cstddef>
17 #include <cstdint>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <iostream>
22 #include <fstream>
23 #include "avsource.h"
24 #include "meta/format.h"
25 #include "avcodec_errors.h"
26 #include "avcodec_common.h"
27 
28 #define FUZZ_PROJECT_NAME "avsource_fuzzer"
29 using namespace std;
30 using namespace OHOS::Media;
31 using namespace OHOS::MediaAVCodec;
32 
33 namespace OHOS {
34 const char *FILE_PATH = "/data/test/fuzz_create.mp4";
DoAVSourceFuncFuzz(const uint8_t * data,size_t size)35 bool DoAVSourceFuncFuzz(const uint8_t *data, size_t size)
36 {
37     std::ofstream file(FILE_PATH, std::ios::binary);
38     if (file.is_open()) {
39         file.write(reinterpret_cast<const char*>(data), size);
40         file.close();
41     }
42     int32_t fd = open(FILE_PATH, O_RDONLY);
43     if (fd < 0) {
44         return false;
45     }
46 
47     std::shared_ptr<AVSource> source = AVSourceFactory::CreateWithFD(fd, 0, size);
48     if (source == nullptr) {
49         return false;
50     }
51     Format metaFormat;
52     int ret = source->GetUserMeta(metaFormat);
53     source = nullptr;
54     close(fd);
55     if (ret != AVCS_ERR_OK) {
56         return false;
57     }
58     ret = remove(FILE_PATH);
59     if (ret != 0) {
60         return false;
61     }
62     return true;
63 }
64 } // namespace OHOS
65 
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69     /* Run your code on data */
70     OHOS::DoAVSourceFuncFuzz(data, size);
71     return 0;
72 }