This is an archive of the discontinued LLVM Phabricator instance.

[sanitizer] Do not over-dup string flags
ClosedPublic

Authored by cryptoad on Aug 21 2017, 9:38 AM.

Details

Summary

String flags values appear to be duped twice. Once in FlagParser::parse_flag
using the LowLevelAllocator via ll_strndup, once in
FlagHandler<const char *>::Parse using the InternalAllocator via
internal_strdup. It looks like the second one is redundant, as the memory
for the first one is never freed and not used for anything else.

Assigning the value to the flag instead of duping it has a few advantages:

  • if it was the only use of the InternalAllocator (which is the case for Scudo), then the related code will not be compiled it, which saves us a whole instantiation of the CombinedAllocator worth of extra code;
  • in the event a string flag is parsed, the InternalAllocator would have created a whole SizeClassAllocator32 region for a single allocation, which is kind of wasteful.
  • also, the string is dup'ed twice for the whole lifetime of a process.

I tested check-{sanitizer,asan,tsan,ubsan,scudo} successfully, so as far as I
can tell this doesn't appear to have bad side effects.

Event Timeline

cryptoad created this revision.Aug 21 2017, 9:38 AM
cryptoad abandoned this revision.Aug 21 2017, 11:11 AM

FlagHandlerInclude actually maps a new buffer before calling Parse and then unmaps it, so that change wouldn't work in that case.
I will find another way to do this.

cryptoad reclaimed this revision.Aug 21 2017, 11:16 AM

Nervermind, totally misread FlagHandlerInclude code, it should still work. Sorry for the noise.

This revision is now accepted and ready to land.Aug 21 2017, 11:32 AM
cryptoad closed this revision.Aug 21 2017, 2:26 PM