toolkit/crashreporter/test/nsTestCrasher.cpp
author Aman Dwivedi <dwivedi.aman96@gmail.com>
Wed, 08 Feb 2017 22:50:00 +0100
changeset 481273 c5fb59f54b4b71075eea292ead746ae4e5bd9f3c
parent 449023 1ba830898e8c2536b5ee8f6259d2687b7a38a312
child 479441 5d70727a468a754c655f748b92bb14f6809f11ae
child 481471 44c715e9d0e510f4ad685f4376ba11c65927788c
child 481473 4a81d2219e41ffe6d1f192e5347d278aeaf0d231
permissions -rw-r--r--
Bug 876002 - Remove nsIFormHistory2 so no synchronous form history code remains, r=markh. MozReview-Commit-ID: 4hNAqttRddP

#include "mozilla/Assertions.h"

#include <stdio.h>

#include "nscore.h"
#include "nsExceptionHandler.h"
#include "mozilla/Unused.h"

/*
 * This pure virtual call example is from MSDN
 */
class A;

void fcn( A* );

class A
{
public:
  virtual void f() = 0;
  A() { fcn( this ); }
};

class B : A
{
  void f() { }
public:
  void use() { }
};

void fcn( A* p )
{
  p->f();
}

void PureVirtualCall()
{
  // generates a pure virtual function call
  B b;
  b.use(); // make sure b's actually used
}

// Keep these in sync with CrashTestUtils.jsm!
const int16_t CRASH_INVALID_POINTER_DEREF = 0;
const int16_t CRASH_PURE_VIRTUAL_CALL     = 1;
const int16_t CRASH_OOM                   = 3;
const int16_t CRASH_MOZ_CRASH             = 4;
const int16_t CRASH_ABORT                 = 5;

extern "C" NS_EXPORT
void Crash(int16_t how)
{
  switch (how) {
  case CRASH_INVALID_POINTER_DEREF: {
    volatile int* foo = (int*)0x42;
    *foo = 0;
    // not reached
    break;
  }
  case CRASH_PURE_VIRTUAL_CALL: {
    PureVirtualCall();
    // not reached
    break;
  }
  case CRASH_OOM: {
    mozilla::Unused << moz_xmalloc((size_t) -1);
    mozilla::Unused << moz_xmalloc((size_t) -1);
    mozilla::Unused << moz_xmalloc((size_t) -1);
    break;
  }
  case CRASH_MOZ_CRASH: {
    MOZ_CRASH();
    break;
  }
  case CRASH_ABORT: {
    abort();
    break;
  }
  default:
    break;
  }
}

char testData[32];

extern "C" NS_EXPORT
uint64_t SaveAppMemory()
{
  for (size_t i=0; i<sizeof(testData); i++)
    testData[i] = i;

  FILE *fp = fopen("crash-addr", "w");
  if (!fp)
    return 0;
  fprintf(fp, "%p\n", (void *)testData);
  fclose(fp);

  return (int64_t)testData;
}

#ifdef XP_WIN32
static LONG WINAPI HandleException(EXCEPTION_POINTERS* exinfo)
{
  TerminateProcess(GetCurrentProcess(), 0);
  return 0;
}

extern "C" NS_EXPORT
void TryOverrideExceptionHandler()
{
  SetUnhandledExceptionFilter(HandleException);
}
#endif