xref: /aosp_15_r20/external/coreboot/src/lib/gcov-io.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-3.0-only WITH GCC-exception-3.1 */
2 
3 /* File format for coverage information
4    Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2007,
5    2008  Free Software Foundation, Inc.
6    Contributed by Bob Manson <[email protected]>.
7    Completely remangled by Nathan Sidwell <[email protected]>.
8 
9 This file is part of GCC.
10 
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15 
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20 
21 Under Section 7 of GPL version 3, you are granted additional
22 permissions described in the GCC Runtime Library Exception, version
23 3.1, as published by the Free Software Foundation.
24 */
25 
26 /* Routines declared in gcov-io.h.  This file should be #included by
27    another source file, after having #included gcov-io.h.  */
28 
29 #if !IN_GCOV
30 static void gcov_write_block(unsigned int);
31 static gcov_unsigned_t *gcov_write_words(unsigned int);
32 #endif
33 static const gcov_unsigned_t *gcov_read_words(unsigned int);
34 #if !IN_LIBGCOV
35 static void gcov_allocate(unsigned int);
36 #endif
37 
from_file(gcov_unsigned_t value)38 static inline gcov_unsigned_t from_file(gcov_unsigned_t value)
39 {
40 #if !IN_LIBGCOV
41 	if (gcov_var.endian) {
42 		value = (value >> 16) | (value << 16);
43 		value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
44 	}
45 #endif
46 	return value;
47 }
48 
49 /* Open a gcov file. NAME is the name of the file to open and MODE
50    indicates whether a new file should be created, or an existing file
51    opened. If MODE is >= 0 an existing file will be opened, if
52    possible, and if MODE is <= 0, a new file will be created. Use
53    MODE=0 to attempt to reopen an existing file and then fall back on
54    creating a new one.  If MODE < 0, the file will be opened in
55    read-only mode.  Otherwise it will be opened for modification.
56    Return zero on failure, >0 on opening an existing file and <0 on
57    creating a new one.  */
58 
59 GCOV_LINKAGE int
60 #if IN_LIBGCOV
gcov_open(const char * name)61 gcov_open(const char *name)
62 #else
63 gcov_open(const char *name, int mode)
64 #endif
65 {
66 #if IN_LIBGCOV
67 	const int mode = 0;
68 #endif
69 #if GCOV_LOCKED
70 	struct flock s_flock;
71 	int fd;
72 
73 	s_flock.l_whence = SEEK_SET;
74 	s_flock.l_start = 0;
75 	s_flock.l_len = 0; /* Until EOF.  */
76 	s_flock.l_pid = getpid();
77 #endif
78 
79 	gcc_assert(!gcov_var.file);
80 	gcov_var.start = 0;
81 	gcov_var.offset = gcov_var.length = 0;
82 	gcov_var.overread = -1u;
83 	gcov_var.error = 0;
84 #if !IN_LIBGCOV
85 	gcov_var.endian = 0;
86 #endif
87 #if GCOV_LOCKED
88 	if (mode > 0) {
89 		/* Read-only mode - acquire a read-lock.  */
90 		s_flock.l_type = F_RDLCK;
91 		fd = open(name, O_RDONLY);
92 	} else {
93 		/* Write mode - acquire a write-lock.  */
94 		s_flock.l_type = F_WRLCK;
95 		fd = open(name, O_RDWR | O_CREAT, 0666);
96 	}
97 	if (fd < 0)
98 		return 0;
99 
100 	while (fcntl(fd, F_SETLKW, &s_flock) && errno == EINTR)
101 		continue;
102 
103 	gcov_var.file = fdopen(fd, (mode > 0) ? "rb" : "r+b");
104 
105 	if (!gcov_var.file) {
106 		close(fd);
107 		return 0;
108 	}
109 
110 	if (mode > 0)
111 		gcov_var.mode = 1;
112 	else if (mode == 0) {
113 		struct stat st;
114 
115 		if (fstat(fd, &st) < 0) {
116 			fclose(gcov_var.file);
117 			gcov_var.file = 0;
118 			return 0;
119 		}
120 		if (st.st_size != 0)
121 			gcov_var.mode = 1;
122 		else
123 			gcov_var.mode = mode * 2 + 1;
124 	} else
125 		gcov_var.mode = mode * 2 + 1;
126 #else
127 	if (mode >= 0)
128 		gcov_var.file = fopen(name, (mode > 0) ? "rb" : "r+b");
129 
130 	if (gcov_var.file)
131 		gcov_var.mode = 1;
132 	else if (mode <= 0) {
133 		gcov_var.file = fopen(name, "w+b");
134 		if (gcov_var.file)
135 			gcov_var.mode = mode * 2 + 1;
136 	}
137 	if (!gcov_var.file)
138 		return 0;
139 #endif
140 
141 	setbuf(gcov_var.file, (char *)0);
142 
143 	return 1;
144 }
145 
146 /* Close the current gcov file. Flushes data to disk. Returns nonzero
147    on failure or error flag set.  */
148 
149 GCOV_LINKAGE int
gcov_close(void)150 gcov_close(void)
151 {
152 	if (gcov_var.file) {
153 #if !IN_GCOV
154 	if (gcov_var.offset && gcov_var.mode < 0)
155 		gcov_write_block(gcov_var.offset);
156 #endif
157 		fclose(gcov_var.file);
158 		gcov_var.file = 0;
159 		gcov_var.length = 0;
160 	}
161 #if !IN_LIBGCOV
162 	free(gcov_var.buffer);
163 	gcov_var.alloc = 0;
164 	gcov_var.buffer = 0;
165 #endif
166 	gcov_var.mode = 0;
167 	return gcov_var.error;
168 }
169 
170 #if !IN_LIBGCOV
171 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
172    file. Returns +1 for same endian, -1 for other endian and zero for
173    not EXPECTED.  */
174 
175 GCOV_LINKAGE int
gcov_magic(gcov_unsigned_t magic,gcov_unsigned_t expected)176 gcov_magic(gcov_unsigned_t magic, gcov_unsigned_t expected)
177 {
178 	if (magic == expected)
179 		return 1;
180 	magic = (magic >> 16) | (magic << 16);
181 	magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff);
182 	if (magic == expected) {
183 		gcov_var.endian = 1;
184 		return -1;
185 	}
186 	return 0;
187 }
188 #endif
189 
190 #if !IN_LIBGCOV
191 static void
gcov_allocate(unsigned int length)192 gcov_allocate(unsigned int length)
193 {
194 	size_t new_size = gcov_var.alloc;
195 
196 	if (!new_size)
197 		new_size = GCOV_BLOCK_SIZE;
198 	new_size += length;
199 	new_size *= 2;
200 
201 	gcov_var.alloc = new_size;
202 	gcov_var.buffer = XRESIZEVAR(gcov_unsigned_t, gcov_var.buffer,
203 		new_size << 2);
204 }
205 #endif
206 
207 #if !IN_GCOV
208 /* Write out the current block, if needs be.  */
209 
210 static void
gcov_write_block(unsigned int size)211 gcov_write_block(unsigned int size)
212 {
213 	if (fwrite(gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
214 		gcov_var.error = 1;
215 	gcov_var.start += size;
216 	gcov_var.offset -= size;
217 }
218 
219 /* Allocate space to write BYTES bytes to the gcov file. Return a
220    pointer to those bytes, or NULL on failure.  */
221 
222 static gcov_unsigned_t *
gcov_write_words(unsigned int words)223 gcov_write_words(unsigned int words)
224 {
225 	gcov_unsigned_t *result;
226 
227 	gcc_assert(gcov_var.mode < 0);
228 #if IN_LIBGCOV
229 	if (gcov_var.offset >= GCOV_BLOCK_SIZE) {
230 		gcov_write_block(GCOV_BLOCK_SIZE);
231 		if (gcov_var.offset) {
232 			gcc_assert(gcov_var.offset == 1);
233 			memcpy(gcov_var.buffer, gcov_var.buffer
234 				+ GCOV_BLOCK_SIZE, 4);
235 		}
236 	}
237 #else
238 	if (gcov_var.offset + words > gcov_var.alloc)
239 		gcov_allocate(gcov_var.offset + words);
240 #endif
241 	result = &gcov_var.buffer[gcov_var.offset];
242 	gcov_var.offset += words;
243 
244 	return result;
245 }
246 
247 /* Write unsigned VALUE to coverage file.  Sets error flag
248    appropriately.  */
249 
250 GCOV_LINKAGE void
gcov_write_unsigned(gcov_unsigned_t value)251 gcov_write_unsigned(gcov_unsigned_t value)
252 {
253 	gcov_unsigned_t *buffer = gcov_write_words(1);
254 
255 	buffer[0] = value;
256 }
257 
258 /* Write counter VALUE to coverage file.  Sets error flag
259    appropriately.  */
260 
261 #if IN_LIBGCOV
262 GCOV_LINKAGE void
gcov_write_counter(gcov_type value)263 gcov_write_counter(gcov_type value)
264 {
265 	gcov_unsigned_t *buffer = gcov_write_words(2);
266 
267 	buffer[0] = (gcov_unsigned_t) value;
268 	if (sizeof(value) > sizeof(gcov_unsigned_t))
269 		buffer[1] = (gcov_unsigned_t) (value >> 32);
270 	else
271 		buffer[1] = 0;
272 }
273 #endif /* IN_LIBGCOV */
274 
275 #if !IN_LIBGCOV
276 /* Write STRING to coverage file.  Sets error flag on file
277    error, overflow flag on overflow */
278 
279 GCOV_LINKAGE void
gcov_write_string(const char * string)280 gcov_write_string(const char *string)
281 {
282 	unsigned int length = 0;
283 	unsigned int alloc = 0;
284 	gcov_unsigned_t *buffer;
285 
286 	if (string) {
287 		length = strlen(string);
288 		alloc = (length + 4) >> 2;
289 	}
290 
291 	buffer = gcov_write_words(1 + alloc);
292 
293 	buffer[0] = alloc;
294 	buffer[alloc] = 0;
295 	memcpy(&buffer[1], string, length);
296 }
297 #endif
298 
299 #if !IN_LIBGCOV
300 /* Write a tag TAG and reserve space for the record length. Return a
301    value to be used for gcov_write_length.  */
302 
303 GCOV_LINKAGE gcov_position_t
gcov_write_tag(gcov_unsigned_t tag)304 gcov_write_tag(gcov_unsigned_t tag)
305 {
306 	gcov_position_t result = gcov_var.start + gcov_var.offset;
307 	gcov_unsigned_t *buffer = gcov_write_words(2);
308 
309 	buffer[0] = tag;
310 	buffer[1] = 0;
311 
312 	return result;
313 }
314 
315 /* Write a record length using POSITION, which was returned by
316    gcov_write_tag.  The current file position is the end of the
317    record, and is restored before returning.  Returns nonzero on
318    overflow.  */
319 
320 GCOV_LINKAGE void
gcov_write_length(gcov_position_t position)321 gcov_write_length(gcov_position_t position)
322 {
323 	unsigned int offset;
324 	gcov_unsigned_t length;
325 	gcov_unsigned_t *buffer;
326 
327 	gcc_assert(gcov_var.mode < 0);
328 	gcc_assert(position + 2 <= gcov_var.start + gcov_var.offset);
329 	gcc_assert(position >= gcov_var.start);
330 	offset = position - gcov_var.start;
331 	length = gcov_var.offset - offset - 2;
332 	buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
333 	buffer[1] = length;
334 	if (gcov_var.offset >= GCOV_BLOCK_SIZE)
335 		gcov_write_block(gcov_var.offset);
336 }
337 
338 #else /* IN_LIBGCOV */
339 
340 /* Write a tag TAG and length LENGTH.  */
341 
342 GCOV_LINKAGE void
gcov_write_tag_length(gcov_unsigned_t tag,gcov_unsigned_t length)343 gcov_write_tag_length(gcov_unsigned_t tag, gcov_unsigned_t length)
344 {
345 	gcov_unsigned_t *buffer = gcov_write_words(2);
346 
347 	buffer[0] = tag;
348 	buffer[1] = length;
349 }
350 
351 /* Write a summary structure to the gcov file.  Return nonzero on
352    overflow.  */
353 
354 GCOV_LINKAGE void
gcov_write_summary(gcov_unsigned_t tag,const struct gcov_summary * summary)355 gcov_write_summary(gcov_unsigned_t tag, const struct gcov_summary *summary)
356 {
357 	unsigned int ix;
358 	const struct gcov_ctr_summary *csum;
359 
360 	gcov_write_tag_length(tag, GCOV_TAG_SUMMARY_LENGTH);
361 	gcov_write_unsigned(summary->checksum);
362 	for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++) {
363 		gcov_write_unsigned(csum->num);
364 		gcov_write_unsigned(csum->runs);
365 		gcov_write_counter(csum->sum_all);
366 		gcov_write_counter(csum->run_max);
367 		gcov_write_counter(csum->sum_max);
368 	}
369 }
370 #endif /* IN_LIBGCOV */
371 
372 #endif /*!IN_GCOV */
373 
374 /* Return a pointer to read BYTES bytes from the gcov file. Returns
375    NULL on failure (read past EOF).  */
376 
377 static const gcov_unsigned_t *
gcov_read_words(unsigned int words)378 gcov_read_words(unsigned int words)
379 {
380 	const gcov_unsigned_t *result;
381 	unsigned int excess = gcov_var.length - gcov_var.offset;
382 
383 	gcc_assert(gcov_var.mode > 0);
384 	if (excess < words) {
385 		gcov_var.start += gcov_var.offset;
386 #if IN_LIBGCOV
387 		if (excess) {
388 			gcc_assert(excess == 1);
389 			memcpy(gcov_var.buffer, gcov_var.buffer
390 				+ gcov_var.offset, 4);
391 		}
392 #else
393 		memmove(gcov_var.buffer, gcov_var.buffer
394 			+ gcov_var.offset, excess * 4);
395 #endif
396 		gcov_var.offset = 0;
397 		gcov_var.length = excess;
398 #if IN_LIBGCOV
399 		gcc_assert(!gcov_var.length || gcov_var.length == 1);
400 		excess = GCOV_BLOCK_SIZE;
401 #else
402 		if (gcov_var.length + words > gcov_var.alloc)
403 			gcov_allocate(gcov_var.length + words);
404 		excess = gcov_var.alloc - gcov_var.length;
405 #endif
406 		excess = fread(gcov_var.buffer + gcov_var.length,
407 			1, excess << 2, gcov_var.file) >> 2;
408 		gcov_var.length += excess;
409 		if (gcov_var.length < words) {
410 			gcov_var.overread += words - gcov_var.length;
411 			gcov_var.length = 0;
412 			return 0;
413 		}
414 	}
415 	result = &gcov_var.buffer[gcov_var.offset];
416 	gcov_var.offset += words;
417 	return result;
418 }
419 
420 /* Read unsigned value from a coverage file. Sets error flag on file
421    error, overflow flag on overflow */
422 
423 GCOV_LINKAGE gcov_unsigned_t
gcov_read_unsigned(void)424 gcov_read_unsigned(void)
425 {
426 	gcov_unsigned_t value;
427 	const gcov_unsigned_t *buffer = gcov_read_words(1);
428 
429 	if (!buffer)
430 		return 0;
431 	value = from_file(buffer[0]);
432 	return value;
433 }
434 
435 /* Read counter value from a coverage file. Sets error flag on file
436    error, overflow flag on overflow */
437 
438 GCOV_LINKAGE gcov_type
gcov_read_counter(void)439 gcov_read_counter(void)
440 {
441 	gcov_type value;
442 	const gcov_unsigned_t *buffer = gcov_read_words(2);
443 
444 	if (!buffer)
445 		return 0;
446 	value = from_file(buffer[0]);
447 	if (sizeof(value) > sizeof(gcov_unsigned_t))
448 		value |= ((gcov_type) from_file(buffer[1])) << 32;
449 	else if (buffer[1])
450 		gcov_var.error = -1;
451 
452 	return value;
453 }
454 
455 /* Read string from coverage file. Returns a pointer to a static
456    buffer, or NULL on empty string. You must copy the string before
457    calling another gcov function.  */
458 
459 #if !IN_LIBGCOV
460 GCOV_LINKAGE const char *
gcov_read_string(void)461 gcov_read_string(void)
462 {
463 	unsigned int length = gcov_read_unsigned();
464 
465 	if (!length)
466 		return 0;
467 
468 	return (const char *) gcov_read_words(length);
469 }
470 #endif
471 
472 GCOV_LINKAGE void
gcov_read_summary(struct gcov_summary * summary)473 gcov_read_summary(struct gcov_summary *summary)
474 {
475 	unsigned int ix;
476 	struct gcov_ctr_summary *csum;
477 
478 	summary->checksum = gcov_read_unsigned();
479 	for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++) {
480 		csum->num = gcov_read_unsigned();
481 		csum->runs = gcov_read_unsigned();
482 		csum->sum_all = gcov_read_counter();
483 		csum->run_max = gcov_read_counter();
484 		csum->sum_max = gcov_read_counter();
485 	}
486 }
487 
488 #if !IN_LIBGCOV
489 /* Reset to a known position.  BASE should have been obtained from
490    gcov_position, LENGTH should be a record length.  */
491 
492 GCOV_LINKAGE void
gcov_sync(gcov_position_t base,gcov_unsigned_t length)493 gcov_sync(gcov_position_t base, gcov_unsigned_t length)
494 {
495 	gcc_assert(gcov_var.mode > 0);
496 	base += length;
497 	if (base - gcov_var.start <= gcov_var.length)
498 		gcov_var.offset = base - gcov_var.start;
499 	else {
500 		gcov_var.offset = gcov_var.length = 0;
501 		fseek(gcov_var.file, base << 2, SEEK_SET);
502 		gcov_var.start = ftell(gcov_var.file) >> 2;
503 	}
504 }
505 #endif
506 
507 #if IN_LIBGCOV
508 /* Move to a given position in a gcov file.  */
509 
510 GCOV_LINKAGE void
gcov_seek(gcov_position_t base)511 gcov_seek(gcov_position_t base)
512 {
513 	gcc_assert(gcov_var.mode < 0);
514 	if (gcov_var.offset)
515 		gcov_write_block(gcov_var.offset);
516 	fseek(gcov_var.file, base << 2, SEEK_SET);
517 	gcov_var.start = ftell(gcov_var.file) >> 2;
518 }
519 #endif
520 
521 #if IN_GCOV > 0
522 /* Return the modification time of the current gcov file.  */
523 
524 GCOV_LINKAGE time_t
gcov_time(void)525 gcov_time(void)
526 {
527 	struct stat status;
528 
529 	if (fstat(fileno(gcov_var.file), &status))
530 		return 0;
531 	else
532 		return status.st_mtime;
533 }
534 #endif /* IN_GCOV */
535