Loading a copy of a module under a different name

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading a copy of a module under a different name

Luca Tansini
Hi all,

I’m trying to load into the VM a copy of a module under a different name.
The way I’m trying to do it is:
- get the binary of the module with code:get_object_code(mod_name)
- replace the module name inside the binary (otherwise the code loader won’t accept it)
- reload it with code:load_binary(new_name, “”, RenamedBinary)

here is some sample code showing the approach: https://pastebin.com/HpdUR0eU.

This approach seems to work as long as the original module doesn’t contain any NIFs.
In that case the module is still loaded (not always, I’ve found at least one exception, more below), but then gives runtime errors when NIFs are trying to be run.

For instance this is what happens with the lists module:

>> module_cp:load_module_copy(lists).
{module,xists}
>> xists:seq(1,10).
[1,2,3,4,5,6,7,8,9,10]
>> xists:reverse([1,2,3,4]).
** exception error: undefined function erlang:nif_error/1
     in function  xists:reverse/2 (xists.erl, line 95)

For the erlang module instead, even the code loading fails:

>> module_cp:load_module_copy(erlang).
{error,badfile}

=ERROR REPORT==== 26-Jul-2019::13:10:11 ===
Loading of  failed: badfile

=ERROR REPORT==== 26-Jul-2019::13:10:11 ===
beam/beam_load.c(2284): Error loading function xrlang:garbage_collect/2: op bif0 u x:
  no specific operation found


I’m not very familiar with how NIFs work and are loaded, but is there a workaround for this?
Maybe renaming only the module attribute is not enough.
I’m on MacOS running Erlang/OTP 20 [erts-9.3].

Thank you



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Loading a copy of a module under a different name

dmkolesnikov
Hello Luca,

I would not give you a straight answer about NIF but I know for sure it is not enough just to load Erlang module in cases of lists. You need to load native library as well.

Typically NIF module has an association with library http://erlang.org/doc/tutorial/nif.html.
Please note that lists is BIF module, you might not be able to re-bind it with other lib.

I would suggest you to study meck implementation. It deals a lot with module re-loading

Best Regards,
Dmitry


On 29 Jul 2019, at 11.28, Luca Tansini <[hidden email]> wrote:

Hi all,

I’m trying to load into the VM a copy of a module under a different name.
The way I’m trying to do it is:
- get the binary of the module with code:get_object_code(mod_name)
- replace the module name inside the binary (otherwise the code loader won’t accept it)
- reload it with code:load_binary(new_name, “”, RenamedBinary)

here is some sample code showing the approach: https://pastebin.com/HpdUR0eU.

This approach seems to work as long as the original module doesn’t contain any NIFs.
In that case the module is still loaded (not always, I’ve found at least one exception, more below), but then gives runtime errors when NIFs are trying to be run.

For instance this is what happens with the lists module:

>> module_cp:load_module_copy(lists).
{module,xists}
>> xists:seq(1,10).
[1,2,3,4,5,6,7,8,9,10]
>> xists:reverse([1,2,3,4]).
** exception error: undefined function erlang:nif_error/1
     in function  xists:reverse/2 (xists.erl, line 95)

For the erlang module instead, even the code loading fails:

>> module_cp:load_module_copy(erlang).
{error,badfile}

=ERROR REPORT==== 26-Jul-2019::13:10:11 ===
Loading of  failed: badfile

=ERROR REPORT==== 26-Jul-2019::13:10:11 ===
beam/beam_load.c(2284): Error loading function xrlang:garbage_collect/2: op bif0 u x:
  no specific operation found


I’m not very familiar with how NIFs work and are loaded, but is there a workaround for this?
Maybe renaming only the module attribute is not enough.
I’m on MacOS running Erlang/OTP 20 [erts-9.3].

Thank you


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Loading a copy of a module under a different name

Danil Zagoskin-2
In reply to this post by Luca Tansini
Hi!
Maybe this approach to a module renaming will be useful for you: https://github.com/klajo/hacks/blob/master/beam/src/beam_renamer.erl
A long while ago I've used beam_renamer in my experiments: https://github.com/stolen/modhack – to inject custom code in other module. I don't remember if that worked with lists module though.

As Dmitry said, it may be tricky to fiddle with NIFs and BIFs.


On Mon, Jul 29, 2019 at 2:56 PM Luca Tansini <[hidden email]> wrote:
Hi all,

I’m trying to load into the VM a copy of a module under a different name.
The way I’m trying to do it is:
- get the binary of the module with code:get_object_code(mod_name)
- replace the module name inside the binary (otherwise the code loader won’t accept it)
- reload it with code:load_binary(new_name, “”, RenamedBinary)

here is some sample code showing the approach: https://pastebin.com/HpdUR0eU.

This approach seems to work as long as the original module doesn’t contain any NIFs.
In that case the module is still loaded (not always, I’ve found at least one exception, more below), but then gives runtime errors when NIFs are trying to be run.

For instance this is what happens with the lists module:

>> module_cp:load_module_copy(lists).
{module,xists}
>> xists:seq(1,10).
[1,2,3,4,5,6,7,8,9,10]
>> xists:reverse([1,2,3,4]).
** exception error: undefined function erlang:nif_error/1
     in function  xists:reverse/2 (xists.erl, line 95)

For the erlang module instead, even the code loading fails:

>> module_cp:load_module_copy(erlang).
{error,badfile}

=ERROR REPORT==== 26-Jul-2019::13:10:11 ===
Loading of  failed: badfile

=ERROR REPORT==== 26-Jul-2019::13:10:11 ===
beam/beam_load.c(2284): Error loading function xrlang:garbage_collect/2: op bif0 u x:
  no specific operation found


I’m not very familiar with how NIFs work and are loaded, but is there a workaround for this?
Maybe renaming only the module attribute is not enough.
I’m on MacOS running Erlang/OTP 20 [erts-9.3].

Thank you


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


--
Danil Zagoskin | [hidden email]

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions