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 "ecmascript/ohos/aot_runtime_info.h"
17#include "setruntimeinfo_fuzzer.h"
18#include <fuzzer/FuzzedDataProvider.h>
19
20using namespace panda::ecmascript::ohos;
21
22namespace OHOS {
23
24class AotRuntimeInfoTest : public AotRuntimeInfo {
25public:
26    void TestSetRuntimeInfo(const char *realOutPath, char lines[][BUFFER_SIZE], int length) const
27    {
28        SetRuntimeInfo(realOutPath, lines, length);
29    }
30
31    void TestGetRuntimeInfoByPath(char lines[][BUFFER_SIZE], const char *realOutPath, const char *soBuildId) const
32    {
33        GetRuntimeInfoByPath(lines, realOutPath, soBuildId);
34    }
35
36    virtual bool TestGetRuntimeBuildId(char *buildId, int length) const
37    {
38        return GetRuntimeBuildId(buildId, length);
39    }
40};
41
42    void SetRuntimeInfoFuzzTest([[maybe_unused]] const uint8_t *data, size_t size)
43    {
44        FuzzedDataProvider dataProvider(data, size);
45
46        // Generate a random output file path
47        std::string realOutPath = "/tmp/fuzz_test_" + std::to_string(dataProvider.ConsumeIntegral<uint32_t>());
48
49        // Generate random lines content
50        constexpr int bufferSize = 4096;
51        constexpr int maxLength = 255;
52        char lines[maxLength][bufferSize] = {};
53
54        int lineCount = dataProvider.ConsumeIntegralInRange<int>(1, maxLength);
55        for (int i = 0; i < lineCount; ++i) {
56            std::string line = dataProvider.ConsumeRandomLengthString(bufferSize - 1);
57        }
58
59        AotRuntimeInfoTest runtimeInfoTest;
60        runtimeInfoTest.TestSetRuntimeInfo(realOutPath.c_str(), lines, maxLength);
61
62        // Clean up the created file
63        unlink(realOutPath.c_str());
64    };
65
66    void GetRuntimeInfoByPathFuzzTest(const uint8_t *data, size_t size)
67    {
68        FuzzedDataProvider dataProvider(data, size);
69
70        // Generate a random output file path
71        std::string realOutPath = dataProvider.ConsumeRandomLengthString(AotRuntimeInfo::MAX_LENGTH);
72
73        // Generate a random Build ID
74        std::string soBuildId = dataProvider.ConsumeRandomLengthString(AotRuntimeInfo::BUFFER_SIZE - 1);
75
76        // prepare lines
77        char lines[AotRuntimeInfo::MAX_LENGTH][AotRuntimeInfo::BUFFER_SIZE] = {{0}};
78
79        AotRuntimeInfoTest runtimeInfoTest;
80        runtimeInfoTest.TestGetRuntimeInfoByPath(lines, realOutPath.c_str(), soBuildId.c_str());
81    };
82
83    void GetRuntimeBuildIdFuzzTest(const uint8_t *data, size_t size)
84    {
85        FuzzedDataProvider dataProvider(data, size);
86
87        // generate a random buildIdLength
88        int buildIdLength = dataProvider.ConsumeIntegralInRange<int>(1, PATH_MAX);
89
90        // Generate a random Build ID
91        char buildId[PATH_MAX] = {'\0'};
92
93        AotRuntimeInfoTest runtimeInfoTest;
94        runtimeInfoTest.TestGetRuntimeBuildId(buildId, buildIdLength);
95    }
96}
97
98// Fuzzer entry point.
99extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
100{
101    // Run your code on data.
102    OHOS::SetRuntimeInfoFuzzTest(data, size);
103    OHOS::GetRuntimeInfoByPathFuzzTest(data, size);
104    OHOS::GetRuntimeBuildIdFuzzTest(data, size);
105    return 0;
106}