title: Segmentation fault from hook on open()
body: <p>I am trying to create a hook on the system function <code>open()</code>. I've done this along the following lines.</p>
  
  <p>I created a wrapper library with the following:</p>
  
  <pre><code>extern int mocked_open(const char* fn, int flags, va_list args);
  
  int open(const char* fn, int flags, ...)
  {
      int r = -1;
      va_list args;
  
      va_start(args, flags);
      r = mocked_open(fn, flags, args);
      va_end(args);
  
      return r;
  }
  </code></pre>
  
  <p>I compile this into libwrapper.so, which I load using LD_PRELOAD.</p>
  
  <p>The implementation of <code>mocked_open()</code> is as follows (I use the CPPUtest framework):</p>
  
  <pre><code>int mocked_open(const char* fn, int flags, va_list args)
  {
      if (strncmp(fn, test_device_id, 11) == 0)
      {
          return mock().actualCall("open").returnValue().getIntValue();
      }
      else
      {
          int r = -1;
          int (*my_open)(const char*, int, ...);
          void* fptr = dlsym(RTLD_NEXT, "open");
          memcpy(&amp;my_open, &amp;fptr, sizeof(my_open));
  
          if (flags &amp; O_CREAT)
          {
              r = my_open(fn, flags, va_arg(args, mode_t));
          }
          else
          {
              r = my_open(fn, flags);
          }
  
          return r;
      }
  }
  </code></pre>
  
  <p>The <code>test_device_id</code> is a simple string ("test_device"), which I hope is not used elsewhere.</p>
  
  <p>During running the tests the executable crashes with a segmentation fault. I've traced this down to the GCC profiling functionality, which wants to open/create a bunch of <code>.gcda</code> files and calls <code>open()</code> for this.</p>
  
  <p>After some debugging with strace (per suggestion below), I found that the line <code>r = my_open(fn, flags, va_arg(args, mode_t));</code> is indeed the culprit. It is being called recursively, or so it seems: I see a lot of calls to this line, without the function returning. Then a segfault. The file being opened is the corresponding .gcda file (for profiling). In fact, <em>the segfault only occurs with profiling enabled</em>.</p>