Packages

rabbit_common

3.8.26
4.2.1 4.2.0 retired 4.2.0-rc.1 retired 4.1.6 4.1.5 retired 4.1.5-rc.2 retired 4.1.5-rc.1 4.0.3 4.0.3-rc.1 4.0.2 4.0.2-rc.2 4.0.2-rc.1 4.0.1 4.0.0 4.0.0-rc.2 4.0.0-rc.1 3.13.7 3.13.6 3.13.5 3.13.4 3.13.3 3.13.2 3.13.2-rc.1 3.13.1 3.13.0 3.13.0-rc.6 3.13.0-rc.5 3.13.0-rc.4 3.13.0-rc.3 3.13.0-rc.2 3.13.0-rc.1 3.12.14 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.2 3.12.1 3.12.0 3.12.0-rc.4 3.12.0-rc.3 3.12.0-rc.2 3.12.0-rc.1 3.11.28 3.11.27 3.11.26 3.11.25 3.11.24 3.11.23 3.11.22 3.11.21 3.11.20 3.11.19 3.11.18 3.11.17 3.11.16 3.11.15 3.11.14 3.11.13 3.11.12 3.11.11 3.11.10 3.11.9 3.11.8 3.11.7 3.11.6 3.11.5 3.11.4 3.11.3 3.11.2 3.11.1 3.11.0 3.11.0-rc.2 3.11.0-rc.1 3.11.0-1 3.10.25 3.10.24 3.10.23 3.10.22 3.10.21 3.10.20 3.10.19 3.10.18 3.10.17 3.10.16 3.10.15 3.10.14 3.10.13 3.10.12 3.10.11 3.10.10 3.10.9 3.10.8 3.10.7 3.10.6 3.10.5 3.10.4 3.10.3 3.10.2 3.10.1 3.10.0 3.10.0-rc.6 3.10.0-rc.5 3.9.29 3.9.28 3.9.27 3.9.26 3.9.25 3.9.24 3.9.23 3.9.22 3.9.21 3.9.20 3.9.19 3.9.18 3.9.17 3.9.16 3.9.15 3.9.11 3.9.10 3.9.9 3.9.8 3.9.7 3.9.6 3.9.5 3.9.4 3.9.3 3.9.2 3.9.1 3.8.35 3.8.34 3.8.33 3.8.32 3.8.31 3.8.30 3.8.26 3.8.25 3.8.24 3.8.23 3.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c.2 3.7.0-alpha.544 3.7.0-alpha.542 3.6.16 3.6.16-rc.1 3.6.15 3.6.15-rc.1 3.6.14 3.6.13 3.6.12 3.6.11 3.6.10 3.6.9 3.6.8 3.6.7 3.6.7-pre.1 3.5.6 3.5.0 3.4.0 3.3.5 3.0.2 0.0.0-rc.1

Modules shared by rabbitmq-server and rabbitmq-erlang-client

Security advisory: This version has known vulnerabilities. View advisories

Current section

Files

Jump to
rabbit_common src code_version.erl
Raw

src/code_version.erl

