1.8 KiB · text History bbb2a48
defmodule Vigil.NativeFixtures do
  @moduledoc """
  Native crash events against test/support/fixtures/native/crash (an ELF
  built from crash.c beside it), as sentry-native would send them: the
  binary loaded at #{inspect(0x555555554000, base: :hex)}, three frames
  oldest first (main → run → compute, which `scale` is inlined into).
  """

  @base 0x555555554000

  def debug_id, do: "17a26fb0-52e9-5d5a-f357-3b8717704094"
  def code_id, do: "b06fa217e9525a5df3573b87177040946738b942"
  def fixture_path, do: "test/support/fixtures/native/crash"
  def base, do: @base

  @doc "The image as sentry-native lists it in `debug_meta.images`."
  def image(overrides \\ %{}) do
    Map.merge(
      %{
        "type" => "elf",
        "code_file" => "/opt/app/crash",
        "debug_id" => debug_id(),
        "code_id" => code_id(),
        "image_addr" => hex(@base),
        "image_size" => 0x4028,
        "arch" => "x86_64"
      },
      overrides
    )
  end

  @doc "Frames, oldest first: return addresses in main and run, the crash in compute."
  def frames do
    for offset <- [0x1069, 0x11A5, 0x1170] do
      %{"instruction_addr" => hex(@base + offset)}
    end
  end

  def crash_event(overrides \\ %{}) do
    Map.merge(
      %{
        "event_id" => Vigil.Schema.uuid7() |> String.replace("-", ""),
        "platform" => "native",
        "level" => "fatal",
        "exception" => %{
          "values" => [
            %{
              "type" => "SIGSEGV",
              "value" => "Segfault",
              "mechanism" => %{"type" => "signalhandler", "handled" => false},
              "stacktrace" => %{"frames" => frames()}
            }
          ]
        },
        "debug_meta" => %{"images" => [image()]}
      },
      overrides
    )
  end

  def hex(n), do: "0x" <> String.downcase(Integer.to_string(n, 16))
end