Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix crash when calling a module with an on_load attribute from a nati…
…ve module The fix consists in adding a new opcode for hipe stub calls. It is based on the following understanding of the loader: when a module is loaded, the export entries are first allocated as stubs to call_error_handler, which loads the function or generates an error. Then, the entries are updated with the address to the function, except when the module has an on_load attribute. Export entries of modules with an on_load attribute are not made callable immediatly. Instead, the function address is saved and the on_load function is called. If this succeeds, the entry is finally fixed. The issue is that hipe can generate a stub with a pointer to the call_error_handler opcode set the loader before on_load function is called. The fix consists in updating the call_error_handler opcode with a new opcode that simply calls the loaded function.
- Loading branch information
Showing
8 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
%% | ||
%% %CopyrightBegin% | ||
%% | ||
%% Copyright Ericsson AB 2010-2010. All Rights Reserved. | ||
%% | ||
%% The contents of this file are subject to the Erlang Public License, | ||
%% Version 1.1, (the "License"); you may not use this file except in | ||
%% compliance with the License. You should have received a copy of the | ||
%% Erlang Public License along with this software. If not, it can be | ||
%% retrieved online at http://www.erlang.org/. | ||
%% | ||
%% Software distributed under the License is distributed on an "AS IS" | ||
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See | ||
%% the License for the specific language governing rights and limitations | ||
%% under the License. | ||
%% | ||
%% %CopyrightEnd% | ||
%% | ||
|
||
-module(hipe_SUITE). | ||
|
||
-include("test_server.hrl"). | ||
|
||
%% Test server specific exports | ||
-export([all/1, init_per_suite/1, end_per_suite/1]). | ||
|
||
%% Test cases | ||
-export([call_beam_module_with_on_load/1]). | ||
|
||
all(suite) -> | ||
[call_beam_module_with_on_load]. | ||
|
||
init_per_suite(Config) when is_list(Config) -> | ||
case erlang:system_info(hipe_architecture) of | ||
undefined -> | ||
{skip, "Native code support is not enabled"}; | ||
_ -> | ||
Config | ||
end. | ||
|
||
end_per_suite(Config) when is_list(Config) -> | ||
ok. | ||
|
||
call_beam_module_with_on_load(suite) -> | ||
[]; | ||
call_beam_module_with_on_load(doc) -> | ||
["Test that we can call a non-native module with a on_load from a native module"]; | ||
call_beam_module_with_on_load(Config) when is_list(Config) -> | ||
?line DataDir = ?config(data_dir, Config), | ||
?line CallerModule = filename:join(DataDir, "native_caller.erl"), | ||
?line CalleeModule = filename:join(DataDir, "beam_callee.erl"), | ||
?line PrivDir = ?config(priv_dir, Config), | ||
test_call_with_on_load(PrivDir, CallerModule, CalleeModule). | ||
|
||
test_call_with_on_load(PrivDir, CallerModule, CalleeModule) -> | ||
% Compile both modules. | ||
?line {ok, CallerModuleName} = compile:file(CallerModule, [{outdir, PrivDir}]), | ||
?line {ok, CalleeModuleName} = compile:file(CalleeModule, [{outdir, PrivDir}]), | ||
% Add PrivDir to the path so we can load modules interactively. | ||
AbsPrivDir = filename:absname(PrivDir), | ||
?line true = code:add_patha(AbsPrivDir), | ||
% Call the function in caller module. | ||
?line ok = CallerModuleName:call(CalleeModuleName), | ||
% Purge both modules now. | ||
?line true = code:soft_purge(CallerModuleName), | ||
?line true = code:soft_purge(CalleeModuleName), | ||
% Remove PrivDir from the path. | ||
?line true = code:del_path(AbsPrivDir), | ||
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-module(beam_callee). | ||
-on_load(on_load/0). | ||
-export([f/0]). | ||
|
||
on_load() -> | ||
ok. | ||
|
||
f() -> | ||
false = code:is_module_native(?MODULE), | ||
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-module(native_caller). | ||
-compile([native]). | ||
-export([call/1]). | ||
|
||
call(CalleeModuleName) -> | ||
true = code:is_module_native(?MODULE), | ||
ok = CalleeModuleName:f(), | ||
ok = CalleeModuleName:f(), | ||
ok. |