This document discusses common C++ bugs and tools to find them. It describes various types of memory access bugs like buffer overflows on the stack, heap, and globals that can lead to crashes or security vulnerabilities. Threading bugs like data races, deadlocks, and race conditions on object destruction are also covered. Other undefined behaviors like initialization order issues, lack of sequence points, and integer overflows are explained. The document provides examples of each type of bug and quizzes the reader to find bugs in a code sample. It recommends resources for further reading on debugging techniques and thread sanitizers that can detect races and data races.
1 of 43
More Related Content
20140531 serebryany lecture01_fantastic_cpp_bugs
1. [2/2] Find scary C++ bugs
before they find you
Konstantin Serebryany, Google
May 2014 @compsciclub.ru
[1/2] Fantastic C++ Bugs
and Where to Find Them
2. Agenda
● How bad the bugs are?
● Most common C++ bugs
○ Memory access bugs
○ Threading bugs
○ Other undefined behavior bugs
● Quiz
● Lecture 2/2: tools that find bugs
3. Why bugs are scary
● Increased development cost
● Increased CPU/RAM consumption
● Decreased user satisfaction
● May cost money or even lives
● Security!
4. Undefined Behavior (UB)
● UB != undefined result
● UB: the program may misbehave depending
on compiler, hardware, system load, current
date, outside temperature, etc
● UB: the program may turn hostile to the host
system or launch nuclear missiles
6. Typical Address Space Layout
Stack
Heap
Globals
Constants (RO)
Code
NULL Page
Malloc Header
User Chunk
Malloc Header
User Chunk
...
Return Address
Local Variables
Return Address
Local Variables
...
Global Variable
Global Variable
...
7. Virtual Function Table (VPTR)
class Foo {
public:
virtual void f1();
virtual void f2();
private:
int data1;
int data2;
};
VPTR
data1
data2
f1
f2
8. Buffer overflow (global, heap, stack)
● Access invalid memory
○ SEGV (Good!!)
● Access some other object in memory
○ Read garbage or corrupt data
■ Subvert further execution
○ Leak private data or memory layout
○ Overwrite function pointers or VPTR
int ar[10];
… ar[i]…
9. Buffer overflow (stack)
int foo(int i) {
int ar[10];
… ar[i] …
● May access the return address and
call arbitrary code
10. Buffer overflow (stack)
void bad() {
std::cout << "I am BADn";
}
int main() {
long a, b, ar[10];
std::cin >> a >> b;
ar[a] = b;
}
11. Buffer overflow (heap)
int foo(int i) {
int *ar = new int [10];
… ar[i] ...
● Access malloc header
○ Crash later in new/delete
○ Deallocate wrong amount of memory
12. Buffer overflow (heap)
void good() {
cout << "I am goodn";
}
void bad() {
cout << "I am BADn";
}
typedef void (*F)(void);
struct Object {
Object(F f) : f_(f) {}
F f_;
};
int main() {
long a, b;
long *x = new long[1];
Object *o = new Object(good);
cin >> a >> b;
x[a] = b;
o->f_();
}
13. Erroneous type cast
struct Base { int data; };
struct Derived : Base { int more_data; };
Base b;
int main() {
Derived *d = (Derived*)&b;
d->more_data = 0; // OOPS
}
14. Use-after-free (heap)
● Even worse than heap-buffer-overflow
because touches arbitrary part of heap
int *x = new int[10];
delete [] x;
x[5] = 0;
15. Use-after-free: privilege escalation
struct Thing { bool has_access; };
int main() {
Thing *security_check = new Thing;
security_check->has_access = false;
delete security_check;
int *x = new int(42);
if (security_check->has_access) // OOPS
cout << "Access Grantedn";
}
16. Use-after-return (stack)
● Relatively rare, but
combines the worst of
heap-use-after-free and
stack-buffer-overflow
int *x;
void foo() {
int local;
x = &local;
}
*x = ...
18. Use-after-scope (stack)
int *p;
if (...) {
int a;
p = &a;
}
if (...) {
int b[100];
*p = … // oops
}
● Behavior depends on
compiler version, flags,
function size, etc
20. Memory leaks, other resource leaks
● Excessive memory consumption
● [D]DOS attacks
void foo() {
int *x = new int [10];
if (...) return;
delete [] x;
}
21. Use of uninitialized memory
● Reading garbage from
heap or stack
● Results change from
run-to run
● Values could be
controlled by attacker
void foo() {
int x[10];
if (x[5])
Something();
}
22. Use after destruction
struct Foo {
void set(string *s) {
s_ = s;
}
~Foo () {
cout << *s_ << endl;
}
string *s_;
};
struct Bar {
Foo foo;
string s;
};
int main() {
Bar b;
b.s = "hello world";
b.foo.set(&b.s);
}
26. Data Races
int var;
void Thread1()
{ var--; }
void Thread2()
{ var++; }
● Two accesses to the same
memory location
● At least one is a store
● No happens-before relation
(no explicit synchronization)
27. Race on a bitfield
struct Foo {
int a : 20;
int b : 12;
};
Foo foo;
void Thread1() {
foo.a++;
}
void Thread2() {
foo.b++;
}
28. Race During Destruction
std::set<int> s; // Global variable
void Thread() {
for (int i = 0; i < 1000000; i++)
s.insert(rand());
}
int main() { new std::thread(Thread); }
29. struct A {
virtual ...
};
struct B : public A {
virtual ...
};
B b;
● ‘A’ is constructed
○ VPTR = A::VPTR
● ‘B’ is constructed
○ VPTR = B::VPTR
● ‘B’ is destroyed
○ VPTR = B::VPTR
● ‘A’ is destroyed
○ VPTR = A::VPTR
VPTR: construction order
37. Init Order Fiasco
// in a.cc
int foo();
int X = foo();
// in b.cc
int Y = X;
int foo() {
return 42;
}
38. ODR (one definition rule) Violation
// in a.cc/a.so
int X;
// in b.cc/b.so
double X;
39. Lack of Sequence Point
int i = 0;
i = ++i + i++;
// What is i?
std::map<int> m;
m[10] = m.size();
// What is m[10]?
● Clang and GCC will
give different
answers (GOOD!)
40. Integer Overflow
Remember:
UB != undefined result
void f (int *array) {
int val = 0x03020100;
for(int i = 0; i < 64; i++) {
array[i] = val;
// Overflow when i==63
val += 0x04040404;
}
}
41. Some more...
● Shift by oversized or negative value
● Missing return statement
● Infinite loops
● ...
42. Quiz: find all bugs
#include <thread> // C++11
int main() {
int *a = new int[4];
int *b = new int[4];
std::thread t{[&](){b++;}};
delete a;
t.detach();
return *a + (*++b) + b[3];
}