Delphi_MemoryModule icon indicating copy to clipboard operation
Delphi_MemoryModule copied to clipboard

Access Violation using MemoryModule for large application built with runtime packages

Open ccy opened this issue 5 years ago • 0 comments

using MemoryModule will cause access violation for large application built with runtime packages.

This happen to FixPtr function in ExecuteTLS.

This is due to the code was allocated via VirtualAlloc to allocate memory at arbitrary position:

    // reserve memory for image of library
    // XXX: is it correct to commit the complete memory region at once?
    //      calling DllEntry raises an exception if we don't...
    code := VirtualAlloc(Pointer(old_header.OptionalHeader.ImageBase),
                         old_header.OptionalHeader.SizeOfImage,
                         MEM_RESERVE or MEM_COMMIT,
                         PAGE_READWRITE);
    if code = nil then
    begin
      // try to allocate memory at arbitrary position
      code := VirtualAlloc(nil,
                           old_header.OptionalHeader.SizeOfImage,
                           MEM_RESERVE or MEM_COMMIT,
                           PAGE_READWRITE);
      if code = nil then
      begin
        SetLastError(ERROR_OUTOFMEMORY);
        Exit;
      end;
    end;

And it further affect in ExecuteTLS:

callback := FixPtr(callback);

I refer to c version of MemoryModule: https://github.com/fancycode/MemoryModule/blob/master/MemoryModule.c

It seems calling FixPtr isn't require.

ccy avatar Jul 04 '20 00:07 ccy