SlideShare ist ein Scribd-Unternehmen logo
1 von 52
Patterns of 64-bit errors in
games
Andrey Karpov
karpov@viva64.com
Speaker
• Karpov Andrey Nikolaevich
• Habr profile: habr.com/users/Andrey2008/
• Microsoft MVP, Intel Black Belt Software Developer
• One of the founders of the PVS-Studio project, which
started right from the search of 64-bit errors
• https://www.viva64.com
Technically speaking, there are no
«64-bit errors»
In practice
• 64-bit errors are the ones which reveal themselves after building a
64-bit application
• By the way, many of them do it only when the 4 Gigabyte limit is
exceeded
Explanation on the example of malloc
• Who will notice an error?
void Foo()
{
const size_t Gigabyte = 1073741824;
void *A[10];
for (size_t i = 0; i < 10; ++i)
{
A[i] = malloc(Gigabyte);
printf("%pn", A[i]);
}
for (size_t i = 0; i < 10; ++i)
free(A[i]);
}
In C-code developers forget to include
headers
• Your program might not contain calls of malloc, but they might be in
third-party libraries
• It is revealed only when consuming large amount of memory
#include <stdio.h>
#include <string.h>
//#include <stdlib.h>
The case isn’t only about malloc
• Fennec Media
• memset
• memcpy
• Ffdshow (media decoder)
• memset
• memcpy
• libZPlay
• strlen
Let’s get back to implicit
truncation of 64-bit values
long type in Win64
const std::vector<Vec2> &vertexList =
body->getCalculatedVertexList();
unsigned long length = vertexList.size();
PVS-Studio: V103 Implicit type conversion from memsize to 32-bit type.
CCArmature.cpp 614
Cocos2d-x
int type is a bad idea in any case
PVS-Studio: V104 Implicit conversion of 'i' to memsize type in an arithmetic
expression: i < points_.size() sweep_context.cc 76
std::vector<Point*> points_;
void SweepContext::InitTriangulation()
{
// Calculate bounds.
for (unsigned int i = 0; i < points_.size(); i++) {
Point& p = *points_[i];
....
Cocos2d-x
By the way, such errors are more insidious
than it looks
• When overflowing int, undefined behavior occurs
• UB might be the case when everything works correctly :)
• More details: "Undefined behavior is closer than you think"
https://www.viva64.com/ru/b/0374/
size_t N = foo();
for (int i = 0; i < N; ++i)
Usage of correct memsize-types
"memsize" types
• ptrdiff_t - signed type (distance between pointers)
• size_t – maximum size of theoretically possible object of any type,
including arrays
• rsize_t – size of a single object
• intptr_t - signed type, can store a pointer
• uintptr_t – unsigned type, can store a pointer
Usage of memsize-types
• Number of elements in an array
• Buffer size
• Counters
• Pointer storing (although, it’s better not to do it)
• Pointer arithmetic
• And so on
Using of memsize-types: bad thing
PVS-Studio: V109 Implicit type conversion of return value 'a * b * c' to memsize
type. test.cpp 22
Potential overflow
size_t Foo(int a, int b, int c)
{
return a * b * c;
}
Using of memsize-types: OK
size_t Foo(int a, int b, int c)
{
return static_cast<size_t>(a)
* static_cast<size_t>(b)
* static_cast<size_t>(c);
}
size_t Foo(int a, int b, int c)
{
return static_cast<size_t>(a) * b * c;
}
JUST DON’T DO IT!
std::vector<unsigned char> buf(
static_cast<size_t>(exr_image->width * h * pixel_data_size));
PVS-Studio: V1028 CWE-190 Possible overflow. Consider casting operands, not the
result. tinyexr.h 11682
Possible
overflow again
TinyEXR
This way errors only hide more
if (components == 1) {
images[0].resize(static_cast<size_t>(width * height));
memcpy(images[0].data(), data, sizeof(float) * size_t(width * height));
} else {
images[0].resize(static_cast<size_t>(width * height));
images[1].resize(static_cast<size_t>(width * height));
images[2].resize(static_cast<size_t>(width * height));
images[3].resize(static_cast<size_t>(width * height));
for (size_t i = 0; i < static_cast<size_t>(width * height); i++) {
TinyEXR
More about pointer arithmetic
int A = -2;
unsigned B = 1;
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array + 3;
ptr = ptr + (A + B);
printf("%in", *ptr); //Access violation
//on 64-bit platform
Developers forget to expand 32-bit
types
int64_t X = 1 << N
• This code doesn’t work in 32-bit programs as well
• Setting most significant bits is usually not needed there
class FMallocBinned : public FMalloc
{
uint64 PoolMask;
....
FMallocBinned(uint32 InPageSize, uint64 AddressLimit)
{
....
PoolMask = (( 1 << ( HashKeyShift - PoolBitShift ) ) - 1);
....
}
};
Unreal Engine 4
PVS-Studio: V629 Consider inspecting the '1 << (HashKeyShift - PoolBitShift)'
expression. Bit shifting of the 32-bit value with a subsequent expansion to the 64-
bit type. mallocbinned.h 800
class FMallocBinned : public FMalloc
{
uint64 PoolMask;
....
FMallocBinned(uint32 InPageSize, uint64 AddressLimit)
{
....
PoolMask = (( 1ULL << ( HashKeyShift - PoolBitShift ) ) - 1);
....
}
};
Unreal Engine 4
Godot Engine
void vp9_adjust_mask(....) {
....
const uint64_t columns = cm->mi_cols - mi_col;
const uint64_t mask_y =
(((1 << columns) - 1)) * 0x0101010101010101ULL;
....
}
PVS-Studio: V629 CWE-190 Consider inspecting the '1 << columns'
expression. Bit shifting of the 32-bit value with a subsequent expansion to
the 64-bit type. vp9_loopfilter.c 851
size_t N = ~0U;
• In 32-bit code everything is OK
• 64-bit code, expectation: 0xFFFFFFFFFFFFFFFF
• 64-bit code, reality: 0x00000000FFFFFFFF
size_t nRenderMeshSize = ~0U;
PVS-Studio: V101 Implicit assignment type conversion to memsize type.
StatObjLoad.cpp 1369
CryEngine
size_t N = ~(1024u - 1);
• In 32-bit code everything is OK
• 64-bit code, expectation: 0xFFFFFFFFFFFFFC00
• 64-bit code, reality: 0x00000000FFFFFC00
static const UINT_PTR SmallBlockAlignMask = ~(SmallBlockLength - 1);
FreeBlockHeader* InsertFreeBlock(FreeBlockHeader* after,
UINT_PTR start, UINT_PTR end)
{
bool isFreeBlockDone = (start & SmallBlockAlignMask) ==
(end & SmallBlockAlignMask) ||
(start > end - (SmallBlockLength / 2));
....
}
CryEngine
Increased consumption of the
stack and dynamic memory
Stack
• I suggest taking it easy and just increase it by 2 times :)
Dynamic memory
• Is it worth doing some fighting?
• Are 64-bit integer types needed everywhere?
• Are the pointers needed everywhere?
• Structures’ size
Redundant growth of structures’ size
struct Candidate {
uint32_t face;
ChartBuildData *chart;
float metric;
};
PVS-Studio: V802 On 64-bit platform, structure size can be reduced from 24 to 16
bytes by rearranging the fields according to their sizes in decreasing order. xatlas.cpp
5237
24 bytes
Redundant growth of structures’ size
struct Candidate {
ChartBuildData *chart;
uint32_t face;
float metric;
};
16 bytes
Other error patterns
Magic constants
• 1024 x 768
• 1600 x 1200
Magic constants
• 1024 x 768
• 1600 x 1200
• 4
• 32
• 0x7FFFFFFF
• 0x80000000
• 0xFFFFFFFF
Dangerous tricks with type casting
ResourceBase::Header *header = (*iter).value;
char fourCC[ 5 ];
*( ( U32* ) fourCC ) = header->getSignature();
fourCC[ 4 ] = 0;
Everything will be fine thanks to header, but still very unstable.
PVS-Studio: V1032 The pointer 'fourCC' is cast to a more strictly aligned pointer
type. resourceManager.cpp 127
Torque 3D
Data serialization
• No specific errors and tips
• Review the code
Arrays of various types
Virtual functions
Virtual functions
How to write a 64-bit program of
high-quality?
Specify the code standard
• Use correct types:
ptrdiff_t, size_t, intptr_t, uintptr_t, INT_PTR, DWORD_PTR и т.д.
• Say «NO!» to magic numbers. Use this:
UINT_MAX, ULONG_MAX, std::numeric_limits<size_t>::max() и т.д.
• Explain how to correctly use the explicit type casting in the
expressions and what’s the difference between 1 and
static_cast<size_t>(1)
• unsigned long != size_t
Tooling: unit-tests
Tooling: dynamic analyzers
Tooling: static analyzers
Conclusion
• Even if you already have released a 64-bit application, this doesn’t
mean that it works correctly
• A 64-bit error can lie low for ages
• How to solve the situation:
• learning
• Code standard
• Static code analysis
Useful links
• Lessons on development of 64-bit C/C++ applications (single file)
https://www.viva64.com/ru/l/full/
• A Collection of Examples of 64-bit Errors in Real Programs
https://www.viva64.com/ru/a/0065/
• Undefined behavior is closer than you think
https://www.viva64.com/ru/b/0374/
Time for your questions!
Andrey Karpov
karpov@viva64.com
www.viva64.com

Weitere ähnliche Inhalte

Was ist angesagt?

WebAssembly向け多倍長演算の実装
WebAssembly向け多倍長演算の実装WebAssembly向け多倍長演算の実装
WebAssembly向け多倍長演算の実装MITSUNARI Shigeo
 
Vectorization with LMS: SIMD Intrinsics
Vectorization with LMS: SIMD IntrinsicsVectorization with LMS: SIMD Intrinsics
Vectorization with LMS: SIMD IntrinsicsETH Zurich
 
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...ETH Zurich
 
Happy To Use SIMD
Happy To Use SIMDHappy To Use SIMD
Happy To Use SIMDWei-Ta Wang
 
深層学習フレームワークにおけるIntel CPU/富岳向け最適化法
深層学習フレームワークにおけるIntel CPU/富岳向け最適化法深層学習フレームワークにおけるIntel CPU/富岳向け最適化法
深層学習フレームワークにおけるIntel CPU/富岳向け最適化法MITSUNARI Shigeo
 
A (brief) overview of Span<T>
A (brief) overview of Span<T>A (brief) overview of Span<T>
A (brief) overview of Span<T>David Wengier
 
IonMonkey Mozilla All-Hands 2011
IonMonkey Mozilla All-Hands 2011IonMonkey Mozilla All-Hands 2011
IonMonkey Mozilla All-Hands 2011iamdvander
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov
 
Practical Two-level Homomorphic Encryption in Prime-order Bilinear Groups
Practical Two-level Homomorphic Encryption in Prime-order Bilinear GroupsPractical Two-level Homomorphic Encryption in Prime-order Bilinear Groups
Practical Two-level Homomorphic Encryption in Prime-order Bilinear GroupsMITSUNARI Shigeo
 
Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Platonov Sergey
 
To Swift 2...and Beyond!
To Swift 2...and Beyond!To Swift 2...and Beyond!
To Swift 2...and Beyond!Scott Gardner
 
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017Andrey Karpov
 
Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and...
Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and...Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and...
Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and...MITSUNARI Shigeo
 
PVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio
 
25 лет истории C++, пролетевшей на моих глазах
25 лет истории C++, пролетевшей на моих глазах25 лет истории C++, пролетевшей на моих глазах
25 лет истории C++, пролетевшей на моих глазахcorehard_by
 
Implementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HWImplementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HWJoe Jiang
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)Angel Boy
 

Was ist angesagt? (20)

WebAssembly向け多倍長演算の実装
WebAssembly向け多倍長演算の実装WebAssembly向け多倍長演算の実装
WebAssembly向け多倍長演算の実装
 
Vectorization with LMS: SIMD Intrinsics
Vectorization with LMS: SIMD IntrinsicsVectorization with LMS: SIMD Intrinsics
Vectorization with LMS: SIMD Intrinsics
 
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
 
Happy To Use SIMD
Happy To Use SIMDHappy To Use SIMD
Happy To Use SIMD
 
深層学習フレームワークにおけるIntel CPU/富岳向け最適化法
深層学習フレームワークにおけるIntel CPU/富岳向け最適化法深層学習フレームワークにおけるIntel CPU/富岳向け最適化法
深層学習フレームワークにおけるIntel CPU/富岳向け最適化法
 
AA-sort with SSE4.1
AA-sort with SSE4.1AA-sort with SSE4.1
AA-sort with SSE4.1
 
A (brief) overview of Span<T>
A (brief) overview of Span<T>A (brief) overview of Span<T>
A (brief) overview of Span<T>
 
IonMonkey Mozilla All-Hands 2011
IonMonkey Mozilla All-Hands 2011IonMonkey Mozilla All-Hands 2011
IonMonkey Mozilla All-Hands 2011
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
 
Practical Two-level Homomorphic Encryption in Prime-order Bilinear Groups
Practical Two-level Homomorphic Encryption in Prime-order Bilinear GroupsPractical Two-level Homomorphic Encryption in Prime-order Bilinear Groups
Practical Two-level Homomorphic Encryption in Prime-order Bilinear Groups
 
Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”
 
To Swift 2...and Beyond!
To Swift 2...and Beyond!To Swift 2...and Beyond!
To Swift 2...and Beyond!
 
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
 
Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and...
Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and...Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and...
Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and...
 
PVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around Disney
 
25 лет истории C++, пролетевшей на моих глазах
25 лет истории C++, пролетевшей на моих глазах25 лет истории C++, пролетевшей на моих глазах
25 лет истории C++, пролетевшей на моих глазах
 
Implementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HWImplementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HW
 
Exploiting vectorization with ISPC
Exploiting vectorization with ISPCExploiting vectorization with ISPC
Exploiting vectorization with ISPC
 
暗認本読書会4
暗認本読書会4暗認本読書会4
暗認本読書会4
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
 

Ähnlich wie 64-bit Errors in Games

Things to Remember When Developing 64-bit Software
Things to Remember When Developing 64-bit SoftwareThings to Remember When Developing 64-bit Software
Things to Remember When Developing 64-bit SoftwareAndrey Karpov
 
Monitoring a program that monitors computer networks
Monitoring a program that monitors computer networksMonitoring a program that monitors computer networks
Monitoring a program that monitors computer networksAndrey Karpov
 
Monitoring a program that monitors computer networks
Monitoring a program that monitors computer networksMonitoring a program that monitors computer networks
Monitoring a program that monitors computer networksPVS-Studio
 
An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
 An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
An Open Discussion of RISC-V BitManip, trends, and comparisons _ ClaireRISC-V International
 
Some examples of the 64-bit code errors
Some examples of the 64-bit code errorsSome examples of the 64-bit code errors
Some examples of the 64-bit code errorsPVS-Studio
 
200 Open Source Projects Later: Source Code Static Analysis Experience
200 Open Source Projects Later: Source Code Static Analysis Experience200 Open Source Projects Later: Source Code Static Analysis Experience
200 Open Source Projects Later: Source Code Static Analysis ExperienceAndrey Karpov
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects 100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects Andrey Karpov
 
Comparison of analyzers' diagnostic possibilities at checking 64-bit code
Comparison of analyzers' diagnostic possibilities at checking 64-bit codeComparison of analyzers' diagnostic possibilities at checking 64-bit code
Comparison of analyzers' diagnostic possibilities at checking 64-bit codePVS-Studio
 
PVS-Studio, a solution for developers of modern resource-intensive applications
PVS-Studio, a solution for developers of modern resource-intensive applicationsPVS-Studio, a solution for developers of modern resource-intensive applications
PVS-Studio, a solution for developers of modern resource-intensive applicationsPVS-Studio
 
Interview with Anatoliy Kuznetsov, the author of BitMagic C++ library
Interview with Anatoliy Kuznetsov, the author of BitMagic C++ libraryInterview with Anatoliy Kuznetsov, the author of BitMagic C++ library
Interview with Anatoliy Kuznetsov, the author of BitMagic C++ libraryPVS-Studio
 
The reasons why 64-bit programs require more stack memory
The reasons why 64-bit programs require more stack memoryThe reasons why 64-bit programs require more stack memory
The reasons why 64-bit programs require more stack memoryPVS-Studio
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerAndrey Karpov
 
A Collection of Examples of 64-bit Errors in Real Programs
A Collection of Examples of 64-bit Errors in Real ProgramsA Collection of Examples of 64-bit Errors in Real Programs
A Collection of Examples of 64-bit Errors in Real ProgramsPVS-Studio
 
Critical errors in CryEngine V code
Critical errors in CryEngine V codeCritical errors in CryEngine V code
Critical errors in CryEngine V codePVS-Studio
 
64-Bit Code in 2015: New in the Diagnostics of Possible Issues
64-Bit Code in 2015: New in the Diagnostics of Possible Issues64-Bit Code in 2015: New in the Diagnostics of Possible Issues
64-Bit Code in 2015: New in the Diagnostics of Possible IssuesPVS-Studio
 
C++ in Action Webinar: Move your C++ projects to C++Builder 10 Seattle
C++ in Action Webinar: Move your C++ projects to C++Builder 10 SeattleC++ in Action Webinar: Move your C++ projects to C++Builder 10 Seattle
C++ in Action Webinar: Move your C++ projects to C++Builder 10 SeattleDavid Intersimone
 
Jurczyk windows kernel reference count vulnerabilities. case study
Jurczyk   windows kernel reference count vulnerabilities. case studyJurczyk   windows kernel reference count vulnerabilities. case study
Jurczyk windows kernel reference count vulnerabilities. case studyDefconRussia
 
A collection of examples of 64 bit errors in real programs
A collection of examples of 64 bit errors in real programsA collection of examples of 64 bit errors in real programs
A collection of examples of 64 bit errors in real programsMichael Scovetta
 

Ähnlich wie 64-bit Errors in Games (20)

Things to Remember When Developing 64-bit Software
Things to Remember When Developing 64-bit SoftwareThings to Remember When Developing 64-bit Software
Things to Remember When Developing 64-bit Software
 
Monitoring a program that monitors computer networks
Monitoring a program that monitors computer networksMonitoring a program that monitors computer networks
Monitoring a program that monitors computer networks
 
Monitoring a program that monitors computer networks
Monitoring a program that monitors computer networksMonitoring a program that monitors computer networks
Monitoring a program that monitors computer networks
 
An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
 An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
 
Some examples of the 64-bit code errors
Some examples of the 64-bit code errorsSome examples of the 64-bit code errors
Some examples of the 64-bit code errors
 
200 Open Source Projects Later: Source Code Static Analysis Experience
200 Open Source Projects Later: Source Code Static Analysis Experience200 Open Source Projects Later: Source Code Static Analysis Experience
200 Open Source Projects Later: Source Code Static Analysis Experience
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects 100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
 
Comparison of analyzers' diagnostic possibilities at checking 64-bit code
Comparison of analyzers' diagnostic possibilities at checking 64-bit codeComparison of analyzers' diagnostic possibilities at checking 64-bit code
Comparison of analyzers' diagnostic possibilities at checking 64-bit code
 
embedded C.pptx
embedded C.pptxembedded C.pptx
embedded C.pptx
 
PVS-Studio, a solution for developers of modern resource-intensive applications
PVS-Studio, a solution for developers of modern resource-intensive applicationsPVS-Studio, a solution for developers of modern resource-intensive applications
PVS-Studio, a solution for developers of modern resource-intensive applications
 
Interview with Anatoliy Kuznetsov, the author of BitMagic C++ library
Interview with Anatoliy Kuznetsov, the author of BitMagic C++ libraryInterview with Anatoliy Kuznetsov, the author of BitMagic C++ library
Interview with Anatoliy Kuznetsov, the author of BitMagic C++ library
 
The reasons why 64-bit programs require more stack memory
The reasons why 64-bit programs require more stack memoryThe reasons why 64-bit programs require more stack memory
The reasons why 64-bit programs require more stack memory
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzer
 
A Collection of Examples of 64-bit Errors in Real Programs
A Collection of Examples of 64-bit Errors in Real ProgramsA Collection of Examples of 64-bit Errors in Real Programs
A Collection of Examples of 64-bit Errors in Real Programs
 
Critical errors in CryEngine V code
Critical errors in CryEngine V codeCritical errors in CryEngine V code
Critical errors in CryEngine V code
 
64-Bit Code in 2015: New in the Diagnostics of Possible Issues
64-Bit Code in 2015: New in the Diagnostics of Possible Issues64-Bit Code in 2015: New in the Diagnostics of Possible Issues
64-Bit Code in 2015: New in the Diagnostics of Possible Issues
 
C++ in Action Webinar: Move your C++ projects to C++Builder 10 Seattle
C++ in Action Webinar: Move your C++ projects to C++Builder 10 SeattleC++ in Action Webinar: Move your C++ projects to C++Builder 10 Seattle
C++ in Action Webinar: Move your C++ projects to C++Builder 10 Seattle
 
Jurczyk windows kernel reference count vulnerabilities. case study
Jurczyk   windows kernel reference count vulnerabilities. case studyJurczyk   windows kernel reference count vulnerabilities. case study
Jurczyk windows kernel reference count vulnerabilities. case study
 
Deep hooks
Deep hooksDeep hooks
Deep hooks
 
A collection of examples of 64 bit errors in real programs
A collection of examples of 64 bit errors in real programsA collection of examples of 64 bit errors in real programs
A collection of examples of 64 bit errors in real programs
 

Mehr von Andrey Karpov

60 антипаттернов для С++ программиста
60 антипаттернов для С++ программиста60 антипаттернов для С++ программиста
60 антипаттернов для С++ программистаAndrey Karpov
 
60 terrible tips for a C++ developer
60 terrible tips for a C++ developer60 terrible tips for a C++ developer
60 terrible tips for a C++ developerAndrey Karpov
 
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
Ошибки, которые сложно заметить на code review, но которые находятся статичес...Ошибки, которые сложно заметить на code review, но которые находятся статичес...
Ошибки, которые сложно заметить на code review, но которые находятся статичес...Andrey Karpov
 
PVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Error ExamplesPVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Error ExamplesAndrey Karpov
 
PVS-Studio in 2021 - Feature Overview
PVS-Studio in 2021 - Feature OverviewPVS-Studio in 2021 - Feature Overview
PVS-Studio in 2021 - Feature OverviewAndrey Karpov
 
PVS-Studio в 2021 - Примеры ошибок
PVS-Studio в 2021 - Примеры ошибокPVS-Studio в 2021 - Примеры ошибок
PVS-Studio в 2021 - Примеры ошибокAndrey Karpov
 
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...Andrey Karpov
 
Best Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesAndrey Karpov
 
Does static analysis need machine learning?
Does static analysis need machine learning?Does static analysis need machine learning?
Does static analysis need machine learning?Andrey Karpov
 
Typical errors in code on the example of C++, C#, and Java
Typical errors in code on the example of C++, C#, and JavaTypical errors in code on the example of C++, C#, and Java
Typical errors in code on the example of C++, C#, and JavaAndrey Karpov
 
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)Andrey Karpov
 
