Argon2_elixir will not run on Apple Silicon

Using macOS Monterey 12.1 with M1 Pro.

Elixir version:

$ elixir --version
Erlang/OTP 24 [erts-12.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [dtrace]

Elixir 1.13.1 (compiled with Erlang/OTP 24)

Argon2 just doesn’t seem to want to compile.
Whenever I try to use Argon2, this happens:

iex(1)> Argon2.Base.init()
** (UndefinedFunctionError) function Argon2.Base.init/0 is undefined (module Argon2.Base is not available)
    (argon2_elixir 2.4.0) Argon2.Base.init()
iex(1)> 
20:37:46.896 [error] Process #PID<0.436.0> raised an exception
** (RuntimeError) An error occurred when loading Argon2.
Make sure you have a C compiler and Erlang 20 installed.
If you are not using Erlang 20, either upgrade to Erlang 20 or
use bcrypt_elixir (version 0.12) or pbkdf2_elixir.
See the Comeonin wiki for more information.

    (argon2_elixir 2.4.0) lib/argon2/base.ex:19: Argon2.Base.init/0
    (kernel 8.2) code_server.erl:1317: anonymous fn/1 in :code_server.handle_on_load/5
 
20:37:46.903 [error] Process #PID<0.438.0> raised an exception
** (RuntimeError) An error occurred when loading Argon2.
Make sure you have a C compiler and Erlang 20 installed.
If you are not using Erlang 20, either upgrade to Erlang 20 or
use bcrypt_elixir (version 0.12) or pbkdf2_elixir.
See the Comeonin wiki for more information.

    (argon2_elixir 2.4.0) lib/argon2/base.ex:19: Argon2.Base.init/0
    (kernel 8.2) code_server.erl:1317: anonymous fn/1 in :code_server.handle_on_load/5
 
20:37:46.897 [warning] The on_load function for module Elixir.Argon2.Base returned:
{%RuntimeError{
   message: "An error occurred when loading Argon2.\nMake sure you have a C compiler and Erlang 20 installed.\nIf you are not using Erlang 20, either upgrade to Erlang 20 or\nuse bcrypt_elixir (version 0.12) or pbkdf2_elixir.\nSee the Comeonin wiki for more information.\n"
 },
 [
   {Argon2.Base, :init, 0,
    [file: 'lib/argon2/base.ex', line: 19, error_info: %{...}]},
   {:code_server, :"-handle_on_load/5-fun-0-", 1,
    [file: 'code_server.erl', line: 1317]}
 ]}

 
20:37:46.908 [warning] The on_load function for module Elixir.Argon2.Base returned:
{%RuntimeError{
   message: "An error occurred when loading Argon2.\nMake sure you have a C compiler and Erlang 20 installed.\nIf you are not using Erlang 20, either upgrade to Erlang 20 or\nuse bcrypt_elixir (version 0.12) or pbkdf2_elixir.\nSee the Comeonin wiki for more information.\n"
 },
 [
   {Argon2.Base, :init, 0,
    [file: 'lib/argon2/base.ex', line: 19, error_info: %{...}]},
   {:code_server, :"-handle_on_load/5-fun-0-", 1,
    [file: 'code_server.erl', line: 1317]}
 ]}

I’ve already tried cleaning all dependencies, reinstalling them. I checked and yes I do have a C compiler. I’m starting to worry that this won’t work on Apple Silicon.

Any other suggestions? Thanks

The error suggests that it’s not finding the artifact it expects, but if that failed to compile it should show up in the output of mix deps.compile --force. Weird. :thinking:

I don’t have an M1 to test with, but one way I imagine this could happen is if the dependency compilation and the iex were mismatched - one on Rosetta and the other not.

Did some digging around, found something interesting.
Firstly, I cloned the argon2_elixir GitHub onto my M1 Pro machine.
Fetched the dependencies, and ran make.

$ make        
mix compile
==> earmark_parser
Compiling 1 file (.yrl)
Compiling 2 files (.xrl)
Compiling 3 files (.erl)
Compiling 32 files (.ex)
Generated earmark_parser app
==> erlex
Compiling 1 file (.yrl)
src/parser.yrl: Warning: conflicts: 27 shift/reduce, 0 reduce/reduce
Compiling 1 file (.xrl)
Compiling 2 files (.erl)
Compiling 1 file (.ex)
Generated erlex app
==> nimble_parsec
Compiling 4 files (.ex)
Generated nimble_parsec app
==> makeup
Compiling 44 files (.ex)
Generated makeup app
==> comeonin
Compiling 4 files (.ex)
Generated comeonin app
==> dialyxir
Compiling 59 files (.ex)
warning: Mix.Project.compile/1 is deprecated. Use Mix.Task.run("compile", args) instead
  lib/mix/tasks/dialyzer.ex:164: Mix.Tasks.Dialyzer.run/1

warning: Mix.Project.compile/1 is deprecated. Use Mix.Task.run("compile", args) instead
  lib/dialyxir/project.ex:47: Dialyxir.Project.cons_apps/0

Generated dialyxir app
==> elixir_make
Compiling 1 file (.ex)
Generated elixir_make app
==> makeup_elixir
Compiling 6 files (.ex)
Generated makeup_elixir app
==> ex_doc
Compiling 22 files (.ex)
Generated ex_doc app
==> argon2_elixir
mkdir -p /Users/joshuazacek/Desktop/argon2_elixir/_build/dev/lib/argon2_elixir/priv
make[1]: *** No rule to make target `argon2/src/argon2.c', needed by `/Users/joshuazacek/Desktop/argon2_elixir/_build/dev/lib/argon2_elixir/priv/argon2_nif.so'.  Stop.
** (Mix) Could not compile with "make" (exit status: 2).
You need to have gcc and make installed. Try running the
commands "gcc --version" and / or "make --version". If these programs
are not installed, you will be prompted to install them.

make: *** [calling_from_make] Error 1
$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i386-apple-darwin11.3.0

I’m not a C developer by any means, so I’m just relying on Stack Overflow, but it seems like some file is missing for some reason.

I did a basic installation as you did but with different results!

kip@Kips-MacBook-Pro argon % iex -S mix
Erlang/OTP 24 [erts-12.1.5] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [dtrace]

==> comeonin
Compiling 4 files (.ex)
Generated comeonin app
==> elixir_make
Compiling 1 file (.ex)
Generated elixir_make app
==> argon2_elixir
mkdir -p /Users/kip/Development/argon/_build/dev/lib/argon2_elixir/priv
cc -O2 -g -fno-stack-check -pthread -Wall -Wno-format-truncation -I"/Users/kip/.asdf/installs/erlang/24.1.7/erts-12.1.5/include" -Iargon2/include -Iargon2/src -Ic_src -dynamiclib -undefined dynamic_lookup  argon2/src/argon2.c argon2/src/core.c argon2/src/blake2/blake2b.c argon2/src/thread.c argon2/src/encoding.c argon2/src/ref.c c_src/argon2_nif.c -o /Users/kip/Development/argon/_build/dev/lib/argon2_elixir/priv/argon2_nif.so
warning: unknown warning option '-Wno-format-truncation' [-Wunknown-warning-option]
1 warning generated.
warning: unknown warning option '-Wno-format-truncation' [-Wunknown-warning-option]
1 warning generated.
warning: unknown warning option '-Wno-format-truncation' [-Wunknown-warning-option]
1 warning generated.
warning: unknown warning option '-Wno-format-truncation' [-Wunknown-warning-option]
1 warning generated.
warning: unknown warning option '-Wno-format-truncation' [-Wunknown-warning-option]
1 warning generated.
warning: unknown warning option '-Wno-format-truncation' [-Wunknown-warning-option]
1 warning generated.
warning: unknown warning option '-Wno-format-truncation' [-Wunknown-warning-option]
1 warning generated.
Compiling 3 files (.ex)
Generated argon2_elixir app
==> argon
Compiling 1 file (.ex)
Generated argon app
Interactive Elixir (1.13.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Argon2.Base.init()
** (RuntimeError) An error occurred when loading Argon2.
Make sure you have a C compiler and Erlang 20 installed.
If you are not using Erlang 20, either upgrade to Erlang 20 or
use bcrypt_elixir (version 0.12) or pbkdf2_elixir.
See the Comeonin wiki for more information.

    (argon2_elixir 2.4.0) lib/argon2/base.ex:19: Argon2.Base.init/0
iex(1)> 

My versions of gcc and make as the same as yours. I think I have the right binary format (all ARM64):

kip@Kips-MacBook-Pro argon % file ./_build/dev/lib/argon2_elixir/priv/argon2_nif.so
./_build/dev/lib/argon2_elixir/priv/argon2_nif.so: Mach-O 64-bit dynamically linked shared library arm64

Not sure what to try next, I’m well out of my depth now…

Okay, so I took the dependency straight out of the /deps folder and use that instead. Got the same results as you.

So, I dug a little deeper and went looking around to see where the error was being thrown. Turns out it came down to this function:

  defp load_nif do
    path = :filename.join(:code.priv_dir(:argon2_elixir), 'argon2_nif')
    :erlang.load_nif(path, 0)
  end

So, I ran this code directly in the IEX shell, and this happened:

iex(2)> :erlang.load_nif(:filename.join(:code.priv_dir(:argon2_elixir), 'argon2_nif'), 0)
{:error,
 {:bad_lib,
  'load_nif/2 must be explicitly called from the NIF module. It cannot be called through apply/3.'}} 

Looks like Erlang isn’t happy. Any good Erlang/C people that can parse this message?

That message basically means what it says: you can’t call :erlang.load_nif/2 from anywhere except within a nif module. So unfortunately we’re no further forward at this point.

Sorry, I ran this in the IEX shell :man_facepalming:

This time, I actually ran it in the module, and got this instead:

{:error,
 {:reload, 'NIF library already loaded (reload disallowed since OTP 20).'}}

I finally found out what it was. As someone suggested earlier in the thread, x86_64 binaries where being compiled instead of arm64 ones. Not sure why, but I found the fix.

  1. Run gcc --version, and copy the target (in my case, arm64-apple-darwin21.2.0)
$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
  1. Open /deps/argon2_elixir/Makefile and add this line: CFLAGS += -target (your target)
  2. Open mix.exs and change {:argon2_elixir: "~> 2.0"} to {:argon2_elixir, path: "deps/argon2_elixir"}
2 Likes