emqx_bpapi_static_checks.erl 14 KB

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