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 "startheaptracking_fuzzer.h"
17
18 #include "ecmascript/dfx/hprof/file_stream.h"
19 #include "ecmascript/ecma_string-inl.h"
20 #include "ecmascript/napi/include/dfx_jsnapi.h"
21 #include "ecmascript/napi/include/jsnapi.h"
22
23
24 using namespace panda;
25 using namespace panda::ecmascript;
26
27 namespace OHOS {
StartHeapTrackingFuzzTest(const uint8_t * data,size_t size)28 void StartHeapTrackingFuzzTest(const uint8_t* data, size_t size)
29 {
30 if (data == nullptr || size < 0) {
31 std::cout << "illegal input!";
32 return;
33 }
34 RuntimeOption option;
35 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
36 EcmaVM *vm = JSNApi::CreateJSVM(option);
37 vm->SetEnableForceGC(false);
38
39 auto factory = vm->GetFactory();
40 bool isVmMode = true;
41 bool traceAllocation = false;
42 double timeInterval = 10; // 10 : time interval 10 ms
43 ecmascript::FileStream *stream = nullptr;
44 DFXJSNApi::StartHeapTracking(vm, timeInterval, isVmMode, stream, traceAllocation);
45
46 sleep(1);
47 int count = 100;
48 while (count-- > 0) {
49 JSHandle<EcmaString> string = factory->NewFromASCII("testString");
50 factory->NewJSString(JSHandle<JSTaggedValue>(string));
51 }
52 const std::string filePath(data, data + size);
53 std::fstream outputString(filePath, std::ios::out);
54 outputString.close();
55 outputString.clear();
56 DFXJSNApi::StopHeapTracking(vm, filePath);
57 std::remove(filePath.c_str());
58 vm->SetEnableForceGC(true);
59 JSNApi::DestroyJSVM(vm);
60 }
61 }
62
63 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66 // Run your code on data.
67 OHOS::StartHeapTrackingFuzzTest(data, size);
68 return 0;
69 }