summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/support/path.cpp29
-rw-r--r--src/tools/wasm-split.cpp48
-rw-r--r--src/wasm-binary.h13
-rw-r--r--src/wasm-io.h5
-rw-r--r--src/wasm/wasm-binary.cpp10
-rw-r--r--src/wasm/wasm-io.cpp3
6 files changed, 87 insertions, 21 deletions
diff --git a/src/support/path.cpp b/src/support/path.cpp
index 2501c636c..1cf5c736d 100644
--- a/src/support/path.cpp
+++ b/src/support/path.cpp
@@ -34,20 +34,33 @@ char getPathSeparator() {
#endif
}
+static std::string getAllPathSeparators() {
+ // The canonical separator on Windows is `\`, but it also accepts `/`.
+#if defined(WIN32) || defined(_WIN32)
+ return "\\/";
+#else
+ return "/";
+#endif
+}
+
std::string getDirName(const std::string& path) {
- auto sep = path.rfind(getPathSeparator());
- if (sep == std::string::npos) {
- return "";
+ for (char c : getAllPathSeparators()) {
+ auto sep = path.rfind(c);
+ if (sep != std::string::npos) {
+ return path.substr(0, sep);
+ }
}
- return path.substr(0, sep);
+ return "";
}
std::string getBaseName(const std::string& path) {
- auto sep = path.rfind(getPathSeparator());
- if (sep == std::string::npos) {
- return path;
+ for (char c : getAllPathSeparators()) {
+ auto sep = path.rfind(c);
+ if (sep != std::string::npos) {
+ return path.substr(sep + 1);
+ }
}
- return path.substr(sep + 1);
+ return path;
}
std::string getBinaryenRoot() {
diff --git a/src/tools/wasm-split.cpp b/src/tools/wasm-split.cpp
index 272582e65..3ac8b2c45 100644
--- a/src/tools/wasm-split.cpp
+++ b/src/tools/wasm-split.cpp
@@ -23,6 +23,7 @@
#include "pass.h"
#include "support/file.h"
#include "support/name.h"
+#include "support/path.h"
#include "support/utilities.h"
#include "tool-options.h"
#include "wasm-builder.h"
@@ -53,6 +54,9 @@ struct WasmSplitOptions : ToolOptions {
bool instrument = false;
+ // TODO: Remove this. See the comment in wasm-binary.h.
+ bool emitModuleNames = false;
+
std::string profileFile;
std::string profileExport = DEFAULT_PROFILE_EXPORT;
@@ -192,6 +196,15 @@ WasmSplitOptions::WasmSplitOptions()
[&](Options* o, const std::string& arguments) {
passOptions.debugInfo = true;
})
+ .add(
+ "--emit-module-names",
+ "",
+ "Emit module names, even if not emitting the rest of the names section. "
+ "Can help differentiate the modules in stack traces. This option will be "
+ "removed once simpler ways of naming modules are widely available. See "
+ "https://bugs.chromium.org/p/v8/issues/detail?id=11808.",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { emitModuleNames = true; })
.add("--initial-table",
"",
"A hack to ensure the split and instrumented modules have the same "
@@ -500,6 +513,18 @@ void adjustTableSize(Module& wasm, int initialSize) {
table->initial = initialSize;
}
+void writeModule(Module& wasm,
+ std::string filename,
+ const WasmSplitOptions& options) {
+ ModuleWriter writer;
+ writer.setBinary(options.emitBinary);
+ writer.setDebugInfo(options.passOptions.debugInfo);
+ if (options.emitModuleNames) {
+ writer.setEmitModuleName(true);
+ }
+ writer.write(wasm, filename);
+}
+
void instrumentModule(Module& wasm, const WasmSplitOptions& options) {
// Check that the profile export name is not already taken
if (wasm.getExportOrNull(options.profileExport) != nullptr) {
@@ -513,10 +538,7 @@ void instrumentModule(Module& wasm, const WasmSplitOptions& options) {
adjustTableSize(wasm, options.initialTableSize);
// Write the output modules
- ModuleWriter writer;
- writer.setBinary(options.emitBinary);
- writer.setDebugInfo(options.passOptions.debugInfo);
- writer.write(wasm, options.output);
+ writeModule(wasm, options.output, options);
}
// See "wasm-split profile format" above for more information.
@@ -665,12 +687,18 @@ void splitModule(Module& wasm, const WasmSplitOptions& options) {
writeSymbolMap(*secondary, options.secondaryOutput + ".symbols");
}
- // Write the output modules
- ModuleWriter writer;
- writer.setBinary(options.emitBinary);
- writer.setDebugInfo(options.passOptions.debugInfo);
- writer.write(wasm, options.primaryOutput);
- writer.write(*secondary, options.secondaryOutput);
+ // Set the names of the split modules. This can help differentiate them in
+ // stack traces.
+ if (options.emitModuleNames) {
+ if (!wasm.name) {
+ wasm.name = Path::getBaseName(options.primaryOutput);
+ }
+ secondary->name = Path::getBaseName(options.secondaryOutput);
+ }
+
+ // write the output modules
+ writeModule(wasm, options.primaryOutput, options);
+ writeModule(*secondary, options.secondaryOutput, options);
}
} // anonymous namespace
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index b2f681b6e..6852c42ab 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -1170,7 +1170,11 @@ public:
std::vector<Entry> functionBodies;
} tableOfContents;
- void setNamesSection(bool set) { debugInfo = set; }
+ void setNamesSection(bool set) {
+ debugInfo = set;
+ emitModuleName = set;
+ }
+ void setEmitModuleName(bool set) { emitModuleName = set; }
void setSourceMap(std::ostream* set, std::string url) {
sourceMap = set;
sourceMapUrl = url;
@@ -1265,6 +1269,13 @@ private:
std::vector<HeapType> types;
bool debugInfo = true;
+
+ // TODO: Remove `emitModuleName` in the future once there are better ways to
+ // ensure modules have meaningful names in stack traces.For example, using
+ // ObjectURLs works in FireFox, but not Chrome. See
+ // https://bugs.chromium.org/p/v8/issues/detail?id=11808.
+ bool emitModuleName = true;
+
std::ostream* sourceMap = nullptr;
std::string sourceMapUrl;
std::string symbolMap;
diff --git a/src/wasm-io.h b/src/wasm-io.h
index 036dc9c8c..1e28b5f92 100644
--- a/src/wasm-io.h
+++ b/src/wasm-io.h
@@ -82,6 +82,10 @@ private:
class ModuleWriter : public ModuleIOBase {
bool binary = true;
+
+ // TODO: Remove `emitModuleName`. See the comment in wasm-binary.h
+ bool emitModuleName = false;
+
std::string symbolMap;
std::string sourceMapFilename;
std::string sourceMapUrl;
@@ -99,6 +103,7 @@ public:
void setSourceMapUrl(std::string sourceMapUrl_) {
sourceMapUrl = sourceMapUrl_;
}
+ void setEmitModuleName(bool set) { emitModuleName = set; }
// write text
void writeText(Module& wasm, Output& output);
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index ddb0ece90..97dad88bb 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -60,7 +60,7 @@ void WasmBinaryWriter::write() {
writeDataCount();
writeFunctions();
writeDataSegments();
- if (debugInfo) {
+ if (debugInfo || emitModuleName) {
writeNames();
}
if (sourceMap && !sourceMapUrl.empty()) {
@@ -661,13 +661,19 @@ void WasmBinaryWriter::writeNames() {
writeInlineString(BinaryConsts::UserSections::Name);
// module name
- if (wasm->name.is()) {
+ if (emitModuleName && wasm->name.is()) {
auto substart =
startSubsection(BinaryConsts::UserSections::Subsection::NameModule);
writeEscapedName(wasm->name.str);
finishSubsection(substart);
}
+ if (!debugInfo) {
+ // We were only writing the module name.
+ finishSection(start);
+ return;
+ }
+
// function names
{
auto substart =
diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp
index 42eb9ab46..795bf40a5 100644
--- a/src/wasm/wasm-io.cpp
+++ b/src/wasm/wasm-io.cpp
@@ -137,6 +137,9 @@ void ModuleWriter::writeBinary(Module& wasm, Output& output) {
WasmBinaryWriter writer(&wasm, buffer);
// if debug info is used, then we want to emit the names section
writer.setNamesSection(debugInfo);
+ if (emitModuleName) {
+ writer.setEmitModuleName(true);
+ }
std::unique_ptr<std::ofstream> sourceMapStream;
if (sourceMapFilename.size()) {
sourceMapStream = make_unique<std::ofstream>();