• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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}
22
23using_security_flag = enable_auto_var_init
24
25if (!is_ohos) {
26  using_security_flag = false
27}
28
29# support_stack_protector_ret = true if clang support -fstack-protector-ret-all
30clang_bin = rebase_path("${default_clang_base_path}/bin/clang", root_build_dir)
31cmd = "${clang_bin} --help | grep fstack-protector-ret-all | wc -l"
32
33# exec_script returns 1 if grep -fstack-protector-ret-all failed, indicating -fstack-protector-ret-all not supported
34res = exec_script("//build/scripts/run_shell_cmd.py", [ cmd ], "value")
35if (target_cpu == "arm64" && res == 1 && is_ohos && is_standard_system &&
36    !is_mingw) {
37  support_stack_protector_ret = true
38} else {
39  support_stack_protector_ret = false
40}
41
42# pac_ret is supported in armv8.
43if (target_cpu == "arm64" && is_ohos && is_standard_system && !is_mingw &&
44    use_pac_ret) {
45  support_branch_protector_pac_ret = true
46}
47
48assert(
49    !using_security_flag || is_clang,
50    "automatic variable initialization requires setting is_clang = true in 'gn args'")
51
52template("ohos_auto_initialize_config") {
53  config(target_name) {
54    forward_variables_from(invoker, [ "auto_var_init" ])
55
56    configs = []
57
58    # Currently, only the clang compiler and standard system support automatic variable initialization.
59    if (is_clang && is_standard_system) {
60      if (defined(auto_var_init)) {
61        assert(
62            auto_var_init == "pattern" || auto_var_init == "zero" ||
63                auto_var_init == "uninit",
64            "auto_var_init can only be set to pattern, zero or uninit, for example, auto_var_init = \"pattern\"")
65
66        if (auto_var_init == "pattern") {
67          configs += [ "//build/config/security:auto_var_pattern_init_config" ]
68        } else if (auto_var_init == "zero") {
69          configs += [ "//build/config/security:auto_var_zero_init_config" ]
70        } else if (auto_var_init == "uninit") {
71          configs += [ "//build/config/security:auto_var_uninit_config" ]
72        }
73      } else {
74        configs += [ "//build/config/security:auto_var_zero_init_config" ]
75      }
76    }
77  }
78}
79
80template("ohos_security_config") {
81  config(target_name) {
82    configs = []
83    _auto_initialize_config_target = "${target_name}__auto_initialize_config"
84    ohos_auto_initialize_config(_auto_initialize_config_target) {
85      forward_variables_from(invoker, [ "auto_var_init" ])
86    }
87
88    configs += [ ":$_auto_initialize_config_target" ]
89  }
90}
91