Exception not caught in Delphi 11.2 Alexandria 64 bit

delphi package - automated exception handling
Fasday@gmail.com
Posts: 5
Joined: Fri Sep 09, 2022 12:19 pm

Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by Fasday@gmail.com »

Exception not caught in Delphi 11.2 Alexandria 64 bit
I enable MadExcept on the project.
By executing "raise Exception.Create('Error Message');" I expected madExcept to catch this, but it doesn't.
In 32 bit all works.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by madshi »

Delphi or BCB? Are you running the EXE inside or outside of the IDE?
Fasday@gmail.com
Posts: 5
Joined: Fri Sep 09, 2022 12:19 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by Fasday@gmail.com »

madshi wrote: Fri Sep 09, 2022 2:16 pm Delphi or BCB?
Delphi.
madshi wrote: Fri Sep 09, 2022 2:16 pm Are you running the EXE inside or outside of the IDE?
In any case.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by madshi »

Can you reproduce this is a new (almost empty) test project?
Fasday@gmail.com
Posts: 5
Joined: Fri Sep 09, 2022 12:19 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by Fasday@gmail.com »

madshi wrote: Mon Sep 12, 2022 6:38 am Can you reproduce this is a new (almost empty) test project?

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
  raise Exception.Create('Error Message');
end;
Google Drive
Image
Image
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by madshi »

Can you explain these images? In one image madExcept seems to work, in another not. What's the difference?
Fasday@gmail.com
Posts: 5
Joined: Fri Sep 09, 2022 12:19 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by Fasday@gmail.com »

madshi wrote: Mon Sep 12, 2022 9:10 am Can you explain these images? In one image madExcept seems to work, in another not. What's the difference?
in first image target platform win32. in second windows 64. All other actions are the same
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by madshi »

I've just tested your project here on my dev PC and it works fine, if I recompile it with 11.1.5. I've not installed 11.2 yet. Have you tried with 11.1.5? AFAIK, 11.2 is supposed to be binary compatible with 11.1.5, so I'm not sure why it would break madExcept.
Fasday@gmail.com
Posts: 5
Joined: Fri Sep 09, 2022 12:19 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by Fasday@gmail.com »

madshi wrote: Mon Sep 12, 2022 9:40 am I've just tested your project here on my dev PC and it works fine, if I recompile it with 11.1.5. I've not installed 11.2 yet. Have you tried with 11.1.5? AFAIK, 11.2 is supposed to be binary compatible with 11.1.5, so I'm not sure why it would break madExcept.
In 11.1 All works fine.
11.2 Have some Compiler Enhancements.

Code: Select all

Delphi Compiler Enhancements
As part of the RAD Studio 11.2 Delphi compiler improvements, Delphi further expands Address Space Layout Randomization (ASLR) support for the Win64 platform by supporting High Entropy ASLR (HE ASLR) by adding the ability to randomize memory addresses at high locations.
I had problems with other components such as fibplus and ehlib. There was an AV due to an incorrect pointer, but I fixed it by changing the type from integer to nativeint. And it seems to work.

Code: Select all

  type
    --IntPtr = integer;	
    IntPtr = NativeInt;	
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by madshi »

Ok, I've installed Delphi 11.2 now and can reproduce the problem. I don't have any time available in the next 2 weeks, though. Is there any way to turn this new ASLR support off for now?
Uwe Raabe
Posts: 3
Joined: Wed Mar 22, 2017 7:45 am

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by Uwe Raabe »

madshi wrote: Mon Sep 12, 2022 10:36 am Is there any way to turn this new ASLR support off for now?
There is an option in the Linker dialog to do that.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by madshi »

Yes, I tried that earlier today, but it doesn't seem to help. Unfortunately, I've absolutely no development resources available in the next 2-3 weeks. So as unlucky as this is, it has to wait... :sorry:
erwind
Posts: 4
Joined: Thu Jun 25, 2020 7:34 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by erwind »

I have recently installed Alexandria update 2 and also suffer from this same issue. Too bad we have to wait such long time before you even intend to find a solution.


On a side note, after upgrading I also had to fix two bugs. Both were related to SetWindowLong. I noticed similar issues in your code as well:

For example in madKernel.pas:

Code: Select all

procedure TIWindow.SetWndProc(value: TWndProc);
begin
  SetParam(GWL_WNDPROC, integer(@value));
end;

procedure TIWindow.SetParam(index: integer; value: integer);
begin
  if IsStillValid or CheckValid then begin
    if FProcessID = GetCurrentProcessID then begin
      windows.SetLastError(0);
      if (SetWindowLong(FHandle, index, value) = 0) and (GetLastError <> 0) then
        SetLastError(GetLastError);
    end else
      if not TIProcess(GetOwnerProcess.SelfAsTObject).SetWindowLong(self.FHandle, index, value) then
        SetLastError(FProcess.LastErrorNo, FProcess.LastErrorStr);
  end;
end;
It obviously works in 32 bit, but a pointer doesn't fit into an Integer. You should either use NativeInt or IntPtr.

And in madShell.pas:

Code: Select all

function ShellObjContextMenuWndProc(wnd, msg: cardinal; wParam, lParam: integer) : integer; stdcall;
var cm  : IContextMenu;
    cm3 : IContextMenu3;
    c1  : LRESULT;
begin
  case Msg of
    WM_CREATE:
      begin
        SetWindowLong(wnd, GWL_USERDATA, integer(PCreateStruct(lParam).lpCreateParams));
Zoë
Posts: 3
Joined: Fri Jul 24, 2015 9:34 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by Zoë »

The problem is that Delphi 11.2 updated the first section of the .map files to use 64-bit addresses. madMapFile's handling is strictly column based so it doesn't think it's a valid file and skips out, so madExceptPatch.exe can't find any of the functions it needs to override. Here's examples of older Delphi .map files and the new ones in Delphi 11.2:

Code: Select all

 Start         Length     Name                   Class
 0001:00401000 019F6D9CH .text                   CODE
 0002:01DF8000 002EDF98H .data                   DATA
 0003:020E6000 0003F250H .bss                    BSS
 0004:00000000 000008F4H .tls                    TLS
 0005:0222E000 00141D2CH .pdata                  PDATA
 

Code: Select all

 Start                 Length     Name                   Class
 0001:0000000000401000 01C7F39CH .text                   CODE
 0002:0000000002081000 002FC7B8H .data                   DATA
 0003:000000000237E000 0003B288H .bss                    BSS
 0004:0000000000400000 000008F4H .tls                    TLS
 0005:00000000024C8000 001493F4H .pdata                  PDATA
 
The attached zip has a quick and dirty patch to madMapFile.pas that fixes the issue. Just apply it, recompile madExceptPatch.dpr, and put the resulting exe in C:\Users\Public\Documents\Embarcadero\Studio\22.0\Tools
Attachments
madMapFile.patch.zip
(804 Bytes) Not downloaded yet
Zoë
Posts: 3
Joined: Fri Jul 24, 2015 9:34 pm

Re: Exception not caught in Delphi 11.2 Alexandria 64 bit

Post by Zoë »

@madshi: A problem that disguises what's going on is that madExceptPatch actually reports that the executable has been successfully patched, so there's no indication that anything has gone wrong at that step. You should probably adjust its behavior to kick out an error if it can't actually patch any routines at all. Here's the relevant bit of the madExceptWizard.txt log:

Code: Select all

binary successfully opened
map file successfully loaded/parsed
binary opened as memory mapped file
patch binary
madExcept.BcbInitExceptBlockLDTC not found
.__InitExceptBlockLDTC not found
madExcept.BcbExceptionHandler not found
.____ExceptionHandler not found
madExcept.BcbThrowExceptionLDTC not found
._ThrowExceptionLDTC not found
madExcept.BcbOrgMalloc not found
.___org_malloc not found
madExcept.BcbMemcpy not found
._memcpy|memcpy not found
madExcept.BcbCallTerminate not found
.___call_terminate not found
madExcept.BcbVclClearEH not found
.___VCL_clear_EH not found
madExcept.Forms_TApplication_HandleException not found
Vcl.Forms.TApplication.HandleException not found
madExcept.Forms_TApplication_ShowException not found
Vcl.Forms.TApplication.ShowException not found
madExcept.Qforms_TApplication_HandleException not found
Qforms.TApplication.HandleException not found
madExcept.Qforms_TApplication_ShowException not found
Qforms.TApplication.ShowException not found
madExcept.FMX_TApplication_HandleException not found
FMX.Forms.TApplication.HandleException not found
madExcept.FMX_TApplication_ShowException not found
FMX.Forms.TApplication.ShowException not found
madExcept.SysUtils_ShowException not found
System.SysUtils.ShowException not found
madExcept.SysUtils_LoadPackage not found
System.SysUtils.LoadPackage not found
madExcept.SysUtils_InitializePackage not found
System.SysUtils.InitializePackage not found
madExcept.SysUtils_Exception_RaisingException not found
System.SysUtils.Exception.RaisingException not found
madExcept.SysUtils_Exception_GetStackTrace not found
System.SysUtils.Exception.GetStackTrace not found
madExcept.SysUtils_Exception_Destroy not found
System.SysUtils.Exception.Destroy not found
madExcept.SysUtils_AnsiStrAlloc not found
System.SysUtils.AnsiStrAlloc|StrAlloc not found
madExcept.SysUtils_WideStrAlloc not found
System.SysUtils.WideStrAlloc not found
madExcept.Classes_CheckSynchronize not found
System.Classes.CheckSynchronize not found
madExcept.CGIApp_TCGIApplication_CGIHandleException not found
Web.CGIApp.TCGIApplication.CGIHandleException not found
madExcept.ISAPIApp_TISAPIApplication_ISAPIHandleException not found
Web.Win.ISAPIApp.TISAPIApplication.ISAPIHandleException not found
madExcept.System_InitUnits not found
System.InitUnits not found
madExcept.System_FinalizeUnits not found
System.FinalizeUnits|FInitUnits not found
madExcept.System_ExceptionHandler not found
System.@ExceptionHandler not found
madExcept.System_runErrMsg not found
System.runErrMsg not found
madExcept.ReportLeaks not found
madExcept.DelphiDebugMm not found
madExcept.CrashOnOverrun not found
append map file to binary
map file is not yet appended
append map file now
Info: map file appended
PatchBinary done
Post Reply