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 "heapprofilerstarttrackingheapobjects_fuzzer.h"
17#include "ecmascript/napi/include/jsnapi.h"
18#include "agent/heapprofiler_impl.h"
19#include "tooling/dispatcher.h"
20
21using namespace panda;
22using namespace panda::ecmascript;
23using namespace panda::ecmascript::tooling;
24
25#define MAXBYTELEN sizeof(int32_t)
26
27namespace OHOS {
28    void HeapprofilerStartTrackingHeapObjectsFuzzTest(const uint8_t* data, size_t size)
29    {
30        RuntimeOption option;
31        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
32        auto vm = JSNApi::CreateJSVM(option);
33        {
34            if (size <= 0) {
35                return;
36            }
37            int32_t input = 0;
38            if (size > MAXBYTELEN) {
39                size = MAXBYTELEN;
40            }
41            if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
42                std::cout << "memcpy_s failed!";
43                UNREACHABLE();
44            }
45            std::string str(data, data + size);
46            auto req = std::make_unique<DispatchRequest>(str);
47            auto heapProfiler = std::make_unique<HeapProfilerImpl>(vm, nullptr);
48            auto dispatcherImpl =
49                std::make_unique<HeapProfilerImpl::DispatcherImpl>(nullptr, std::move(heapProfiler));
50            dispatcherImpl->StartTrackingHeapObjects(*req);
51            dispatcherImpl->StopTrackingHeapObjects(*req);
52        }
53        JSNApi::DestroyJSVM(vm);
54    }
55}
56
57// Fuzzer entry point.
58extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
59{
60    // Run your code on data.
61    OHOS::HeapprofilerStartTrackingHeapObjectsFuzzTest(data, size);
62    return 0;
63}