forked from erlang/otp
-
Notifications
You must be signed in to change notification settings - Fork 1
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 functions in a module with an on_load attribut…
…e from a native 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
83 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,35 @@ | ||
-module(hipe_SUITE). | ||
|
||
-include("test_server.hrl"). | ||
|
||
-export([all/1,call_beam_module_with_on_load/1]). | ||
|
||
all(suite) -> | ||
[call_beam_module_with_on_load]. | ||
|
||
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. |