Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/support/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,23 @@ size_t wasm::file_size(std::string filename) {
std::ifstream::ate | std::ifstream::binary);
return infile.tellg();
}

void wasm::flush_and_quick_exit(int code) {
// We expect C++ files to be flushed by their destructors already. Flush the
// standard streams manually.
std::cout << std::flush;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use std::at_quick_exit to register a function to explicitly flush the open descriptors. Can that be done for the output file too, or is that already guaranteed by some other means?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(the advantage of this would be that you could just use std::quick_exit(0) directly at the current flush_and_quick_exit callsites)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the output files should be flushed by their destructors? (this PR ensures those destructors run before exit)

Interesting about at_quick_exit - that could work, though I don't have a preference myself (doesn't seem like a problem to wrap quick_exit, in fact it might be nice for debugging) - wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion, it doesn't make that much difference in the end, you either have this wrapper, or a callback. You wouldn't have to worry about plumbing the error code through or not, but you are right that the control is conceptually simpler without the callback.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's simpler without the callback - plumbing through the code, which I just added, is easy enough.

std::cerr << std::flush;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to add fflush(NULL) here, either in addtion or insteadof the above two lines.

If the stream argument is NULL, fflush() flushes all open output streams.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not an expert on this, but from the docs I think that is not needed, if all the C++ ostreams are closed? Closing them (e.g. by their destructor) should flush them, so only the standard output streams need special handling?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure but fflush(NULL) is fewer lines :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is there some difference between flushing C++ ostreams and C fflush?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure but fflush(NULL) is fewer lines :)

But I think that C command flushes the file handles themselves, while the C++ command also flushes anything buffered at the C++ level..? That is, C++ has buffering in ostream, iirc - am I wrong?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I guess you never know if C++ is doing anything here... but i doubt its doing more buffering on top of what C is doing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added fflush(NULL). From the docs I am not certain it is not needed, and it seems safe/harmless.


// To be safe, also flush at the C level.
fflush(NULL);

#if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || \
__has_feature(memory_sanitizer) || __has_feature(leak_sanitizer) || \
__has_feature(undefined_behavior_sanitizer)
// Avoid quick_exit when using sanitizers, so that leak checks and other
// things can run during shutdown normally.
std::exit(code);
#else
std::quick_exit(code);
#endif
}
8 changes: 7 additions & 1 deletion src/support/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ class Output {
// Copies a file to another file
void copy_file(std::string input, std::string output);

// Retusn the size of a file
// Return the size of a file
size_t file_size(std::string filename);

// Flush stdout and stderr, and quickly exit. This assumes that all files that
// need flushing have been flushed, that is, no global ctors or RAII in main's
// top level are depended on (using quick_exit then avoids running global dtors,
// which saves otherwise wasted time).
[[noreturn]] void flush_and_quick_exit(int code);

} // namespace wasm

#endif // wasm_support_file_h
26 changes: 16 additions & 10 deletions src/tools/wasm-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,25 @@ int main(int argc, const char* argv[]) {
if (options.debug) {
std::cerr << "writing..." << std::endl;
}
ModuleWriter writer(options.passOptions);
writer.setBinary(true);
writer.setDebugInfo(debugInfo);
if (sourceMapFilename.size()) {
writer.setSourceMapFilename(sourceMapFilename);
writer.setSourceMapUrl(sourceMapUrl);
}
if (symbolMap.size() > 0) {
writer.setSymbolMap(symbolMap);

// Ensure the destructor of ModuleWriter runs before quick_exit.
{
ModuleWriter writer(options.passOptions);
writer.setBinary(true);
writer.setDebugInfo(debugInfo);
if (sourceMapFilename.size()) {
writer.setSourceMapFilename(sourceMapFilename);
writer.setSourceMapUrl(sourceMapUrl);
}
if (symbolMap.size() > 0) {
writer.setSymbolMap(symbolMap);
}
writer.write(wasm, options.extra["output"]);
}
writer.write(wasm, options.extra["output"]);

if (options.debug) {
std::cerr << "Done." << std::endl;
}

flush_and_quick_exit(0);
}
2 changes: 2 additions & 0 deletions src/tools/wasm-ctor-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1618,4 +1618,6 @@ int main(int argc, const char* argv[]) {
writer.setDebugInfo(debugInfo);
writer.write(wasm, options.extra["output"]);
}

flush_and_quick_exit(0);
}
10 changes: 8 additions & 2 deletions src/tools/wasm-dis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ int main(int argc, const char* argv[]) {
if (options.debug) {
std::cerr << "Printing..." << std::endl;
}
Output output(options.extra["output"], Flags::Text);
output.getStream() << wasm << '\n';

// Ensure the destructor of Output runs before quick_exit.
{
Output output(options.extra["output"], Flags::Text);
output.getStream() << wasm << '\n';
}

if (options.debug) {
std::cerr << "Done." << std::endl;
}

flush_and_quick_exit(0);
}
3 changes: 2 additions & 1 deletion src/tools/wasm-emscripten-finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,6 @@ int main(int argc, const char* argv[]) {
}
writer.write(wasm, output);
}
return 0;

