This is an archive of the discontinued LLVM Phabricator instance.

[ms] Fix mangling of string literals used to initialize arrays larger or smaller than the literal
ClosedPublic

Authored by hans on Jul 4 2018, 3:24 AM.

Details

Summary

A Chromium developer reported a bug which turned out to be a mangling collision between these two literals:

char s[] = "foo";
char t[32] = "foo";

They may look the same, but for the initialization of t we will (under some circumstances) use a literal that's extended with zeros, and both the length and those zeros should be accounted for by the mangling.

This actually makes the mangling code simpler: where it previously had special logic for null terminators, which are not part of the StringLiteral, that is now covered by the general algorithm.

Diff Detail

Repository
rC Clang

Event Timeline

hans created this revision.Jul 4 2018, 3:24 AM
hans added a comment.Jul 4 2018, 3:26 AM

I couldn't get MSVC to create a symbol with padding at the end; they seem to always insert the padding with a separate loop during initialization, but this does show the truncated case: https://godbolt.org/g/B8ktA3

kwiberg added a subscriber: kwiberg.Jul 4 2018, 3:46 AM
thakis accepted this revision.Jul 5 2018, 2:31 PM

Nice!

lib/AST/MicrosoftMangle.cpp
3198

nit: Also do unsigned StingByteLength = StringLength * SL->getCharByteWidth() here and use it the 3 times you have that computation below

3211

You're capturing StringLength here, but then you don't use it in the lambda body.

3220

likewise

test/CodeGen/mangle-ms-string-literals.c
8

Nice, we used to mismangle this. MSVC matches the string here: https://godbolt.org/g/dbm6jT (old clang: https://godbolt.org/g/Yy7iCK)

This revision is now accepted and ready to land.Jul 5 2018, 2:31 PM
hans marked 3 inline comments as done.Jul 5 2018, 11:47 PM
hans added inline comments.
lib/AST/MicrosoftMangle.cpp
3198

Good idea, thanks!

3211

Oops, a leftover from an older version of the patch. Removed.

This revision was automatically updated to reflect the committed changes.
hans marked 2 inline comments as done.