core_banned_functions.py 3.6 KB

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