Game Engine Code Quality: Is Everything Really That Bad?
Game Engine Code Quality: Is Everything Really That Bad?Game Engine Code Quality: Is Everything Really That Bad?
Game Engine Code Quality: Is Everything Really That Bad?Andrey Karpov
 
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
The Use of Static Code Analysis When Teaching or Developing Open-Source SoftwareThe Use of Static Code Analysis When Teaching or Developing Open-Source Software
The Use of Static Code Analysis When Teaching or Developing Open-Source SoftwareAndrey Karpov
 
Static Code Analysis for Projects, Built on Unreal Engine
Static Code Analysis for Projects, Built on Unreal EngineStatic Code Analysis for Projects, Built on Unreal Engine
Static Code Analysis for Projects, Built on Unreal EngineAndrey Karpov
 
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Safety on the Max: How to Write Reliable C/C++ Code for Embedded SystemsSafety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Safety on the Max: How to Write Reliable C/C++ Code for Embedded SystemsAndrey Karpov
 
The Great and Mighty C++
The Great and Mighty C++The Great and Mighty C++
The Great and Mighty C++Andrey Karpov
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?Andrey Karpov
 
Zero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for youZero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for youAndrey Karpov
 
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOpsPVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOpsAndrey Karpov
 

Mehr von Andrey Karpov (20)

60 антипаттернов для С++ программиста
60 антипаттернов для С++ программиста60 антипаттернов для С++ программиста
60 антипаттернов для С++ программиста
 
60 terrible tips for a C++ developer
60 terrible tips for a C++ developer60 terrible tips for a C++ developer
60 terrible tips for a C++ developer
 
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
Ошибки, которые сложно заметить на code review, но которые находятся статичес...Ошибки, которые сложно заметить на code review, но которые находятся статичес...
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
 
PVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Error ExamplesPVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Error Examples
 
PVS-Studio in 2021 - Feature Overview
PVS-Studio in 2021 - Feature OverviewPVS-Studio in 2021 - Feature Overview
PVS-Studio in 2021 - Feature Overview
 
PVS-Studio в 2021 - Примеры ошибок
PVS-Studio в 2021 - Примеры ошибокPVS-Studio в 2021 - Примеры ошибок
PVS-Studio в 2021 - Примеры ошибок
 
PVS-Studio в 2021
PVS-Studio в 2021PVS-Studio в 2021
PVS-Studio в 2021
 
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
 
Best Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' Mistakes
 
Does static analysis need machine learning?
Does static analysis need machine learning?Does static analysis need machine learning?
Does static analysis need machine learning?
 
Typical errors in code on the example of C++, C#, and Java
Typical errors in code on the example of C++, C#, and JavaTypical errors in code on the example of C++, C#, and Java
Typical errors in code on the example of C++, C#, and Java
 
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
 
Game Engine Code Quality: Is Everything Really That Bad?
Game Engine Code Quality: Is Everything Really That Bad?Game Engine Code Quality: Is Everything Really That Bad?
Game Engine Code Quality: Is Everything Really That Bad?
 
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
The Use of Static Code Analysis When Teaching or Developing Open-Source SoftwareThe Use of Static Code Analysis When Teaching or Developing Open-Source Software
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
 
Static Code Analysis for Projects, Built on Unreal Engine
Static Code Analysis for Projects, Built on Unreal EngineStatic Code Analysis for Projects, Built on Unreal Engine
Static Code Analysis for Projects, Built on Unreal Engine
 
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Safety on the Max: How to Write Reliable C/C++ Code for Embedded SystemsSafety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
 
The Great and Mighty C++
The Great and Mighty C++The Great and Mighty C++
The Great and Mighty C++
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?
 
Zero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for youZero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for you
 
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOpsPVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
 

Kürzlich hochgeladen

PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 

Kürzlich hochgeladen (20)

PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 

64-bit Errors in Games

  • 1. Patterns of 64-bit errors in games Andrey Karpov karpov@viva64.com
  • 2.
  • 3. Speaker • Karpov Andrey Nikolaevich • Habr profile: habr.com/users/Andrey2008/ • Microsoft MVP, Intel Black Belt Software Developer • One of the founders of the PVS-Studio project, which started right from the search of 64-bit errors • https://www.viva64.com
  • 4. Technically speaking, there are no «64-bit errors»
  • 5. In practice • 64-bit errors are the ones which reveal themselves after building a 64-bit application • By the way, many of them do it only when the 4 Gigabyte limit is exceeded
  • 6. Explanation on the example of malloc • Who will notice an error?
  • 7. void Foo() { const size_t Gigabyte = 1073741824; void *A[10]; for (size_t i = 0; i < 10; ++i) { A[i] = malloc(Gigabyte); printf("%pn", A[i]); } for (size_t i = 0; i < 10; ++i) free(A[i]); }
  • 8.
  • 9. In C-code developers forget to include headers • Your program might not contain calls of malloc, but they might be in third-party libraries • It is revealed only when consuming large amount of memory #include <stdio.h> #include <string.h> //#include <stdlib.h>
  • 10. The case isn’t only about malloc • Fennec Media • memset • memcpy • Ffdshow (media decoder) • memset • memcpy • libZPlay • strlen
  • 11. Let’s get back to implicit truncation of 64-bit values
  • 12. long type in Win64 const std::vector<Vec2> &vertexList = body->getCalculatedVertexList(); unsigned long length = vertexList.size(); PVS-Studio: V103 Implicit type conversion from memsize to 32-bit type. CCArmature.cpp 614 Cocos2d-x
  • 13. int type is a bad idea in any case PVS-Studio: V104 Implicit conversion of 'i' to memsize type in an arithmetic expression: i < points_.size() sweep_context.cc 76 std::vector<Point*> points_; void SweepContext::InitTriangulation() { // Calculate bounds. for (unsigned int i = 0; i < points_.size(); i++) { Point& p = *points_[i]; .... Cocos2d-x
  • 14. By the way, such errors are more insidious than it looks • When overflowing int, undefined behavior occurs • UB might be the case when everything works correctly :) • More details: "Undefined behavior is closer than you think" https://www.viva64.com/ru/b/0374/ size_t N = foo(); for (int i = 0; i < N; ++i)
  • 15. Usage of correct memsize-types
  • 16. "memsize" types • ptrdiff_t - signed type (distance between pointers) • size_t – maximum size of theoretically possible object of any type, including arrays • rsize_t – size of a single object • intptr_t - signed type, can store a pointer • uintptr_t – unsigned type, can store a pointer
  • 17. Usage of memsize-types • Number of elements in an array • Buffer size • Counters • Pointer storing (although, it’s better not to do it) • Pointer arithmetic • And so on
  • 18. Using of memsize-types: bad thing PVS-Studio: V109 Implicit type conversion of return value 'a * b * c' to memsize type. test.cpp 22 Potential overflow size_t Foo(int a, int b, int c) { return a * b * c; }
  • 19. Using of memsize-types: OK size_t Foo(int a, int b, int c) { return static_cast<size_t>(a) * static_cast<size_t>(b) * static_cast<size_t>(c); } size_t Foo(int a, int b, int c) { return static_cast<size_t>(a) * b * c; }
  • 20. JUST DON’T DO IT! std::vector<unsigned char> buf( static_cast<size_t>(exr_image->width * h * pixel_data_size)); PVS-Studio: V1028 CWE-190 Possible overflow. Consider casting operands, not the result. tinyexr.h 11682 Possible overflow again TinyEXR
  • 21. This way errors only hide more if (components == 1) { images[0].resize(static_cast<size_t>(width * height)); memcpy(images[0].data(), data, sizeof(float) * size_t(width * height)); } else { images[0].resize(static_cast<size_t>(width * height)); images[1].resize(static_cast<size_t>(width * height)); images[2].resize(static_cast<size_t>(width * height)); images[3].resize(static_cast<size_t>(width * height)); for (size_t i = 0; i < static_cast<size_t>(width * height); i++) { TinyEXR
  • 22. More about pointer arithmetic int A = -2; unsigned B = 1; int array[5] = { 1, 2, 3, 4, 5 }; int *ptr = array + 3; ptr = ptr + (A + B); printf("%in", *ptr); //Access violation //on 64-bit platform
  • 23. Developers forget to expand 32-bit types
  • 24. int64_t X = 1 << N • This code doesn’t work in 32-bit programs as well • Setting most significant bits is usually not needed there
  • 25. class FMallocBinned : public FMalloc { uint64 PoolMask; .... FMallocBinned(uint32 InPageSize, uint64 AddressLimit) { .... PoolMask = (( 1 << ( HashKeyShift - PoolBitShift ) ) - 1); .... } }; Unreal Engine 4 PVS-Studio: V629 Consider inspecting the '1 << (HashKeyShift - PoolBitShift)' expression. Bit shifting of the 32-bit value with a subsequent expansion to the 64- bit type. mallocbinned.h 800
  • 26. class FMallocBinned : public FMalloc { uint64 PoolMask; .... FMallocBinned(uint32 InPageSize, uint64 AddressLimit) { .... PoolMask = (( 1ULL << ( HashKeyShift - PoolBitShift ) ) - 1); .... } }; Unreal Engine 4
  • 27. Godot Engine void vp9_adjust_mask(....) { .... const uint64_t columns = cm->mi_cols - mi_col; const uint64_t mask_y = (((1 << columns) - 1)) * 0x0101010101010101ULL; .... } PVS-Studio: V629 CWE-190 Consider inspecting the '1 << columns' expression. Bit shifting of the 32-bit value with a subsequent expansion to the 64-bit type. vp9_loopfilter.c 851
  • 28. size_t N = ~0U; • In 32-bit code everything is OK • 64-bit code, expectation: 0xFFFFFFFFFFFFFFFF • 64-bit code, reality: 0x00000000FFFFFFFF
  • 29. size_t nRenderMeshSize = ~0U; PVS-Studio: V101 Implicit assignment type conversion to memsize type. StatObjLoad.cpp 1369 CryEngine
  • 30. size_t N = ~(1024u - 1); • In 32-bit code everything is OK • 64-bit code, expectation: 0xFFFFFFFFFFFFFC00 • 64-bit code, reality: 0x00000000FFFFFC00
  • 31. static const UINT_PTR SmallBlockAlignMask = ~(SmallBlockLength - 1); FreeBlockHeader* InsertFreeBlock(FreeBlockHeader* after, UINT_PTR start, UINT_PTR end) { bool isFreeBlockDone = (start & SmallBlockAlignMask) == (end & SmallBlockAlignMask) || (start > end - (SmallBlockLength / 2)); .... } CryEngine
  • 32. Increased consumption of the stack and dynamic memory
  • 33. Stack • I suggest taking it easy and just increase it by 2 times :)
  • 34. Dynamic memory • Is it worth doing some fighting? • Are 64-bit integer types needed everywhere? • Are the pointers needed everywhere? • Structures’ size
  • 35. Redundant growth of structures’ size struct Candidate { uint32_t face; ChartBuildData *chart; float metric; }; PVS-Studio: V802 On 64-bit platform, structure size can be reduced from 24 to 16 bytes by rearranging the fields according to their sizes in decreasing order. xatlas.cpp 5237 24 bytes
  • 36. Redundant growth of structures’ size struct Candidate { ChartBuildData *chart; uint32_t face; float metric; }; 16 bytes
  • 38. Magic constants • 1024 x 768 • 1600 x 1200
  • 39. Magic constants • 1024 x 768 • 1600 x 1200 • 4 • 32 • 0x7FFFFFFF • 0x80000000 • 0xFFFFFFFF
  • 40. Dangerous tricks with type casting ResourceBase::Header *header = (*iter).value; char fourCC[ 5 ]; *( ( U32* ) fourCC ) = header->getSignature(); fourCC[ 4 ] = 0; Everything will be fine thanks to header, but still very unstable. PVS-Studio: V1032 The pointer 'fourCC' is cast to a more strictly aligned pointer type. resourceManager.cpp 127 Torque 3D
  • 41. Data serialization • No specific errors and tips • Review the code
  • 45. How to write a 64-bit program of high-quality?
  • 46. Specify the code standard • Use correct types: ptrdiff_t, size_t, intptr_t, uintptr_t, INT_PTR, DWORD_PTR и т.д. • Say «NO!» to magic numbers. Use this: UINT_MAX, ULONG_MAX, std::numeric_limits<size_t>::max() и т.д. • Explain how to correctly use the explicit type casting in the expressions and what’s the difference between 1 and static_cast<size_t>(1) • unsigned long != size_t
  • 50. Conclusion • Even if you already have released a 64-bit application, this doesn’t mean that it works correctly • A 64-bit error can lie low for ages • How to solve the situation: • learning • Code standard • Static code analysis
  • 51. Useful links • Lessons on development of 64-bit C/C++ applications (single file) https://www.viva64.com/ru/l/full/ • A Collection of Examples of 64-bit Errors in Real Programs https://www.viva64.com/ru/a/0065/ • Undefined behavior is closer than you think https://www.viva64.com/ru/b/0374/
  • 52. Time for your questions! Andrey Karpov karpov@viva64.com www.viva64.com