xref: /llvm-project/compiler-rt/test/profile/instrprof-write-buffer-internal.c (revision 16e74fd48988ac95551d0f64e1b36f78a82a89a2)
12f6439bcSPaul Robinson // UNSUPPORTED: target={{.*windows.*}}
22fcc3f4bSVedant Kumar // The sanitizer-windows bot is saying:
32fcc3f4bSVedant Kumar // instrprof-write-buffer-internal.c.tmp.buf.profraw: Invalid instrumentation profile data (file header is corrupt)
42fcc3f4bSVedant Kumar 
518adbb86SVedant Kumar // RUN: rm -f %t.buf.profraw %t.profraw
68f7dc996SVedant Kumar // RUN: %clang_profgen -w -o %t %s
718adbb86SVedant Kumar // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t %t.buf.profraw
818adbb86SVedant Kumar // RUN: llvm-profdata show %t.buf.profraw | FileCheck %s -check-prefix=WRITE-BUFFER
918adbb86SVedant Kumar // RUN: not llvm-profdata show %t.profraw 2>&1 | FileCheck %s -check-prefix=ALREADY-DUMPED
1018adbb86SVedant Kumar 
1118adbb86SVedant Kumar // WRITE-BUFFER: Instrumentation level: Front-end
1218adbb86SVedant Kumar // WRITE-BUFFER: Total functions: 1
1318adbb86SVedant Kumar // WRITE-BUFFER: Maximum function count: 1
1418adbb86SVedant Kumar // WRITE-BUFFER: Maximum internal block count: 0
1518adbb86SVedant Kumar 
168ea2a58aSFangrui Song // ALREADY-DUMPED: error: {{.+}}: empty raw profile file
1718adbb86SVedant Kumar 
1818adbb86SVedant Kumar #include <stdint.h>
1918adbb86SVedant Kumar #include <stdio.h>
2018adbb86SVedant Kumar #include <stdlib.h>
2118adbb86SVedant Kumar 
2218adbb86SVedant Kumar const void *__llvm_profile_begin_data(void);
2318adbb86SVedant Kumar const void *__llvm_profile_end_data(void);
2418adbb86SVedant Kumar const char *__llvm_profile_begin_names(void);
2518adbb86SVedant Kumar const char *__llvm_profile_end_names(void);
26f2147375SEllis Hoag char *__llvm_profile_begin_counters(void);
27f2147375SEllis Hoag char *__llvm_profile_end_counters(void);
28f95b2f1aSAlan Phipps char *__llvm_profile_begin_bitmap(void);
29f95b2f1aSAlan Phipps char *__llvm_profile_end_bitmap(void);
3018adbb86SVedant Kumar 
3118adbb86SVedant Kumar uint64_t __llvm_profile_get_size_for_buffer_internal(
32f2147375SEllis Hoag     const void *DataBegin, const void *DataEnd, const char *CountersBegin,
33f95b2f1aSAlan Phipps     const char *CountersEnd, const char *BitmapBegin, const char *BitmapEnd,
34*16e74fd4SMingming Liu     const char *NamesBegin, const char *NamesEnd, const void *VTableBegin,
35*16e74fd4SMingming Liu     const void *VTableEnd, const char *VNamesBegin, const char *VNamesEnd);
3618adbb86SVedant Kumar 
37f95b2f1aSAlan Phipps int __llvm_profile_write_buffer_internal(
38f95b2f1aSAlan Phipps     char *Buffer, const void *DataBegin, const void *DataEnd,
39f95b2f1aSAlan Phipps     const char *CountersBegin, const char *CountersEnd, const char *BitmapBegin,
40f95b2f1aSAlan Phipps     const char *BitmapEnd, const char *NamesBegin, const char *NamesEnd);
4118adbb86SVedant Kumar 
4218adbb86SVedant Kumar void __llvm_profile_set_dumped(void);
4318adbb86SVedant Kumar 
main(int argc,const char * argv[])4418adbb86SVedant Kumar int main(int argc, const char *argv[]) {
4518adbb86SVedant Kumar   uint64_t bufsize = __llvm_profile_get_size_for_buffer_internal(
4618adbb86SVedant Kumar       __llvm_profile_begin_data(), __llvm_profile_end_data(),
4718adbb86SVedant Kumar       __llvm_profile_begin_counters(), __llvm_profile_end_counters(),
48f95b2f1aSAlan Phipps       __llvm_profile_begin_bitmap(), __llvm_profile_end_bitmap(),
49*16e74fd4SMingming Liu       __llvm_profile_begin_names(), __llvm_profile_end_names(), NULL, NULL,
50*16e74fd4SMingming Liu       NULL, NULL);
5118adbb86SVedant Kumar 
5218adbb86SVedant Kumar   char *buf = malloc(bufsize);
53f95b2f1aSAlan Phipps   int ret = __llvm_profile_write_buffer_internal(
54f95b2f1aSAlan Phipps       buf, __llvm_profile_begin_data(), __llvm_profile_end_data(),
5518adbb86SVedant Kumar       __llvm_profile_begin_counters(), __llvm_profile_end_counters(),
56f95b2f1aSAlan Phipps       __llvm_profile_begin_bitmap(), __llvm_profile_end_bitmap(),
5718adbb86SVedant Kumar       __llvm_profile_begin_names(), __llvm_profile_end_names());
5818adbb86SVedant Kumar 
5918adbb86SVedant Kumar   if (ret != 0) {
6018adbb86SVedant Kumar     fprintf(stderr, "failed to write buffer");
6118adbb86SVedant Kumar     return ret;
6218adbb86SVedant Kumar   }
6318adbb86SVedant Kumar 
6418adbb86SVedant Kumar   FILE *f = fopen(argv[1], "w");
6518adbb86SVedant Kumar   fwrite(buf, bufsize, 1, f);
6618adbb86SVedant Kumar   fclose(f);
6718adbb86SVedant Kumar   free(buf);
6818adbb86SVedant Kumar 
6918adbb86SVedant Kumar   __llvm_profile_set_dumped();
7018adbb86SVedant Kumar 
7118adbb86SVedant Kumar   return 0;
7218adbb86SVedant Kumar }
73