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 "dumpheapsnapshot3_fuzzer.h"
17
18#include "ecmascript/napi/include/jsnapi.h"
19#include "ecmascript/napi/include/dfx_jsnapi.h"
20#include "ecmascript/ecma_string-inl.h"
21#include "ecmascript/dfx/hprof/file_stream.h"
22
23using namespace panda;
24using namespace panda::ecmascript;
25using panda::ecmascript::FileStream;
26
27#define MAXBYTELEN sizeof(double)
28
29namespace OHOS {
30    void DumpHeapSnapshot3FuzzTest(const uint8_t* data, size_t size)
31    {
32        if (size <= 0) {
33            return;
34        }
35        RuntimeOption option;
36        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
37        EcmaVM *vm = JSNApi::CreateJSVM(option);
38        size_t maxEnumNum = static_cast<size_t>(DumpFormat::OTHER) + 1;
39        DumpFormat dumpFormat = static_cast<DumpFormat>(size % maxEnumNum);
40        DumpSnapShotOption dumpOption;
41        dumpOption.dumpFormat = dumpFormat;
42        dumpOption.isVmMode = true;
43        dumpOption.isPrivate = false;
44        dumpOption.captureNumericValue = false;
45        if (size > MAXBYTELEN) {
46            size = MAXBYTELEN;
47        }
48        std::string path(data, data + size);
49        FileStream stream(path);
50        Progress *progress = nullptr;
51        DFXJSNApi::DumpHeapSnapshot(vm, &stream, dumpOption, progress);
52        JSNApi::DestroyJSVM(vm);
53    }
54}
55
56// Fuzzer entry point.
57extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
58{
59    // Run your code on data.
60    OHOS::DumpHeapSnapshot3FuzzTest(data, size);
61    return 0;
62}