You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
wasm-opt(Windows): child not found in parent, UNREACHABLE executed #6639
Very strange that there would be a windows-specific bug. Perhaps undefined behavior in some C++ STL stuff?
I don't have a windows machine so this would be hard for me to investigate. Can you perhaps reduce this with wasm-reduce? A tiny testcase might be much easier to reason about.
@kripken I did some digging and found out that -O2 and -O3 cause this behavior (-O1 works fine). bisecting commits lead me to this commit 141f7ca (someone already made a comment on the commit but he didn't follow up I guess).
I don't have a guess as to what could be going wrong in the code, very strange - there should be no way for the child not to have a proper parent. That this only happens on windows makes me wonder if this is a compiler bug. Which compilers do you see this with? I'd be especially interested in comparing MSVC, clang, and clang+libc++ (the last would not be using the system C++ libraries).
Reducing could still help here, but if this is a compiler bug then the more important thing might be to narrow this down to which compiler and version it happens on.
@kripken I did some digging and found out that -O2 and -O3 cause this behavior (-O1 works fine). bisecting commits lead me to this commit 141f7ca (someone already made a comment on the commit but he didn't follow up I guess).
I will see what I can do for/with wasm-reduce.
the -O1 works, because it doesn't hit the path with partially precompute
Available release version from this repo (unmodified)
Debug/Release of HEAD built with MSVC 22 14.40.33807
So I guess this isn't a compiler issue since official release is built with mingw(?) and my version with MSVC.
I tried my best using wasm-reduce but I wasn't able to produce any useful result.
So I went through the rabbit hole of trying to debug this issue : (Disclaimer my C++ and Windows skills are a bit Rust-y and I might be missing things here)
Stepping into debug mode and walking through execution ends up every-time in trap.
Trying to debug 'print' using dump function, leads to scrambled output (multi-threading).
I applied few changes as per the diff below, indicate a use after free. (or at least this what I observed)
Patch : (activate thread sanitizer + change the builtin trap for msvc compatibility )
Perhaps this is UB due to how the stack works on windows somehow, I'm really not sure. I don't get an error with ASan on Linux on that file with the command from the top comment, at least. But the ASan error line does point to a for loop with a stack allocation, so maybe that is the issue? I'm not an expert on that but I recall the rules are subtle. Does this diff help?
I submitted a PR before seeing your comment, so yeah I confirm the use after free is on ChildIterator.
my fix is slightly different that your suggestion.
Sounds good, thanks for the PR. I guess in C++ the stack allocation really does not live til the end of the loop... mysterious that only on windows does this end up noticeable.
Activity
kripken commentedon Jun 1, 2024
Very strange that there would be a windows-specific bug. Perhaps undefined behavior in some C++ STL stuff?
I don't have a windows machine so this would be hard for me to investigate. Can you perhaps reduce this with wasm-reduce? A tiny testcase might be much easier to reason about.
mtb0x1 commentedon Jun 1, 2024
@kripken I did some digging and found out that
-O2and-O3cause this behavior (-O1works fine). bisecting commits lead me to this commit 141f7ca (someone already made a comment on the commit but he didn't follow up I guess).I will see what I can do for/with wasm-reduce.
cyk2018 commentedon Jun 3, 2024
It seems item (with index i) in your stack is not the child of item(with index i - 1).
kripken commentedon Jun 3, 2024
I don't have a guess as to what could be going wrong in the code, very strange - there should be no way for the child not to have a proper parent. That this only happens on windows makes me wonder if this is a compiler bug. Which compilers do you see this with? I'd be especially interested in comparing MSVC, clang, and clang+libc++ (the last would not be using the system C++ libraries).
Reducing could still help here, but if this is a compiler bug then the more important thing might be to narrow this down to which compiler and version it happens on.
cyk2018 commentedon Jun 4, 2024
Yes, very strange! maybe we can wait for more information.
radekdoulik commentedon Jun 4, 2024
I see it when using
wasm-optfrom binaryen version_117 on windows too:radekdoulik commentedon Jun 4, 2024
the
-O1works, because it doesn't hit the path with partially precomputemtb0x1 commentedon Jun 4, 2024
@kripken
Just to make sure there is no miscommunication, the issue happens with :
So I guess this isn't a compiler issue since official release is built with mingw(?) and my version with MSVC.
I tried my best using wasm-reduce but I wasn't able to produce any useful result.
So I went through the rabbit hole of trying to debug this issue : (Disclaimer my C++ and Windows skills are a bit Rust-y and I might be missing things here)
Patch : (activate thread sanitizer + change the builtin trap for msvc compatibility )
Result :
kripken commentedon Jun 4, 2024
Interesting, thanks for investigating @mtb0x1 ...
Perhaps this is UB due to how the stack works on windows somehow, I'm really not sure. I don't get an error with ASan on Linux on that file with the command from the top comment, at least. But the ASan error line does point to a for loop with a stack allocation, so maybe that is the issue? I'm not an expert on that but I recall the rules are subtle. Does this diff help?
mtb0x1 commentedon Jun 4, 2024
I submitted a PR before seeing your comment, so yeah I confirm the use after free is on ChildIterator.
my fix is slightly different that your suggestion.
kripken commentedon Jun 4, 2024
Sounds good, thanks for the PR. I guess in C++ the stack allocation really does not live til the end of the loop... mysterious that only on windows does this end up noticeable.
Fix stack-use-after-scope on Windows in Precompute (WebAssembly#6643)
Fix stack-use-after-scope on Windows in Precompute (WebAssembly#6643)
Fix stack-use-after-scope on Windows in Precompute (WebAssembly#6643)