This is an archive of the discontinued LLVM Phabricator instance.

[clang] remove dereferencing of invalid pointer
ClosedPublic

Authored by ashay-github on Apr 24 2023, 6:37 AM.

Details

Summary

A line in the demangling code for float literals dereferences the
.end() iterator, which causes the Windows debug build of llvm-cxxfilt
to crash. The failure can be reproduced by passing the string
_Z5dummyIXtl8wrapper1IdEtlNS1_Ut_Edi9RightNametlNS2_Ut_ELd405ec00000000000EEEEEEvv
to llvm-cxxfilt -n.

This patch rewrites the code to use the .size() member of the
string_view type to avoid dereferencing past the buffer.

Diff Detail

Event Timeline

ashay-github created this revision.Apr 24 2023, 6:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 24 2023, 6:37 AM
ashay-github requested review of this revision.Apr 24 2023, 6:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 24 2023, 6:37 AM

Seems fine to me, that is what size is for after all :). Just one comment.

llvm/include/llvm/Demangle/ItaniumDemangle.h
2332–2333

Might as well merge this into the const char* t = line below.

inlined the definition of first into its two uses

DavidSpickett accepted this revision.Apr 24 2023, 7:41 AM

LGTM, thanks.

This revision is now accepted and ready to land.Apr 24 2023, 7:41 AM
This revision was automatically updated to reflect the committed changes.

Thanks for the patch! It would have been good to include your test case though.

It would have been good to include your test case though.

Ah, I forgot to mention that the error was also reproducible using an existing test (the one in clang/test/CodeGenCXX/mangle-nttp-anon-union.cpp). The specific case that I mentioned ("_Z5dummyIXtl8wrapper1IdEtlNS1_Ut_Edi9RightNametlNS2_Ut_ELd405ec00000000000EEEEEEvv") is just a subset of the mangle-nttp-anon-union.cpp test, specifically the lines below:

dummy<wrapper1<double>{123.0}>();
// CHECK: call void @_Z5dummyIXtl8wrapper1IdEtlNS1_Ut_Edi9RightNametlNS2_Ut_ELd405ec00000000000EEEEEEvv
// DEMANGLED: call void @void dummy<wrapper1<double>{wrapper1<double>::'unnamed'{.RightName = wrapper1<double>::'unnamed'::'unnamed'{0x1.ec{{.*}}p+6}}}>()()

It would have been good to include your test case though.

Ah, I forgot to mention that the error was also reproducible using an existing test (the one in clang/test/CodeGenCXX/mangle-nttp-anon-union.cpp). The specific case that I mentioned ("_Z5dummyIXtl8wrapper1IdEtlNS1_Ut_Edi9RightNametlNS2_Ut_ELd405ec00000000000EEEEEEvv") is just a subset of the mangle-nttp-anon-union.cpp test, specifically the lines below:

dummy<wrapper1<double>{123.0}>();
// CHECK: call void @_Z5dummyIXtl8wrapper1IdEtlNS1_Ut_Edi9RightNametlNS2_Ut_ELd405ec00000000000EEEEEEvv
// DEMANGLED: call void @void dummy<wrapper1<double>{wrapper1<double>::'unnamed'{.RightName = wrapper1<double>::'unnamed'::'unnamed'{0x1.ec{{.*}}p+6}}}>()()

Got it. Any ideas why I'm not able to reproduce on a non-windows host (and AFAIK that specific test case hasn't been flagged by any build bots)? I would think we should still be able to test the windows demangling scheme on any host.

Also, llvm/include/llvm/Demangle/ is the downstream copy from libcxxabi/. I'll have to copy it back in https://reviews.llvm.org/D148566.

Any ideas why I'm not able to reproduce on a non-windows host (and AFAIK that specific test case hasn't been flagged by any build bots)? I would think we should still be able to test the windows demangling scheme on any host.

My apologies for dropping this. I was out of town when your message arrived.

My hypothesis is that you may have build LLVM in Release configuration (and perhaps the bots build in Release configuration as well). Our Windows Release builds were passing but the Debug builds failed. When I looked through the standard library functions in MSVC header files, I found that a vast number of them contained #ifdef statements that were predicated on whether the compiler is called with debug flags, with the debug builds containing far more conservative checks.