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 21enum EnumBetaDevelopMode { 22 NOT_CHECK, 23 ENABLE, 24 DISABLE, 25}; 26 27_Atomic(fdtrack_hook) __fdtrack_hook; 28bool __fdtrack_enabled = false; 29 30static char *__is_beta_version = "beta"; 31static char *__key_version_type = "const.logsystem.versiontype"; 32static char *__is_develop_mode = "enable"; 33static char *__key_develop_mode = "persist.hiview.leak_detector"; 34static enum EnumBetaDevelopMode __beta_develop_mode = NOT_CHECK; 35static int TIME_IN_MILLISECONDS = 1000000; 36 37void set_fdtrack_enabled(bool newValue) 38{ 39 __fdtrack_enabled = newValue; 40} 41 42bool fdtrack_cas_hook(fdtrack_hook* expected, fdtrack_hook value) 43{ 44 return atomic_compare_exchange_strong(&__fdtrack_hook, expected, value); 45} 46 47bool normal_flow_control(struct timeval prevTime, int interval) 48{ 49 struct timeval currentTime; 50 gettimeofday(¤tTime, 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 56bool 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 63bool 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 76bool 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