toolkit/crashreporter/test/nsTestCrasher.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/test/nsTestCrasher.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,126 @@
     1.4 +#include "mozilla/Assertions.h"
     1.5 +
     1.6 +#include <stdio.h>
     1.7 +
     1.8 +#include "nscore.h"
     1.9 +#include "nsXULAppAPI.h"
    1.10 +#include "nsExceptionHandler.h"
    1.11 +#include "mozilla/unused.h"
    1.12 +
    1.13 +/*
    1.14 + * This pure virtual call example is from MSDN
    1.15 + */
    1.16 +class A;
    1.17 +
    1.18 +void fcn( A* );
    1.19 +
    1.20 +class A
    1.21 +{
    1.22 +public:
    1.23 +  virtual void f() = 0;
    1.24 +  A() { fcn( this ); }
    1.25 +};
    1.26 +
    1.27 +class B : A
    1.28 +{
    1.29 +  void f() { }
    1.30 +public:
    1.31 +  void use() { }
    1.32 +};
    1.33 +
    1.34 +void fcn( A* p )
    1.35 +{
    1.36 +  p->f();
    1.37 +}
    1.38 +
    1.39 +void PureVirtualCall()
    1.40 +{
    1.41 +  // generates a pure virtual function call
    1.42 +  B b;
    1.43 +  b.use(); // make sure b's actually used
    1.44 +}
    1.45 +
    1.46 +// Keep these in sync with CrashTestUtils.jsm!
    1.47 +const int16_t CRASH_INVALID_POINTER_DEREF = 0;
    1.48 +const int16_t CRASH_PURE_VIRTUAL_CALL     = 1;
    1.49 +const int16_t CRASH_RUNTIMEABORT          = 2;
    1.50 +const int16_t CRASH_OOM                   = 3;
    1.51 +const int16_t CRASH_MOZ_CRASH             = 4;
    1.52 +const int16_t CRASH_ABORT                 = 5;
    1.53 +
    1.54 +extern "C" NS_EXPORT
    1.55 +void Crash(int16_t how)
    1.56 +{
    1.57 +  switch (how) {
    1.58 +  case CRASH_INVALID_POINTER_DEREF: {
    1.59 +    volatile int* foo = (int*)0x42;
    1.60 +    *foo = 0;
    1.61 +    // not reached
    1.62 +    break;
    1.63 +  }
    1.64 +  case CRASH_PURE_VIRTUAL_CALL: {
    1.65 +    PureVirtualCall();
    1.66 +    // not reached
    1.67 +    break;
    1.68 +  }
    1.69 +  case CRASH_RUNTIMEABORT: {
    1.70 +    NS_RUNTIMEABORT("Intentional crash");
    1.71 +    break;
    1.72 +  }
    1.73 +  case CRASH_OOM: {
    1.74 +    mozilla::unused << moz_xmalloc((size_t) -1);
    1.75 +    mozilla::unused << moz_xmalloc((size_t) -1);
    1.76 +    mozilla::unused << moz_xmalloc((size_t) -1);
    1.77 +    break;
    1.78 +  }
    1.79 +  case CRASH_MOZ_CRASH: {
    1.80 +    MOZ_CRASH();
    1.81 +    break;
    1.82 +  }
    1.83 +  case CRASH_ABORT: {
    1.84 +    abort();
    1.85 +    break;
    1.86 +  }
    1.87 +  default:
    1.88 +    break;
    1.89 +  }
    1.90 +}
    1.91 +
    1.92 +extern "C" NS_EXPORT
    1.93 +nsISupports* LockDir(nsIFile *directory)
    1.94 +{
    1.95 +  nsISupports* lockfile = nullptr;
    1.96 +  XRE_LockProfileDirectory(directory, &lockfile);
    1.97 +  return lockfile;
    1.98 +}
    1.99 +
   1.100 +char testData[32];
   1.101 +
   1.102 +extern "C" NS_EXPORT
   1.103 +uint64_t SaveAppMemory()
   1.104 +{
   1.105 +  for (size_t i=0; i<sizeof(testData); i++)
   1.106 +    testData[i] = i;
   1.107 +
   1.108 +  FILE *fp = fopen("crash-addr", "w");
   1.109 +  if (!fp)
   1.110 +    return 0;
   1.111 +  fprintf(fp, "%p\n", (void *)testData);
   1.112 +  fclose(fp);
   1.113 +
   1.114 +  return (int64_t)testData;
   1.115 +}
   1.116 +
   1.117 +#ifdef XP_WIN32
   1.118 +static LONG WINAPI HandleException(EXCEPTION_POINTERS* exinfo)
   1.119 +{
   1.120 +  TerminateProcess(GetCurrentProcess(), 0);
   1.121 +  return 0;
   1.122 +}
   1.123 +
   1.124 +extern "C" NS_EXPORT
   1.125 +void TryOverrideExceptionHandler()
   1.126 +{
   1.127 +  SetUnhandledExceptionFilter(HandleException);
   1.128 +}
   1.129 +#endif

mercurial