tim2CF

tim2CF

Decompile BEAM files to Elixir source code

Is it possible to decompile BEAM files with :debug_info chunk to actual Elixir source code? This data is from :debug_info

iex(2)> Secret |> :code.which |> :beam_lib.chunks([:debug_info]) |> elem(1) |> elem(1) |> Keyword.get(:debug_info) |> elem(2) |> elem(1) |> Map.get(:definitions)
[{{:hello, 0}, :def, [line: 26], [{[line: 26], [], [], :world}]}]

And this is actual Elixir AST

iex(3)> quote do
...(3)> defmodule Secret do
...(3)>   def hello, do: :world
...(3)> end
...(3)> end
{:defmodule, [context: Elixir, import: Kernel],
 [
   {:__aliases__, [alias: false], [:Secret]},
   [
     do: {:def, [context: Elixir, import: Kernel],
      [{:hello, [context: Elixir], Elixir}, [do: :world]]}
   ]
 ]}

If it’s possible to transform :debug_info to AST - please write how, thanks!

Most Liked Responses

Marcus

Marcus

I have also done some experiments with the BEAM file. See the little beam_file project.

With BeamFile you can create byte, erl, and elixir-code (with the limitation discussed here) from the beam file.

Example:

defmodule Example.Math do
  @moduledoc "Math is Fun"

  def add(number_a, number_b), do: number_a + number_b

  def odd_or_even(a) do
    if rem(a, 2) == 0 do
      :even
    else
      :odd
    end
  end
end
iex> {:ok, code} = BeamFile.elixir_code(Example.Math)
iex> IO.puts(code)
defmodule Elixir.Example.Math do
  @moduledoc """
  Math is Fun
  """

  def add(number_a, number_b) do
    :erlang.+(number_a, number_b)
  end

  def odd_or_even(a) do
    case(:erlang.==(:erlang.rem(a, 2), 0)) do
      false ->
        :odd

      true ->
        :even
    end
  end
end
olafura

olafura

My project works with a lot of code but not all code:

cdegroot

cdegroot

The problem is that the transformation from Elixir to BEAM bytecode loses information. A simple example:

a = 1
a = 2

will result in bytecode somewhat like

a = 1
a1= 2

Because rebinding variables isn’t possible in bytecode, so Elixir fakes it. There are plenty of such examples. Decompiling BEAM bytecode to Erlang is fairly straightforward and precise, but getting back to the original Elixir AST is, I think, almost impossible. The simpler solution would be to just stash the AST in a BEAM segment (the format is pretty extensible so while I’m not sure whether this is already happening somewhere, I’m sure it is not too hard to do this).

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement