• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "common_define.h"
17 #include "common_utils.h"
18 #include "hitracemeter_fuzzer.h"
19 #include "hitrace_fuzztest_common.h"
20 #include "hitrace_meter.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace Hitrace {
HitraceMeterTest(const uint8_t * data,size_t size)25 void HitraceMeterTest(const uint8_t* data, size_t size)
26 {
27     int32_t taskId = 0;
28     if (size < sizeof(taskId)) {
29         return;
30     }
31     StreamToValueInfo(data, taskId);
32     std::string name(reinterpret_cast<const char*>(data), size - sizeof(taskId));
33     std::string customArgs = name;
34     std::string customCategory = name;
35     StartTraceEx(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, name.c_str(), customArgs.c_str());
36     FinishTraceEx(HITRACE_LEVEL_INFO, HITRACE_TAG_APP);
37     StartAsyncTraceEx(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, name.c_str(), taskId,
38                       customCategory.c_str(), customArgs.c_str());
39     FinishAsyncTraceEx(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, name.c_str(), taskId);
40     CountTraceEx(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, name.c_str(), taskId);
41 }
42 
43 } // namespace Hitrace
44 } // namespace HiviewDFX
45 } // namespace OHOS
46 
47 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)48 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
49 {
50     if (data == nullptr || size == 0) {
51         return 0;
52     }
53     std::string value = std::to_string(HITRACE_TAG_APP);
54     bool ret = OHOS::HiviewDFX::Hitrace::SetPropertyInner(TRACE_TAG_ENABLE_FLAGS, value);
55     if (!ret) {
56         return 0;
57     }
58     /* Run your code on data */
59     OHOS::HiviewDFX::Hitrace::HitraceMeterTest(data, size);
60     return 0;
61 }
62