This is an archive of the discontinued LLVM Phabricator instance.

[WIP][ASan] Apply -ffile-prefix-map mappings to ASan instrumentation
Changes PlannedPublic

Authored by leonardchan on Mar 18 2020, 11:40 PM.

Details

Summary

Some asan-instrumented binaries that are distributed as part of a clang toolchain result in different sizes depending on the length of the build path. This is because ASan is including the path to the source file from the module ID and sanitizer metadata. To have a deterministic build independent of the source file's location, we can extend the usage of -ffile-prefix-map to also apply to sanitizers.

In this patch:

  • Add a StringMap that can be used by anyone with access to CodeGenOptions that holds all mappings passed to -ffile-prefix-map.
  • Replace path prefixes for module IDs on module creation.
  • Replace path prefixes for sanitizer metadata source locations when creating a GlobalsMetadata object.

Design considerations:

  • We avoid adding a new flag for sanitizers to prevent adding an extra knob. We would also expect that -ffile-prefix-map generally apply to all instances of paths we would see emitted into a stripped object file.
  • I applied the mapping on GlobalsMetadata creation when the ASanGlobalsMetadataWrapperPass runs because I couldn't think of a better place to apply the prefix mapping. The path was originally passed down as a StringRef all the way from a PresumedLoc in SanitizerMetadata::getLocationMetadata(), which cannot be edited with replace_path_prefix.
  • I added the mapping as metadata (llvm.asan.file.prefix.map) to prevent having to pass the original StringMap from clang all the way down to ASan and iterating over the map whenever ASan creates a private string. Adding it to metadata that ASanGlobalsMetadataWrapperPass can read should allow us to do this once when that pass is created.

Diff Detail

Event Timeline

leonardchan created this revision.Mar 18 2020, 11:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 18 2020, 11:40 PM
leonardchan retitled this revision from [ASan] Apply -ffile-prefix-map mappings to ASan instrumentation to [WIP][ASan] Apply -ffile-prefix-map mappings to ASan instrumentation.Mar 19 2020, 7:09 PM

Putting this on hold for now. Although this implementation works for ASan, it would be have to be repeated for other tools like SourceBasedCoverage or other sanitizers.

After discussing with @phosek and @mcgrathr, a more general approach would be to develop some internal API that allows users to get SourceLocations for metadata or other paths that will be embeded in IR. Then once we have a single source for retrieving these paths, we could apply -ffile-prefix-mappings once to prevent having to propagate the mapping down to IR.

Putting this on hold for now. Although this implementation works for ASan, it would be have to be repeated for other tools like SourceBasedCoverage or other sanitizers.

(You can click "Add Action..."->"Plan Changes" so that this Differential will disappear from others' review list for a while..)

leonardchan planned changes to this revision.Mar 26 2020, 11:23 AM