llvm/llvm/test/tools/llvm-profgen/callback-external-addr.test

; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/callback-external-addr.perfscript --binary=%S/Inputs/callback-external-addr.perfbin --output=%t --skip-symbolization
; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-UNWINDER

; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/callback-external-addr.perfscript --binary=%S/Inputs/callback-external-addr.perfbin --output=%t --profile-summary-cold-count=0 --csspgo-preinliner=0 --gen-cs-nested-profile=0
; RUN: FileCheck %s --input-file %t

; Test if call stack is correctly truncated.
; CHECK-UNWINDER-NOT: main:3 @ bar
; CHECK-UNWINDER-NOT: main:3 @ foo
; CHECK-UNWINDER-NOT: qux:3 @ baz
; CHECK-UNWINDER-NOT: qux:3 @ bar

; Test if return to wrong internal target
; CHECK-UNWINDER-NOT: baz:0 @ bar
; CHECK-UNWINDER-NOT: bar:0 @ baz
; CHECK-UNWINDER-NOT: baz:0 @ main
; CHECK-UNWINDER-NOT: bar:0 @ foo
; CHECK-UNWINDER-NOT: baz:0 @ qux

; Test for callback return from internal address to external address.
; [foo:2 @ qux:2 @ callBeforeReturn] and [foo:2 @ qux:4 @ callAfterReturn] should exist
; which means the callback return won't interrupt the previous call stack


; CHECK-UNWINDER: []
; CHECK-UNWINDER:   0
; CHECK-UNWINDER:   5
; CHECK-UNWINDER:   ffffffffffc00001->690:13
; CHECK-UNWINDER:   ffffffffffc00001->6a0:7
; CHECK-UNWINDER:   ffffffffffc00001->715:7
; CHECK-UNWINDER:   ffffffffffc00001->730:5
; CHECK-UNWINDER:   ffffffffffc00001->7b0:5
; CHECK-UNWINDER: [bar]
; CHECK-UNWINDER:   1
; CHECK-UNWINDER:   690-69e:13
; CHECK-UNWINDER:   0
; CHECK-UNWINDER: [baz]
; CHECK-UNWINDER:   1
; CHECK-UNWINDER:   6a0-6ae:7
; CHECK-UNWINDER:   0
; CHECK-UNWINDER: [foo]
; CHECK-UNWINDER:   2
; CHECK-UNWINDER:   730-747:5
; CHECK-UNWINDER:   74c-751:5
; CHECK-UNWINDER:   1
; CHECK-UNWINDER:   747->6d0:5
; CHECK-UNWINDER: [foo:2 @ qux]
; CHECK-UNWINDER:   4
; CHECK-UNWINDER:   6d0-6e7:5
; CHECK-UNWINDER:   6ec-710:6
; CHECK-UNWINDER:   715-71b:7
; CHECK-UNWINDER:   720-72b:6
; CHECK-UNWINDER:   3
; CHECK-UNWINDER:   6e7->6b0:6
; CHECK-UNWINDER:   71b->6c0:7
; CHECK-UNWINDER:   72b->74c:6
; CHECK-UNWINDER: [foo:2 @ qux:2 @ callBeforeReturn]
; CHECK-UNWINDER:   1
; CHECK-UNWINDER:   6b0-6be:6
; CHECK-UNWINDER:   1
; CHECK-UNWINDER:   6be->6ec:7
; CHECK-UNWINDER: [foo:2 @ qux:4 @ callAfterReturn]
; CHECK-UNWINDER:   1
; CHECK-UNWINDER:   6c0-6ce:7
; CHECK-UNWINDER:   1
; CHECK-UNWINDER:   6ce->720:7
; CHECK-UNWINDER: [main]
; CHECK-UNWINDER:   2
; CHECK-UNWINDER:   77d-7ab:5
; CHECK-UNWINDER:   7b0-7bf:5
; CHECK-UNWINDER:   1
; CHECK-UNWINDER:   7bf->77d:5

; CHECK: [foo:2 @ qux]:132:5
; CHECK:  0: 5
; CHECK:  1: 5
; CHECK:  2: 6 callBeforeReturn:6
; CHECK:  3: 7
; CHECK:  4: 7 callAfterReturn:7
; CHECK:  5: 6
; CHECK: [bar]:91:13
; CHECK:  0: 13
; CHECK:  1: 13
; CHECK: [main]:65:0
; CHECK:  2.1: 5
; CHECK:  2.2: 5
; CHECK:  3: 5
; CHECK: [foo]:60:5
; CHECK:  0: 5
; CHECK:  1: 5
; CHECK:  2: 5 qux:5
; CHECK: [baz]:49:7
; CHECK:  0: 7
; CHECK:  1: 7
; CHECK: [foo:2 @ qux:4 @ callAfterReturn]:49:7
; CHECK:  0: 7
; CHECK:  1: 7
; CHECK: [foo:2 @ qux:2 @ callBeforeReturn]:42:6
; CHECK:  0: 6
; CHECK:  1: 6

; libcallback.c
; clang -shared -fPIC -o libcallback.so libcallback.c

int callback(int *cnt, int (*func1)(int), int (*func2)(int), int p) {
  (*cnt)++;
  return func1(p) + func2(p);
}

; test.c
; clang test.c  -O0 -g -fno-optimize-sibling-calls -fdebug-info-for-profiling -L $PWD -lcallback -fno-inline

#include <stdio.h>

int callbackCnt = 0;

int callback(int *cnt, int (*func1)(int), int (*func2)(int), int p);

int bar(int p) {
  return p + 1;
}

int baz(int p) {
  return p - 1;
}

int callBeforeReturn(int p) {
  return p + 10;
}

int callAfterReturn(int p) {
  return p - 10;
}

int qux(int p) {
  p += 10;
  int ret = callBeforeReturn(p);
  ret = callback(&callbackCnt, bar, baz, ret);
  ret = callAfterReturn(ret);
  return ret;
}

int foo (int p) {
  p -= 10;
  return qux(p);
}

int  main(void) {
  int sum = 0;
  for (int i = 0; i < 1000 * 1000; i++) {
    sum += callback(&callbackCnt, foo, bar, i);
  }
  printf("callback count=%d, sum=%d\n", callbackCnt, sum);
}