Skip to content
Draft
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
1 change: 1 addition & 0 deletions rustler/src/nif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::sys::{c_char, c_int, c_uint, ErlNifEnv, ErlNifFunc, ErlNifTerm};

pub struct Nif {
pub name: *const c_char,
pub signature: &'static str,
pub arity: c_uint,
pub flags: c_uint,
// pub func: DEF_NIF_FUNC,
Expand Down
4 changes: 4 additions & 0 deletions rustler_codegen/src/nif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::LitStr;

const RUSTLER_TAG: &str = "?RUSTLER-NIF-SIG-V1";
const VALID_SCHEDULE_OPTIONS: [&str; 3] = ["Normal", "DirtyCpu", "DirtyIo"];

#[derive(Default)]
Expand Down Expand Up @@ -53,11 +54,14 @@ pub fn transcoder_decorator(nif_attributes: NifAttributes, fun: syn::ItemFn) ->
panic!("Only non-Control ASCII strings are supported as function names");
}

let signature_s = format!("<{RUSTLER_TAG}>{erl_func_name}/{arity}</{RUSTLER_TAG}>");

quote! {
rustler::codegen_runtime::inventory::submit!(
rustler::Nif {
name: concat!(#erl_func_name, "\0").as_ptr()
as *const rustler::codegen_runtime::c_char,
signature: #signature_s,
arity: #arity,
flags: #flags as rustler::codegen_runtime::c_uint,
raw_func: {
Expand Down
113 changes: 100 additions & 13 deletions rustler_mix/lib/rustler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,72 @@ defmodule Rustler do

defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
import Rustler, only: [sigil_CARGO: 2, sigil_RUST: 2]

otp_app = Keyword.fetch!(opts, :otp_app)
env = Application.compile_env(otp_app, __MODULE__, [])
config = Rustler.Compiler.compile_crate(otp_app, env, opts)

for resource <- config.external_resources do
@external_resource resource
end
Module.register_attribute(__MODULE__, :rustler_cargo, accumulate: true)
Module.register_attribute(__MODULE__, :rustler_rust, accumulate: true)
@rustler_opts opts
@rustler_config env
@before_compile Rustler
end
end

if config.lib do
@load_from config.load_from
@load_data config.load_data
@load_data_fun config.load_data_fun
defmacro __before_compile__(env) do
opts = Module.get_attribute(env.module, :rustler_opts)
config_env = Module.get_attribute(env.module, :rustler_config)
cargo_blocks = Module.get_attribute(env.module, :rustler_cargo) |> Enum.reverse()
rust_blocks = Module.get_attribute(env.module, :rustler_rust) |> Enum.reverse()
otp_app = Keyword.fetch!(opts, :otp_app)
crate = config_env[:crate] || opts[:crate] || otp_app

opts =
if cargo_blocks == [] and rust_blocks == [] do
opts
else
path =
config_env[:path] || opts[:path] ||
if mix_application?() do
Path.join(["native", to_string(crate)])
else
Path.join([".rustler", to_string(crate)])
end

generate_crate(path, cargo_blocks, rust_blocks)
Keyword.put(opts, :path, path)
end

@before_compile Rustler
config = Rustler.Compiler.compile_crate(otp_app, config_env, opts)

resources =
Enum.map(config.external_resources, fn resource ->
quote do
@external_resource unquote(resource)
end
end)
|> Kernel.++([
quote do
@external_resource unquote(Rustler.Compiler.signature_file(config))
end
])

if config.lib do
compile_module(env, config, resources)
else
quote do
(unquote_splicing(resources))
end
end
end

defmacro __before_compile__(env) do
defp compile_module(env, config, resources) do
default_load_data_value = %Rustler.Compiler.Config{}.load_data
default_fun_value = %Rustler.Compiler.Config{}.load_data_fun

load_data = Module.get_attribute(env.module, :load_data)
load_data_fun = Module.get_attribute(env.module, :load_data_fun)
load_data = config.load_data
load_data_fun = config.load_data_fun

load_data =
case {load_data, load_data_fun} do
Expand Down Expand Up @@ -145,7 +187,22 @@ defmodule Rustler do
"""
end

stubs =
for {name, arity} <- config.signatures,
fun = String.to_atom(name),
not Module.defines?(env.module, {fun, arity}) do
args = Macro.generate_arguments(arity, env.module)

quote do
@doc false
def unquote(fun)(unquote_splicing(args)), do: :erlang.nif_error(:nif_not_loaded)
end
end

quote do
unquote_splicing(resources)
unquote_splicing(stubs)

@on_load :rustler_init

@doc false
Expand All @@ -154,7 +211,7 @@ defmodule Rustler do
# {:error, {:upgrade, 'Upgrade not supported by this NIF library.'}}
:code.purge(__MODULE__)

{otp_app, path} = @load_from
{otp_app, path} = unquote(Macro.escape(config.load_from))

load_path =
otp_app
Expand All @@ -167,4 +224,34 @@ defmodule Rustler do
end
end
end

defp mix_application? do
Process.whereis(Mix.ProjectStack) && Mix.Project.config()[:app]
end

defp generate_crate(path, cargo_blocks, rust_blocks) do
if rust_blocks != [] do
if cargo_blocks == [] do
Mix.shell().error("Must define a ~CARGO block for inline ~RUST")
raise "Missing ~CARGO block"
end

File.mkdir_p!(Path.join(path, "src"))

File.write!(Path.join(path, "Cargo.toml"), Enum.join(cargo_blocks, "\n"))
File.write!(Path.join([path, "src", "lib.rs"]), Enum.join(rust_blocks, "\n"))
end
end

defmacro sigil_CARGO({:<<>>, _, _} = content, []) do
quote do
@rustler_cargo unquote(content)
end
end

defmacro sigil_RUST({:<<>>, _, _} = content, []) do
quote do
@rustler_rust unquote(content)
end
end
end
39 changes: 38 additions & 1 deletion rustler_mix/lib/rustler/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,31 @@ defmodule Rustler.Compiler do
end
end

config
%{config | signatures: read_signatures(config)}
end

@doc false
def signature_file(config) do
if config.skip_compilation? do
{otp_app, path} = config.load_from
:rustler.signature_path(Application.app_dir(otp_app, path))
else
:rustler.signature_path(
Path.join(priv_dir(), output_file(config.crate, :lib, config.target))
)
end
end

defp read_signatures(config) do
if config.lib do
case :rustler.read_signatures(signature_file(config)) do
{:ok, signatures} -> signatures
{:error, :enoent} -> []
{:error, reason} -> raise "Failed to read Rustler signatures: #{inspect(reason)}"
end
else
[]
end
end

defp do_compile(config, recompile \\ false) do
Expand Down Expand Up @@ -155,6 +179,19 @@ defmodule Rustler.Compiler do
Mix.shell().info("Copying #{artifact.filename} to #{destination_lib}")
File.rm(destination_lib)
File.cp!(artifact.filename, destination_lib)

if artifact.kind == :lib do
case :rustler.write_signatures(
destination_lib,
:rustler.signature_path(destination_lib)
) do
:ok ->
:ok

{:error, reason} ->
raise "Failed to write Rustler signatures: #{inspect(reason)}"
end
end
end
end)
end
Expand Down
1 change: 1 addition & 0 deletions rustler_mix/lib/rustler/compiler/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule Rustler.Compiler.Config do
mode: :release,
otp_app: nil,
path: "",
signatures: [],
skip_compilation?: false,
target: nil,
metadata: nil
Expand Down
127 changes: 127 additions & 0 deletions rustler_mix/src/rustler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
-module(rustler).

-export([
get_path/2,
get_signatures/2,
signature_path/1,
write_signatures/2,
read_signatures/1
]).

-define(CHUNK_SIZE, 65536). % 64 KB chunks

get_path(Application, Crate) ->
filename:join([
code:priv_dir(Application),
binary:join([Crate, platform_ext()], <<".">>)
]).

get_signatures(Application, Crate) ->
Path = get_path(Application, Crate),
extract_signatures(Path, 1).

signature_path(LibraryPath) ->
RootPath = filename:rootname(LibraryPath, filename:extension(LibraryPath)),
<<RootPath/binary, ".signature-v1.txt">>.

write_signatures(LibraryPath, SignaturePath) ->
case extract_signatures(LibraryPath, 1) of
Signatures when is_list(Signatures) ->
Lines = [signature_line(Signature) || Signature <- Signatures],
file:write_file(SignaturePath, Lines);
{error, Reason} ->
{error, Reason}
end.

read_signatures(SignaturePath) ->
case file:read_file(SignaturePath) of
{ok, Content} ->
{ok, [parse_signature_line(Line) ||
Line <- binary:split(Content, <<"\n">>, [global]),
Line =/= <<>>]};
{error, Reason} ->
{error, Reason}
end.

signature_line(Signature) ->
{Name, Arity} = parse_signature_line(Signature),
[Name, "/", integer_to_binary(Arity), "\n"].

parse_signature_line(Signature) ->
[ArityBin | ReversedName] =
lists:reverse(binary:split(Signature, <<"/">>, [global])),
{iolist_to_binary(lists:join(<<"/">>, lists:reverse(ReversedName))),
binary_to_integer(ArityBin)}.

platform_ext() ->
platform_ext(os:type()).

platform_ext(Platform) ->
case Platform of
{unix, _} -> <<".so">>;
{win32, _} -> <<".dll">>
end.

%% Public API: Pass the target version (e.g., 1)
extract_signatures(FilePath, Version) when is_integer(Version) ->
VBin = integer_to_binary(Version),
StartTag = iolist_to_binary([<<"<?RUSTLER-NIF-SIG-V">>, VBin, <<">">>]),
EndTag = iolist_to_binary([<<"</?RUSTLER-NIF-SIG-V">>, VBin, <<">">>]),

case file:open(FilePath, [read, binary, raw, {read_ahead, ?CHUNK_SIZE}]) of
{ok, IoDevice} ->
try
read_chunks(IoDevice, StartTag, EndTag, <<>>, [])
after
file:close(IoDevice)
end;
{error, Reason} ->
{error, Reason}
end.

%% Internal: Stream chunks through file:read/2
read_chunks(IoDevice, StartTag, EndTag, AccBin, Signatures) ->
case file:read(IoDevice, ?CHUNK_SIZE) of
{ok, Data} ->
{NewAcc, NewSigs} = parse_binary(<<AccBin/binary, Data/binary>>, StartTag, EndTag, Signatures),
read_chunks(IoDevice, StartTag, EndTag, NewAcc, NewSigs);
eof ->
{_, FinalSigs} = parse_binary(AccBin, StartTag, EndTag, Signatures),
lists:reverse(FinalSigs);
{error, Reason} ->
{error, Reason}
end.

%% Internal: Dynamic matching with memory cleanup & boundary protection
parse_binary(Bin, StartTag, EndTag, Signatures) ->
case binary:match(Bin, StartTag) of
{StartPos, StartLen} ->
<<_:StartPos/binary, _:StartLen/binary, Rest/binary>> = Bin,
case binary:match(Rest, EndTag) of
{EndPos, EndTagLen} ->
<<Sig:EndPos/binary, _Rest2/binary>> = Rest,
<<_:EndPos/binary, _:EndTagLen/binary, Remaining/binary>> = Rest,

% Copy signature to local heap, releasing the massive chunk buffer
CopiedSig = binary:copy(Sig),
parse_binary(Remaining, StartTag, EndTag, [CopiedSig | Signatures]);
nomatch ->
% End tag is cut off by chunk boundary. Keep from StartTag onwards.
<<_:StartPos/binary, KeepBin/binary>> = Bin,
{KeepBin, Signatures}
end;
nomatch ->
% No start tag found. Keep only the trailing tail (TagLen - 1 bytes)
% in case a start tag was split right across the chunk boundary.
TagLen = byte_size(StartTag),
BinLen = byte_size(Bin),
if
BinLen >= TagLen ->
KeepLen = TagLen - 1,
SkipLen = BinLen - KeepLen,
<<_:SkipLen/binary, KeepBin/binary>> = Bin,
{KeepBin, Signatures};
true ->
{Bin, Signatures}
end
end.
Loading
Loading