Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build for wasm #1483

Closed
Kayryu opened this issue Apr 12, 2022 · 13 comments
Closed

Build for wasm #1483

Kayryu opened this issue Apr 12, 2022 · 13 comments

Comments

@Kayryu
Copy link

Kayryu commented Apr 12, 2022

My goal is to make the Ring library usable in WASM.
In a simple example, project DEMO relies on Ring and builds project DEMO with wasm-pack, but cannot be used directly in JS. I then used wasm2wat to convert the compiled files and found the following functions is missing.

  (import "env" "GFp_nistz256_mul_mont" (func (;0;) (type 3)))
  (import "env" "GFp_nistz256_add" (func (;1;) (type 3)))
  (import "env" "GFp_nistz256_point_double" (func (;3;) (type 2)))
  (import "env" "GFp_nistz256_neg" (func (;4;) (type 2)))
  (import "env" "GFp_nistz256_point_add_affine" (func (;5;) (type 3)))
  (import "env" "GFp_bn_mul_mont" (func (;6;) (type 13)))

I discovered that the GFp_bn_mul_mont function was written in Perl, But I'm not sure perl is involved.

@maxcountryman
Copy link

I'm successfully using ring within a JavaScript-enabled WASM runtime (on the browser, specifically).

You might consider trying something like this:

[dependencies.ring]
version = "0.17.0-alpha.11"
features = ["std", "wasm32_c"]

@Kayryu
Copy link
Author

Kayryu commented May 7, 2022

Yes, if I only use ECDSA-related functions of ring, it can compile and run on the browser, but once I use Ed25519 related functions, I will get an error. The code is as follows:

use ring::{
         rand,
         signature::{self, KeyPair},
     };
use hex::FromHex;
let rng = rand::SystemRandom::new();
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
let key_pair = signature::Ed25519KeyPair::from_pkcs8(pkcs8_bytes.as_ref()).unwrap();
const MESSAGE: &[u8] = b"hello, world";
let sig = key_pair.sign(MESSAGE);

@chertov
Copy link

chertov commented Jun 2, 2022

i have successful build my application with ring from last main git for wasm on macos
we need llvm with wasm target and clang brew install llvm

LLVM_PATH=/opt/homebrew/opt/llvm/

RUSTC_WRAPPER="" \
\
CC_wasm32_unknown_unknown=${LLVM_PATH:?}/bin/clang \
CXX_wasm32_unknown_unknown=${LLVM_PATH:?}/bin/clang++ \
AS_wasm32_unknown_unknown=${LLVM_PATH:?}/bin/llvm-as \
AR_wasm32_unknown_unknown=${LLVM_PATH:?}/bin/llvm-ar \
STRIP_wasm32_unknown_unknown=${LLVM_PATH:?}/bin/llvm-strip \
\
wasm-pack build \
    --out-dir "${output_path:?}" \
    --target web \
    --release

need to comment #[cfg(not(target_arch = "wasm32"))] in ring code for 25519 DH

#[cfg(not(target_arch = "wasm32"))]

ring/src/ec.rs

Line 39 in 0f3bf00

#[cfg(not(target_arch = "wasm32"))]

#[cfg(not(target_arch = "wasm32"))]

#[cfg(not(target_arch = "wasm32"))]

@paulyoung
Copy link

Has anyone here encountered or had to work around issues with ring expecting exports that don't exist, like LIMBS_are_zero?

#1453 (comment)

I can successfully build but then my environment complains about things like Module imports function 'LIMBS_are_zero' from 'env' that is not exported by the runtime.

Do I need to try to build things differently to fix that?

I tried the env vars @chertov suggested that failed for me:

(Click to expand)
error: failed to run custom build command for `ring v0.17.0-not-released-yet (/Users/py/projects/briansmith/ring/codebase-labs/wasm32-unknown-unknown)`
Caused by:
  process didn't exit successfully: `/private/tmp/nix-build-rust-workspace-deps-unknown.drv-1/dummy-src/target/release/build/ring-7aedbc6fa20be512/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=RING_PREGENERATE_ASM
  cargo:rustc-env=RING_CORE_PREFIX=ring_core_0_17_0_not_released_yet_
  OPT_LEVEL = Some("z")
  TARGET = Some("wasm32-unknown-unknown")
  HOST = Some("aarch64-apple-darwin")
  CC_wasm32-unknown-unknown = None
  CC_wasm32_unknown_unknown = Some("clang")
  CFLAGS_wasm32-unknown-unknown = None
  CFLAGS_wasm32_unknown_unknown = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  --- stderr
  running "clang" "-Oz" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "include" "-I" "/private/tmp/nix-build-rust-workspace-deps-unknown.drv-1/dummy-src/target/wasm32-unknown-unknown/release/build/ring-e9d9bfabc386e4cd/out" "-Wall" "-Wextra" "-std=c1x" "-Wbad-function-cast" "-Wnested-externs" "-Wstrict-prototypes" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-g3" "-nostdlibinc" "-DNDEBUG" "-DRING_CORE_NOSTDLIBINC=1" "-Werror" "-c" "-o/private/tmp/nix-build-rust-workspace-deps-unknown.drv-1/dummy-src/target/wasm32-unknown-unknown/release/build/ring-e9d9bfabc386e4cd/out/curve25519.o" "crypto/curve25519/curve25519.c"
  clang-11: error: argument unused during compilation: '-mmacos-version-min=11.0' [-Werror,-Wunused-command-line-argument]
  clang-11: error: argument unused during compilation: '-nostdlibinc' [-Werror,-Wunused-command-line-argument]
  clang-11: error: argument unused during compilation: '-arch arm64' [-Werror,-Wunused-command-line-argument]
  thread 'main' panicked at 'execution failed', /Users/py/projects/briansmith/ring/codebase-labs/wasm32-unknown-unknown/build.rs:707:9
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/panicking.rs:584:5
     1: core::panicking::panic_fmt
               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/core/src/panicking.rs:142:14
     2: build_script_build::run_command
     3: build_script_build::compile
     4: build_script_build::build_library::{{closure}}
     5: core::iter::adapters::map::map_fold::{{closure}}
     6: core::iter::traits::iterator::Iterator::fold
     7:  as core::iter::traits::iterator::Iterator>::fold
     8:  as core::iter::traits::iterator::Iterator>::fold
     9: core::iter::traits::iterator::Iterator::for_each
    10:  as alloc::vec::spec_extend::SpecExtend>::spec_extend
    11:  as alloc::vec::spec_from_iter_nested::SpecFromIterNested>::from_iter
    12:  as alloc::vec::spec_from_iter::SpecFromIter>::from_iter
    13:  as core::iter::traits::collect::FromIterator>::from_iter
    14: core::iter::traits::iterator::Iterator::collect
    15: build_script_build::build_library
    16: build_script_build::build_c_code::{{closure}}
    17:  as core::iter::traits::iterator::Iterator>::for_each
    18: build_script_build::build_c_code
    19: build_script_build::ring_build_rs_main
    20: build_script_build::main
    21: core::ops::function::FnOnce::call_once

