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

How to use? #1

Open
NicolasDorier opened this issue Apr 24, 2019 · 26 comments
Open

How to use? #1

NicolasDorier opened this issue Apr 24, 2019 · 26 comments

Comments

@NicolasDorier
Copy link

Is there any instruction somewhere on how to use your project?

I would like to try on this module: https://aois.blob.core.windows.net/public/secp256k1.wasm

@ericsink
Copy link
Owner

Sorry, no documentation yet. :-)

But if you want to just try converting a module, the console app in c-testsuite/build is currently configured to do that. It wants two command line args. The first is the path to your wasm file. The second is the destination to write the assembly.

dotnet run foo.wasm foo.dll

But I just tried the module you linked, and it doesn't work. :-(

Looks like this issue is another instance of RyanLamansky/dotnet-webassembly#11

I don't have table imports working yet either.

And if I did, there would also be the minor problem of needing a reference assembly for the other imports. That's not a big issue, but the console app mentioned above is currently configured only to provide WASI.

@NicolasDorier
Copy link
Author

@ericsink what is a "table import"? I thought it was something needed for pulling external dependencies. However, this lib, secp256k1 should be pure C without dependencies, which does not even have heap allocation (no malloc/free).

@ericsink
Copy link
Owner

In Wasm, a "table" is a used for indirect function calls. Like a "vtable". Conceptually, it's a list of function pointers, and the call indirect instruction is saying "call the nth function in this list".

What produced this Wasm file? Where did it come from?

@NicolasDorier
Copy link
Author

@ericsink this file has been generated by this dockerfile

https://github.com/bitauth/bitcoin-ts/blob/master/wasm/docker/secp256k1.Dockerfile

The project itself is https://github.com/bitcoin-core/secp256k1 which is a pure minimalist cryptographic library made in C without dependency.

@ericsink
Copy link
Owner

Ah, I see.

I'm guessing the table import isn't necessary.

So my code currently can't cope with the kinds of Wasm produced by emsdk. But the original C code here looks very straightforward, so it should be possible to get it compiling with clang-8 and wasi-sysroot, which should result in something I can convert to an assembly.

@ericsink
Copy link
Owner

That code appears to also have a dependency on gmp.

@NicolasDorier
Copy link
Author

Interesting. I will try to make it works with clang-8.

@NicolasDorier
Copy link
Author

If I include gmp statically should be OK. Though I am surprised about this dependency.

@NicolasDorier
Copy link
Author

@ericsink I am trying to build the library with the wasi-sdk

You can see my progress on https://github.com/NicolasDorier/NBitcoin.Secp256k1

It build but I am surprised because there is no wasm file created anywhere...
But it still seems to use clang under the hood.

I think this library is a good test for your project, because it has no dependency (if I turn bignum feature off)

@NicolasDorier
Copy link
Author

So I am getting a libsecp256k1.a checking if it is wasm... I am surprised it does not have the .wasm extension.

@NicolasDorier
Copy link
Author

sadly, it is not a wasm file :(

@ericsink
Copy link
Owner

Yeah, I'm not sure how all that autoconf/configure stuff interacts with the need to use --sysroot and --target flags.

@NicolasDorier
Copy link
Author

I managed to build the wasm file!

https://aois.blob.core.windows.net/public/libsecp256k1.wasm

It seems to be working with RyanLamansky/dotnet-webassembly as well!! :)
Will try your project (you can see how I did on my repo)

@NicolasDorier
Copy link
Author

So

Unhandled Exception: System.Exception: import global not found: env.__stack_pointer
   at wasm.import.import_global(ModuleDefinition md, ImportedGlobal s, Assembly a) in C:\Sources\wasm2cil\wasm\Import.fs:line 63
   at [email protected](Int32 i, GlobalLookupItem gi) in C:\Sources\wasm2cil\wasm\Cecil.fs:line 1025
   at Microsoft.FSharp.Collections.ArrayModule.MapIndexed[T,TResult](FSharpFunc`2 mapping, T[] array)
   at wasm.cecil.create_globals[a](ModuleIndex ndx, BasicTypes bt, ModuleDefinition md, FSharpOption`1 env_assembly) in C:\Sources\wasm2cil\wasm\Cecil.fs:line 1032
   at wasm.cecil.gen_assembly(Target target, Module m, String assembly_name, String ns, String classname, Version ver, Stream dest) in C:\Sources\wasm2cil\wasm\Cecil.fs:line 1548
   at Program.main(String[] argv) in C:\Sources\wasm2cil\c-testsuite\build\Program.fs:line 34

@NicolasDorier
Copy link
Author

Still a bunch of import:
That's strange.

Imports:
__indirect_function_table
__stack_pointer
__memory_base
__table_base
fd_prestat_get
fd_prestat_dir_name
environ_sizes_get
environ_get
args_sizes_get
args_get
main
proc_exit
fd_fdstat_get
fd_close
fd_seek
fd_write

@NicolasDorier
Copy link
Author

Nowhere in my lib source code I see any reference to all of this.

@NicolasDorier
Copy link
Author

Ok I managed to remove some by making sure the linker does not create an entry point: Still have

__indirect_function_table
__stack_pointer
__memory_base
__table_base
fd_close
fd_seek
fd_write

@NicolasDorier
Copy link
Author

I checked the wat file those imports are not referenced by any code my library is exporting :(

@ericsink
Copy link
Owner

Most of those imports are basic calls from the WASI API.

@ericsink
Copy link
Owner

The "env" __ imports are a surprise. I'm not sure why they're showing up. Usually that stuff shows up with Clang when I'm not building with the wasi target, but with the wasi target, only the wasi functions get declared as imports.

It is interesting to note that __indirect_function_table is a table import, which is the problem we started with.

@NicolasDorier
Copy link
Author

It seems that it depends on your version. See

image

https://reviews.llvm.org/D40875

The fd_ are a surprise to me as those does not seem to be used at all in the wat

@NicolasDorier
Copy link
Author

(import "wasi_unstable" "fd_close" (func $__wasi_fd_close (type 4))) oh I get it they are indeed provided by wasi.

Usually that stuff shows up with Clang when I'm not building with the wasi target, but with the wasi target, only the wasi functions get declared as imports.

I don't understand, as far as I understood your blog post, by using clang --sysroot=/opt/wasi-sdk/share/sysroot --target=wasm32-unknown-wasi, I thought that wasi would statically compile any missing symbol like malloc/free. Why fd_* are not included?

@NicolasDorier
Copy link
Author

Actually I just tracked fd_ to some printf call. I can just comment those.

@NicolasDorier
Copy link
Author

Latest: wat and wasm.

The only imports remaining are

  (import "env" "__indirect_function_table" (table (;0;) 4 funcref))
  (import "env" "__stack_pointer" (global (;0;) (mut i32)))
  (import "env" "__memory_base" (global (;1;) i32))
  (import "env" "__table_base" (global (;2;) i32))

If you can fix this, then things would work like expected :)

@NicolasDorier
Copy link
Author

As far as __stack_pointer is concerned the reference is here https://reviews.llvm.org/D34172 I am quite unsure about what it means.

@Serentty
Copy link

I'm trying to build with .NET 5.0 preview, and if I change the version in the project to get it to build, I get this:

Unhandled exception. System.NotSupportedException: Specified method is not supported.
   at Mono.Cecil.ModuleParameters.GetCurrentRuntime()
   at Mono.Cecil.ModuleParameters..ctor()

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

3 participants