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

Need help to run a supplied WASM file #19

Open
jlb6740 opened this issue Jul 10, 2019 · 14 comments
Open

Need help to run a supplied WASM file #19

jlb6740 opened this issue Jul 10, 2019 · 14 comments
Labels
upstream This is an upstream dependency issue.
Milestone

Comments

@jlb6740
Copy link

jlb6740 commented Jul 10, 2019

Hi .. What is the command line to use to run innative standalone (innative-cmd) with a wasm file that I supply. For example I have compiled fibonaci.c to a wasm, that relies on no imports and like to simply run this on the linux command line using innative-cmd

I see these options in help:

Usage: innative-cmd [-r] [-c] [-i [lite]] [-u] [-v] [-f FLAG...] [-l FILE] [-L FILE] [-o FILE] [-a FILE] [-d PATH] [-j PATH] [-s [FILE]] [-w [MODULE:]FUNCTION] FILE...
  -r : Run the compiled result immediately and display output. Requires a start function.
 -w <[MODULE:]FUNCTION> : whitelists a given C import, does name-mangling if the module is specified.

But it is still not clear the command line options to use.
What start function is innative looking for default? Can you provide a simple example here?

Thanks!

@ErikMcClure
Copy link
Contributor

The start function is defined by the wasm module as the entry point, e.g. a main() function. As long as your c file contains a main() function, your compiler should generate a start function, and you would simply do:

innative-cmd fibonacci.wasm -r

However, this won't generate any output, so it'll be hard to tell if it works.

@jlb6740
Copy link
Author

jlb6740 commented Jul 10, 2019

Hi .. Thanks.
I am trying to run this example: https://gist.github.com/jlb6740/2764edadc6b361ff4c851b6f20d43eb8 and can tell based on clock ticks if it runs, but for me it is appearing that it does not run using the command:

innative-cmd fib2.wasm -r

I realize now though that it is creating a .so file so do I misunderstand the function of the -r flag? Should I not interpret this -r flag will immediately run the fib2.so that is generated or should I assume innative in fact tried to run the .so but that it failed silently for some reason?

Thanks.

@ErikMcClure
Copy link
Contributor

Innative generates a .so file and then loads and runs it when -r is specified. If this doesn't appear to work, I suggest dropping -r and instead running innative-cmd fib2.wasm, which will generate an executable you can then run yourself to see if it properly executes.

@jlb6740
Copy link
Author

jlb6740 commented Jul 10, 2019

Ignore the previous comment. I think there was flag I passed to clang that prevented this from recognizing main. I'll confirm.

@jlb6740
Copy link
Author

jlb6740 commented Jul 11, 2019

You know .. I just can't get this to run. Is it looking for an export of _start or is it looking for main?

  (export "memory" (memory 0))
  (export "__wasm_call_ctors" (func 0))
  (export "__heap_base" (global 1))
  (export "__data_end" (global 2))
  (export "__dso_handle" (global 3))
  (export "fib2_setup" (func 1))
  (export "__original_main" (func 2))
  (export "main" (func 5))

This above is what is being exported. Should innative register these exports and call main?

@ErikMcClure
Copy link
Contributor

None of these are a start function. WebAssembly defines a start function in a special section in the webassembly module. Someone else, however, has alerted me that compilers aren't actually using this part of the webassembly standard, so it seems that every single webassembly compiler isn't actually standards compliant. I may have to modify inNative to take an alternative start function if this is the case.

For now, you can manually define main as your start function by simply adding:
(start 5)
assuming the main function is still index 5 in the above example.

@ErikMcClure ErikMcClure added the upstream This is an upstream dependency issue. label Jul 11, 2019
@ErikMcClure
Copy link
Contributor

I've added an issue tracking this and will be filing a bug with LLVM's webassembly codegen to try to fix this upstream.

@jlb6740
Copy link
Author

jlb6740 commented Jul 11, 2019

Thanks .. I was able to manually add the start after dumping to wat, but at first it did not work because my main function had been given 2 extra arguments at some point during compilation and required a return based on my original C code.
'''
Compile error: ERR_VALIDATION_ERROR
Error ERR_INVALID_START_FUNCTION: Starting function must have no parameters and no return value, instead it has 2 parameters and 1 return values.
'''

