Download lib/forge/beam.ex from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 6.53 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/lib/forge/beam.ex
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/lib/forge/beam.ex
-
curl -L -o beam.ex https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/lib/forge/beam.ex
6.53 kB
| defmodule Forge.Beam do | |
| @moduledoc """ | |
| Beam - BEAM file analysis and metadata extraction. | |
| This module provides comprehensive analysis of BEAM compiled files, | |
| extracting metadata, function information, and instruction sequences. | |
| ## BEAM File Structure | |
| A BEAM file contains: | |
| - Module identity (atom) | |
| - Exports list | |
| - Imports list | |
| - Function definitions | |
| - Function atoms, strings, literals | |
| - Code section with instructions | |
| - Debug information (when present) | |
| - Line information (when present) | |
| - Compiler metadata | |
| """ | |
| alias Forge.Hash | |
| alias Forge.Provenance | |
| @type beam_metadata :: %{ | |
| module: atom() | nil, | |
| exports: list(atom()) | nil, | |
| imports: list({atom(), integer()}) | nil, | |
| attributes: map() | nil, | |
| functions: list(Forge.Beam.Function.t()) | nil, | |
| atoms: list(atom()) | nil, | |
| strings: list(String.t()) | nil, | |
| literals: list(any()) | nil, | |
| version: String.t() | nil, | |
| source: String.t() | nil, | |
| compiler: String.t() | nil, | |
| md5: String.t() | nil, | |
| debug_info: boolean(), | |
| line_info: boolean(), | |
| raw_size: integer(), | |
| section_offsets: %{atom() => integer()} | |
| } | |
| @type instruction :: %{ | |
| opcode: atom(), | |
| args: list(any()), | |
| offset: integer(), | |
| raw_bytes: binary(), | |
| operands: list(map()), | |
| label: String.t() | nil, | |
| comments: list(String.t()) | |
| } | |
| @doc """ | |
| Reads and analyzes a BEAM file. | |
| ## Parameters | |
| - path: Path to the BEAM file | |
| - options: Analysis options | |
| ## Returns | |
| - {:ok, metadata} with complete BEAM analysis | |
| - {:error, reason} on failure | |
| """ | |
| @spec read(String.t(), map()) :: {:ok, beam_metadata()} | {:error, String.t()} | |
| def read(path, _options \\ []) do | |
| try do | |
| beam = :beam_lib.read(path) | |
| metadata = extract_metadata(beam, path) | |
| {:ok, metadata} | |
| rescue | |
| e -> {:error, "BEAM read failed: #{inspect(e)}"} | |
| end | |
| end | |
| @doc """ | |
| Extracts metadata from a beam_lib beam structure. | |
| """ | |
| defp extract_metadata(beam, path) do | |
| %{ | |
| module: beam.module, | |
| exports: beam.export_list || [], | |
| imports: extract_imports(beam), | |
| attributes: extract_attributes(beam), | |
| functions: extract_functions(beam), | |
| atoms: extract_atoms(beam), | |
| strings: extract_strings(beam), | |
| literals: extract_literals(beam), | |
| version: beam.version, | |
| source: beam.source, | |
| compiler: beam.compiler, | |
| md5: beam.md5, | |
| debug_info: has_debug_info?(beam), | |
| line_info: has_line_info?(beam), | |
| raw_size: File.stat!(path).size, | |
| section_offsets: extract_section_offsets(beam) | |
| } | |
| end | |
| @doc """ | |
| Extracts import information from BEAM. | |
| """ | |
| defp extract_imports(beam) do | |
| beam.import_list || [] | |
| end | |
| @doc """ | |
| Extracts module attributes from BEAM. | |
| """ | |
| defp extract_attributes(beam) do | |
| beam.attributes || %{} | |
| end | |
| @doc """ | |
| Extracts function information from BEAM. | |
| """ | |
| defp extract_functions(beam) do | |
| case beam.functions do | |
| nil -> [] | |
| functions -> | |
| Enum.map(functions, fn func -> | |
| %Forge.Beam.Function{ | |
| name: func.name, | |
| arity: func.arity, | |
| index: func.index, | |
| labels: func.labels || [], | |
| instructions: extract_instructions(func) | |
| } | |
| end) | |
| end | |
| end | |
| @doc """ | |
| Extracts instruction list from a function. | |
| """ | |
| defp extract_instructions(func) do | |
| case func.code do | |
| nil -> [] | |
| code -> | |
| Enum.map(code, fn {opcode, args, offset} -> | |
| %{ | |
| opcode: opcode, | |
| args: args, | |
| offset: offset, | |
| raw_bytes: <<>>, | |
| operands: [], | |
| label: nil, | |
| comments: [] | |
| } | |
| end) | |
| end | |
| end | |
| @doc """ | |
| Extracts atom table from BEAM. | |
| """ | |
| defp extract_atoms(beam) do | |
| beam.atoms || [] | |
| end | |
| @doc """ | |
| Extracts string table from BEAM. | |
| """ | |
| defp extract_strings(beam) do | |
| beam.strings || [] | |
| end | |
| @doc """ | |
| Extracts literal table from BEAM. | |
| """ | |
| defp extract_literals(beam) do | |
| beam.literals || [] | |
| end | |
| @doc """ | |
| Checks if BEAM file has debug information. | |
| """ | |
| defp has_debug_info?(beam) do | |
| beam.debug_info != nil && beam.debug_info != [] | |
| end | |
| @doc """ | |
| Checks if BEAM file has line information. | |
| """ | |
| defp has_line_info?(beam) do | |
| beam.line_info != nil && beam.line_info != [] | |
| end | |
| @doc """ | |
| Extracts section offsets from BEAM. | |
| """ | |
| defp extract_section_offsets(beam) do | |
| %{ | |
| code: beam.code_offset || 0, | |
| atoms: beam.atom_offset || 0, | |
| literals: beam.literal_offset || 0, | |
| functions: beam.function_offset || 0, | |
| imports: beam.import_offset || 0, | |
| exports: beam.export_offset || 0 | |
| } | |
| end | |
| @doc """ | |
| Gets instruction count for a BEAM file. | |
| """ | |
| def instruction_count(path) do | |
| case read(path) do | |
| {:ok, metadata} -> | |
| case metadata.functions do | |
| nil -> 0 | |
| functions -> | |
| Enum.reduce(functions, 0, fn func, acc -> | |
| acc + length(func.instructions) | |
| end) | |
| end | |
| {:error, _} -> 0 | |
| end | |
| end | |
| @doc """ | |
| Gets function count for a BEAM file. | |
| """ | |
| def function_count(path) do | |
| case read(path) do | |
| {:ok, metadata} -> length(metadata.functions || []) | |
| {:error, _} -> 0 | |
| end | |
| end | |
| @doc """ | |
| Gets module name from a BEAM file. | |
| """ | |
| def module_name(path) do | |
| case read(path) do | |
| {:ok, metadata} -> metadata.module | |
| {:error, _} -> nil | |
| end | |
| end | |
| @doc """ | |
| Checks if a BEAM file is valid. | |
| """ | |
| def valid?(path) do | |
| case read(path) do | |
| {:ok, _} -> true | |
| {:error, _} -> false | |
| end | |
| end | |
| @doc """ | |
| Gets all exported functions from a BEAM file. | |
| """ | |
| def exports(path) do | |
| case read(path) do | |
| {:ok, metadata} -> metadata.exports || [] | |
| {:error, _} -> [] | |
| end | |
| end | |
| @doc """ | |
| Gets all imported modules from a BEAM file. | |
| """ | |
| def imports(path) do | |
| case read(path) do | |
| {:ok, metadata} -> metadata.imports || [] | |
| {:error, _} -> [] | |
| end | |
| end | |
| end | |
| # Define the Function struct for beam analysis | |
| defmodule Forge.Beam.Function do | |
| @moduledoc """Function information extracted from BEAM files.""" | |
| defstruct [ | |
| :name, | |
| :arity, | |
| :index, | |
| :labels, | |
| :instructions | |
| ] | |
| @type t :: %__MODULE__{} | |
| end | |