Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions lib/rustler_precompiled.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ defmodule RustlerPrecompiled do

%{"x86_64-unknown-linux-gnu" => [old_glibc: fn _config -> has_old_glibc?() end]}

* `:load_data` - Any valid term. This value is passed into the NIF when it is loaded.
Defaults to `0`.

* `:load_data_fun` - A `{module, function}` tuple naming a zero-arity function that
returns the load data. Cannot be combined with `:load_data`.

In case "force build" is used, all options except the ones use by RustlerPrecompiled
are going to be passed down to `Rustler`.
So if you need to configure the build, check the `Rustler` options.
Expand Down Expand Up @@ -198,7 +204,6 @@ defmodule RustlerPrecompiled do
{:ok, config} ->
@on_load :load_rustler_precompiled
@rustler_precompiled_load_from config.load_from
@rustler_precompiled_load_data config.load_data

@doc false
def load_rustler_precompiled do
Expand All @@ -212,7 +217,22 @@ defmodule RustlerPrecompiled do
|> Application.app_dir(path)
|> to_charlist()

:erlang.load_nif(load_path, @rustler_precompiled_load_data)
:erlang.load_nif(load_path, __rustler_precompiled_load_data__())
end

if config.load_data_fun do
@rustler_precompiled_load_data_fun config.load_data_fun

# Called on load, not at compile time, like `Rustler` does:
# https://github.com/rusterlium/rustler/blob/rustler-0.38.0/rustler_mix/lib/rustler.ex#L152-L167
defp __rustler_precompiled_load_data__ do
{module, function} = @rustler_precompiled_load_data_fun
apply(module, function, [])
end
else
@rustler_precompiled_load_data config.load_data

defp __rustler_precompiled_load_data__, do: @rustler_precompiled_load_data
end

{:error, precomp_error} ->
Expand Down Expand Up @@ -744,7 +764,8 @@ defmodule RustlerPrecompiled do
result = %{
load?: true,
load_from: {name, Path.join("priv/native", lib_name)},
load_data: config.load_data
load_data: config.load_data,
load_data_fun: config.load_data_fun
}

if File.exists?(cached_tar_gz) do
Expand Down
32 changes: 30 additions & 2 deletions lib/rustler_precompiled/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule RustlerPrecompiled.Config do
:crate,
:base_cache_dir,
:load_data,
:load_data_fun,
:force_build?,
:targets,
:nif_versions,
Expand Down Expand Up @@ -54,15 +55,18 @@ defmodule RustlerPrecompiled.Config do
|> Keyword.get(:nif_versions, @default_nif_versions)
|> validate_list!(:nif_versions, @available_nif_versions)

# Defaults to 0 like Rustler
load_data = opts[:load_data] || 0

%__MODULE__{
otp_app: otp_app,
base_url: base_url,
module: Keyword.fetch!(opts, :module),
version: version,
force_build?: pre_release?(version) or Keyword.fetch!(opts, :force_build),
crate: opts[:crate],
# Default to `0` like `Rustler`.
load_data: opts[:load_data] || 0,
load_data: load_data,
load_data_fun: validate_load_data_fun!(load_data, opts[:load_data_fun]),
base_cache_dir: opts[:base_cache_dir],
targets: targets,
nif_versions: nif_versions,
Expand Down Expand Up @@ -111,6 +115,30 @@ defmodule RustlerPrecompiled.Config do
end
end

# Same validations as done by Rustler
# https://github.com/rusterlium/rustler/blob/rustler-0.38.0/rustler_mix/lib/rustler.ex#L114-L146
Comment thread
philss marked this conversation as resolved.
Resolved
defp validate_load_data_fun!(_load_data, nil), do: nil

defp validate_load_data_fun!(0, {module, function} = load_data_fun)
when is_atom(module) and is_atom(function) do
load_data_fun
end

defp validate_load_data_fun!(0, provided_value) do
raise """
`load_data_fun` has to be `{Module, :function}`.
Instead received: #{inspect(provided_value)}
"""
end

defp validate_load_data_fun!(load_data, load_data_fun) do
raise """
Only `load_data` or `load_data_fun` can be provided. Instead received:
>>> load_data: #{inspect(load_data)}
>>> load_data_fun: #{inspect(load_data_fun)}
"""
end

defp validate_list!(nil, option, _valid_values), do: raise_for_nil_field_value(option)

defp validate_list!([_ | _] = values, option, valid_values) do
Expand Down
52 changes: 52 additions & 0 deletions test/rustler_precompiled/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,58 @@ defmodule RustlerPrecompiled.ConfigTest do
end
end

test "new/1 accepts `load_data_fun`" do
config =
Config.new(
otp_app: :rustler_precompiled,
module: RustlerPrecompilationExample.Native,
base_url:
"https://github.com/philss/rustler_precompilation_example/releases/download/v0.2.0",
version: "0.2.0-dev",
load_data_fun: {RustlerPrecompilationExample.Native, :load_data}
)

assert config.load_data_fun == {RustlerPrecompilationExample.Native, :load_data}
end

test "new/1 validates `load_data_fun`" do
opts = [
otp_app: :rustler_precompiled,
module: RustlerPrecompilationExample.Native,
base_url:
"https://github.com/philss/rustler_precompilation_example/releases/download/v0.2.0",
version: "0.2.0-dev"
]

assert_raise RuntimeError,
"""
`load_data_fun` has to be `{Module, :function}`.
Instead received: {RustlerPrecompilationExample.Native, :load_data, []}
""",
fn ->
Config.new(
opts ++
[load_data_fun: {RustlerPrecompilationExample.Native, :load_data, []}]
)
end

assert_raise RuntimeError,
"""
Only `load_data` or `load_data_fun` can be provided. Instead received:
>>> load_data: :data
>>> load_data_fun: {RustlerPrecompilationExample.Native, :load_data}
""",
fn ->
Config.new(
opts ++
[
load_data: :data,
load_data_fun: {RustlerPrecompilationExample.Native, :load_data}
]
)
end
end

test "new/1 validates the given targets" do
opts = [
otp_app: :rustler_precompiled,
Expand Down
51 changes: 51 additions & 0 deletions test/rustler_precompiled_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,50 @@ defmodule RustlerPrecompiledTest do
end
end

@tag :tmp_dir
test "use RustlerPrecompiled calls `load_data_fun` every time the module is loaded", %{
tmp_dir: tmp_dir
} do
nif_fixtures_dir = Path.join(File.cwd!(), "test/fixtures")
checksum_sample = File.read!(Path.join(nif_fixtures_dir, "checksum-sample-file.exs"))
Process.register(self(), :load_data_fun_test)

in_tmp(tmp_dir, fn ->
File.write!("checksum-Elixir.RustlerPrecompilationExample.Native.exs", checksum_sample)

{[{module, binary}], _log} =
with_log(fn ->
Code.compile_quoted(
quote do
defmodule RustlerPrecompilationExample.Native do
use RustlerPrecompiled,
otp_app: :rustler_precompiled,
crate: "example",
base_url:
"https://github.com/philss/rustler_precompilation_example/releases/download/v0.2.0",
base_cache_dir: unquote(nif_fixtures_dir),
version: "0.2.0",
force_build: false,
load_data_fun: {RustlerPrecompiledTest.LoadData, :load_data}

def add(_a, _b), do: :erlang.nif_error(:nif_not_loaded)
end
end
)
end)

assert_receive :load_data_called
assert module.add(1, 2) == 3

# Loading the same binary again calls it again,
# so the data is not fixed at compile time.
:code.delete(module)
:code.purge(module)
assert {:module, ^module} = :code.load_binary(module, ~c"nofile", binary)
assert_receive :load_data_called
end)
end

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leandrocp This test is giving a SIGSEGV for me (like it does on CI), and I couldn't figure out how to fix it.

Please give it a try, but if you can't, I'm comfortable with proceeding without this integration test.

@leandrocp leandrocp Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @philss I was able to reproduce on a Linux container (it works on my local Mac). I think the root cause was caused by 051306b where it added the archive extension so File.rm/1 stopped working silently and the tests in this PR tries to reload an existing NIF and it crashes. But I can't explain why it works in some envs but not others so I'm not completely sure.

Note that 6e7d7bc matches File.rm/1 returns. I think it's correct but I'm happy to make more changes! Thanks for your time reviewing this!

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, and I agree it looks correct. Would you mind to open a isolated PR with that commit? I think it would be better for documenting the fix. We can rebase it after the merge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a problem! Done #109

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's odd that we don't have a conflict here, but please rebase so I can merge with only the changes for the load_data_fun.

@leandrocp leandrocp Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied the same patch in #109 and just added one test so I think that wouldn't create a conflict :)


describe "build_metadata/1" do
test "builds a valid metadata" do
config =
Expand Down Expand Up @@ -1218,6 +1262,13 @@ defmodule RustlerPrecompiledTest do
end
end

defmodule LoadData do
def load_data do
send(:load_data_fun_test, :load_data_called)
:load_data
end
end

def in_tmp(tmp_path, function) do
path = Path.join([tmp_path, random_string(10)])

Expand Down
Loading