Skip to content
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

vector_algorithms.cpp: __std_find_trivial_unsized_impl assumes N-byte elements are N-aligned #4454

Closed
StephanTLavavej opened this issue Mar 6, 2024 · 1 comment · Fixed by #4450
Labels
bug Something isn't working fixed Something works now, yay! ranges C++20/23 ranges

Comments

@StephanTLavavej
Copy link
Member

StephanTLavavej commented Mar 6, 2024

This initially appeared to be x86 /clr specific - but it wasn't, see below.

With #4450 and an MSVC-internal build:

C:\Temp>cl /clr /nologo /W4 /std:c++20 /MDd /Od D:\msvc\src\vctools\crt\github\tests\std\tests\VSO_0000000_vector_algorithms\test.cpp /I D:\msvc\src\vctools\crt\github\tests\std\include
test.cpp

C:\Temp>test
This is a randomized test.
DO NOT IGNORE/RERUN ANY FAILURES.
You must report them to the STL maintainers.

Seed vector: [...]

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at __std_find_trivial_unsized_8(Void* , UInt64 )
   at std.ranges._Find_unchecked<unsigned __int64 *,struct std::unreachable_sentinel_t,unsigned __int64,struct std::identity>(UInt64* _First, unreachable_sentinel_t _Last, UInt64* _Val, identity _Proj)
   at std.ranges._Find_fn.operator()<unsigned __int64 *,struct std::unreachable_sentinel_t,unsigned __int64,struct std::identity>(_Find_fn* , UInt64* _First, unreachable_sentinel_t _Last, UInt64* _Val, identity _Proj)
   at test_gh_4449<unsigned __int64>()
   at test_vector_algorithms(mersenne_twister_engine<unsigned __int64\,64\,312\,156\,31\,-5403634167711393303\,29\,6148914691236517205\,17\,8202884508482404352\,37\,-2270628950310912\,43\,6364136223846793005>* gen)
   at main()
   at _mainCRTStartup()

I can also repro this with plain VS 2022 17.10 Preview 1 x86, if I take #4450's test and alter it to disable the assert(where == arr + mid2), so this doesn't appear to be a regression caused by #4450.

@StephanTLavavej StephanTLavavej added bug Something isn't working ranges C++20/23 ranges labels Mar 6, 2024
@StephanTLavavej
Copy link
Member Author

@AlexGuteniev realized that this isn't /clr-specific! Vectorized find-unsized assumes that its N-byte elements are N-aligned, but x86 notoriously only has a 4-aligned stack even for 8-byte elements. When this happens, we'll generally run off the end - unrelated to the before-begin masking.

#pragma pack repros this quite nicely. I believe I can fix this by adding an alignment check to the algorithm.

@StephanTLavavej StephanTLavavej changed the title vector_algorithms.cpp: x86 /clr System.AccessViolationException at __std_find_trivial_unsized_8 vector_algorithms.cpp: __std_find_trivial_unsized_impl assumes N-byte elements are N-aligned Mar 6, 2024
@StephanTLavavej StephanTLavavej added the fixed Something works now, yay! label Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay! ranges C++20/23 ranges
Projects
None yet
1 participant