1 /*
2  * Copyright (c) 2023 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 <cstdio>
17 #include <cstdlib>
18 #include <unistd.h>
19 #include "hdf_base.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23     int32_t AudioEffectGetConfigDescriptor(const char *path, struct ConfigDescriptor **cfgDesc);
24     void AudioEffectReleaseCfgDesc(struct ConfigDescriptor *cfgDesc);
25 }
26 #endif
27 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)28 static bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
29 {
30     if (rawData == NULL) {
31         return false;
32     }
33 
34     // Create a temporary file with the data
35     char tmpfile[] = "/tmp/fuzz-XXXXXX";
36     int fd = mkstemp(tmpfile);
37     if (fd < 0) {
38         return 0;
39     }
40     write(fd, rawData, size);
41     close(fd);
42 
43     struct ConfigDescriptor *cfgDesc;
44     // Call the interface with the temporary file
45     int ret = AudioEffectGetConfigDescriptor(tmpfile, &cfgDesc);
46     if (ret != HDF_SUCCESS) {
47         return false;
48     }
49     AudioEffectReleaseCfgDesc(cfgDesc);
50 
51     // Remove the temporary file
52     remove(tmpfile);
53     return true;
54 }
55 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
57 {
58     DoSomethingInterestingWithMyAPI(data, size);
59 
60     return 0;
61 }
62