@paulyoung
Copy link

@chertov which version of llvm are you using?

@paulyoung
Copy link

I tried the env vars @chertov suggested that failed for me

Based on the errors there I set CFLAGS_wasm32_unknown_unknown = "-Qunused-arguments"; and then arrived at:

error: unknown target CPU 'apple-a13'
note: valid target CPU values are: mvp, bleeding-edge, generic

I've gotten stuck on this before and never gotten past it.

Instead of all the env vars listed I can set TARGET_CC to build otherwise, but then I'm back to the problem about LIMBS_are_zero.

@chertov
Copy link

chertov commented Oct 31, 2022

@paulyoung hi, my buildscript and clang version

function build_wasm() {
    LLVM_PATH=/opt/homebrew/opt/llvm/

    RUSTFLAGS=--cfg=web_sys_unstable_apis \
    RUSTC_WRAPPER="" \
    \
    CC_wasm32_unknown_unknown=${LLVM_PATH}/bin/clang \
    CXX_wasm32_unknown_unknown=${LLVM_PATH}/bin/clang++ \
    AS_wasm32_unknown_unknown=${LLVM_PATH}/bin/llvm-as \
    AR_wasm32_unknown_unknown=${LLVM_PATH}/bin/llvm-ar \
    STRIP_wasm32_unknown_unknown=${LLVM_PATH}/bin/llvm-strip \
    \
    rustup run stable wasm-pack build \
        --out-dir "${output_path:?}" \
        --target web \
        --release
}
/opt/homebrew/opt/llvm/bin/clang --version
Homebrew clang version 14.0.6
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

@paulyoung
Copy link

@chertov thanks!

RUSTFLAGS seems to be the only difference from #1483 (comment). I’ll give this a try in the morning.

@paulyoung
Copy link

…although that seems unrelated to my issue.

@briansmith
Copy link
Owner

I'm closing this as a duplicate of #918, which is about the same thing: Not all of the ring features are implemented when targeting wasm32 targets.

@emirror-de
Copy link

Hi,

I am using a Nix flake setup with a web application build using trunk. I do get a build error after upgrading from ring 0.16.20 to 0.17.8.

At first, I got the same target unknown apple-a13 as written in #1483 (comment).

After adding CFLAGS_wasm32_unknown_unknown = "-Qunused-arguments"; as @paulyoung did, the following error occurs:
cargo:warning=clang-16: error: unsupported option '-fzero-call-used-regs=used-gpr' for target 'wasm32-unknown-unknown'

Is there anything additionally to change/add to my setup to make it build again? The same flake setup works flawlessly with 0.16.20.

@imalsogreg
Copy link

I'm running into this issue under nix, too. cargo:warning=clang: error: unsupported option '-fzero-call-used-regs=used-gpr' for target 'wasm32-unknown-unknown'

@tqwewe
Copy link

tqwewe commented Nov 5, 2024

I'm also getting this error on Nix with MacOS. Tried adding clang to my flake.nix file, but didn't have any success with this.

Edit: I got this working on MacOS with Nix by using the homebrew llvm thanks to @chertov's comment.

shellHook = ''
  export LLVM_PATH=/usr/local/Cellar/llvm/17.0.6_1
  export RUSTC_WRAPPER=""
  export CC_wasm32_unknown_unknown=$LLVM_PATH/bin/clang
  export CXX_wasm32_unknown_unknown=$LLVM_PATH/bin/clang++
  export AS_wasm32_unknown_unknown=$LLVM_PATH/bin/llvm-as
  export AR_wasm32_unknown_unknown=$LLVM_PATH/bin/llvm-ar
  export STRIP_wasm32_unknown_unknown=$LLVM_PATH/bin/llvm-strip
'';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants