Things to Remember When Developing 64-bit Software


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Things to Remember When Developing 64-bit Software

  1. 1. Things to Remember WhenDeveloping 64-bit SoftwareOOO "Program Verification Systems"
  2. 2. Advantages of 64-bit Software• You can store all the data right inmemory• Performance boost (when thedata format is chosen correctly)• Additional performance boostdue to architectural solutions• No resources wasted to passthrough the WoW64 layer
  3. 3. Who can writeideal,sphericalcode?Differences in Programming• No differences. C/C++ is a languagedesigned to develop crossplatformapplications• Only the data model is different• But in practice, programs are notcompletely crossplatform• A compiled 64-bit program != a correct64-bit program
  4. 4. What Has Changed?• Pointer size: 4 bytes -> 8 bytes.• Sizes of types size_t, ptrdiff_t, intptr_t,uintptr_t, INT_PTR, UINTPTR_T: 4 bytes -> 8bytes.• Data alignment. Mind it when serializingstructures.• Array index size.• So few? More than enough for troubles!
  5. 5. From Simple to Complex.Magic NumbershFileMapping = CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL, PAGE_READWRITE,(DWORD) 0, (DWORD) (szBufIm),(LPCTSTR) &FileShareNameMap[0]);//Number 0xFFFFFFFF is “usigned int”. The fixed code:#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)
  6. 6. Storing Pointers in a 32-bit Variablechar *ptr = ...;int n = (int) ptr;...ptr = (char *) n;A very annoying bug.It only occurs when theprogram is running fora long time.
  7. 7. The Most Important Thing toRemember with 64-bit Development• The pointer is not the same thing as int or long.By the way, the long type is different in the datamodels LP64 and LLP64. It also adds some moreconfusion.• Don’t use 32-bit types and address arithmetictogether. Use these types:– size_t– ptrdiff_t– intptr_t– uintptr_t– INT_PTR, ….
  8. 8. A More Creative Solution for PointerStorageint *p1, *p2;....char str[128];sprintf(str, "%X %X", p1, p2);int *p1, *p2;sscanf(str, "%X %X", &p1, &p2);Use “%p” to print pointer values. Also, programmers often incorrectlyprint size_t variables. “%Iu” should be used for this purpose.const char *invalidFormat = "%u";size_t value = SIZE_MAX;printf(invalidFormat, value);
  9. 9. A few more words about functionswith variable number of arguments
  10. 10. Implicit Type Conversionunsigned n = it->find("ABC");if (n != string::npos)return true;
  11. 11. Address Arithmetic. Signed andUnsigned Typesfloat p1[100];unsigned x = 5;int y = -1;float *p2 = p1 + 50;p2 = p2 + x * y;// Access violation*p2 = 1.0f;Correct code!!!!!!!!!!!!!!!!!!!!!!!!!!
  12. 12. Address Arithmetic. Overflows.extern int Width, Height, Depth;size_t GetIndex(int x, int y, int z) {return z * Width * Height + y * Width + x;}...MyArray[GetIndex(x, y, z)] = 0.0f;return size_t(z) * Width * Height +size_t(y) * Width +x;These errors reveal themselves only at LARGE data amounts. Unit tests won’thelp you here, neither will dynamic analyzers such as Bounds Checker (it is tooslow).
  13. 13. Incorrect Solutionsreturn size_t(z * Width * Height + y * Width + x);return size_t(z) * Width * Height + y * Width + x;
  14. 14. Infinite LoopsP.S. The funny thing is thatinfinite loops sometimes failto occur – thanks to compiler-performed optimization.
  15. 15. Obsolete typedef and #ifdef#ifdef _MY_BUILD_32// Win32const unsigned BufferSize = 1024*1024;#else// Win16const unsigned BufferSize = 16*1024;#endif// A do-it-yourself thing in one filetypedef unsigned UINT_PTR;
  16. 16. Exotic Issues: Buffer Overflow
  17. 17. Structure Size Growthstruct MyStruct{bool m_bool;char *m_pointer;int m_int;};struct MyStructOpt{char *m_pointer;int m_int;bool m_bool;};
  18. 18. Other Issues• Use of deprecated functions (SetClassLong,GetClassLong, GetFileSize, EnumProcessModules,GlobalMemoryStatus)• Undeclared functions in C• Different parameters of virtual functions• Array type change• Pointers in unions• Exceptions• And so on…
  19. 19. How to Prepare a Reliable 64-bitVersion of a Product?• Unit-tests (but don’t rely solely on them)• Regression tests• Compiler warning elimination• Using specialized tools, for example theViva64 rule set included into the PVS-Studiostatic analyzer.
  20. 20. PVS-Studio• Rule sets:– The most powerful analysis for64-bit errors among other similartools– General diagnostics– Micro optimizations– OpenMP• About the tool:• Download:
  21. 21. Visual Studio Integration
  22. 22. C++Builder Integration
  23. 23. References• Lessons on development of 64-bit C/C++applications.• An article. A Collection of Examples of 64-bitErrors in Real Programs.• Article reviews• Knowledge base