%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
%%
-module(code_version).
-export([update/1, get_otp_version/0]).
%%----------------------------------------------------------------------------
%% API
%%----------------------------------------------------------------------------
%%----------------------------------------------------------------------------
%% @doc Reads the abstract code of the given `Module`, modifies it to adapt to
%% the current Erlang version, compiles and loads the result.
%% This function finds the current Erlang version and then selects the function
%% call for that version, removing all other versions declared in the original
%% beam file. `code_version:update/1` is triggered by the module itself the
%% first time an affected function is called.
%%
%% The purpose of this functionality is to support the new time API introduced
%% in ERTS 7.0, while providing compatibility with previous versions.
%%
%% `Module` must contain an attribute `erlang_version_support` containing a list of
%% tuples:
%%
%% {ErlangVersion, [{OriginalFunction, Arity, PreErlangVersionFunction,
%% PostErlangVersionFunction}]}
%%
%% All these new functions may be exported, and implemented as follows:
%%
%% OriginalFunction() ->
%% code_version:update(?MODULE),
%% ?MODULE:OriginalFunction().
%%
%% PostErlangVersionFunction() ->
%% %% implementation using new time API
%% ..
%%
%% PreErlangVersionFunction() ->
%% %% implementation using fallback solution
%% ..
%%
%% CAUTION: Make sure that all functions in the module are patched this
%% way! If you have "regular" functions, you might hit a race condition
%% between the unload of the old module and the load of the patched
%% module. If all functions are patched, loading will be serialized,
%% thanks to a lock acquired by `code_version`. However, if you have
%% regular functions, any call to them will bypass that lock and the old
%% code will be reloaded from disk. This will kill the process trying to
%% patch the module.
%%
%% end
%%----------------------------------------------------------------------------
-spec update(atom()) -> ok | no_return().
update(Module) ->
AbsCode = get_abs_code(Module),
Forms = replace_forms(Module, get_otp_version(), AbsCode),
Code = compile_forms(Forms),
load_code(Module, Code).
%%----------------------------------------------------------------------------
%% Internal functions
%%----------------------------------------------------------------------------
load_code(Module, Code) ->
LockId = {{?MODULE, Module}, self()},
FakeFilename = "Loaded by rabbit_common",
global:set_lock(LockId, [node()]),
case code:which(Module) of
FakeFilename ->
ok;
_ ->
unload(Module),
case code:load_binary(Module, FakeFilename, Code) of
{module, _} -> ok;
{error, Reason} -> throw({cannot_load, Module, Reason})
end
end,
global:del_lock(LockId, [node()]),
ok.
unload(Module) ->
code:soft_purge(Module),
code:delete(Module).
compile_forms(Forms) ->
case compile:forms(Forms, [debug_info, return_errors]) of
{ok, _ModName, Code} ->
Code;
{ok, _ModName, Code, _Warnings} ->
Code;
Error ->
throw({cannot_compile_forms, Error})
end.
get_abs_code(Module) ->
get_forms(get_object_code(Module)).
get_object_code(Module) ->
case code:get_object_code(Module) of
{_Mod, Code, _File} ->
Code;
error ->
throw({not_found, Module})
end.
get_forms(Code) ->
case beam_lib:chunks(Code, [abstract_code]) of
{ok, {_, [{abstract_code, {raw_abstract_v1, Forms}}]}} ->
Forms;
{ok, {Module, [{abstract_code, no_abstract_code}]}} ->
throw({no_abstract_code, Module});
{error, beam_lib, Reason} ->
throw({no_abstract_code, Reason})
end.
get_otp_version() ->
Version = erlang:system_info(otp_release),
case re:run(Version, "^[0-9][0-9]", [{capture, first, list}]) of
{match, [V]} ->
list_to_integer(V);
_ ->
%% Could be anything below R17, we are not interested
0
end.
get_original_pairs(VersionSupport) ->
[{Orig, Arity} || {Orig, Arity, _Pre, _Post} <- VersionSupport].
get_delete_pairs(true, VersionSupport) ->
[{Pre, Arity} || {_Orig, Arity, Pre, _Post} <- VersionSupport];
get_delete_pairs(false, VersionSupport) ->
[{Post, Arity} || {_Orig, Arity, _Pre, Post} <- VersionSupport].
get_rename_pairs(true, VersionSupport) ->
[{Post, Arity} || {_Orig, Arity, _Pre, Post} <- VersionSupport];
get_rename_pairs(false, VersionSupport) ->
[{Pre, Arity} || {_Orig, Arity, Pre, _Post} <- VersionSupport].
%% Pairs of {Renamed, OriginalName} functions
get_name_pairs(true, VersionSupport) ->
[{{Post, Arity}, Orig} || {Orig, Arity, _Pre, Post} <- VersionSupport];
get_name_pairs(false, VersionSupport) ->
[{{Pre, Arity}, Orig} || {Orig, Arity, Pre, _Post} <- VersionSupport].
delete_abstract_functions(ToDelete) ->
fun(Tree, Function) ->
case lists:member(Function, ToDelete) of
true ->
erl_syntax:comment(["Deleted unused function"]);
false ->
Tree
end
end.
rename_abstract_functions(ToRename, ToName) ->
fun(Tree, Function) ->
case lists:member(Function, ToRename) of
true ->
FunctionName = proplists:get_value(Function, ToName),
erl_syntax:function(
erl_syntax:atom(FunctionName),
erl_syntax:function_clauses(Tree));
false ->
Tree
end
end.
replace_forms(Module, ErlangVersion, AbsCode) ->
%% Obtain attribute containing the list of functions that must be updated
Attr = Module:module_info(attributes),
VersionSupport = proplists:get_value(erlang_version_support, Attr),
{Pre, Post} = lists:splitwith(fun({Version, _Pairs}) ->
Version > ErlangVersion
end, VersionSupport),
%% Replace functions in two passes: replace for Erlang versions > current
%% first, Erlang versions =< current afterwards.
replace_version_forms(
true, replace_version_forms(false, AbsCode, get_version_functions(Pre)),
get_version_functions(Post)).
get_version_functions(List) ->
lists:append([Pairs || {_Version, Pairs} <- List]).
replace_version_forms(IsPost, AbsCode, VersionSupport) ->
%% Get pairs of {Function, Arity} for the triggering functions, which
%% are also the final function names.
Original = get_original_pairs(VersionSupport),
%% Get pairs of {Function, Arity} for the unused version
ToDelete = get_delete_pairs(IsPost, VersionSupport),
%% Delete original functions (those that trigger the code update) and
%% the unused version ones
DeleteFun = delete_abstract_functions(ToDelete ++ Original),
AbsCode0 = replace_function_forms(AbsCode, DeleteFun),
%% Get pairs of {Function, Arity} for the current version which must be
%% renamed
ToRename = get_rename_pairs(IsPost, VersionSupport),
%% Get paris of {Renamed, OriginalName} functions
ToName = get_name_pairs(IsPost, VersionSupport),
%% Rename versioned functions with their final name
RenameFun = rename_abstract_functions(ToRename, ToName),
AbsCode1 = replace_function_forms(AbsCode0, RenameFun),
%% Adjust `-dialyzer` attribute.
AbsCode2 = fix_dialyzer_attribute(AbsCode1, ToDelete, ToName),
%% Remove exports of all versioned functions
remove_exports(AbsCode2, ToDelete ++ ToRename).
replace_function_forms(AbsCode, Fun) ->
ReplaceFunction =
fun(Tree) ->
Function = erl_syntax_lib:analyze_function(Tree),
Fun(Tree, Function)
end,
Filter = fun(Tree) ->
case erl_syntax:type(Tree) of
function -> ReplaceFunction(Tree);
_Other -> Tree
end
end,
fold_syntax_tree(Filter, AbsCode).
fix_dialyzer_attribute(AbsCode, ToDelete, ToName) ->
FixDialyzer =
fun(Tree) ->
case erl_syntax_lib:analyze_attribute(Tree) of
{dialyzer, {_, Value}} ->
FixedValue = fix_dialyzer_attribute_value(Value,
ToDelete,
ToName),
rebuild_dialyzer({dialyzer, FixedValue});
_ ->
Tree
end
end,
Filter = fun(Tree) ->
case erl_syntax:type(Tree) of
attribute -> FixDialyzer(Tree);
_ -> Tree
end
end,
fold_syntax_tree(Filter, AbsCode).
fix_dialyzer_attribute_value(Info, ToDelete, ToName)
when is_list(Info) ->
lists:map(
fun(I) ->
fix_dialyzer_attribute_value(I, ToDelete, ToName)
end,
Info);
fix_dialyzer_attribute_value({Warn, FunList}, ToDelete, ToName) ->
FixedFunList = fix_dialyzer_attribute_funlist(FunList, ToDelete, ToName),
{Warn, FixedFunList};
fix_dialyzer_attribute_value(Info, _, _)
when is_atom(Info) ->
Info.
fix_dialyzer_attribute_funlist(FunList, ToDelete, ToName)
when is_list(FunList) ->
lists:filtermap(
fun(I) ->
case fix_dialyzer_attribute_funlist(I, ToDelete, ToName) of
[] -> false;
R -> {true, R}
end
end,
FunList);
fix_dialyzer_attribute_funlist({FunName, Arity} = Fun,
ToDelete, ToName)
when is_atom(FunName) andalso is_integer(Arity) andalso Arity >= 0 ->
remove_or_rename(Fun, ToDelete, ToName);
fix_dialyzer_attribute_funlist(FunList, _, _) ->
FunList.
remove_or_rename(Fun, ToDelete, ToName) ->
case lists:member(Fun, ToDelete) of
true ->
[];
false ->
case proplists:get_value(Fun, ToName) of
undefined -> Fun;
NewName -> setelement(1, Fun, NewName)
end
end.
rebuild_dialyzer({dialyzer, Value}) ->
erl_syntax:attribute(
erl_syntax:atom(dialyzer),
[rebuild_dialyzer_value(Value)]).
rebuild_dialyzer_value(Value) when is_list(Value) ->
erl_syntax:list(
[rebuild_dialyzer_value(V) || V <- Value]);
rebuild_dialyzer_value({Warn, FunList}) ->
erl_syntax:tuple(
[rebuild_dialyzer_warn(Warn),
rebuild_dialyzer_funlist(FunList)]);
rebuild_dialyzer_value(Warn) when is_atom(Warn) ->
rebuild_dialyzer_warn(Warn).
rebuild_dialyzer_warn(Warn) when is_list(Warn) ->
erl_syntax:list(
[rebuild_dialyzer_warn(W) || W <- Warn]);
rebuild_dialyzer_warn(Warn) when is_atom(Warn) ->
erl_syntax:atom(Warn).
rebuild_dialyzer_funlist(FunList) when is_list(FunList) ->
erl_syntax:list(
[rebuild_dialyzer_funlist({N, A}) || {N, A} <- FunList]);
rebuild_dialyzer_funlist({FunName, Arity}) ->
erl_syntax:tuple([erl_syntax:atom(FunName), erl_syntax:integer(Arity)]).
filter_export_pairs(Info, ToDelete) ->
lists:filter(fun(Pair) ->
not lists:member(Pair, ToDelete)
end, Info).
remove_exports(AbsCode, ToDelete) ->
RemoveExports =
fun(Tree) ->
case erl_syntax_lib:analyze_attribute(Tree) of
{export, Info} ->
Remaining = filter_export_pairs(Info, ToDelete),
rebuild_export(Remaining);
_Other -> Tree
end
end,
Filter = fun(Tree) ->
case erl_syntax:type(Tree) of
attribute -> RemoveExports(Tree);
_Other -> Tree
end
end,
fold_syntax_tree(Filter, AbsCode).
rebuild_export(Args) ->
erl_syntax:attribute(
erl_syntax:atom(export),
[erl_syntax:list(
[erl_syntax:arity_qualifier(erl_syntax:atom(N),
erl_syntax:integer(A))
|| {N, A} <- Args])]).
fold_syntax_tree(Filter, Forms) ->
Tree = erl_syntax:form_list(Forms),
NewTree = erl_syntax_lib:map(Filter, Tree),
erl_syntax:revert_forms(NewTree).