1# Copyright (c) 2022 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13import("//build/config/clang/clang.gni") 14 15declare_args() { 16 # Enable the config that variables are automatically initialized by default. 17 enable_auto_var_init = false 18 support_stack_protector_ret = false 19 support_branch_protector_pac_ret = false 20 use_pac_ret = true 21 support_branch_protector_bti = false 22} 23 24using_security_flag = enable_auto_var_init 25 26if (!is_ohos) { 27 using_security_flag = false 28} 29 30# support_stack_protector_ret = true if clang support -fstack-protector-ret-all 31clang_bin = rebase_path("${default_clang_base_path}/bin/clang", root_build_dir) 32cmd = "${clang_bin} --help | grep fstack-protector-ret-all | wc -l" 33 34# exec_script returns 1 if grep -fstack-protector-ret-all failed, indicating -fstack-protector-ret-all not supported 35res = exec_script("//build/scripts/run_shell_cmd.py", [ cmd ], "value") 36if (target_cpu == "arm64" && res == 1 && is_ohos && is_standard_system && 37 !is_mingw) { 38 support_stack_protector_ret = true 39} else { 40 support_stack_protector_ret = false 41} 42 43# pac_ret is supported in armv8. 44# bti is supported in armv8.5 45if (target_cpu == "arm64" && is_ohos && is_standard_system && !is_mingw) { 46 if (use_pac_ret) { 47 support_branch_protector_pac_ret = true 48 } 49 support_branch_protector_bti = true 50} 51 52assert( 53 !using_security_flag || is_clang, 54 "automatic variable initialization requires setting is_clang = true in 'gn args'") 55 56template("ohos_auto_initialize_config") { 57 config(target_name) { 58 forward_variables_from(invoker, [ "auto_var_init" ]) 59 60 configs = [] 61 62 # Currently, only the clang compiler and standard system support automatic variable initialization. 63 if (is_clang && is_standard_system) { 64 if (defined(auto_var_init)) { 65 assert( 66 auto_var_init == "pattern" || auto_var_init == "zero" || 67 auto_var_init == "uninit", 68 "auto_var_init can only be set to pattern, zero or uninit, for example, auto_var_init = \"pattern\"") 69 70 if (auto_var_init == "pattern") { 71 configs += [ "//build/config/security:auto_var_pattern_init_config" ] 72 } else if (auto_var_init == "zero") { 73 configs += [ "//build/config/security:auto_var_zero_init_config" ] 74 } else if (auto_var_init == "uninit") { 75 configs += [ "//build/config/security:auto_var_uninit_config" ] 76 } 77 } else { 78 configs += [ "//build/config/security:auto_var_zero_init_config" ] 79 } 80 } 81 } 82} 83 84template("ohos_security_config") { 85 config(target_name) { 86 configs = [] 87 _auto_initialize_config_target = "${target_name}__auto_initialize_config" 88 ohos_auto_initialize_config(_auto_initialize_config_target) { 89 forward_variables_from(invoker, [ "auto_var_init" ]) 90 } 91 92 configs += [ ":$_auto_initialize_config_target" ] 93 } 94} 95