core_banned_functions.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright 2016 gRPC authors.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 """Explicitly ban select functions from being used in src/core/**.
17 
18 Most of these functions have internal versions that should be used instead."""
19 
20 import os
21 import sys
22 
23 os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..'))
24 
25 # map of banned function signature to allowlist
26 BANNED_EXCEPT = {
27  'grpc_slice_from_static_buffer(': ['src/core/lib/slice/slice.cc'],
28  'grpc_resource_quota_ref(': ['src/core/lib/resource_quota/api.cc'],
29  'grpc_resource_quota_unref(': [
30  'src/core/lib/resource_quota/api.cc', 'src/core/lib/surface/server.cc'
31  ],
32  'grpc_slice_buffer_destroy(': ['src/core/lib/slice/slice_buffer_api.cc'],
33  'grpc_slice_buffer_reset_and_unref(': [
34  'src/core/lib/slice/slice_buffer_api.cc'
35  ],
36  'grpc_slice_ref(': ['src/core/lib/slice/slice_api.cc'],
37  'grpc_slice_unref(': ['src/core/lib/slice/slice_api.cc'],
38  'grpc_error_create(': [
39  'src/core/lib/iomgr/error.cc', 'src/core/lib/iomgr/error_cfstream.cc'
40  ],
41  'grpc_error_ref(': ['src/core/lib/iomgr/error.cc'],
42  'grpc_error_unref(': ['src/core/lib/iomgr/error.cc'],
43  'grpc_os_error(': ['src/core/lib/iomgr/error.cc'],
44  'grpc_wsa_error(': ['src/core/lib/iomgr/error.cc'],
45  'grpc_log_if_error(': ['src/core/lib/iomgr/error.cc'],
46  'grpc_slice_malloc(': ['src/core/lib/slice/slice.cc'],
47  'grpc_call_cancel(': ['src/core/lib/surface/call.cc'],
48  'grpc_closure_create(': ['src/core/lib/iomgr/closure.cc'],
49  'grpc_closure_init(': ['src/core/lib/iomgr/closure.cc'],
50  'grpc_closure_sched(': ['src/core/lib/iomgr/closure.cc'],
51  'grpc_closure_run(': ['src/core/lib/iomgr/closure.cc'],
52  'grpc_closure_list_sched(': ['src/core/lib/iomgr/closure.cc'],
53  'grpc_error*': ['src/core/lib/iomgr/error.cc'],
54  'grpc_error_string': ['src/core/lib/iomgr/error.cc'],
55  # std::random_device needs /dev/random which is not available on all linuxes that we support.
56  # Any usage must be optional and opt-in, so that those platforms can use gRPC without problem.
57  'std::random_device': [
58  'src/core/ext/filters/client_channel/lb_policy/rls/rls.cc',
59  'src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc',
60  ]
61 }
62 
63 errors = 0
64 num_files = 0
65 for root, dirs, files in os.walk('src/core'):
66  if root.startswith('src/core/tsi'):
67  continue
68  for filename in files:
69  num_files += 1
70  path = os.path.join(root, filename)
71  if os.path.splitext(path)[1] != '.cc':
72  continue
73  with open(path) as f:
74  text = f.read()
75  for banned, exceptions in list(BANNED_EXCEPT.items()):
76  if path in exceptions:
77  continue
78  if banned in text:
79  print(('Illegal use of "%s" in %s' % (banned, path)))
80  errors += 1
81 
82 assert errors == 0
83 # This check comes about from this issue:
84 # https://github.com/grpc/grpc/issues/15381
85 # Basically, a change rendered this script useless and we did not realize it.
86 # This dumb check ensures that this type of issue doesn't occur again.
87 assert num_files > 300 # we definitely have more than 300 files
open
#define open
Definition: test-fs.c:46


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:57