1// UNSUPPORTED: windows
2// The sanitizer-windows bot is saying:
3// instrprof-write-buffer-internal.c.tmp.buf.profraw: Invalid instrumentation profile data (file header is corrupt)
4
5// RUN: rm -f %t.buf.profraw %t.profraw
6// RUN: %clang_profgen -w -o %t %s
7// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t %t.buf.profraw
8// RUN: llvm-profdata show %t.buf.profraw | FileCheck %s -check-prefix=WRITE-BUFFER
9// RUN: not llvm-profdata show %t.profraw 2>&1 | FileCheck %s -check-prefix=ALREADY-DUMPED
10
11// WRITE-BUFFER: Instrumentation level: Front-end
12// WRITE-BUFFER: Total functions: 1
13// WRITE-BUFFER: Maximum function count: 1
14// WRITE-BUFFER: Maximum internal block count: 0
15
16// ALREADY-DUMPED: error: {{.*}} Empty raw profile file
17
18#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21
22const void *__llvm_profile_begin_data(void);
23const void *__llvm_profile_end_data(void);
24const char *__llvm_profile_begin_names(void);
25const char *__llvm_profile_end_names(void);
26uint64_t *__llvm_profile_begin_counters(void);
27uint64_t *__llvm_profile_end_counters(void);
28
29uint64_t __llvm_profile_get_size_for_buffer_internal(
30 const void *DataBegin, const void *DataEnd,
31 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
32 const char *NamesBegin, const char *NamesEnd);
33
34int __llvm_profile_write_buffer_internal(
35 char *Buffer, const void *DataBegin,
36 const void *DataEnd, const uint64_t *CountersBegin,
37 const uint64_t *CountersEnd, const char *NamesBegin,
38 const char *NamesEnd);
39
40void __llvm_profile_set_dumped(void);
41
42int main(int argc, const char *argv[]) {
43 uint64_t bufsize = __llvm_profile_get_size_for_buffer_internal(
44 __llvm_profile_begin_data(), __llvm_profile_end_data(),
45 __llvm_profile_begin_counters(), __llvm_profile_end_counters(),
46 __llvm_profile_begin_names(), __llvm_profile_end_names());
47
48 char *buf = malloc(bufsize);
49 int ret = __llvm_profile_write_buffer_internal(buf,
50 __llvm_profile_begin_data(), __llvm_profile_end_data(),
51 __llvm_profile_begin_counters(), __llvm_profile_end_counters(),
52 __llvm_profile_begin_names(), __llvm_profile_end_names());
53
54 if (ret != 0) {
55 fprintf(stderr, "failed to write buffer");
56 return ret;
57 }
58
59 FILE *f = fopen(argv[1], "w");
60 fwrite(buf, bufsize, 1, f);
61 fclose(f);
62 free(buf);
63
64 __llvm_profile_set_dumped();
65
66 return 0;
67}
68