This is an archive of the discontinued LLVM Phabricator instance.

[profile] Create a copy of profile filename inside of lprofCurFilename.
ClosedPublic

Authored by Dor1s on Aug 18 2017, 3:43 PM.

Details

Reviewers
kcc
davidxl
Summary

It seems much safer to store a copy of the filename, as environment may be
modified our moved between llvm_profile_initialize_file() and
llvm_profile_write_file() function calls.

For example, Chromium does move the environment[1], so the pointer returned by
getenv() on a program startup is not valid at the moment when the program is
closed. Due to that, LLVM profiler cannot write data to the file.

1: https://cs.chromium.org/chromium/src/services/service_manager/embedder/set_process_title_linux.cc?l=73

Diff Detail

Event Timeline

Dor1s created this revision.Aug 18 2017, 3:43 PM
Dor1s added inline comments.Aug 18 2017, 3:48 PM
lib/profile/InstrProfilingFile.c
524

Is that branch really used anywhere? It seems like it doesn't. And INSTR_PROF_PROFILE_NAME_VAR is just an array of 2 chars...

I'd actually prefer to do the following:

  1. remove that branch as it is not used
  2. remove unsigned CopyFilenamePat argument from parseAndSetFilename and parseFilenamePattern functions
  3. always use strdup(FilenamePat) and lprofCurFilename.OwnsFilenamePat = 1;, as it seems to be done only once on a program startup

But I might be missing something, so decided to start with this the least intrusive patch :)

Please let me know what you think about that.

Dor1s added inline comments.Aug 18 2017, 4:01 PM
lib/profile/InstrProfilingFile.c
524

Hm, I see, it might be specified from compiler command line. In that case, I still can either implement points 2) and 3), or pass CopyFilenamePat = 1 only for EnvFilenamePat case.

davidxl edited edge metadata.Aug 18 2017, 4:07 PM

I am not sure what you try to achieve here. Basically, there is no need to to use environment variable.

lib/profile/InstrProfilingFile.c
524

It is used. The compiler will generate the variable which is a weak symbol.

This is basically used when user specifies -fprofile-generate=<filename>

Dor1s added a comment.Aug 18 2017, 4:34 PM

Thank you David for taking a look. Sorry that I didn't make my goal clear. The documentation suggests to use LLVM_PROFILE_FILE env variable if you need to specify a path for the profile. In that case, the profiler basically calls getenv("LLVM_PROFILE_FILE") and preserves the pointer returned by getenv(). Note that happens on a program startup. However, there is no warranty that the pointer still be pointing to the value of LLVM_PROFILE_FILE at the moment when __llvm_profile_write_file() is being executed (i.e. when program exits). That is a TOCTOU bug and leads to an error like the following one:

$ LLVM_PROFILE_FILE="foo.profraw" ./chrome
LLVM Profile Error: Failed to write file "oz/Projects/new/chromium/src/out/CleanCoverage/chrome": No such file or directory
davidxl accepted this revision.Aug 18 2017, 4:51 PM

lgtm

Please add a comment why copy is needed.

This revision is now accepted and ready to land.Aug 18 2017, 4:51 PM
Dor1s updated this revision to Diff 111976.Aug 21 2017, 8:05 AM

Added a comment as requested.

Dor1s added a comment.Aug 21 2017, 8:09 AM

David, may I ask you please to commit this patch, as I don't have Commit Access?