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
23 using namespace panda;
24 using namespace panda::ecmascript;
25 using panda::ecmascript::FileStream;
26
27 #define MAXBYTELEN sizeof(double)
28
29 namespace OHOS {
DumpHeapSnapshot3FuzzTest(const uint8_t * data,size_t size)30 void DumpHeapSnapshot3FuzzTest(const uint8_t* data, size_t size)
31 {
32 RuntimeOption option;
33 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
34 EcmaVM *vm = JSNApi::CreateJSVM(option);
35 double input = 0;
36 if (size <= 0) {
37 return;
38 }
39 if (size > MAXBYTELEN) {
40 size = MAXBYTELEN;
41 }
42 if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
43 std::cout << "memcpy_s failed!";
44 UNREACHABLE();
45 }
46 bool isVmMode = true;
47 bool isPrivate = false;
48 std::string path(data, data + size);
49 FileStream stream(path);
50 Progress *progress = nullptr;
51 DFXJSNApi::DumpHeapSnapshot(vm, input, &stream, progress, isVmMode, isPrivate);
52 JSNApi::DestroyJSVM(vm);
53 }
54 }
55
56 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "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 }