Bug Summary

File:projects/compiler-rt/lib/profile/InstrProfilingFile.c
Warning:line 405, column 7
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name InstrProfilingFile.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/projects/compiler-rt/lib/profile -I /build/llvm-toolchain-snapshot-8~svn345461/projects/compiler-rt/lib/profile -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/include -U NDEBUG -D COMPILER_RT_HAS_ATOMICS=1 -D COMPILER_RT_HAS_FCNTL_LCK=1 -D COMPILER_RT_HAS_UNAME=1 -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -Wno-unused-parameter -Wno-pedantic -fconst-strings -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/projects/compiler-rt/lib/profile -fdebug-prefix-map=/build/llvm-toolchain-snapshot-8~svn345461=. -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-10-27-211344-32123-1 -x c /build/llvm-toolchain-snapshot-8~svn345461/projects/compiler-rt/lib/profile/InstrProfilingFile.c -faddrsig
1/*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
2|*
3|* The LLVM Compiler Infrastructure
4|*
5|* This file is distributed under the University of Illinois Open Source
6|* License. See LICENSE.TXT for details.
7|*
8\*===----------------------------------------------------------------------===*/
9
10#if !defined(__Fuchsia__)
11
12#include <errno(*__errno_location ()).h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#ifdef _MSC_VER
17/* For _alloca. */
18#include <malloc.h>
19#endif
20#if defined(_WIN32)
21#include "WindowsMMap.h"
22/* For _chsize_s */
23#include <io.h>
24#include <process.h>
25#else
26#include <sys/file.h>
27#include <sys/mman.h>
28#include <unistd.h>
29#if defined(__linux__1)
30#include <sys/types.h>
31#endif
32#endif
33
34#include "InstrProfiling.h"
35#include "InstrProfilingInternal.h"
36#include "InstrProfilingUtil.h"
37
38/* From where is profile name specified.
39 * The order the enumerators define their
40 * precedence. Re-order them may lead to
41 * runtime behavior change. */
42typedef enum ProfileNameSpecifier {
43 PNS_unknown = 0,
44 PNS_default,
45 PNS_command_line,
46 PNS_environment,
47 PNS_runtime_api
48} ProfileNameSpecifier;
49
50static const char *getPNSStr(ProfileNameSpecifier PNS) {
51 switch (PNS) {
52 case PNS_default:
53 return "default setting";
54 case PNS_command_line:
55 return "command line";
56 case PNS_environment:
57 return "environment variable";
58 case PNS_runtime_api:
59 return "runtime API";
60 default:
61 return "Unknown";
62 }
63}
64
65#define MAX_PID_SIZE16 16
66/* Data structure holding the result of parsed filename pattern. */
67typedef struct lprofFilename {
68 /* File name string possibly with %p or %h specifiers. */
69 const char *FilenamePat;
70 /* A flag indicating if FilenamePat's memory is allocated
71 * by runtime. */
72 unsigned OwnsFilenamePat;
73 const char *ProfilePathPrefix;
74 const char *Filename;
75 char PidChars[MAX_PID_SIZE16];
76 char Hostname[COMPILER_RT_MAX_HOSTLEN128];
77 unsigned NumPids;
78 unsigned NumHosts;
79 /* When in-process merging is enabled, this parameter specifies
80 * the total number of profile data files shared by all the processes
81 * spawned from the same binary. By default the value is 1. If merging
82 * is not enabled, its value should be 0. This parameter is specified
83 * by the %[0-9]m specifier. For instance %2m enables merging using
84 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
85 * can only appear once at the end of the name pattern. */
86 unsigned MergePoolSize;
87 ProfileNameSpecifier PNS;
88} lprofFilename;
89
90COMPILER_RT_WEAK__attribute__((weak)) lprofFilename lprofCurFilename = {0, 0, 0, 0, {0},
91 {0}, 0, 0, 0, PNS_unknown};
92
93static int getCurFilenameLength();
94static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
95static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
96
97/* Return 1 if there is an error, otherwise return 0. */
98static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
99 uint32_t NumIOVecs) {
100 uint32_t I;
101 FILE *File = (FILE *)This->WriterCtx;
102 for (I = 0; I < NumIOVecs; I++) {
103 if (IOVecs[I].Data) {
104 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
105 IOVecs[I].NumElm)
106 return 1;
107 } else {
108 if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR1) == -1)
109 return 1;
110 }
111 }
112 return 0;
113}
114
115static void initFileWriter(ProfDataWriter *This, FILE *File) {
116 This->Write = fileWriter;
117 This->WriterCtx = File;
118}
119
120COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) ProfBufferIO *
121lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
122 FreeHook = &free;
123 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
124 VPBufferSize = BufferSz;
125 ProfDataWriter *fileWriter =
126 (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1);
127 initFileWriter(fileWriter, File);
128 ProfBufferIO *IO = lprofCreateBufferIO(fileWriter);
129 IO->OwnFileWriter = 1;
130 return IO;
131}
132
133static void setupIOBuffer() {
134 const char *BufferSzStr = 0;
135 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
136 if (BufferSzStr && BufferSzStr[0]) {
137 VPBufferSize = atoi(BufferSzStr);
138 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
139 }
140}
141
142/* Read profile data in \c ProfileFile and merge with in-memory
143 profile counters. Returns -1 if there is fatal error, otheriwse
144 0 is returned. Returning 0 does not mean merge is actually
145 performed. If merge is actually done, *MergeDone is set to 1.
146*/
147static int doProfileMerging(FILE *ProfileFile, int *MergeDone) {
148 uint64_t ProfileFileSize;
149 char *ProfileBuffer;
150
151 if (fseek(ProfileFile, 0L, SEEK_END2) == -1) {
152 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, unable to get size: %s\n"
, strerror((*__errno_location ())));
153 strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, unable to get size: %s\n"
, strerror((*__errno_location ())));
;
154 return -1;
155 }
156 ProfileFileSize = ftell(ProfileFile);
157
158 /* Restore file offset. */
159 if (fseek(ProfileFile, 0L, SEEK_SET0) == -1) {
160 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, unable to rewind: %s\n"
, strerror((*__errno_location ())));
161 strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, unable to rewind: %s\n"
, strerror((*__errno_location ())));
;
162 return -1;
163 }
164
165 /* Nothing to merge. */
166 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
167 if (ProfileFileSize)
168 PROF_WARN("Unable to merge profile data: %s\n",fprintf(stderr, "LLVM Profile Warning: " "Unable to merge profile data: %s\n"
, "source profile file is too small.");
169 "source profile file is too small.")fprintf(stderr, "LLVM Profile Warning: " "Unable to merge profile data: %s\n"
, "source profile file is too small.");
;
170 return 0;
171 }
172
173 ProfileBuffer = mmap(NULL((void*)0), ProfileFileSize, PROT_READ0x1, MAP_SHARED0x01 | MAP_FILE0,
174 fileno(ProfileFile), 0);
175 if (ProfileBuffer == MAP_FAILED((void *) -1)) {
176 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, mmap failed: %s\n"
, strerror((*__errno_location ())));
177 strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, mmap failed: %s\n"
, strerror((*__errno_location ())));
;
178 return -1;
179 }
180
181 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
182 (void)munmap(ProfileBuffer, ProfileFileSize);
183 PROF_WARN("Unable to merge profile data: %s\n",fprintf(stderr, "LLVM Profile Warning: " "Unable to merge profile data: %s\n"
, "source profile file is not compatible.");
184 "source profile file is not compatible.")fprintf(stderr, "LLVM Profile Warning: " "Unable to merge profile data: %s\n"
, "source profile file is not compatible.");
;
185 return 0;
186 }
187
188 /* Now start merging */
189 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
190
191 // Truncate the file in case merging of value profile did not happend to
192 // prevent from leaving garbage data at the end of the profile file.
193 COMPILER_RT_FTRUNCATE(ProfileFile, __llvm_profile_get_size_for_buffer())ftruncate(fileno(ProfileFile),__llvm_profile_get_size_for_buffer
())
;
194
195 (void)munmap(ProfileBuffer, ProfileFileSize);
196 *MergeDone = 1;
197
198 return 0;
199}
200
201/* Create the directory holding the file, if needed. */
202static void createProfileDir(const char *Filename) {
203 size_t Length = strlen(Filename);
204 if (lprofFindFirstDirSeparator(Filename)) {
205 char *Copy = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1);
206 strncpy(Copy, Filename, Length + 1)__builtin_strncpy (Copy, Filename, Length + 1);
207 __llvm_profile_recursive_mkdir(Copy);
208 }
209}
210
211/* Open the profile data for merging. It opens the file in r+b mode with
212 * file locking. If the file has content which is compatible with the
213 * current process, it also reads in the profile data in the file and merge
214 * it with in-memory counters. After the profile data is merged in memory,
215 * the original profile data is truncated and gets ready for the profile
216 * dumper. With profile merging enabled, each executable as well as any of
217 * its instrumented shared libraries dump profile data into their own data file.
218*/
219static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) {
220 FILE *ProfileFile;
221 int rc;
222
223 createProfileDir(ProfileFileName);
224 ProfileFile = lprofOpenFileEx(ProfileFileName);
225 if (!ProfileFile)
226 return NULL((void*)0);
227
228 rc = doProfileMerging(ProfileFile, MergeDone);
229 if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)ftruncate(fileno(ProfileFile),0L)) ||
230 fseek(ProfileFile, 0L, SEEK_SET0) == -1) {
231 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,fprintf(stderr, "LLVM Profile Error: " "Profile Merging of file %s failed: %s\n"
, ProfileFileName, strerror((*__errno_location ())));
232 strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Profile Merging of file %s failed: %s\n"
, ProfileFileName, strerror((*__errno_location ())));
;
233 fclose(ProfileFile);
234 return NULL((void*)0);
235 }
236 return ProfileFile;
237}
238
239/* Write profile data to file \c OutputName. */
240static int writeFile(const char *OutputName) {
241 int RetVal;
242 FILE *OutputFile;
243
244 int MergeDone = 0;
245 VPMergeHook = &lprofMergeValueProfData;
246 if (!doMerging())
247 OutputFile = fopen(OutputName, "ab");
248 else
249 OutputFile = openFileForMerging(OutputName, &MergeDone);
250
251 if (!OutputFile)
252 return -1;
253
254 FreeHook = &free;
255 setupIOBuffer();
256 ProfDataWriter fileWriter;
257 initFileWriter(&fileWriter, OutputFile);
258 RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone);
259
260 fclose(OutputFile);
261 return RetVal;
262}
263
264static void truncateCurrentFile(void) {
265 const char *Filename;
266 char *FilenameBuf;
267 FILE *File;
268 int Length;
269
270 Length = getCurFilenameLength();
271 FilenameBuf = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1);
272 Filename = getCurFilename(FilenameBuf, 0);
273 if (!Filename)
274 return;
275
276 /* By pass file truncation to allow online raw profile
277 * merging. */
278 if (lprofCurFilename.MergePoolSize)
279 return;
280
281 createProfileDir(Filename);
282
283 /* Truncate the file. Later we'll reopen and append. */
284 File = fopen(Filename, "w");
285 if (!File)
286 return;
287 fclose(File);
288}
289
290static const char *DefaultProfileName = "default.profraw";
291static void resetFilenameToDefault(void) {
292 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
293 free((void *)lprofCurFilename.FilenamePat);
294 }
295 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
296 lprofCurFilename.FilenamePat = DefaultProfileName;
297 lprofCurFilename.PNS = PNS_default;
298}
299
300static int containsMergeSpecifier(const char *FilenamePat, int I) {
301 return (FilenamePat[I] == 'm' ||
302 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
303 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
304 * to be in-bound as the string is null terminated. */
305 FilenamePat[I + 1] == 'm'));
306}
307
308/* Parses the pattern string \p FilenamePat and stores the result to
309 * lprofcurFilename structure. */
310static int parseFilenamePattern(const char *FilenamePat,
311 unsigned CopyFilenamePat) {
312 int NumPids = 0, NumHosts = 0, I;
313 char *PidChars = &lprofCurFilename.PidChars[0];
314 char *Hostname = &lprofCurFilename.Hostname[0];
315 int MergingEnabled = 0;
316
317 /* Clean up cached prefix and filename. */
318 if (lprofCurFilename.ProfilePathPrefix)
10
Assuming the condition is false
11
Taking false branch
319 free((void *)lprofCurFilename.ProfilePathPrefix);
320 if (lprofCurFilename.Filename)
12
Assuming the condition is false
13
Taking false branch
321 free((void *)lprofCurFilename.Filename);
322
323 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
14
Assuming the condition is true
15
Taking true branch
324 free((void *)lprofCurFilename.FilenamePat);
16
Memory is released
325 }
326
327 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
328
329 if (!CopyFilenamePat)
17
Taking false branch
330 lprofCurFilename.FilenamePat = FilenamePat;
331 else {
332 lprofCurFilename.FilenamePat = strdup(FilenamePat)(__extension__ (__builtin_constant_p (FilenamePat) &&
((size_t)(const void *)((FilenamePat) + 1) - (size_t)(const void
*)(FilenamePat) == 1) ? (((const char *) (FilenamePat))[0] ==
'\0' ? (char *) calloc ((size_t) 1, (size_t) 1) : ({ size_t __len
= strlen (FilenamePat) + 1; char *__retval = (char *) malloc
(__len); if (__retval != ((void*)0)) __retval = (char *) memcpy
(__retval, FilenamePat, __len); __retval; })) : __strdup (FilenamePat
)))
;
333 lprofCurFilename.OwnsFilenamePat = 1;
334 }
335 /* Check the filename for "%p", which indicates a pid-substitution. */
336 for (I = 0; FilenamePat[I]; ++I)
18
Loop condition is false. Execution continues on line 371
337 if (FilenamePat[I] == '%') {
338 if (FilenamePat[++I] == 'p') {
339 if (!NumPids++) {
340 if (snprintf(PidChars, MAX_PID_SIZE16, "%ld", (long)getpid()) <= 0) {
341 PROF_WARN("Unable to get pid for filename pattern %s. Using the "fprintf(stderr, "LLVM Profile Warning: " "Unable to get pid for filename pattern %s. Using the "
"default name.", FilenamePat);
342 "default name.",fprintf(stderr, "LLVM Profile Warning: " "Unable to get pid for filename pattern %s. Using the "
"default name.", FilenamePat);
343 FilenamePat)fprintf(stderr, "LLVM Profile Warning: " "Unable to get pid for filename pattern %s. Using the "
"default name.", FilenamePat);
;
344 return -1;
345 }
346 }
347 } else if (FilenamePat[I] == 'h') {
348 if (!NumHosts++)
349 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)lprofGetHostName(Hostname, 128)) {
350 PROF_WARN("Unable to get hostname for filename pattern %s. Using "fprintf(stderr, "LLVM Profile Warning: " "Unable to get hostname for filename pattern %s. Using "
"the default name.", FilenamePat);
351 "the default name.",fprintf(stderr, "LLVM Profile Warning: " "Unable to get hostname for filename pattern %s. Using "
"the default name.", FilenamePat);
352 FilenamePat)fprintf(stderr, "LLVM Profile Warning: " "Unable to get hostname for filename pattern %s. Using "
"the default name.", FilenamePat);
;
353 return -1;
354 }
355 } else if (containsMergeSpecifier(FilenamePat, I)) {
356 if (MergingEnabled) {
357 PROF_WARN("%%m specifier can only be specified once in %s.\n",fprintf(stderr, "LLVM Profile Warning: " "%%m specifier can only be specified once in %s.\n"
, FilenamePat);
358 FilenamePat)fprintf(stderr, "LLVM Profile Warning: " "%%m specifier can only be specified once in %s.\n"
, FilenamePat);
;
359 return -1;
360 }
361 MergingEnabled = 1;
362 if (FilenamePat[I] == 'm')
363 lprofCurFilename.MergePoolSize = 1;
364 else {
365 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
366 I++; /* advance to 'm' */
367 }
368 }
369 }
370
371 lprofCurFilename.NumPids = NumPids;
372 lprofCurFilename.NumHosts = NumHosts;
373 return 0;
374}
375
376static void parseAndSetFilename(const char *FilenamePat,
377 ProfileNameSpecifier PNS,
378 unsigned CopyFilenamePat) {
379
380 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
381 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
382
383 if (PNS < OldPNS)
2
Assuming 'PNS' is >= 'OldPNS'
3
Taking false branch
384 return;
385
386 if (!FilenamePat)
4
Assuming 'FilenamePat' is non-null
5
Taking false branch
387 FilenamePat = DefaultProfileName;
388
389 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(OldFilenamePat) && __builtin_constant_p (FilenamePat
) && (__s1_len = __builtin_strlen (OldFilenamePat), __s2_len
= __builtin_strlen (FilenamePat), (!((size_t)(const void *)(
(OldFilenamePat) + 1) - (size_t)(const void *)(OldFilenamePat
) == 1) || __s1_len >= 4) && (!((size_t)(const void
*)((FilenamePat) + 1) - (size_t)(const void *)(FilenamePat) ==
1) || __s2_len >= 4)) ? __builtin_strcmp (OldFilenamePat,
FilenamePat) : (__builtin_constant_p (OldFilenamePat) &&
((size_t)(const void *)((OldFilenamePat) + 1) - (size_t)(const
void *)(OldFilenamePat) == 1) && (__s1_len = __builtin_strlen
(OldFilenamePat), __s1_len < 4) ? (__builtin_constant_p (
FilenamePat) && ((size_t)(const void *)((FilenamePat)
+ 1) - (size_t)(const void *)(FilenamePat) == 1) ? __builtin_strcmp
(OldFilenamePat, FilenamePat) : (__extension__ ({ const unsigned
char *__s2 = (const unsigned char *) (const char *) (FilenamePat
); int __result = (((const unsigned char *) (const char *) (OldFilenamePat
))[0] - __s2[0]); if (__s1_len > 0 && __result == 0
) { __result = (((const unsigned char *) (const char *) (OldFilenamePat
))[1] - __s2[1]); if (__s1_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) (OldFilenamePat
))[2] - __s2[2]); if (__s1_len > 2 && __result == 0
) __result = (((const unsigned char *) (const char *) (OldFilenamePat
))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p (
FilenamePat) && ((size_t)(const void *)((FilenamePat)
+ 1) - (size_t)(const void *)(FilenamePat) == 1) && (
__s2_len = __builtin_strlen (FilenamePat), __s2_len < 4) ?
(__builtin_constant_p (OldFilenamePat) && ((size_t)(
const void *)((OldFilenamePat) + 1) - (size_t)(const void *)(
OldFilenamePat) == 1) ? __builtin_strcmp (OldFilenamePat, FilenamePat
) : -(__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) (OldFilenamePat); int __result = (((const
unsigned char *) (const char *) (FilenamePat))[0] - __s2[0])
; if (__s2_len > 0 && __result == 0) { __result = (
((const unsigned char *) (const char *) (FilenamePat))[1] - __s2
[1]); if (__s2_len > 1 && __result == 0) { __result
= (((const unsigned char *) (const char *) (FilenamePat))[2]
- __s2[2]); if (__s2_len > 2 && __result == 0) __result
= (((const unsigned char *) (const char *) (FilenamePat))[3]
- __s2[3]); } } __result; }))) : __builtin_strcmp (OldFilenamePat
, FilenamePat)))); })
) {
6
Assuming 'OldFilenamePat' is non-null
7
Assuming the condition is false
8
Taking false branch
390 lprofCurFilename.PNS = PNS;
391 return;
392 }
393
394 /* When PNS >= OldPNS, the last one wins. */
395 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
9
Calling 'parseFilenamePattern'
19
Returning; memory was released
20
Taking false branch
396 resetFilenameToDefault();
397 lprofCurFilename.PNS = PNS;
398
399 if (!OldFilenamePat) {
21
Taking false branch
400 if (getenv("LLVM_PROFILE_VERBOSE"))
401 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",fprintf(stderr, "LLVM Profile Note: " "Set profile file path to \"%s\" via %s.\n"
, lprofCurFilename.FilenamePat, getPNSStr(PNS));
402 lprofCurFilename.FilenamePat, getPNSStr(PNS))fprintf(stderr, "LLVM Profile Note: " "Set profile file path to \"%s\" via %s.\n"
, lprofCurFilename.FilenamePat, getPNSStr(PNS));
;
403 } else {
404 if (getenv("LLVM_PROFILE_VERBOSE"))
22
Assuming the condition is true
23
Taking true branch
405 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",fprintf(stderr, "LLVM Profile Note: " "Override old profile path \"%s\" via %s to \"%s\" via %s.\n"
, OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat
, getPNSStr(PNS));
24
Within the expansion of the macro 'PROF_NOTE':
a
Use of memory after it is freed
406 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,fprintf(stderr, "LLVM Profile Note: " "Override old profile path \"%s\" via %s to \"%s\" via %s.\n"
, OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat
, getPNSStr(PNS));
407 getPNSStr(PNS))fprintf(stderr, "LLVM Profile Note: " "Override old profile path \"%s\" via %s to \"%s\" via %s.\n"
, OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat
, getPNSStr(PNS));
;
408 }
409
410 truncateCurrentFile();
411}
412
413/* Return buffer length that is required to store the current profile
414 * filename with PID and hostname substitutions. */
415/* The length to hold uint64_t followed by 2 digit pool id including '_' */
416#define SIGLEN24 24
417static int getCurFilenameLength() {
418 int Len;
419 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
420 return 0;
421
422 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
423 lprofCurFilename.MergePoolSize))
424 return strlen(lprofCurFilename.FilenamePat);
425
426 Len = strlen(lprofCurFilename.FilenamePat) +
427 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
428 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
429 if (lprofCurFilename.MergePoolSize)
430 Len += SIGLEN24;
431 return Len;
432}
433
434/* Return the pointer to the current profile file name (after substituting
435 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
436 * to store the resulting filename. If no substitution is needed, the
437 * current filename pattern string is directly returned, unless ForceUseBuf
438 * is enabled. */
439static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) {
440 int I, J, PidLength, HostNameLength, FilenamePatLength;
441 const char *FilenamePat = lprofCurFilename.FilenamePat;
442
443 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
444 return 0;
445
446 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
447 lprofCurFilename.MergePoolSize)) {
448 if (!ForceUseBuf)
449 return lprofCurFilename.FilenamePat;
450
451 FilenamePatLength = strlen(lprofCurFilename.FilenamePat);
452 memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength);
453 FilenameBuf[FilenamePatLength] = '\0';
454 return FilenameBuf;
455 }
456
457 PidLength = strlen(lprofCurFilename.PidChars);
458 HostNameLength = strlen(lprofCurFilename.Hostname);
459 /* Construct the new filename. */
460 for (I = 0, J = 0; FilenamePat[I]; ++I)
461 if (FilenamePat[I] == '%') {
462 if (FilenamePat[++I] == 'p') {
463 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
464 J += PidLength;
465 } else if (FilenamePat[I] == 'h') {
466 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
467 J += HostNameLength;
468 } else if (containsMergeSpecifier(FilenamePat, I)) {
469 char LoadModuleSignature[SIGLEN24];
470 int S;
471 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
472 S = snprintf(LoadModuleSignature, SIGLEN24, "%" PRIu64"l" "u" "_%d",
473 lprofGetLoadModuleSignature(), ProfilePoolId);
474 if (S == -1 || S > SIGLEN24)
475 S = SIGLEN24;
476 memcpy(FilenameBuf + J, LoadModuleSignature, S);
477 J += S;
478 if (FilenamePat[I] != 'm')
479 I++;
480 }
481 /* Drop any unknown substitutions. */
482 } else
483 FilenameBuf[J++] = FilenamePat[I];
484 FilenameBuf[J] = 0;
485
486 return FilenameBuf;
487}
488
489/* Returns the pointer to the environment variable
490 * string. Returns null if the env var is not set. */
491static const char *getFilenamePatFromEnv(void) {
492 const char *Filename = getenv("LLVM_PROFILE_FILE");
493 if (!Filename || !Filename[0])
494 return 0;
495 return Filename;
496}
497
498COMPILER_RT_VISIBILITY__attribute__((visibility("hidden")))
499const char *__llvm_profile_get_path_prefix(void) {
500 int Length;
501 char *FilenameBuf, *Prefix;
502 const char *Filename, *PrefixEnd;
503
504 if (lprofCurFilename.ProfilePathPrefix)
505 return lprofCurFilename.ProfilePathPrefix;
506
507 Length = getCurFilenameLength();
508 FilenameBuf = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1);
509 Filename = getCurFilename(FilenameBuf, 0);
510 if (!Filename)
511 return "\0";
512
513 PrefixEnd = lprofFindLastDirSeparator(Filename);
514 if (!PrefixEnd)
515 return "\0";
516
517 Length = PrefixEnd - Filename + 1;
518 Prefix = (char *)malloc(Length + 1);
519 if (!Prefix) {
520 PROF_ERR("Failed to %s\n", "allocate memory.")fprintf(stderr, "LLVM Profile Error: " "Failed to %s\n", "allocate memory."
);
;
521 return "\0";
522 }
523 memcpy(Prefix, Filename, Length);
524 Prefix[Length] = '\0';
525 lprofCurFilename.ProfilePathPrefix = Prefix;
526 return Prefix;
527}
528
529COMPILER_RT_VISIBILITY__attribute__((visibility("hidden")))
530const char *__llvm_profile_get_filename(void) {
531 int Length;
532 char *FilenameBuf;
533 const char *Filename;
534
535 if (lprofCurFilename.Filename)
536 return lprofCurFilename.Filename;
537
538 Length = getCurFilenameLength();
539 FilenameBuf = (char *)malloc(Length + 1);
540 if (!FilenameBuf) {
541 PROF_ERR("Failed to %s\n", "allocate memory.")fprintf(stderr, "LLVM Profile Error: " "Failed to %s\n", "allocate memory."
);
;
542 return "\0";
543 }
544 Filename = getCurFilename(FilenameBuf, 1);
545 if (!Filename)
546 return "\0";
547
548 lprofCurFilename.Filename = FilenameBuf;
549 return FilenameBuf;
550}
551
552/* This method is invoked by the runtime initialization hook
553 * InstrProfilingRuntime.o if it is linked in. Both user specified
554 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
555 * environment variable can override this default value. */
556COMPILER_RT_VISIBILITY__attribute__((visibility("hidden")))
557void __llvm_profile_initialize_file(void) {
558 const char *EnvFilenamePat;
559 const char *SelectedPat = NULL((void*)0);
560 ProfileNameSpecifier PNS = PNS_unknown;
561 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR__llvm_profile_filename[0] != 0);
562
563 EnvFilenamePat = getFilenamePatFromEnv();
564 if (EnvFilenamePat) {
565 /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
566 at the moment when __llvm_profile_write_file() gets executed. */
567 parseAndSetFilename(EnvFilenamePat, PNS_environment, 1);
568 return;
569 } else if (hasCommandLineOverrider) {
570 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR__llvm_profile_filename;
571 PNS = PNS_command_line;
572 } else {
573 SelectedPat = NULL((void*)0);
574 PNS = PNS_default;
575 }
576
577 parseAndSetFilename(SelectedPat, PNS, 0);
578}
579
580/* This API is directly called by the user application code. It has the
581 * highest precedence compared with LLVM_PROFILE_FILE environment variable
582 * and command line option -fprofile-instr-generate=<profile_name>.
583 */
584COMPILER_RT_VISIBILITY__attribute__((visibility("hidden")))
585void __llvm_profile_set_filename(const char *FilenamePat) {
586 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
1
Calling 'parseAndSetFilename'
587}
588
589/* The public API for writing profile data into the file with name
590 * set by previous calls to __llvm_profile_set_filename or
591 * __llvm_profile_override_default_filename or
592 * __llvm_profile_initialize_file. */
593COMPILER_RT_VISIBILITY__attribute__((visibility("hidden")))
594int __llvm_profile_write_file(void) {
595 int rc, Length;
596 const char *Filename;
597 char *FilenameBuf;
598 int PDeathSig = 0;
599
600 if (lprofProfileDumped()) {
601 PROF_NOTE("Profile data not written to file: %s.\n",fprintf(stderr, "LLVM Profile Note: " "Profile data not written to file: %s.\n"
, "already written");
602 "already written")fprintf(stderr, "LLVM Profile Note: " "Profile data not written to file: %s.\n"
, "already written");
;
603 return 0;
604 }
605
606 Length = getCurFilenameLength();
607 FilenameBuf = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1);
608 Filename = getCurFilename(FilenameBuf, 0);
609
610 /* Check the filename. */
611 if (!Filename) {
612 PROF_ERR("Failed to write file : %s\n", "Filename not set")fprintf(stderr, "LLVM Profile Error: " "Failed to write file : %s\n"
, "Filename not set");
;
613 return -1;
614 }
615
616 /* Check if there is llvm/runtime version mismatch. */
617 if (GET_VERSION(__llvm_profile_get_version())((__llvm_profile_get_version()) & ~0xff00000000000000ULL) != INSTR_PROF_RAW_VERSION4) {
618 PROF_ERR("Runtime and instrumentation version mismatch : "fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : "
"expected %d, but get %d\n", 4, (int)((__llvm_profile_get_version
()) & ~0xff00000000000000ULL));
619 "expected %d, but get %d\n",fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : "
"expected %d, but get %d\n", 4, (int)((__llvm_profile_get_version
()) & ~0xff00000000000000ULL));
620 INSTR_PROF_RAW_VERSION,fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : "
"expected %d, but get %d\n", 4, (int)((__llvm_profile_get_version
()) & ~0xff00000000000000ULL));
621 (int)GET_VERSION(__llvm_profile_get_version()))fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : "
"expected %d, but get %d\n", 4, (int)((__llvm_profile_get_version
()) & ~0xff00000000000000ULL));
;
622 return -1;
623 }
624
625 // Temporarily suspend getting SIGKILL when the parent exits.
626 PDeathSig = lprofSuspendSigKill();
627
628 /* Write profile data to the file. */
629 rc = writeFile(Filename);
630 if (rc)
631 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Failed to write file \"%s\": %s\n"
, Filename, strerror((*__errno_location ())));
;
632
633 // Restore SIGKILL.
634 if (PDeathSig == 1)
635 lprofRestoreSigKill();
636
637 return rc;
638}
639
640COMPILER_RT_VISIBILITY__attribute__((visibility("hidden")))
641int __llvm_profile_dump(void) {
642 if (!doMerging())
643 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "fprintf(stderr, "LLVM Profile Warning: " "Later invocation of __llvm_profile_dump can lead to clobbering "
" of previously dumped profile data : %s. Either use %%m " "in profile name or change profile name before dumping.\n"
, "online profile merging is not on");
644 " of previously dumped profile data : %s. Either use %%m "fprintf(stderr, "LLVM Profile Warning: " "Later invocation of __llvm_profile_dump can lead to clobbering "
" of previously dumped profile data : %s. Either use %%m " "in profile name or change profile name before dumping.\n"
, "online profile merging is not on");
645 "in profile name or change profile name before dumping.\n",fprintf(stderr, "LLVM Profile Warning: " "Later invocation of __llvm_profile_dump can lead to clobbering "
" of previously dumped profile data : %s. Either use %%m " "in profile name or change profile name before dumping.\n"
, "online profile merging is not on");
646 "online profile merging is not on")fprintf(stderr, "LLVM Profile Warning: " "Later invocation of __llvm_profile_dump can lead to clobbering "
" of previously dumped profile data : %s. Either use %%m " "in profile name or change profile name before dumping.\n"
, "online profile merging is not on");
;
647 int rc = __llvm_profile_write_file();
648 lprofSetProfileDumped();
649 return rc;
650}
651
652static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
653
654COMPILER_RT_VISIBILITY__attribute__((visibility("hidden")))
655int __llvm_profile_register_write_file_atexit(void) {
656 static int HasBeenRegistered = 0;
657
658 if (HasBeenRegistered)
659 return 0;
660
661 lprofSetupValueProfiler();
662
663 HasBeenRegistered = 1;
664 return atexit(writeFileWithoutReturn);
665}
666
667#endif