-
Notifications
You must be signed in to change notification settings - Fork 786
Fixing asm2wasm crashing on \r when built with MinGW #986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -26,7 +26,8 @@ T wasm::read_file(const std::string &filename, Flags::BinaryOption binary, Flags | |||
if (debug == Flags::Debug) std::cerr << "Loading '" << filename << "'..." << std::endl; | |||
std::ifstream infile; | |||
std::ios_base::openmode flags = std::ifstream::in; | |||
if (binary == Flags::Binary) flags |= std::ifstream::binary; | |||
// asm2wasm.exe built with MinGW would crash with files containing '\r' when opened without the binary flag |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does anyone know why mingw is special here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible that it's not MinGW specific but also an issue when built with Visual Studio?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, but the reason I am guessing it's mingw-specific is that people have built and used this with vs. At least I think so, I didn't do it myself...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the code already always handles skipping \r and works on Linux which does not have a non-binary mode, then there's no reason why the file should be opened without the binary flag, and I can't see how it would work better on Visual Studio.
There's other reports of asm2wasm crashing, see for example: #327 which crashes at exactly the same spot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are \r even generated though? Are they only generated when done on Windows? It doesn't make sense to me to generate files meant to be used across platforms differently whether they're created on one platform or another. The 'text' mode and \r\n really needs to disappear anyway, it's such a horrible relic of the past.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think when we generate the asm.js text, on windows the text-handling python code in windows just emits \r
s automatically. @juj knows more.
If there is no danger here, then opening as binary sounds ok, but I don't know enough about windows and text handling there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again opening as binary when outputting the asm.js will avoid those \r's. This will be an issue trying to get reproducible output across systems. Really, the 'text' mode has no reason to be.
@@ -148,6 +148,7 @@ ELSE() | |||
ADD_COMPILE_FLAG("-Wno-unused-parameter") | |||
ADD_COMPILE_FLAG("-fno-omit-frame-pointer") | |||
IF(WIN32) | |||
ADD_LINK_FLAG("-Wl,--stack,8388608") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a comment for this. i assume it's that unix stack sizes are normally 8MB but we need to add this for mingw?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was just making it the same as the MSVC section. MinGW defaults to 2 MB I think.
ping @juj, see question higher up |
Sorry for missing this before! Before we fix this, we really need to have a test case in Emscripten suite about this, since these types of items are critical and we don't routinely test MinGW compiler at the moment. Currently we run the Binaryen suite on VS2015 on each commit, but that is not catching the issue at the moment. @griffin2000, @jerstlouis: do you know of a short test case to repro to see the crash? I'm now building with Binaryen to test this out. |
Investigated this in closer detail, I see that building Emscripten and Binaryen with MinGW 7.1.0, it does fail e.g. in Emscripten's test The problem indeed is MinGW specific and does not appear in Visual Studio. However this is due to a bug in function The behavior that text file reading on Windows (in both VS and MinGW) has is that if a file has a byte sequence This has the effect that the size on disk of a file is larger in bytes than the file size in memory, since The behavioral difference that is observed here between Visual Studio and MinGW is that on Visual Studio, calling The bug is in The difference with the fix here and #1088 is that unconditionally passing binary flag to file open means that, on Windows, the The PR #1088 retains the current property that text content in memory only ever has |
The difference seems to be as to how VS and MinGW perform the contraction of |
Emscripten has a linker flag |
http://www.cplusplus.com/reference/istream/istream/read/ "The number of characters successfully read and stored by this function can be accessed by calling member gcount." |
My approach to parsing text files that may or may not contain \r is always to ignore \r. Simpler that way. I would recommend doing the same and avoiding all these "text" mode caveats. Through I think gcount should still be used here. |
Rewrote the PR to use |
I believe this has been fixed by #1088, thanks everyone. |
There was that unrelated change in this PR, added #1103 for that. |
Also enlarging stack size for MinGW