I was forced to change the main in the original code to some other name and remove the int as a return ..

- int main(void)
+void main_innative(void)

Then manually adding start by hand and running. I still am trying to figure a way to automate this though. I am using clang-8 with WASI support. My biggest problem appears to be a bunch of imports for wasi_unstable that get included unless I used the -nostartfiles flag. I am unsure of this flag because it is not mentioned in the --help but nonetheless greatly simplifies the WASM file and doesn't include unneeded imports. Unfortunately though no start function gets specified. Using linker flags like lto and gc-sections do not seem to help.

@ErikMcClure
Copy link
Contributor

inNative doesn't support WASI yet, so if clang is insisting on requiring WASI to compile standalone EXEs, there isn't much I can do until I add WASI support. I believe WASI defines it's own imaginary entry-point, which is incompatible with the standard webassembly entry-point, which is causing a lot of problems. Technically, main shouldn't even be the entry point, it should be _start which should do the job of finding the command line arguments and passing them into main, and reporting the exit code to the operating system after main exits. I have no idea why clang refuses to do this or why WASI insists on creating imaginary standards to embed this behavior into the webassembly runtimes.

@jlb6740
Copy link
Author

jlb6740 commented Jul 11, 2019

Ok, Thanks. It's your call on whether to close this. I think I understand how to run, it's just that the toolchain I am using isn't producing WASM files that are compatible at this time.

@jlb6740 jlb6740 closed this as completed Jul 11, 2019
@jlb6740 jlb6740 reopened this Jul 11, 2019
@jlb6740
Copy link
Author

jlb6740 commented Jul 12, 2019

Related .. I am looking at your benchmark-fac.wat file and wondering how is the test script running this. I do not see a (start) instruction in the wat. I see in test.cpp and benchmark.cpp where it looks like you are loading benchmark-fac and then n-body, etc. But I am having a hard time parsing where you actually instantiate and where you decide to run. When I compile a wasm file it creates a .so file and then does nothing unless I manually add a start function that has the exactly zero return and zero parameters. If I wanted to automate running (without modifying files by hand and adding start functions) what are the steps then to link and run standalone this .so that is generated if I always have the same name for function entry?

innative-cmd my-file.wasm # Produces my-file.so ? # Step to run my-file.so

I was trying to follow your test script. innative-cmd seems to generate a .so file and I assume it is loading and running .so when I supply the -r flag. If this is the case I would expect it to be possible to link that generated .so that's missing the entry with someother file that does have proper entry function that innative expects and then load and run that new .so with innative-cmd. However, I am not sure innative-cmd can load an .so that it didn't generate? Any pointers?

@ErikMcClure
Copy link
Contributor

innative-cmd cannot do this. This is the feature that needs to be added. The benchmark tests build and compile their own functions, which are then called manually with custom parameters, as detailed in the Quick Start Guide. There is no way to do this without downloading the entire SDK and embedding the inNative runtime in a program that loads the .wasm file.

I suggest reviewing the readme file, because you don't seem to completely understand how innative works. inNative can load any webassembly you want if you embed the library into your program. innative-cmd is simply for compiling standalone webassembly files.

A standalone webassembly module is one that has a start function. If it doesn't have a start function, it's not standalone. The benchmarks are not standalone webassembly modules.

@jlb6740
Copy link
Author

jlb6740 commented Jul 12, 2019

Yes .. I see. I've actually run the embedded case with innative but was hoping innative-cmd was capable as a standalone. Sounds like it is, but there is no flexibility if the start function is not specified. Would it be straight-forward (which files) to add a flag to innative-cmd to specify a custom start function?

@ErikMcClure
Copy link
Contributor

Simply adding a flag to change the start function would be easy, but that's not necessarily sufficient. Clang is generating a start function with a return value and arguments, so the flag must either be defined as passing 0 to all parameters, or parameters must be added to the -r command. This is also possible but much more involved.

@ErikMcClure ErikMcClure modified the milestones: v0.1.4, v0.1.5 Nov 25, 2019
@ErikMcClure ErikMcClure modified the milestones: v0.1.8, v0.1.9 Apr 29, 2020
@ErikMcClure ErikMcClure modified the milestones: v0.1.10, v0.2.0 Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream This is an upstream dependency issue.
Projects
None yet
Development

No branches or pull requests

2 participants