[erlang-questions] Loading a copy of a module under a different name

Danil Zagoskin z@REDACTED
Mon Aug 5 10:35:11 CEST 2019


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 <luca.tansini95@REDACTED>
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
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>


-- 
Danil Zagoskin | z@REDACTED
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190805/7d2b50fe/attachment.htm>


More information about the erlang-questions mailing list