2011 年 Google 在 Chrome 中使用了 NaCl 技术,可以使得 C 语言编写的程序运行到浏览器中,下边是维基百科的定义。
Google Native Client(缩写为NaCl),是一个由谷歌所发起的开放源代码计划,采用BSD许可证。它采用沙盒技术,让Intel x86、ARM或MIPS子集的机器代码直接在沙盒上运行。它能够从浏览器直接运行程序机器代码,独立于用户的操作系统之外,使Web应用程序可以用接近于机器代码运作的速度来运行,同时兼顾安全性。其功能类似于微软的 ActiveX,但是ActiveX只支持视窗系统。
但一个完整的 NaCl 应用,在分发时需要提供支持多个架构平台(X86 / X64 / ARM 等)的模块文件,后来谷歌又推出了与底层架构无关的 PNaCl 技术。但由于其开发难度、兼容性等问题最终没有普及开来。在 2017 年 Google 宣布放弃 PNaCl 转向 WebAssembly。
ASM.js
ASM.js 是 Mozilla 在 2013 年推出的,是 javaScript 的一个严格子集,可以作为 C/C++ 编译的目标语言,从而使得 js 引擎可以采用 AOT(Ahead Of Time) 的编译策略,也就是在运行前直接编译成机器码,因此运行速度会有一定的提升。
ASM.js 通常不直接编写,而是作为一种通过编译器生成的中间语言,该编译器获取 C++ 或其他语言的源代码,然后输出 ASM.js。
“-g“ [compile+link] Controls the level of debuggability. Each level builds on the previous one:
* "-g0": Make no effort to keep code debuggable.
* "-g1": When linking, preserve whitespace in JavaScript.
* "-g2": When linking, preserve function names in compiled code.
* "-g3": When compiling to object files, keep debug info,
including JS whitespace, function names, and LLVM debug info
(DWARF) if any (this is the same as -g).
Commands: run Runs a WebAssembly module config Controls Wasmtime configuration settings compile Compiles a WebAssembly module explore Explore the compilation of a WebAssembly module to native code serve Serves requests from a wasi-http proxy component settings Displays available Cranelift settings for a target wast Runs a WebAssembly test script file help Print this message or the help of the given subcommand(s)
run
运行wasm文件或者cwasm文件,不过大概率要求加—allow-precompiled选项
compile
将wasm文件编译为当前架构下的可执行文件格式(e.g. ELF)
后缀.cwasm(compiled wasm)
不过依然不能直接运行,还是要wasmtime执行
更多选项
-D
调试
1 2
-D, --debug <KEY[=VAL[,..]]> Debug-related configuration options, `-D help` to see all
—env
设置环境变量
1 2
--env <NAME[=VAL]> Pass an environment variable to the program.
—invoke
单独执行某个函数,可以指定参数
1 2 3 4 5 6
--invoke <FUNCTION> The name of the function to run
Invoking a specific function (e.g. `add`) in a WebAssembly module:
wasmtime --invoke add example.wasm 1 2
—allow-precompiled
允许提前编译
1 2 3 4
--allow-precompiled Allow executing precompiled WebAssembly modules as `*.cwasm` files. Note that this option is not safe to pass if the module being passed in is arbitrary user input. Only `wasmtime`-precompiled modules generated via the `wasmtime compile` command or equivalent should be passed as an argument with this option specified.
WABT (we pronounce it "wabbit") is a suite of tools for WebAssembly, including:
wat2wasm: translate from WebAssembly text format to the WebAssembly binary format wasm2wat: the inverse of wat2wasm, translate from the binary format back to the text format (also known as a .wat) wasm-objdump: print information about a wasm binary. Similiar to objdump. wasm-interp: decode and run a WebAssembly binary file using a stack-based interpreter wasm-decompile: decompile a wasm binary into readable C-like syntax. wat-desugar: parse .wat text form as supported by the spec interpreter (s-expressions, flat syntax, or mixed) and print "canonical" flat format wasm2c: convert a WebAssembly binary file to a C source and header wasm-strip: remove sections of a WebAssembly binary file wasm-validate: validate a file in the WebAssembly binary format wast2json: convert a file in the wasm spec test format to a JSON file and associated wasm binary files wasm-stats: output stats for a module spectest-interp: read a Spectest JSON file, and run its tests in the interpreter
pwndbg> bt #0 0x00007ffff7d38607 in __GI___readv (fd=0, iov=0x555556fbb5c0, iovcnt=1) at ../sysdeps/unix/sysv/linux/readv.c:26 #1 0x0000555556424da4 in std::sys::unix::fd::FileDesc::read_vectored () at library/std/src/sys/unix/fd.rs:99 #2 std::sys::unix::fs::File::read_vectored () at library/std/src/sys/unix/fs.rs:1119 #3 <&std::fs::File as std::io::Read>::read_vectored () at library/std/src/fs.rs:810 #4 0x0000555555b704e0 in <wasi_cap_std_sync::stdio::Stdin as wasi_common::file::WasiFile>::read_vectored::{{closure}} () at library/core/src/str/pattern.rs:1796 #5 0x0000555555b93eaf in wasi_common::snapshots::preview_1::<impl wasi_common::snapshots::preview_1::wasi_snapshot_preview1::WasiSnapshotPreview1 for wasi_common::ctx::WasiCtx>::fd_read::{{closure}} () at library/core/src/str/pattern.rs:1796 #6 0x000055555583be3f in <tracing::instrument::Instrumented<T> as core::future::future::Future>::poll () at library/core/src/str/pattern.rs:1796 #7 0x00005555557e3db3 in wiggle::run_in_dummy_executor () at library/core/src/str/pattern.rs:1796 #8 0x0000555555859720 in <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once () at library/core/src/str/pattern.rs:1796 #9 0x00005555558bcd04 in wasmtime_runtime::instance::Instance::from_vmctx () at library/core/src/str/pattern.rs:1796 #10 0x00005555558eede9 in <F as wasmtime::func::IntoFunc<T,(wasmtime::func::Caller<T>,A1,A2,A3,A4),R>>::into_func::native_call_shim () at library/core/src/str/pattern.rs:1796 #11 0x00007ffff7c0a70a in ?? () #12 0x00007fffffffba30 in ?? () #13 0x00007ffff7c01f75 in ?? () #14 0x00007ffe77bb1000 in ?? () #15 0x0000000000501c50 in ?? () #16 0x0000000000000000 in ?? ()