/src/WasmEdge/include/driver/tool.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | //===-- wasmedge/driver/tool.h - Tool entry point -------------------------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the entry point for the tooling executable. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | #include "driver/options.h" |
16 | | #include "plugin/plugin.h" |
17 | | #include "po/argument_parser.h" |
18 | | #include <optional> |
19 | | #include <string_view> |
20 | | |
21 | | namespace WasmEdge { |
22 | | namespace Driver { |
23 | | |
24 | | using namespace std::literals; |
25 | | |
26 | | struct DriverToolOptions : public DriverProposalOptions { |
27 | | DriverToolOptions() |
28 | 0 | : SoName(PO::Description("Wasm or so file"sv), |
29 | 0 | PO::MetaVar("WASM_OR_SO"sv)), |
30 | 0 | Args(PO::Description("Execution arguments"sv), PO::MetaVar("ARG"sv)), |
31 | 0 | Reactor(PO::Description( |
32 | 0 | "Enable reactor mode. Reactor mode calls `_initialize` if exported."sv)), |
33 | 0 | Dir(PO::Description( |
34 | 0 | "Binding directories into WASI virtual filesystem. Each " |
35 | 0 | "directory can be specified as --dir `host_path`. You can also " |
36 | 0 | "map a guest directory to a host directory by --dir " |
37 | 0 | "`guest_path:host_path`, where `guest_path` specifies the path " |
38 | 0 | "that will correspond to `host_path` for calls like `fopen` in " |
39 | 0 | "the guest. The default permission is `readwrite`, however, " |
40 | 0 | "you can use --dir `guest_path:host_path:readonly` to make the " |
41 | 0 | "mapping directory become a read only mode."sv), |
42 | 0 | PO::MetaVar("PREOPEN_DIRS"sv)), |
43 | 0 | Env(PO::Description( |
44 | 0 | "Environ variables. Each variable can be specified as --env " |
45 | 0 | "`NAME=VALUE`."sv), |
46 | 0 | PO::MetaVar("ENVS"sv)), |
47 | | // TODO: Move PropExceptionHandling into addProposalOptions after |
48 | | // AOT mode of exception handling proposal is ready. |
49 | 0 | PropExceptionHandling( |
50 | 0 | PO::Description("Disable Exception handling proposal"sv)), |
51 | 0 | PropExceptionHandlingDeprecated(PO::Description( |
52 | 0 | "(DEPRECATED) Enable Exception handling proposal. WASM 3.0 " |
53 | 0 | "includes this proposal, and this option will be removed in the " |
54 | 0 | "future."sv)), |
55 | 0 | PropComponent(PO::Description( |
56 | 0 | "Enable Component Model proposal, this is experimental"sv)), |
57 | 0 | ConfEnableInstructionCounting(PO::Description( |
58 | 0 | "Enable generating code for counting Wasm instructions executed."sv)), |
59 | 0 | ConfEnableGasMeasuring(PO::Description( |
60 | 0 | "Enable generating code for counting gas burned during execution."sv)), |
61 | 0 | ConfEnableTimeMeasuring(PO::Description( |
62 | 0 | "Enable generating code for counting time during execution."sv)), |
63 | 0 | ConfEnableAllStatistics(PO::Description( |
64 | 0 | "Enable generating code for all statistics options include " |
65 | 0 | "instruction counting, gas measuring, and execution time"sv)), |
66 | 0 | ConfEnableJIT( |
67 | 0 | PO::Description("Enable Just-In-Time compiler for running WASM"sv)), |
68 | 0 | ConfEnableCoredump(PO::Description( |
69 | 0 | "Enable coredump when WebAssembly enters a trap"sv)), |
70 | 0 | ConfCoredumpWasmgdb( |
71 | 0 | PO::Description("Enable coredump for wasm-gdb to debug"sv)), |
72 | 0 | ConfForceInterpreter( |
73 | 0 | PO::Description("Forcibly run WASM in interpreter mode."sv)), |
74 | 0 | ConfRunMode(PO::Description("Set execution mode. Valid values: " |
75 | 0 | "interpreter, jit, aot, lazyjit. " |
76 | 0 | "Default is interpreter."sv), |
77 | 0 | PO::MetaVar("MODE"sv), PO::DefaultValue(std::string())), |
78 | 0 | ConfAFUNIX(PO::Description("Enable UNIX domain sockets"sv)), |
79 | 0 | TimeLim( |
80 | 0 | PO::Description( |
81 | 0 | "Limitation of maximum time(in milliseconds) for execution, " |
82 | 0 | "default value is 0 for no limitations"sv), |
83 | 0 | PO::MetaVar("TIMEOUT"sv), PO::DefaultValue<uint64_t>(0)), |
84 | 0 | GasLim( |
85 | 0 | PO::Description( |
86 | 0 | "Limitation of execution gas. Upper bound can be specified as " |
87 | 0 | "--gas-limit `GAS_LIMIT`."sv), |
88 | 0 | PO::MetaVar("GAS_LIMIT"sv)), |
89 | 0 | MemLim( |
90 | 0 | PO::Description( |
91 | 0 | "Limitation of pages(as size of 64 KiB) in every memory " |
92 | 0 | "instance. Upper bound can be specified as --memory-page-limit " |
93 | 0 | "`PAGE_COUNT`."sv), |
94 | 0 | PO::MetaVar("PAGE_COUNT"sv)), |
95 | 0 | LinkedModules( |
96 | 0 | PO::Description( |
97 | 0 | "Register additional WASM modules for linking. Each module " |
98 | 0 | "can be specified as --module `name:path`, where `name` is " |
99 | 0 | "the module name to export and `path` is the WASM file path."sv), |
100 | 0 | PO::MetaVar("MODULES"sv)), |
101 | 0 | ForbiddenPlugins(PO::Description("List of plugins to ignore."sv), |
102 | 0 | PO::MetaVar("NAMES"sv)), |
103 | 0 | LogLevel( |
104 | 0 | PO::Description( |
105 | 0 | "Set logging level. Valid values: off, trace, debug, info, " |
106 | 0 | "warning, error, fatal. Default is info."sv), |
107 | 0 | PO::MetaVar("LEVEL"sv), PO::DefaultValue(std::string())) {} |
108 | | |
109 | | PO::Option<std::string> SoName; |
110 | | PO::List<std::string> Args; |
111 | | PO::Option<PO::Toggle> Reactor; |
112 | | PO::List<std::string> Dir; |
113 | | PO::List<std::string> Env; |
114 | | PO::Option<PO::Toggle> PropExceptionHandling; |
115 | | PO::Option<PO::Toggle> PropExceptionHandlingDeprecated; |
116 | | PO::Option<PO::Toggle> PropComponent; |
117 | | PO::Option<PO::Toggle> ConfEnableInstructionCounting; |
118 | | PO::Option<PO::Toggle> ConfEnableGasMeasuring; |
119 | | PO::Option<PO::Toggle> ConfEnableTimeMeasuring; |
120 | | PO::Option<PO::Toggle> ConfEnableAllStatistics; |
121 | | PO::Option<PO::Toggle> ConfEnableJIT; |
122 | | PO::Option<PO::Toggle> ConfEnableCoredump; |
123 | | PO::Option<PO::Toggle> ConfCoredumpWasmgdb; |
124 | | PO::Option<PO::Toggle> ConfForceInterpreter; |
125 | | PO::Option<std::string> ConfRunMode; |
126 | | PO::Option<PO::Toggle> ConfAFUNIX; |
127 | | PO::Option<uint64_t> TimeLim; |
128 | | PO::List<int> GasLim; |
129 | | PO::List<int> MemLim; |
130 | | PO::List<std::string> LinkedModules; |
131 | | PO::List<std::string> ForbiddenPlugins; |
132 | | PO::Option<std::string> LogLevel; |
133 | | |
134 | | private: |
135 | 0 | void addGlobalOptions(PO::ArgumentParser &Parser) noexcept { |
136 | 0 | Parser.add_option("log-level"sv, LogLevel) |
137 | 0 | .add_option("forbidden-plugin"sv, ForbiddenPlugins); |
138 | 0 | } |
139 | | |
140 | | public: |
141 | 0 | void addParserOptions(PO::ArgumentParser &Parser) noexcept { |
142 | 0 | addGlobalOptions(Parser); |
143 | 0 | addProposalOptions(Parser); |
144 | 0 | Parser.add_option("disable-exception-handling"sv, PropExceptionHandling) |
145 | 0 | .add_option("enable-exception-handling"sv, |
146 | 0 | PropExceptionHandlingDeprecated) |
147 | 0 | .add_option("enable-component"sv, PropComponent) |
148 | 0 | .add_option(SoName); |
149 | 0 | } |
150 | | |
151 | 0 | void addLinkerOptions(PO::ArgumentParser &Parser) noexcept { |
152 | 0 | addParserOptions(Parser); |
153 | |
|
154 | 0 | Plugin::Plugin::loadFromDefaultPaths(); |
155 | 0 | Plugin::Plugin::addPluginOptions(Parser); |
156 | |
|
157 | 0 | Parser.add_option("dir"sv, Dir) |
158 | 0 | .add_option("env"sv, Env) |
159 | 0 | .add_option("module"sv, LinkedModules) |
160 | 0 | .add_option("memory-page-limit"sv, MemLim); |
161 | 0 | } |
162 | | |
163 | 0 | void addOptions(PO::ArgumentParser &Parser) noexcept { |
164 | 0 | addLinkerOptions(Parser); |
165 | | |
166 | | // pure Execution and Profiling flags |
167 | 0 | Parser.add_option(Args) |
168 | 0 | .add_option("enable-instruction-count"sv, ConfEnableInstructionCounting) |
169 | 0 | .add_option("enable-gas-measuring"sv, ConfEnableGasMeasuring) |
170 | 0 | .add_option("enable-time-measuring"sv, ConfEnableTimeMeasuring) |
171 | 0 | .add_option("enable-all-statistics"sv, ConfEnableAllStatistics) |
172 | 0 | .add_option("enable-jit"sv, ConfEnableJIT) |
173 | 0 | .add_option("enable-coredump"sv, ConfEnableCoredump) |
174 | 0 | .add_option("coredump-for-wasmgdb"sv, ConfCoredumpWasmgdb) |
175 | 0 | .add_option("force-interpreter"sv, ConfForceInterpreter) |
176 | 0 | .add_option("run-mode"sv, ConfRunMode) |
177 | 0 | .add_option("allow-af-unix"sv, ConfAFUNIX) |
178 | 0 | .add_option("time-limit"sv, TimeLim) |
179 | 0 | .add_option("gas-limit"sv, GasLim) |
180 | 0 | .add_option("reactor"sv, Reactor); |
181 | 0 | } |
182 | | }; |
183 | | Configure createConfigure(const struct DriverToolOptions &Opt) noexcept; |
184 | | std::optional<RunMode> parseRunModeArg(std::string_view S) noexcept; |
185 | | |
186 | | int Tool(struct DriverToolOptions &Opt) noexcept; |
187 | | int ParseTool(struct DriverToolOptions &Opt) noexcept; |
188 | | int ValidateTool(struct DriverToolOptions &Opt) noexcept; |
189 | | int InstantiateTool(struct DriverToolOptions &Opt) noexcept; |
190 | | |
191 | | } // namespace Driver |
192 | | } // namespace WasmEdge |