• 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 #ifdef OHOS_FDTRACK_HOOK_ENABLE
17 #include "musl_fdtrack.h"
18 #include "stdatomic_impl.h"
19 #include "sys_param.h"
20 
21 enum EnumBetaDevelopMode {
22 	NOT_CHECK,
23 	ENABLE,
24 	DISABLE,
25 };
26 
27 _Atomic(fdtrack_hook) __fdtrack_hook;
28 bool __fdtrack_enabled = false;
29 
30 static char *__is_beta_version = "beta";
31 static char *__key_version_type = "const.logsystem.versiontype";
32 static char *__is_develop_mode = "enable";
33 static char *__key_develop_mode = "persist.hiview.leak_detector";
34 static enum EnumBetaDevelopMode __beta_develop_mode = NOT_CHECK;
35 static int TIME_IN_MILLISECONDS = 1000000;
36 
set_fdtrack_enabled(bool newValue)37 void set_fdtrack_enabled(bool newValue)
38 {
39 	__fdtrack_enabled = newValue;
40 }
41 
fdtrack_cas_hook(fdtrack_hook * expected,fdtrack_hook value)42 bool fdtrack_cas_hook(fdtrack_hook* expected, fdtrack_hook value)
43 {
44 	return atomic_compare_exchange_strong(&__fdtrack_hook, expected, value);
45 }
46 
normal_flow_control(struct timeval prevTime,int interval)47 bool normal_flow_control(struct timeval prevTime, int interval)
48 {
49 	struct timeval currentTime;
50 	gettimeofday(&currentTime, NULL);
51 	int timeDifferenceSeconds = ((currentTime.tv_sec * TIME_IN_MILLISECONDS + currentTime.tv_usec) -
52 								(prevTime.tv_sec * TIME_IN_MILLISECONDS + prevTime.tv_usec)) / TIME_IN_MILLISECONDS;
53 	return timeDifferenceSeconds > interval;
54 }
55 
check_open_func(const char * expected,const char * key)56 bool check_open_func(const char*expected, const char* key)
57 {
58 	CachedHandle handle = CachedParameterCreate(key, "unknown");
59 	const char *value = CachedParameterGet(handle);
60 	return (value != NULL && strncmp(value, expected, strlen(expected)) == 0);
61 }
62 
check_beta_develop_before()63 bool check_beta_develop_before()
64 {
65 	if (__beta_develop_mode == NOT_CHECK) {
66 		if (check_open_func(__is_beta_version, __key_version_type) ||
67 			check_open_func(__is_develop_mode, __key_develop_mode)) {
68 			__beta_develop_mode = ENABLE;
69 		} else {
70 			__beta_develop_mode = DISABLE;
71 		}
72 	}
73 	return __beta_develop_mode == ENABLE;
74 }
75 
check_before_memory_allocate(struct timeval prevTime,int interval)76 bool check_before_memory_allocate(struct timeval prevTime, int interval)
77 {
78 	if (!check_beta_develop_before()) {
79 		return false;
80 	}
81 	if (!(prevTime.tv_sec == 0 || normal_flow_control(prevTime, interval))) {
82 		return false;
83 	}
84 	return true;
85 }
86 
87 #endif