Skip to content

Conversation

@fsub
Copy link
Contributor

@fsub fsub commented Nov 22, 2020

Particularly fixes 'AddressSanitizer: new-delete-type-mismatch' for std::valarray<int> on x64.

@fsub fsub requested a review from a team as a code owner November 22, 2020 16:53
@fsub

This comment has been minimized.

@StephanTLavavej

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@StephanTLavavej StephanTLavavej added the bug Something isn't working label Nov 24, 2020
Copy link
Contributor

@cbezault cbezault left a comment

Choose a reason for hiding this comment

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

This looks good to me. I took a look and I think that _Myptr will always be allocated with the global operator new.

Could you elaborate on how this is to satisfy ASan? Is it that this breaks when a user provides delete but global operator new is overridden by ASan? Or is it just that ASan detects the difference between operator delete and a delete expression?

@fsub
Copy link
Contributor Author

fsub commented Nov 25, 2020

@cbezault I can't see any reason why the original code could generate wrong instructions for std::valarray<int>. Asymmetric usage of new and delete smells, however, and can lead to real bugs, at least in presence of class-specific overloads.

Reduced repro of the issue reported by ASan (originally found in production code compiled with VS 2019 16.8.2 on x64):

$ cat repro.cxx 
int main(int argc, [[maybe_unused]] char** argv)
{
   auto p = static_cast<int*>(::operator new(argc * sizeof(int)));
   delete p;
}
$ g++ -std=c++17 -fsanitize=address repro.cxx 
$ ./a.out 
$ ./a.out boom
=================================================================
==4208==ERROR: AddressSanitizer: new-delete-type-mismatch on 0x602000000010 in thread T0:
  object passed to delete has wrong type:
  size of the allocated type:   8 bytes;
  size of the deallocated type: 4 bytes.
    #0 0x7f178fa25009 in operator delete(void*, unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:172
    #1 0x56448d26b1a2 in main (/tmp/a.out+0x11a2)
    #2 0x7f178f492151 in __libc_start_main (/usr/lib/libc.so.6+0x28151)
    #3 0x56448d26b09d in _start (/tmp/a.out+0x109d)

0x602000000010 is located 0 bytes inside of 8-byte region [0x602000000010,0x602000000018)
allocated by thread T0 here:
    #0 0x7f178fa23f41 in operator new(unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:99
    #1 0x56448d26b188 in main (/tmp/a.out+0x1188)
    #2 0x7f178f492151 in __libc_start_main (/usr/lib/libc.so.6+0x28151)

SUMMARY: AddressSanitizer: new-delete-type-mismatch /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:172 in operator delete(void*, unsigned long)
==4208==HINT: if you don't care about these errors you may set ASAN_OPTIONS=new_delete_type_mismatch=0
==4208==ABORTING

Copy link
Contributor

@cbezault cbezault left a comment

Choose a reason for hiding this comment

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

That makes sense.

Copy link
Contributor

@CaseyCarter CaseyCarter left a comment

Choose a reason for hiding this comment

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

I pushed a clarification of the comment on line 40; looks good to me, too.

@cbezault
Copy link
Contributor

cbezault commented Dec 1, 2020

I pushed the change that I believe @StephanTLavavej requested.
I didn't add a test to cover checking the mismatch yet. If @StephanTLavavej thinks it is necessary that should also be added.

@StephanTLavavej
Copy link
Member

@cbezault I pushed a small change to clean up a pre-existing cosmetic issue that was being copied here. __cpp_aligned_new implies we're in C++17 mode or later (but not the converse, due to the /Zc:alignedNew- escape hatch), which implies that if constexpr is available. We already assume this elsewhere:

STL/stl/inc/xmemory

Lines 1383 to 1384 in 19c683d

#ifdef __cpp_aligned_new
if constexpr (alignof(_Ty) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {

I attempted to add test coverage for over-aligned types, but encountered error C2719: '_Val': formal parameter with requested alignment of 128 won't be aligned, due to how valarray member functions take _Ty by value. This might mean that over-aligned types have never actually worked with valarray.

@fsub I believe you're being prompted to sign our CLA because our commits editing additional lines have pushed your PR above a minimum threshold.

@StephanTLavavej StephanTLavavej removed their assignment Dec 1, 2020
@fsub
Copy link
Contributor Author

fsub commented Dec 1, 2020

Thank you all for your reviews, comments, and improvements!
@StephanTLavavej Is it really necessary to sign the CLA? I'd rather avoid it. After all, my share of the work is insignificant. I've never been interested in an attribution. My only concern was to draw attention to the problem and find a solution quickly.

@CaseyCarter
Copy link
Contributor

@StephanTLavavej Is it really necessary to sign the CLA? I'd rather avoid it. After all, my share of the work is insignificant. I've never been interested in an attribution. My only concern was to draw attention to the problem and find a solution quickly.

It's only necessary if you want us to merge your PR ;)

I'm not a lawyer, but my understanding is that the CLA is necessary to establish that both parties clearly understand what code contribution means, i.e., what rights over the changes you are sharing with Microsoft.

@fsub
Copy link
Contributor Author

fsub commented Dec 1, 2020

That's too bad. I hope that retracting the pull request will solve all legal issues. I'm sorry for the inconvenience.

@fsub fsub closed this Dec 1, 2020
@StephanTLavavej
Copy link
Member

Since my last commit was cosmetic, I want to try reverting it and seeing if the CLA bot will accept the PR as minimal again. (I wouldn't have pushed that change if I knew this would happen.)

@StephanTLavavej
Copy link
Member

@fsub We can proceed with merging your PR 😸

@StephanTLavavej StephanTLavavej self-assigned this Dec 2, 2020
@StephanTLavavej StephanTLavavej merged commit 89c9592 into microsoft:master Dec 2, 2020
@StephanTLavavej
Copy link
Member

Thanks for fixing this bug, and congratulations on your first microsoft/STL commit! This will ship in VS 2019 16.9 Preview 3. 😺 🚀

@fsub
Copy link
Contributor Author

fsub commented Dec 3, 2020

@StephanTLavavej Thank you for your efforts and your open-mindedness.
Keep up the great work on the C++ standard library! It is greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants