Exemplo n.º 1
0
void walkstack() {
  int ebp = getebp();
  int eip;
  int rc;
  Dl_info info;

  while (ebp) {
    eip = *((int*)ebp + 1);
    rc = dladdr((void*)eip, &info);
    if (!rc||(!info.dli_sname && !info.dli_fname)) {
      printf("0x%x (unknown)\n", eip);
    } else {
      if (!info.dli_sname) {
        printf("0x%x (%s)\n", eip, info.dli_fname);
      } else {
        printf("%s+%i (%s)\n",
               info.dli_sname,
               eip - (int)info.dli_saddr,
               info.dli_fname);
      }
    }
    ebp = *((int*)ebp);
  }
}
int main()
{
    int local = 1;

    unsigned long stackBefore = getstack();
    unsigned long framePtrBefore = getebp();
    try
    {
        MyClass ins;
        foo1();
        local = 0;
    }
    catch(...)
    {
        // If Pin translated probed code properly, exception will reach the handler
        printf("Exception\n");
    }
    unsigned long stackAfter = getstack();
    unsigned long framePtrAfter = getebp();
    
    if ((stackBefore != stackAfter) || (framePtrBefore != framePtrAfter))
    {
        printf("before try  Stack at 0x%x, ebp 0x%x\n", getstack(), getebp());
        printf("after catch Stack at 0x%x, ebp 0x%x\n", getstack(), getebp());
        return -1;
    }
    
    int param1 = 1;
    int param2 = param1 * 5;
    float param3 = 0.5*1.5;
    float expectedResult = 0;
    try
    {
        expectedResult = param3+param2+param1;
        foo2();
    }
    catch(...)
    {
        float afterCatchResult = param3+param2+param1;
        if (afterCatchResult != expectedResult)
        {
            printf("expectedResult = %f; afterCatchResult = %f\n", expectedResult, afterCatchResult);
            printf("Registers may be restored incorrectly in the catch block\n");
            return -1;
        }
        else
        {
            printf("Try-catch works correctly while exception propagation from dll\n");
        }
        
    }
    stackAfter = getstack();
    framePtrAfter = getebp();
    
    if ((stackBefore != stackAfter) || (framePtrBefore != framePtrAfter))
    {
        printf("Incorrect stack of frame ptr after exception propagation from dll");
        printf("before try  Stack at 0x%x, ebp 0x%x\n", getstack(), getebp());
        printf("after catch Stack at 0x%x, ebp 0x%x\n", getstack(), getebp());
        return -1;
    }
            
    // Check that destructor was called and local var value was not changed when exception was handled
    if (!destructed || (local != 1))
    {
        return 1;
    }

    pBar = bar;

    try
    {
        foo1();
    }
    catch(...)
    {
        // No exception expected
        printf("Exception\n");
    }

    return 0;
}