CCTLib
Calling-context and data-object attribution library for Intel Pin
exc_catchall.cpp
Go to the documentation of this file.
1 // catch(...) exercises the general handler with a variety of thrown types:
2 // int, POD struct, std::string, and a class with a virtual destructor.
3 // Ensures the resolver reaches the exception path regardless of throw type.
4 //
5 // catchall_try_marker (in-try) and catchall_catch_marker (in-catch(...)),
6 // asserted to be direct children of main by the shape checks.
7 #include <cstdint>
8 #include <cstdio>
9 #include <string>
10 #define ITERS 400
11 static volatile uint64_t sink;
12 static uint64_t buf[ITERS];
13 
14 extern "C" __attribute__((noinline)) void catchall_try_marker(int i) {
15  __asm__ __volatile__("" ::: "memory"); sink ^= (uint64_t)i;
16 }
17 extern "C" __attribute__((noinline)) void catchall_catch_marker(int i) {
18  __asm__ __volatile__("" ::: "memory"); sink ^= (uint64_t)i << 8;
19 }
20 
21 struct Pod { uint64_t a, b, c; };
22 struct Virt {
23  uint64_t x;
24  virtual ~Virt() {}
25 };
26 
27 static void thrower(int kind, int i) {
28  buf[i] = kind * 0x111ULL;
29  switch (kind & 3) {
30  case 0: throw 42;
31  case 1: { Pod p{1,2,3}; throw p; }
32  case 2: throw std::string("boom");
33  case 3: { Virt v; v.x = i; throw v; }
34  }
35 }
36 
37 int main(int argc, char** argv) {
38  (void)argc; (void)argv;
39  int caught = 0;
40  for (int i = 0; i < ITERS; ++i) {
41  try {
42  catchall_try_marker(i);
43  thrower(i, i);
44  } catch (...) {
45  catchall_catch_marker(i);
46  buf[i] ^= 0x88;
47  ++caught;
48  }
49  }
50  for (int i = 0; i < ITERS; ++i) sink ^= buf[i];
51  fprintf(stderr, "exc_catchall: caught=%d iters=%d sink=%llx\n",
52  caught, ITERS, (unsigned long long)sink);
53  return caught == ITERS ? 0 : 1;
54 }
#define ITERS
int main(int argc, char **argv)
static uint64_t buf[ITERS]
static void thrower(int kind, int i)
static volatile uint64_t sink
__attribute__((noinline)) void catchall_try_marker(int i)
uint64_t a
uint64_t c
uint64_t b
virtual ~Virt()
uint64_t x