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