• 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 
16 #include <cstddef>
17 #include <cstdint>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <fuzzer/FuzzedDataProvider.h>
22 #include <iostream>
23 #include "parser_sample.h"
24 
25 #define FUZZ_PROJECT_NAME "demuxer_fuzzer"
26 using namespace std;
27 using namespace OHOS::Media;
28 using namespace OHOS::MediaAVCodec;
29 namespace OHOS {
30 const char *MP4_PATH = "/data/test/fuzz_create.mp4";
31 const int64_t EXPECT_SIZE = 7;
32 const int64_t STRIDE = 7;
33 
DoReferenceParserWithDemuxerAPI(const uint8_t * data,size_t size)34 bool DoReferenceParserWithDemuxerAPI(const uint8_t *data, size_t size)
35 {
36     if (size < sizeof(int64_t)) {
37         return false;
38     }
39     int32_t fd = open(MP4_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
40     if (fd < 0) {
41         return false;
42     }
43     int len = write(fd, data, size);
44     if (len <= EXPECT_SIZE) {
45         close(fd);
46         return false;
47     }
48     close(fd);
49     FuzzedDataProvider fdp(data, size);
50     int64_t pts = fdp.ConsumeIntegral<int64_t>();
51     int64_t ptsForPtsIndex = fdp.ConsumeIntegral<int64_t>();
52     int64_t frameIndex = fdp.ConsumeIntegral<int64_t>();
53     uint8_t *dataConver = const_cast<uint8_t *>(data);
54     uint32_t *createSize = reinterpret_cast<uint32_t *>(dataConver + size - STRIDE);
55     shared_ptr<ParserSample> parserSample = make_shared<ParserSample>();
56     parserSample->filePath = MP4_PATH;
57     parserSample->RunReferenceParser(pts, ptsForPtsIndex, frameIndex, *createSize);
58     int ret = remove(MP4_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::DoReferenceParserWithDemuxerAPI(data, size);
71     return 0;
72 }
73