emqx_bpapi_static_checks.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  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. %%--------------------------------------------------------------------
  16. -module(emqx_bpapi_static_checks).
  17. -export([run/0, dump/1, dump/0, check_compat/1, versions_file/0, dumps_dir/0]).
  18. %% Using an undocumented API here :(
  19. -include_lib("dialyzer/src/dialyzer.hrl").
  20. -type api_dump() :: #{
  21. {emqx_bpapi:api(), emqx_bpapi:api_version()} =>
  22. #{
  23. calls := [emqx_bpapi:rpc()],
  24. casts := [emqx_bpapi:rpc()]
  25. }
  26. }.
  27. -type dialyzer_spec() :: {_Type, [_Type]}.
  28. -type dialyzer_dump() :: #{mfa() => dialyzer_spec()}.
  29. -type fulldump() :: #{
  30. api => api_dump(),
  31. signatures => dialyzer_dump(),
  32. release => string()
  33. }.
  34. -type dump_options() :: #{
  35. reldir := file:name(),
  36. plt := file:name()
  37. }.
  38. -type param_types() :: #{emqx_bpapi:var_name() => _Type}.
  39. %% Applications and modules we wish to ignore in the analysis:
  40. -define(IGNORED_APPS,
  41. "gen_rpc, recon, redbug, observer_cli, snabbkaffe, ekka, mria, amqp_client, rabbit_common, esaml"
  42. ).
  43. -define(IGNORED_MODULES, "emqx_rpc").
  44. -define(FORCE_DELETED_MODULES, [
  45. emqx_statsd,
  46. emqx_statsd_proto_v1,
  47. emqx_persistent_session_proto_v1
  48. ]).
  49. -define(FORCE_DELETED_APIS, [
  50. {emqx_statsd, 1},
  51. {emqx_plugin_libs, 1},
  52. {emqx_persistent_session, 1}
  53. ]).
  54. %% List of known RPC backend modules:
  55. -define(RPC_MODULES, "gen_rpc, erpc, rpc, emqx_rpc").
  56. %% List of known functions also known to do RPC:
  57. -define(RPC_FUNCTIONS,
  58. "emqx_cluster_rpc:multicall/3, emqx_cluster_rpc:multicall/5"
  59. ).
  60. %% List of functions in the RPC backend modules that we can ignore:
  61. % TODO: handle pmap
  62. -define(IGNORED_RPC_CALLS, "gen_rpc:nodes/0, emqx_rpc:unwrap_erpc/1").
  63. %% List of business-layer functions that are exempt from the checks:
  64. %% erlfmt-ignore
  65. -define(EXEMPTIONS,
  66. % Reason: legacy code. A fun and a QC query are
  67. % passed in the args, it's futile to try to statically
  68. % check it
  69. "emqx_mgmt_api:do_query/2, emqx_mgmt_api:collect_total_from_tail_nodes/2"
  70. ).
  71. -define(XREF, myxref).
  72. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  73. %% Functions related to BPAPI compatibility checking
  74. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  75. -spec run() -> boolean().
  76. run() ->
  77. case dump() of
  78. true ->
  79. Dumps = filelib:wildcard(dumps_dir() ++ "/*.bpapi"),
  80. case Dumps of
  81. [] ->
  82. logger:error("No BPAPI dumps are found in ~s, abort", [dumps_dir()]),
  83. false;
  84. _ ->
  85. logger:notice("Running API compatibility checks for ~p", [Dumps]),
  86. check_compat(Dumps)
  87. end;
  88. false ->
  89. logger:critical("Backplane API violations found on the current branch.", []),
  90. false
  91. end.
  92. -spec check_compat([file:filename()]) -> boolean().
  93. check_compat(DumpFilenames) ->
  94. put(bpapi_ok, true),
  95. Dumps = lists:map(
  96. fun(FN) ->
  97. {ok, [Dump]} = file:consult(FN),
  98. Dump
  99. end,
  100. DumpFilenames
  101. ),
  102. [check_compat(I, J) || I <- Dumps, J <- Dumps],
  103. erase(bpapi_ok).
  104. %% Note: sets nok flag
  105. -spec check_compat(fulldump(), fulldump()) -> ok.
  106. check_compat(Dump1 = #{release := Rel1}, Dump2 = #{release := Rel2}) ->
  107. check_api_immutability(Dump1, Dump2),
  108. Rel2 >= Rel1 andalso
  109. typecheck_apis(Dump1, Dump2).
  110. %% It's not allowed to change BPAPI modules. Check that no changes
  111. %% have been made. (sets nok flag)
  112. -spec check_api_immutability(fulldump(), fulldump()) -> ok.
  113. check_api_immutability(#{release := Rel1, api := APIs1}, #{release := Rel2, api := APIs2}) when
  114. Rel2 >= Rel1
  115. ->
  116. %% TODO: Handle API deprecation
  117. _ = maps:map(
  118. fun(Key = {API, Version}, Val) ->
  119. case maps:get(Key, APIs2, undefined) of
  120. Val ->
  121. ok;
  122. undefined ->
  123. case lists:member({API, Version}, ?FORCE_DELETED_APIS) of
  124. true ->
  125. ok;
  126. false ->
  127. setnok(),
  128. logger:error(
  129. "API ~p v~p was removed in release ~p without being deprecated.",
  130. [API, Version, Rel2]
  131. )
  132. end;
  133. _Val ->
  134. setnok(),
  135. logger:error(
  136. "API ~p v~p was changed between ~p and ~p. Backplane API should be immutable.",
  137. [API, Version, Rel1, Rel2]
  138. )
  139. end
  140. end,
  141. APIs1
  142. ),
  143. ok;
  144. check_api_immutability(_, _) ->
  145. ok.
  146. filter_calls(Calls) ->
  147. F = fun({{Mf, _, _}, {Mt, _, _}}) ->
  148. (not lists:member(Mf, ?FORCE_DELETED_MODULES)) andalso
  149. (not lists:member(Mt, ?FORCE_DELETED_MODULES))
  150. end,
  151. lists:filter(F, Calls).
  152. %% Note: sets nok flag
  153. -spec typecheck_apis(fulldump(), fulldump()) -> ok.
  154. typecheck_apis(
  155. #{release := CallerRelease, api := CallerAPIs, signatures := CallerSigs},
  156. #{release := CalleeRelease, signatures := CalleeSigs}
  157. ) ->
  158. AllCalls0 = lists:flatten([
  159. [Calls, Casts]
  160. || #{calls := Calls, casts := Casts} <- maps:values(CallerAPIs)
  161. ]),
  162. AllCalls = filter_calls(AllCalls0),
  163. lists:foreach(
  164. fun({From, To}) ->
  165. Caller = get_param_types(CallerSigs, From),
  166. Callee = get_param_types(CalleeSigs, To),
  167. %% TODO: check return types
  168. case typecheck_rpc(Caller, Callee) of
  169. [] ->
  170. ok;
  171. TypeErrors ->
  172. setnok(),
  173. [
  174. logger:error(
  175. "Incompatible RPC call: "
  176. "type of the parameter ~p of RPC call ~s in release ~p "
  177. "is not a subtype of the target function ~s in release ~p.~n"
  178. "Caller type: ~s~nCallee type: ~s~n",
  179. [
  180. Var,
  181. format_call(From),
  182. CallerRelease,
  183. format_call(To),
  184. CalleeRelease,
  185. erl_types:t_to_string(CallerType),
  186. erl_types:t_to_string(CalleeType)
  187. ]
  188. )
  189. || {Var, CallerType, CalleeType} <- TypeErrors
  190. ]
  191. end
  192. end,
  193. AllCalls
  194. ).
  195. -spec typecheck_rpc(param_types(), param_types()) -> [{emqx_bpapi:var_name(), _Type, _Type}].
  196. typecheck_rpc(Caller, Callee) ->
  197. maps:fold(
  198. fun(Var, CalleeType, Acc) ->
  199. #{Var := CallerType} = Caller,
  200. case erl_types:t_is_subtype(CallerType, CalleeType) of
  201. true -> Acc;
  202. false -> [{Var, CallerType, CalleeType} | Acc]
  203. end
  204. end,
  205. [],
  206. Callee
  207. ).
  208. -spec get_param_types(dialyzer_dump(), emqx_bpapi:call()) -> param_types().
  209. get_param_types(Signatures, {M, F, A}) ->
  210. Arity = length(A),
  211. case Signatures of
  212. #{{M, F, Arity} := {_RetType, AttrTypes}} ->
  213. % assert
  214. Arity = length(AttrTypes),
  215. maps:from_list(lists:zip(A, AttrTypes));
  216. _ ->
  217. logger:critical("Call ~p:~p/~p is not found in PLT~n", [M, F, Arity]),
  218. error({badkey, {M, F, A}})
  219. end.
  220. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  221. %% Functions related to BPAPI dumping
  222. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  223. dump() ->
  224. case
  225. {
  226. filelib:wildcard(project_root_dir() ++ "/*_plt"),
  227. filelib:wildcard(project_root_dir() ++ "/_build/check/lib")
  228. }
  229. of
  230. {[PLT | _], [RelDir | _]} ->
  231. dump(#{
  232. plt => PLT,
  233. reldir => RelDir
  234. });
  235. _ ->
  236. error("failed to guess run options")
  237. end.
  238. %% Collect the local BPAPI modules to a dump file
  239. -spec dump(dump_options()) -> boolean().
  240. dump(Opts) ->
  241. put(bpapi_ok, true),
  242. PLT = prepare(Opts),
  243. %% First we run XREF to find all callers of any known RPC backend:
  244. Callers = find_remote_calls(Opts),
  245. {BPAPICalls, NonBPAPICalls} = lists:partition(fun is_bpapi_call/1, Callers),
  246. warn_nonbpapi_rpcs(NonBPAPICalls),
  247. APIDump = collect_bpapis(BPAPICalls),
  248. DialyzerDump = collect_signatures(PLT, APIDump),
  249. dump_api(#{api => APIDump, signatures => DialyzerDump, release => "master"}),
  250. dump_versions(APIDump),
  251. xref:stop(?XREF),
  252. erase(bpapi_ok).
  253. prepare(#{reldir := RelDir, plt := PLT}) ->
  254. logger:info("Starting xref...", []),
  255. xref:start(?XREF),
  256. filelib:wildcard(RelDir ++ "/*/ebin/") =:= [] andalso
  257. error("No applications found in the release directory. Wrong directory?"),
  258. xref:set_default(?XREF, [{warnings, false}]),
  259. xref:add_release(?XREF, RelDir),
  260. %% Now to the dialyzer stuff:
  261. logger:info("Loading PLT...", []),
  262. dialyzer_plt:from_file(PLT).
  263. %% erlfmt-ignore
  264. find_remote_calls(_Opts) ->
  265. Query =
  266. "XC | (A - ["?IGNORED_APPS"]:App - ["?IGNORED_MODULES"]:Mod - ["?EXEMPTIONS"])
  267. || ((["?RPC_MODULES"] : Mod + ["?RPC_FUNCTIONS"]) - ["?IGNORED_RPC_CALLS"])",
  268. {ok, Calls} = xref:q(?XREF, Query),
  269. logger:info("Calls to RPC modules ~p", [Calls]),
  270. {Callers, _Callees} = lists:unzip(Calls),
  271. Callers.
  272. -spec warn_nonbpapi_rpcs([mfa()]) -> ok.
  273. warn_nonbpapi_rpcs([]) ->
  274. ok;
  275. warn_nonbpapi_rpcs(L) ->
  276. setnok(),
  277. lists:foreach(
  278. fun({M, F, A}) ->
  279. logger:error(
  280. "~p:~p/~p does a remote call outside of a dedicated "
  281. "backplane API module. "
  282. "It may break during rolling cluster upgrade",
  283. [M, F, A]
  284. )
  285. end,
  286. L
  287. ).
  288. -spec is_bpapi_call(mfa()) -> boolean().
  289. is_bpapi_call({Module, _Function, _Arity}) ->
  290. case catch Module:bpapi_meta() of
  291. #{api := _} -> true;
  292. _ -> false
  293. end.
  294. -spec dump_api(fulldump()) -> ok.
  295. dump_api(Term = #{api := _, signatures := _, release := Release}) ->
  296. Filename = filename:join(dumps_dir(), Release ++ ".bpapi"),
  297. ok = filelib:ensure_dir(Filename),
  298. file:write_file(Filename, io_lib:format("~0p.~n", [Term])).
  299. -spec dump_versions(api_dump()) -> ok.
  300. dump_versions(APIs) ->
  301. Filename = versions_file(),
  302. logger:notice("Dumping API versions to ~p", [Filename]),
  303. ok = filelib:ensure_dir(Filename),
  304. {ok, FD} = file:open(Filename, [write]),
  305. io:format(
  306. FD, "%% This file is automatically generated by `make static_checks`, do not edit.~n", []
  307. ),
  308. lists:foreach(
  309. fun(API) ->
  310. ok = io:format(FD, "~p.~n", [API])
  311. end,
  312. lists:sort(maps:keys(APIs))
  313. ),
  314. file:close(FD).
  315. -spec collect_bpapis([mfa()]) -> api_dump().
  316. collect_bpapis(L) ->
  317. Modules = lists:usort([M || {M, _F, _A} <- L]),
  318. lists:foldl(
  319. fun(Mod, Acc) ->
  320. #{
  321. api := API,
  322. version := Vsn,
  323. calls := Calls,
  324. casts := Casts
  325. } = Mod:bpapi_meta(),
  326. Acc#{
  327. {API, Vsn} => #{
  328. calls => Calls,
  329. casts => Casts
  330. }
  331. }
  332. end,
  333. #{},
  334. Modules
  335. ).
  336. -spec collect_signatures(_PLT, api_dump()) -> dialyzer_dump().
  337. collect_signatures(PLT, APIs) ->
  338. maps:fold(
  339. fun(_APIAndVersion, #{calls := Calls, casts := Casts}, Acc0) ->
  340. Acc1 = lists:foldl(fun enrich/2, {Acc0, PLT}, Calls),
  341. {Acc, PLT} = lists:foldl(fun enrich/2, Acc1, Casts),
  342. Acc
  343. end,
  344. #{},
  345. APIs
  346. ).
  347. %% Add information about the call types from the PLT
  348. -spec enrich(emqx_bpapi:rpc(), {dialyzer_dump(), _PLT}) -> {dialyzer_dump(), _PLT}.
  349. enrich({From0, To0}, {Acc0, PLT}) ->
  350. From = call_to_mfa(From0),
  351. To = call_to_mfa(To0),
  352. case {dialyzer_plt:lookup_contract(PLT, From), dialyzer_plt:lookup(PLT, To)} of
  353. {{value, #contract{args = FromArgs}}, {value, TTo}} ->
  354. %% TODO: Check return type
  355. FromRet = erl_types:t_any(),
  356. Acc = Acc0#{
  357. From => {FromRet, FromArgs},
  358. To => TTo
  359. },
  360. {Acc, PLT};
  361. {{value, _}, none} ->
  362. setnok(),
  363. logger:critical(
  364. "Backplane API function ~s calls a missing remote function ~s",
  365. [format_call(From0), format_call(To0)]
  366. ),
  367. error(missing_target)
  368. end.
  369. -spec call_to_mfa(emqx_bpapi:call()) -> mfa().
  370. call_to_mfa({M, F, A}) ->
  371. {M, F, length(A)}.
  372. format_call({M, F, A}) ->
  373. io_lib:format("~p:~p/~p", [M, F, length(A)]).
  374. setnok() ->
  375. put(bpapi_ok, false).
  376. dumps_dir() ->
  377. filename:join(project_root_dir(), "apps/emqx/test/emqx_static_checks_data").
  378. project_root_dir() ->
  379. string:trim(os:cmd("git rev-parse --show-toplevel")).
  380. versions_file() ->
  381. filename:join(project_root_dir(), "apps/emqx/priv/bpapi.versions").