Debian Codegraph @

tools/run_tests/sanity/core_banned_functions.py

raw
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_error_create(': [
33 'src/core/lib/iomgr/error.cc', 'src/core/lib/iomgr/error_cfstream.cc'
34 ],
35 'grpc_error_ref(': ['src/core/lib/iomgr/error.cc'],
36 'grpc_error_unref(': ['src/core/lib/iomgr/error.cc'],
37 'grpc_os_error(': ['src/core/lib/iomgr/error.cc'],
38 'grpc_wsa_error(': ['src/core/lib/iomgr/error.cc'],
39 'grpc_log_if_error(': ['src/core/lib/iomgr/error.cc'],
40 'grpc_slice_malloc(': ['src/core/lib/slice/slice.cc'],
41 'grpc_call_cancel(': ['src/core/lib/surface/call.cc'],
42 'grpc_closure_create(': ['src/core/lib/iomgr/closure.cc'],
43 'grpc_closure_init(': ['src/core/lib/iomgr/closure.cc'],
44 'grpc_closure_sched(': ['src/core/lib/iomgr/closure.cc'],
45 'grpc_closure_run(': ['src/core/lib/iomgr/closure.cc'],
46 'grpc_closure_list_sched(': ['src/core/lib/iomgr/closure.cc'],
47 'grpc_error*': ['src/core/lib/iomgr/error.cc'],
48 'grpc_error_string': ['src/core/lib/iomgr/error.cc'],
49 # use grpc_core::CSlice{Ref,Unref} instead inside core
50 # (or prefer grpc_core::Slice!)
51 'grpc_slice_ref(': ['src/core/lib/slice/slice.cc'],
52 'grpc_slice_unref(': ['src/core/lib/slice/slice.cc'],
53 # std::random_device needs /dev/random which is not available on all linuxes that we support.
54 # Any usage must be optional and opt-in, so that those platforms can use gRPC without problem.
55 'std::random_device': [
56 'src/core/ext/filters/client_channel/lb_policy/rls/rls.cc',
57 'src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc',
58 ],
59
60 # Use `std::exchange()` instead.
61 'absl::exchange': [],
62 # Use `std::make_unique()` instead.
63 'absl::make_unique': [],
64 }
65
66 errors = 0
67 num_files = 0
68 for root, dirs, files in os.walk('src/core'):
69 if root.startswith('src/core/tsi'):
70 continue
71 for filename in files:
72 num_files += 1
73 path = os.path.join(root, filename)
74 if os.path.splitext(path)[1] != '.cc':
75 continue
76 with open(path) as f:
77 text = f.read()
78 for banned, exceptions in list(BANNED_EXCEPT.items()):
79 if path in exceptions:
80 continue
81 if banned in text:
82 print(('Illegal use of "%s" in %s' % (banned, path)))
83 errors += 1
84
85 assert errors == 0
86 # This check comes about from this issue:
87 # https://github.com/grpc/grpc/issues/15381
88 # Basically, a change rendered this script useless and we did not realize it.
89 # This dumb check ensures that this type of issue doesn't occur again.
90 assert num_files > 300 # we definitely have more than 300 files