This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho][nfci] Don't include null terminator in StringRefs
ClosedPublic

Authored by int3 on Sep 12 2022, 2:43 PM.

Details

Summary

So @keith observed
here that the
StringRefs we were returning from CStringInputSection::getStringRef()
included the null terminator in their total length, but regular
StringRefs do not. Let's fix that so these StringRefs are less confusing
to use.

Diff Detail

Event Timeline

int3 created this revision.Sep 12 2022, 2:43 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
int3 requested review of this revision.Sep 12 2022, 2:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 12 2022, 2:43 PM
int3 added inline comments.
lld/MachO/MapFile.cpp
91

dereferencing end iterators from the STL is undefined, but fortunately a StringRef isn't part of the STL...

int3 removed a reviewer: ributzka.Sep 12 2022, 2:46 PM
keith accepted this revision.Sep 12 2022, 4:07 PM

thanks! I started on this by had some off by one breaking some other test, but I assume your case ironed that out!

This revision is now accepted and ready to land.Sep 12 2022, 4:07 PM
Roger accepted this revision.Sep 12 2022, 5:20 PM
Roger added a subscriber: Roger.

Generally looks good :)

lld/MachO/SyntheticSections.cpp
1546

It looks to me that we used to copy over a null terminator here but we no longer will do that. Am I correct?

thakis added a subscriber: thakis.Sep 12 2022, 6:00 PM
thakis added inline comments.
lld/MachO/InputSection.cpp
244–245

There's a bunch of places that assume that the trailing \0 byte is still there, but that's generally not the case for StringRefs. Maybe we should use ArrayRefs in the places you touched?

lld/MachO/MapFile.cpp
91

Looks very fishy though. See above :)

int3 marked 2 inline comments as done.Sep 13 2022, 4:29 AM
int3 added inline comments.
lld/MachO/InputSection.cpp
244–245

actually I think it's only that one assert that assumes this, and that assert is no longer actually necessary

all the other places that "skip the null terminator" are basically just skipping a byte in the output buffer. they don't assume that the StringRef itself has a null terminator

lld/MachO/MapFile.cpp
91

I realize that assert was only done because we were trimming off the null terminator in the (now deleted) substr call. So we can remove the assert

lld/MachO/SyntheticSections.cpp
1546

indeed. Though not guaranteed by a spec, it appears that our mmaped output buffer is zero-initialized -- at least, a good deal of the code already assumes that this is the case (and tests would fail otherwise)

int3 updated this revision to Diff 459715.Sep 13 2022, 4:41 AM
int3 marked 2 inline comments as done.

remove assert

int3 added a comment.Sep 13 2022, 6:20 PM

@thakis I'm gonna land this now, will do a follow-up if you have further comments :)

I should probably put an explicit "LGTM" here: LGTM :)

(You landed this at the right time, should've approved with my original comment.)