flush_and_quick_exit(0);
}
2 changes: 2 additions & 0 deletions src/tools/wasm-merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,4 +832,6 @@ Input source maps can be specified by adding an -ism option right after the modu
}
writer.write(merged, options.extra["output"]);
}

flush_and_quick_exit(0);
}
2 changes: 2 additions & 0 deletions src/tools/wasm-metadce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,6 @@ int main(int argc, const char* argv[]) {

// Clean up
free(copy);

flush_and_quick_exit(0);
}
24 changes: 15 additions & 9 deletions src/tools/wasm-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,18 +476,23 @@ For more on how to optimize effectively, see
std::cerr << "warning: no output file specified, not emitting output\n";
}
}

flush_and_quick_exit(0);
return 0;
}

BYN_TRACE("writing...\n");
ModuleWriter writer(options.passOptions);
writer.setBinary(emitBinary);
writer.setDebugInfo(options.passOptions.debugInfo);
if (outputSourceMapFilename.size()) {
writer.setSourceMapFilename(outputSourceMapFilename);
writer.setSourceMapUrl(outputSourceMapUrl);
// Ensure the destructor of ModuleWriter runs before quick_exit.
{
BYN_TRACE("writing...\n");
ModuleWriter writer(options.passOptions);
writer.setBinary(emitBinary);
writer.setDebugInfo(options.passOptions.debugInfo);
if (outputSourceMapFilename.size()) {
writer.setSourceMapFilename(outputSourceMapFilename);
writer.setSourceMapUrl(outputSourceMapUrl);
}
writer.write(wasm, options.extra["output"]);
}
writer.write(wasm, options.extra["output"]);

if (extraFuzzCommand.size() > 0) {
auto secondOutput = runCommand(extraFuzzCommand);
Expand All @@ -496,5 +501,6 @@ For more on how to optimize effectively, see
Fatal() << "extra fuzz command output differs\n";
}
}
return 0;

flush_and_quick_exit(0);
}
2 changes: 2 additions & 0 deletions src/tools/wasm-shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,4 +603,6 @@ int main(int argc, const char* argv[]) {
Colors::bold(std::cerr);
std::cerr << "all checks passed.\n";
Colors::normal(std::cerr);

flush_and_quick_exit(0);
}
16 changes: 11 additions & 5 deletions src/tools/wasm2js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,14 +993,20 @@ int main(int argc, const char* argv[]) {
if (options.debug) {
std::cerr << "j-printing..." << std::endl;
}
Output output(options.extra["output"], Flags::Text);
if (script && options.extra["asserts"] == "1") {
AssertionEmitter(*script, output, flags, options).emit();
} else {
emitWasm(*wasm, output, flags, options.passOptions, "asmFunc");

// Ensure the destructor of Output runs before quick_exit.
{
Output output(options.extra["output"], Flags::Text);
if (script && options.extra["asserts"] == "1") {
AssertionEmitter(*script, output, flags, options).emit();
} else {
emitWasm(*wasm, output, flags, options.passOptions, "asmFunc");
}
}

if (options.debug) {
std::cerr << "done." << std::endl;
}

flush_and_quick_exit(0);
}
Loading