• Save
Things to Remember When Developing 64-bit Software
Upcoming SlideShare
Loading in...5
×
 

Things to Remember When Developing 64-bit Software

on

  • 552 views

 

Statistics

Views

Total Views
552
Views on SlideShare
552
Embed Views
0

Actions

Likes
2
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Things to Remember When Developing 64-bit Software Things to Remember When Developing 64-bit Software Presentation Transcript

  • Things to Remember WhenDeveloping 64-bit SoftwareOOO "Program Verification Systems"www.viva64.com
  • 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
  • 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
  • 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!
  • 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)
  • 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.
  • 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, ….
  • 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);
  • A few more words about functionswith variable number of arguments
  • Implicit Type Conversionunsigned n = it->find("ABC");if (n != string::npos)return true;
  • 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!!!!!!!!!!!!!!!!!!!!!!!!!!
  • 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).
  • Incorrect Solutionsreturn size_t(z * Width * Height + y * Width + x);return size_t(z) * Width * Height + y * Width + x;
  • Infinite LoopsP.S. The funny thing is thatinfinite loops sometimes failto occur – thanks to compiler-performed optimization.
  • 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;
  • Exotic Issues: Buffer Overflow
  • Structure Size Growthstruct MyStruct{bool m_bool;char *m_pointer;int m_int;};struct MyStructOpt{char *m_pointer;int m_int;bool m_bool;};
  • 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…
  • 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.
  • PVS-Studio• Rule sets:– The most powerful analysis for64-bit errors among other similartools– General diagnostics– Micro optimizations– OpenMP• About the tool:http://www.viva64.com/en/pvs-studio/• Download:http://www.viva64.com/en/pvs-studio-download/
  • Visual Studio Integration
  • C++Builder Integration
  • References• Lessons on development of 64-bit C/C++applications. http://www.viva64.com/en/l/• An article. A Collection of Examples of 64-bitErrors in Real Programs.http://www.viva64.com/en/a/0065• Article reviewshttp://www.viva64.com/en/r/tag/x64/• Knowledge basehttp://www.viva64.com/en/k/