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 18Most of these functions have internal versions that should be used instead.""" 19 20import os 21import sys 22 23os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..')) 24 25# map of banned function signature to allowlist 26BANNED_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(': [ 38 'src/core/lib/iomgr/error.cc', 'src/core/lib/iomgr/error.h' 39 ], 40 'grpc_wsa_error(': [ 41 'src/core/lib/iomgr/error.cc', 'src/core/lib/iomgr/error.h' 42 ], 43 'grpc_log_if_error(': [ 44 'src/core/lib/iomgr/error.cc', 'src/core/lib/iomgr/error.h' 45 ], 46 'grpc_slice_malloc(': [ 47 'src/core/lib/slice/slice.cc', 'src/core/lib/slice/slice.h' 48 ], 49 'grpc_call_cancel(': ['src/core/lib/surface/call.cc'], 50 'grpc_channel_destroy(': [ 51 'src/core/lib/surface/channel.cc', 52 'src/core/tsi/alts/handshaker/alts_shared_resource.cc', 53 ], 54 'grpc_closure_create(': [ 55 'src/core/lib/iomgr/closure.cc', 'src/core/lib/iomgr/closure.h' 56 ], 57 'grpc_closure_init(': [ 58 'src/core/lib/iomgr/closure.cc', 'src/core/lib/iomgr/closure.h' 59 ], 60 'grpc_closure_sched(': ['src/core/lib/iomgr/closure.cc'], 61 'grpc_closure_run(': ['src/core/lib/iomgr/closure.cc'], 62 'grpc_closure_list_sched(': ['src/core/lib/iomgr/closure.cc'], 63 'grpc_error*': ['src/core/lib/iomgr/error.cc'], 64 'grpc_error_string': ['src/core/lib/iomgr/error.cc'], 65 # use grpc_core::CSlice{Ref,Unref} instead inside core 66 # (or prefer grpc_core::Slice!) 67 'grpc_slice_ref(': ['src/core/lib/slice/slice.cc'], 68 'grpc_slice_unref(': ['src/core/lib/slice/slice.cc'], 69 # std::random_device needs /dev/random which is not available on all linuxes that we support. 70 # Any usage must be optional and opt-in, so that those platforms can use gRPC without problem. 71 'std::random_device': [ 72 'src/core/ext/filters/client_channel/lb_policy/rls/rls.cc', 73 'src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc', 74 ], 75 # use 'grpc_core::Crash' instead 76 'GPR_ASSERT(false': [], 77 78 # Use `std::exchange()` instead. 79 'absl::exchange': [], 80 # Use `std::make_unique()` instead. 81 'absl::make_unique': [], 82} 83 84errors = 0 85num_files = 0 86for root, dirs, files in os.walk('src/core'): 87 if root.startswith('src/core/tsi'): 88 continue 89 for filename in files: 90 num_files += 1 91 path = os.path.join(root, filename) 92 if os.path.splitext(path)[1] not in ('.h', '.cc'): 93 continue 94 with open(path) as f: 95 text = f.read() 96 for banned, exceptions in list(BANNED_EXCEPT.items()): 97 if path in exceptions: 98 continue 99 if banned in text: 100 print(('Illegal use of "%s" in %s' % (banned, path))) 101 errors += 1 102 103assert errors == 0 104# This check comes about from this issue: 105# https://github.com/grpc/grpc/issues/15381 106# Basically, a change rendered this script useless and we did not realize it. 107# This dumb check ensures that this type of issue doesn't occur again. 108assert num_files > 300 # we definitely have more than 300 files 109