Recent

Author Topic: Migration to 64-bit. Find corrupt pointers  (Read 2876 times)

Igor

  • Sr. Member
  • ****
  • Posts: 385
Migration to 64-bit. Find corrupt pointers
« on: December 19, 2018, 07:29:45 pm »
Hi,

We're adapting an old 32-bit app for 64-bit. Mainly it works, but sometimes we get random SIGSEG errors due to corrupt pointers. We already found and fixed many cases when pointers were processed through Integer or DWORD that is not correct for 64-bit.

Is there any way to find all such cases? Some help from the compiler or memory manager?

The problem in a fact that many 64-bit pointers are located below 2GB address space. And wrong processing of pointers via Integer/DWORD/Cardinal doesn't reveal the dangerous problem. So when new pointers are being created above 2-3GB we face a problem (SIGSEG).

Currently we found one trick, at beginning of our program we call GetMem() for 4GB buffer and it shifts all new pointers above 4GB and I can see what doesn't work correctly and try to debug it.

Is it possible to temporarily adjust 64-bit memory manager to generate pointers in address space above 4GB ?
« Last Edit: December 19, 2018, 07:35:37 pm by Igor Kokarev »

440bx

  • Hero Member
  • *****
  • Posts: 5896
Re: Migration to 64-bit. Find corrupt pointers
« Reply #1 on: December 20, 2018, 12:30:03 am »
Is it possible to temporarily adjust 64-bit memory manager to generate pointers in address space above 4GB ?
If I were facing the problem you described, I'd do whatever I can to correct the code.  To do that, what comes to mind at this time is to create an AWK script that scans the source code for occurrences of typecasts, such as "integer(", "dword(", "absolute" and dereferences (")^") and possibly others depending on how the program is written.  Such a script would likely need to be refined to produce a usable list of potentially offending lines of code but, it seems like something worth a try.

It will probably be a bit tedious but, if you want the result to be "solid", there doesn't seem to be a way around inspecting all the potentially offending lines of code.  A carefully tested AWK script can probably produce such a list.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Igor

  • Sr. Member
  • ****
  • Posts: 385
Re: Migration to 64-bit. Find corrupt pointers
« Reply #2 on: December 20, 2018, 08:22:12 am »
Thanks for your advice. It's a lot of work, but I don't see another solution.

440bx

  • Hero Member
  • *****
  • Posts: 5896
Re: Migration to 64-bit. Find corrupt pointers
« Reply #3 on: December 20, 2018, 08:54:42 am »
Thanks for your advice. It's a lot of work, but I don't see another solution.
You're welcome.  If I can think of something that is less work and also effective, I'll mention it but, for now, that's the only thing that comes to mind and, you're right, it will likely be a lot of work.

if you could post various pieces of code that suffer from the problem, anywhere from 1000 to 5000 lines, maybe looking at it would bring other ideas.

ETA: one thing that comes to mind is simply replacing all the occurrences of cardinal and dword with PtrUInt, that's fairly easy, should be safe (of course, to be safe and prudent, test it on a copy of the code) and can be automated.  At least for those two types, it should take care of the problem you mentioned.    The integer typecasts are definitely problematic because those are simply wrong in any bitness.

By replacing the occurrences of dword and cardinal with PtrUInt, that would make the list of typecasts to review smaller.

HTH.

« Last Edit: December 20, 2018, 09:36:02 am by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: Migration to 64-bit. Find corrupt pointers
« Reply #4 on: December 20, 2018, 10:18:50 am »
It will always be a lot of work, but the refactoring options from either a Delphi or Lazarus IDE are your friend.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Igor

  • Sr. Member
  • ****
  • Posts: 385
Re: Migration to 64-bit. Find corrupt pointers
« Reply #5 on: December 21, 2018, 03:51:33 pm »
After hard work we catched several mistakes in the code. And now 64-bit app works correctly.

 

TinyPortal © 2005-2018