/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 <filesystem> |
19 | | #include <optional> |
20 | | #include <string_view> |
21 | | |
22 | | namespace WasmEdge { |
23 | | namespace VM { |
24 | | class VM; |
25 | | } // namespace VM |
26 | | |
27 | | namespace Driver { |
28 | | |
29 | | using namespace std::literals; |
30 | | |
31 | | struct DriverToolOptions : public DriverProposalOptions, |
32 | | public DriverStatisticsOptions, |
33 | | public DriverLoggingOptions { |
34 | | DriverToolOptions() |
35 | 0 | : SoName(PO::Description("Wasm or so file"sv), |
36 | 0 | PO::MetaVar("WASM_OR_SO"sv)), |
37 | 0 | Args(PO::Description("Execution arguments"sv), PO::MetaVar("ARG"sv)), |
38 | 0 | Reactor(PO::Description( |
39 | 0 | "Enable reactor mode. Reactor mode calls `_initialize` if exported."sv)), |
40 | 0 | Dir(PO::Description( |
41 | 0 | "Binding directories into WASI virtual filesystem. Each " |
42 | 0 | "directory can be specified as --dir `host_path`. You can also " |
43 | 0 | "map a guest directory to a host directory by --dir " |
44 | 0 | "`guest_path:host_path`, where `guest_path` specifies the path " |
45 | 0 | "that will correspond to `host_path` for calls like `fopen` in " |
46 | 0 | "the guest. The default permission is `readwrite`, however, " |
47 | 0 | "you can use --dir `guest_path:host_path:readonly` to make the " |
48 | 0 | "mapping directory become a read only mode."sv), |
49 | 0 | PO::MetaVar("PREOPEN_DIRS"sv)), |
50 | 0 | Env(PO::Description( |
51 | 0 | "Environ variables. Each variable can be specified as --env " |
52 | 0 | "`NAME=VALUE`."sv), |
53 | 0 | PO::MetaVar("ENVS"sv)), |
54 | 0 | PropComponent(PO::Description( |
55 | 0 | "Enable Component Model proposal, this is experimental"sv)), |
56 | 0 | ConfEnableJIT( |
57 | 0 | PO::Description("Enable Just-In-Time compiler for running WASM"sv)), |
58 | 0 | ConfEnableCoredump(PO::Description( |
59 | 0 | "Enable coredump when WebAssembly enters a trap. Requires the " |
60 | 0 | "interpreter run mode, otherwise it is disabled"sv)), |
61 | 0 | ConfCoredumpWasmgdb( |
62 | 0 | PO::Description("Enable coredump in the format expected by the " |
63 | 0 | "wasmgdb debugger, which differs from the one of " |
64 | 0 | "the WebAssembly tool conventions"sv)), |
65 | 0 | ConfForceInterpreter( |
66 | 0 | PO::Description("Forcibly run WASM in interpreter mode."sv)), |
67 | 0 | ConfRunMode(PO::Description("Set execution mode. Valid values: " |
68 | 0 | "interpreter, jit, aot, lazyjit. " |
69 | 0 | "Default is interpreter."sv), |
70 | 0 | PO::MetaVar("MODE"sv), PO::DefaultValue(std::string())), |
71 | 0 | ConfAFUNIX(PO::Description("Enable UNIX domain sockets"sv)), |
72 | 0 | TimeLim( |
73 | 0 | PO::Description( |
74 | 0 | "Limitation of maximum time(in milliseconds) for execution, " |
75 | 0 | "default value is 0 for no limitations"sv), |
76 | 0 | PO::MetaVar("TIMEOUT"sv), PO::DefaultValue<uint64_t>(0)), |
77 | 0 | GasLim( |
78 | 0 | PO::Description( |
79 | 0 | "Limitation of execution gas. Upper bound can be specified as " |
80 | 0 | "--gas-limit `GAS_LIMIT`."sv), |
81 | 0 | PO::MetaVar("GAS_LIMIT"sv)), |
82 | 0 | MemLim( |
83 | 0 | PO::Description( |
84 | 0 | "Limitation of pages(as size of 64 KiB) in every memory " |
85 | 0 | "instance. Upper bound can be specified as --memory-page-limit " |
86 | 0 | "`PAGE_COUNT`."sv), |
87 | 0 | PO::MetaVar("PAGE_COUNT"sv)), |
88 | 0 | StackLim(PO::Description( |
89 | 0 | "Limitation of stack size (in bytes) for execution, " |
90 | 0 | "default value is 0 for the engine default (8 MiB for " |
91 | 0 | "the interpreter, 512 KiB for compiled code)"sv), |
92 | 0 | PO::MetaVar("BYTES"sv), PO::DefaultValue<uint64_t>(0)), |
93 | 0 | LinkedModules( |
94 | 0 | PO::Description( |
95 | 0 | "Register additional WASM modules for linking. Each module " |
96 | 0 | "can be specified as --module `name:path`, where `name` is " |
97 | 0 | "the module name to export and `path` is the WASM file path."sv), |
98 | 0 | PO::MetaVar("MODULES"sv)), |
99 | 0 | ForbiddenPlugins(PO::Description("List of plugins to ignore."sv), |
100 | 0 | PO::MetaVar("NAMES"sv)) {} |
101 | | |
102 | | PO::Option<std::string> SoName; |
103 | | PO::List<std::string> Args; |
104 | | PO::Option<PO::Toggle> Reactor; |
105 | | PO::List<std::string> Dir; |
106 | | PO::List<std::string> Env; |
107 | | PO::Option<PO::Toggle> PropComponent; |
108 | | PO::Option<PO::Toggle> ConfEnableJIT; |
109 | | PO::Option<PO::Toggle> ConfEnableCoredump; |
110 | | PO::Option<PO::Toggle> ConfCoredumpWasmgdb; |
111 | | PO::Option<PO::Toggle> ConfForceInterpreter; |
112 | | PO::Option<std::string> ConfRunMode; |
113 | | PO::Option<PO::Toggle> ConfAFUNIX; |
114 | | PO::Option<uint64_t> TimeLim; |
115 | | PO::List<uint64_t> GasLim; |
116 | | PO::List<uint64_t> MemLim; |
117 | | PO::Option<uint64_t> StackLim; |
118 | | PO::List<std::string> LinkedModules; |
119 | | PO::List<std::string> ForbiddenPlugins; |
120 | | |
121 | | private: |
122 | 0 | void addGlobalOptions(PO::ArgumentParser &Parser) noexcept { |
123 | 0 | addLoggingOptions(Parser); |
124 | 0 | Parser.add_option("forbidden-plugin"sv, ForbiddenPlugins); |
125 | 0 | } |
126 | | |
127 | | public: |
128 | | /// Take the global options given before the subcommand. |
129 | 0 | void inheritGlobalOptions(const DriverToolOptions &Parent) noexcept { |
130 | 0 | inheritLoggingOptions(Parent); |
131 | 0 | ForbiddenPlugins.value().insert(ForbiddenPlugins.value().begin(), |
132 | 0 | Parent.ForbiddenPlugins.value().begin(), |
133 | 0 | Parent.ForbiddenPlugins.value().end()); |
134 | 0 | } |
135 | | |
136 | 0 | void addParserOptions(PO::ArgumentParser &Parser) noexcept { |
137 | 0 | addGlobalOptions(Parser); |
138 | 0 | addProposalOptions(Parser); |
139 | 0 | Parser.add_option("enable-component"sv, PropComponent).add_option(SoName); |
140 | 0 | } |
141 | | |
142 | 0 | void addLinkerOptions(PO::ArgumentParser &Parser) noexcept { |
143 | 0 | addParserOptions(Parser); |
144 | |
|
145 | 0 | Plugin::Plugin::loadFromDefaultPaths(); |
146 | 0 | Plugin::Plugin::addPluginOptions(Parser); |
147 | |
|
148 | 0 | Parser.add_option("dir"sv, Dir) |
149 | 0 | .add_option("env"sv, Env) |
150 | 0 | .add_option("module"sv, LinkedModules) |
151 | 0 | .add_option("memory-page-limit"sv, MemLim); |
152 | 0 | } |
153 | | |
154 | 0 | void addOptions(PO::ArgumentParser &Parser) noexcept { |
155 | 0 | addLinkerOptions(Parser); |
156 | | |
157 | | // pure Execution and Profiling flags |
158 | 0 | Parser.add_option(Args); |
159 | 0 | addStatisticsOptions(Parser); |
160 | 0 | Parser.add_option("enable-jit"sv, ConfEnableJIT) |
161 | 0 | .add_option("enable-coredump"sv, ConfEnableCoredump) |
162 | 0 | .add_option("coredump-for-wasmgdb"sv, ConfCoredumpWasmgdb) |
163 | 0 | .add_option("force-interpreter"sv, ConfForceInterpreter) |
164 | 0 | .add_option("run-mode"sv, ConfRunMode) |
165 | 0 | .add_option("allow-af-unix"sv, ConfAFUNIX) |
166 | 0 | .add_option("time-limit"sv, TimeLim) |
167 | 0 | .add_option("gas-limit"sv, GasLim) |
168 | 0 | .add_option("stack-size-limit"sv, StackLim) |
169 | 0 | .add_option("reactor"sv, Reactor); |
170 | 0 | } |
171 | | }; |
172 | | Configure createConfigure(const struct DriverToolOptions &Opt) noexcept; |
173 | | std::optional<RunMode> parseRunModeArg(std::string_view S) noexcept; |
174 | | std::optional<std::filesystem::path> |
175 | | getInputPath(const struct DriverToolOptions &Opt) noexcept; |
176 | | void setMemoryPageLimit(const struct DriverToolOptions &Opt, |
177 | | Configure &Conf) noexcept; |
178 | | bool registerLinkedModules(const struct DriverToolOptions &Opt, |
179 | | VM::VM &VM) noexcept; |
180 | | |
181 | | int Tool(struct DriverToolOptions &Opt) noexcept; |
182 | | int ParseTool(struct DriverToolOptions &Opt) noexcept; |
183 | | int ValidateTool(struct DriverToolOptions &Opt) noexcept; |
184 | | int InstantiateTool(struct DriverToolOptions &Opt) noexcept; |
185 | | |
186 | | } // namespace Driver |
187 | | } // namespace WasmEdge |