SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
[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
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
Why bugs are scary
● Increased development cost
● Increased CPU/RAM consumption
● Decreased user satisfaction
● May cost money or even lives
● Security!
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
Memory access bugs
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
...
Virtual Function Table (VPTR)
class Foo {
public:
virtual void f1();
virtual void f2();
private:
int data1;
int data2;
};
VPTR
data1
data2
f1
f2
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]…
Buffer overflow (stack)
int foo(int i) {
int ar[10];
… ar[i] …
● May access the return address and
call arbitrary code
Buffer overflow (stack)
void bad() {
std::cout << "I am BADn";
}
int main() {
long a, b, ar[10];
std::cin >> a >> b;
ar[a] = b;
}
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
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_();
}
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
}
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;
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";
}
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 = ...
stack-u-a-r: VPTR replacement
struct Foo {
virtual void good() {
cout << "goodn";
}};
struct Bar {
virtual void bad() {
cout << "BADn";
}};
long **p;
void leak() {
long *local[10];
p = &local[0]; };
void use(Foo *foo) {
*p[5] -= 0x20;
foo->good(); }
void oops() {
Foo foo;
use(&foo); }
int main() {leak(); oops();}
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
[De]allocation bugs
● Double-free
● Invalid free
● “new []” vs “delete”
Memory leaks, other resource leaks
● Excessive memory consumption
● [D]DOS attacks
void foo() {
int *x = new int [10];
if (...) return;
delete [] x;
}
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();
}
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);
}
Why SEGV?
● NULL dereference
● Buffer overflow
● Use-after-free
● Read from uninitialized pointer
● Stack overflow
Threading bugs
Mutex Deadlock
void Thread1() {
mu1.lock()
mu2.lock()
mu2.unlock()
mu1.unlock()
}
void Thread2() {
mu2.lock()
mu1.lock()
mu2.unlock()
mu1.unlock()
}
std::mutex mu1, mu2;
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)
Race on a bitfield
struct Foo {
int a : 20;
int b : 12;
};
Foo foo;
void Thread1() {
foo.a++;
}
void Thread2() {
foo.b++;
}
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); }
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
Race on VPTR
struct A {
A() : done_(false) {}
virtual void F() { printf("A::Fn"); }
void Done() {
std::unique_lock<std::mutex> lk(m_);
done_ = true;
cv_.notify_one();
}
virtual ~A() { // Wait for Done()
std::unique_lock<std::mutex> lk(m_);
cv_.wait(lk, [this] {return done_;});
}
private:
std::mutex m_; std::condition_variable cv_; bool done_;
};
Race on VPTR (cont)
class B : public A {
public:
virtual void F() { printf("B::Fn"); }
virtual ~B() {}
};
int main() {
A *a = new B;
std::thread t1([a] {a->F(); a->Done();});
std::thread t2([a] {delete a;});
t1.join(); t2.join();
}
Atomicity violationstruct ProtectedVector {
bool empty() { // Protected by m_
std::lock_guard<std::mutex> g(m_);
return v_.empty();
}
void pop_back() { // Protected by m_
std::lock_guard<std::mutex> g(m_);
v_.pop_back();
} ...
private:
std::mutex m_;
std::vector<int> v_;
};
Atomicity violation (cont)
ProtectedVector v;
void Thread1() {
if (!v.empty())
v.pop_back();
}
void Thread2() {
if (!v.empty())
v.pop_back();
}
Threading bugs cause memory bugs
● Racey use-after-free
● Race on reference counter
○ double-free
○ leak
Async-signal safety
● No real threading
● But similar to races
● malloc is not safe in
signal handlers!
struct Node {
Node *next, *prev;
};
Node *head;
void PushFront() {
Node *n = new Node;
n->next = head;
n->prev = NULL;
if(head) head->prev = n;
head = n;
}
void SignalHandler(...) {
assert(!head || !head->prev);
}
Other Undefined Behavior
Init Order Fiasco
// in a.cc
int foo();
int X = foo();
// in b.cc
int Y = X;
int foo() {
return 42;
}
ODR (one definition rule) Violation
// in a.cc/a.so
int X;
// in b.cc/b.so
double X;
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!)
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;
}
}
Some more...
● Shift by oversized or negative value
● Missing return statement
● Infinite loops
● ...
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];
}
Links
● https://en.wikipedia.org/wiki/Buffer_overflow
● https://en.wikipedia.org/wiki/Dangling_pointer
● http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
● https://code.google.com/p/thread-sanitizer/wiki/AboutRaces
● (*) http://people.freebsd.org/~lstewart/articles/cpumemory.pdf
● (*) https://www.usenix.org/legacy/event/hotpar11/tech/final_files/Boehm.pdf

Weitere ähnliche Inhalte

Was ist angesagt?

Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency APISeok-joon Yun
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++vidyamittal
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Sergey Platonov
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321Teddy Hsiung
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Platonov Sergey
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itSergey Platonov
 
WebAssembly向け多倍長演算の実装
WebAssembly向け多倍長演算の実装WebAssembly向け多倍長演算の実装
WebAssembly向け多倍長演算の実装MITSUNARI Shigeo
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPMiller Lee
 
Антон Бикинеев, Writing good std::future&lt; C++ >
Антон Бикинеев, Writing good std::future&lt; C++ >Антон Бикинеев, Writing good std::future&lt; C++ >
Антон Бикинеев, Writing good std::future&lt; C++ >Sergey Platonov
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnMoriyoshi Koizumi
 
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingDemetrio Siragusa
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereSergey Platonov
 
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
 
Let’s talk about microbenchmarking
Let’s talk about microbenchmarkingLet’s talk about microbenchmarking
Let’s talk about microbenchmarkingAndrey Akinshin
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineMatt Provost
 

Was ist angesagt? (20)

Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
 
AA-sort with SSE4.1
AA-sort with SSE4.1AA-sort with SSE4.1
AA-sort with SSE4.1
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
 
WebAssembly向け多倍長演算の実装
WebAssembly向け多倍長演算の実装WebAssembly向け多倍長演算の実装
WebAssembly向け多倍長演算の実装
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
 
Антон Бикинеев, Writing good std::future&lt; C++ >
Антон Бикинеев, Writing good std::future&lt; C++ >Антон Бикинеев, Writing good std::future&lt; C++ >
Антон Бикинеев, Writing good std::future&lt; C++ >
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 Autumn
 
Clang tidy
Clang tidyClang tidy
Clang tidy
 
Mintz q207
Mintz q207Mintz q207
Mintz q207
 
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhere
 
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
 
Let’s talk about microbenchmarking
Let’s talk about microbenchmarkingLet’s talk about microbenchmarking
Let’s talk about microbenchmarking
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 

Andere mochten auch

Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...Computer Science Club
 
Знакомьтесь, Kotlin
Знакомьтесь, KotlinЗнакомьтесь, Kotlin
Знакомьтесь, KotlinTech Talks @NSU
 
Android opetuksessa 11.9.14
Android opetuksessa 11.9.14Android opetuksessa 11.9.14
Android opetuksessa 11.9.14Matleena Laakso
 
Infinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using KotlinInfinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using KotlinInfinum
 
Do Languages Matter?
Do Languages Matter?Do Languages Matter?
Do Languages Matter?Bruce Eckel
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1Mukesh Kumar
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersBartosz Kosarzycki
 
Kotlin в production. Как и зачем?
Kotlin в production. Как и зачем?Kotlin в production. Как и зачем?
Kotlin в production. Как и зачем?DotNetConf
 
JavaOne 2016 - Kotlin: The Language of The Future For JVM?
JavaOne 2016 - Kotlin: The Language of The Future For JVM?JavaOne 2016 - Kotlin: The Language of The Future For JVM?
JavaOne 2016 - Kotlin: The Language of The Future For JVM?Leonardo Zanivan
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?intelliyole
 
TMPA-2015: Kotlin: From Null Dereference to Smart Casts
TMPA-2015: Kotlin: From Null Dereference to Smart CastsTMPA-2015: Kotlin: From Null Dereference to Smart Casts
TMPA-2015: Kotlin: From Null Dereference to Smart CastsIosif Itkin
 
Scala in practice
Scala in practiceScala in practice
Scala in practiceTomer Gabel
 
[Expert Fridays] Java MeetUp - Борис Ташкулов (Teamlead Enterprise): "Почему ...
[Expert Fridays] Java MeetUp - Борис Ташкулов (Teamlead Enterprise): "Почему ...[Expert Fridays] Java MeetUp - Борис Ташкулов (Teamlead Enterprise): "Почему ...
[Expert Fridays] Java MeetUp - Борис Ташкулов (Teamlead Enterprise): "Почему ...Provectus
 
Who's More Functional: Kotlin, Groovy, Scala, or Java?
Who's More Functional: Kotlin, Groovy, Scala, or Java?Who's More Functional: Kotlin, Groovy, Scala, or Java?
Who's More Functional: Kotlin, Groovy, Scala, or Java?Andrey Breslav
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Scott Wlaschin
 

Andere mochten auch (19)

Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
 
Знакомьтесь, Kotlin
Знакомьтесь, KotlinЗнакомьтесь, Kotlin
Знакомьтесь, Kotlin
 
Scala
ScalaScala
Scala
 
Android opetuksessa 11.9.14
Android opetuksessa 11.9.14Android opetuksessa 11.9.14
Android opetuksessa 11.9.14
 
Scala Day by Day
Scala Day by DayScala Day by Day
Scala Day by Day
 
Kotlin Overview
Kotlin OverviewKotlin Overview
Kotlin Overview
 
Infinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using KotlinInfinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using Kotlin
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
 
Do Languages Matter?
Do Languages Matter?Do Languages Matter?
Do Languages Matter?
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Kotlin в production. Как и зачем?
Kotlin в production. Как и зачем?Kotlin в production. Как и зачем?
Kotlin в production. Как и зачем?
 
JavaOne 2016 - Kotlin: The Language of The Future For JVM?
JavaOne 2016 - Kotlin: The Language of The Future For JVM?JavaOne 2016 - Kotlin: The Language of The Future For JVM?
JavaOne 2016 - Kotlin: The Language of The Future For JVM?
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
TMPA-2015: Kotlin: From Null Dereference to Smart Casts
TMPA-2015: Kotlin: From Null Dereference to Smart CastsTMPA-2015: Kotlin: From Null Dereference to Smart Casts
TMPA-2015: Kotlin: From Null Dereference to Smart Casts
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
[Expert Fridays] Java MeetUp - Борис Ташкулов (Teamlead Enterprise): "Почему ...
[Expert Fridays] Java MeetUp - Борис Ташкулов (Teamlead Enterprise): "Почему ...[Expert Fridays] Java MeetUp - Борис Ташкулов (Teamlead Enterprise): "Почему ...
[Expert Fridays] Java MeetUp - Борис Ташкулов (Teamlead Enterprise): "Почему ...
 
Who's More Functional: Kotlin, Groovy, Scala, or Java?
Who's More Functional: Kotlin, Groovy, Scala, or Java?Who's More Functional: Kotlin, Groovy, Scala, or Java?
Who's More Functional: Kotlin, Groovy, Scala, or Java?
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 

Ähnlich wie 20140531 serebryany lecture01_fantastic_cpp_bugs

Tema3_Introduction_to_CUDA_C.pdf
Tema3_Introduction_to_CUDA_C.pdfTema3_Introduction_to_CUDA_C.pdf
Tema3_Introduction_to_CUDA_C.pdfpepe464163
 
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Rust: код может быть одновременно безопасным и быстрым, Степан КольцовRust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Rust: код может быть одновременно безопасным и быстрым, Степан КольцовYandex
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Yandex
 
Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelVitaly Nikolenko
 
Introduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : NotesIntroduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : NotesSubhajit Sahu
 
DesignPatterns-IntroPresentation.pptx
DesignPatterns-IntroPresentation.pptxDesignPatterns-IntroPresentation.pptx
DesignPatterns-IntroPresentation.pptxMariusIoacara2
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptKamil Toman
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsAzul Systems, Inc.
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?勇浩 赖
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
Cross Platform App Development with C++
Cross Platform App Development with C++Cross Platform App Development with C++
Cross Platform App Development with C++Joan Puig Sanz
 
2011.02.18 marco parenzan - modelli di programmazione per le gpu
2011.02.18   marco parenzan - modelli di programmazione per le gpu2011.02.18   marco parenzan - modelli di programmazione per le gpu
2011.02.18 marco parenzan - modelli di programmazione per le gpuMarco Parenzan
 
Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Jigar Maheshwari
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20DefconRussia
 
FreeBSD and Drivers
FreeBSD and DriversFreeBSD and Drivers
FreeBSD and DriversKernel TLV
 

Ähnlich wie 20140531 serebryany lecture01_fantastic_cpp_bugs (20)

Tema3_Introduction_to_CUDA_C.pdf
Tema3_Introduction_to_CUDA_C.pdfTema3_Introduction_to_CUDA_C.pdf
Tema3_Introduction_to_CUDA_C.pdf
 
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Rust: код может быть одновременно безопасным и быстрым, Степан КольцовRust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
 
Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernel
 
Introduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : NotesIntroduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : Notes
 
DesignPatterns-IntroPresentation.pptx
DesignPatterns-IntroPresentation.pptxDesignPatterns-IntroPresentation.pptx
DesignPatterns-IntroPresentation.pptx
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM Mechanics
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
Cross Platform App Development with C++
Cross Platform App Development with C++Cross Platform App Development with C++
Cross Platform App Development with C++
 
2011.02.18 marco parenzan - modelli di programmazione per le gpu
2011.02.18   marco parenzan - modelli di programmazione per le gpu2011.02.18   marco parenzan - modelli di programmazione per le gpu
2011.02.18 marco parenzan - modelli di programmazione per le gpu
 
Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)
 
Why learn Internals?
Why learn Internals?Why learn Internals?
Why learn Internals?
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20
 
FreeBSD and Drivers
FreeBSD and DriversFreeBSD and Drivers
FreeBSD and Drivers
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 

Mehr von Computer Science Club

20140511 parallel programming_kalishenko_lecture12
20140511 parallel programming_kalishenko_lecture1220140511 parallel programming_kalishenko_lecture12
20140511 parallel programming_kalishenko_lecture12Computer Science Club
 
20140427 parallel programming_zlobin_lecture11
20140427 parallel programming_zlobin_lecture1120140427 parallel programming_zlobin_lecture11
20140427 parallel programming_zlobin_lecture11Computer Science Club
 
20140420 parallel programming_kalishenko_lecture10
20140420 parallel programming_kalishenko_lecture1020140420 parallel programming_kalishenko_lecture10
20140420 parallel programming_kalishenko_lecture10Computer Science Club
 
20140413 parallel programming_kalishenko_lecture09
20140413 parallel programming_kalishenko_lecture0920140413 parallel programming_kalishenko_lecture09
20140413 parallel programming_kalishenko_lecture09Computer Science Club
 
20140329 graph drawing_dainiak_lecture02
20140329 graph drawing_dainiak_lecture0220140329 graph drawing_dainiak_lecture02
20140329 graph drawing_dainiak_lecture02Computer Science Club
 
20140329 graph drawing_dainiak_lecture01
20140329 graph drawing_dainiak_lecture0120140329 graph drawing_dainiak_lecture01
20140329 graph drawing_dainiak_lecture01Computer Science Club
 
20140310 parallel programming_kalishenko_lecture03-04
20140310 parallel programming_kalishenko_lecture03-0420140310 parallel programming_kalishenko_lecture03-04
20140310 parallel programming_kalishenko_lecture03-04Computer Science Club
 
20140216 parallel programming_kalishenko_lecture01
20140216 parallel programming_kalishenko_lecture0120140216 parallel programming_kalishenko_lecture01
20140216 parallel programming_kalishenko_lecture01Computer Science Club
 
20130928 automated theorem_proving_harrison
20130928 automated theorem_proving_harrison20130928 automated theorem_proving_harrison
20130928 automated theorem_proving_harrisonComputer Science Club
 

Mehr von Computer Science Club (20)

20141223 kuznetsov distributed
20141223 kuznetsov distributed20141223 kuznetsov distributed
20141223 kuznetsov distributed
 
Computer Vision
Computer VisionComputer Vision
Computer Vision
 
20140511 parallel programming_kalishenko_lecture12
20140511 parallel programming_kalishenko_lecture1220140511 parallel programming_kalishenko_lecture12
20140511 parallel programming_kalishenko_lecture12
 
20140427 parallel programming_zlobin_lecture11
20140427 parallel programming_zlobin_lecture1120140427 parallel programming_zlobin_lecture11
20140427 parallel programming_zlobin_lecture11
 
20140420 parallel programming_kalishenko_lecture10
20140420 parallel programming_kalishenko_lecture1020140420 parallel programming_kalishenko_lecture10
20140420 parallel programming_kalishenko_lecture10
 
20140413 parallel programming_kalishenko_lecture09
20140413 parallel programming_kalishenko_lecture0920140413 parallel programming_kalishenko_lecture09
20140413 parallel programming_kalishenko_lecture09
 
20140329 graph drawing_dainiak_lecture02
20140329 graph drawing_dainiak_lecture0220140329 graph drawing_dainiak_lecture02
20140329 graph drawing_dainiak_lecture02
 
20140329 graph drawing_dainiak_lecture01
20140329 graph drawing_dainiak_lecture0120140329 graph drawing_dainiak_lecture01
20140329 graph drawing_dainiak_lecture01
 
20140310 parallel programming_kalishenko_lecture03-04
20140310 parallel programming_kalishenko_lecture03-0420140310 parallel programming_kalishenko_lecture03-04
20140310 parallel programming_kalishenko_lecture03-04
 
20140223-SuffixTrees-lecture01-03
20140223-SuffixTrees-lecture01-0320140223-SuffixTrees-lecture01-03
20140223-SuffixTrees-lecture01-03
 
20140216 parallel programming_kalishenko_lecture01
20140216 parallel programming_kalishenko_lecture0120140216 parallel programming_kalishenko_lecture01
20140216 parallel programming_kalishenko_lecture01
 
20131106 h10 lecture6_matiyasevich
20131106 h10 lecture6_matiyasevich20131106 h10 lecture6_matiyasevich
20131106 h10 lecture6_matiyasevich
 
20131027 h10 lecture5_matiyasevich
20131027 h10 lecture5_matiyasevich20131027 h10 lecture5_matiyasevich
20131027 h10 lecture5_matiyasevich
 
20131027 h10 lecture5_matiyasevich
20131027 h10 lecture5_matiyasevich20131027 h10 lecture5_matiyasevich
20131027 h10 lecture5_matiyasevich
 
20131013 h10 lecture4_matiyasevich
20131013 h10 lecture4_matiyasevich20131013 h10 lecture4_matiyasevich
20131013 h10 lecture4_matiyasevich
 
20131006 h10 lecture3_matiyasevich
20131006 h10 lecture3_matiyasevich20131006 h10 lecture3_matiyasevich
20131006 h10 lecture3_matiyasevich
 
20131006 h10 lecture3_matiyasevich
20131006 h10 lecture3_matiyasevich20131006 h10 lecture3_matiyasevich
20131006 h10 lecture3_matiyasevich
 
20131006 h10 lecture2_matiyasevich
20131006 h10 lecture2_matiyasevich20131006 h10 lecture2_matiyasevich
20131006 h10 lecture2_matiyasevich
 
20130922 h10 lecture1_matiyasevich
20130922 h10 lecture1_matiyasevich20130922 h10 lecture1_matiyasevich
20130922 h10 lecture1_matiyasevich
 
20130928 automated theorem_proving_harrison
20130928 automated theorem_proving_harrison20130928 automated theorem_proving_harrison
20130928 automated theorem_proving_harrison
 

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 = ...
  • 17. stack-u-a-r: VPTR replacement struct Foo { virtual void good() { cout << "goodn"; }}; struct Bar { virtual void bad() { cout << "BADn"; }}; long **p; void leak() { long *local[10]; p = &local[0]; }; void use(Foo *foo) { *p[5] -= 0x20; foo->good(); } void oops() { Foo foo; use(&foo); } int main() {leak(); oops();}
  • 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
  • 19. [De]allocation bugs ● Double-free ● Invalid free ● “new []” vs “delete”
  • 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); }
  • 23. Why SEGV? ● NULL dereference ● Buffer overflow ● Use-after-free ● Read from uninitialized pointer ● Stack overflow
  • 25. Mutex Deadlock void Thread1() { mu1.lock() mu2.lock() mu2.unlock() mu1.unlock() } void Thread2() { mu2.lock() mu1.lock() mu2.unlock() mu1.unlock() } std::mutex mu1, mu2;
  • 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
  • 30. Race on VPTR struct A { A() : done_(false) {} virtual void F() { printf("A::Fn"); } void Done() { std::unique_lock<std::mutex> lk(m_); done_ = true; cv_.notify_one(); } virtual ~A() { // Wait for Done() std::unique_lock<std::mutex> lk(m_); cv_.wait(lk, [this] {return done_;}); } private: std::mutex m_; std::condition_variable cv_; bool done_; };
  • 31. Race on VPTR (cont) class B : public A { public: virtual void F() { printf("B::Fn"); } virtual ~B() {} }; int main() { A *a = new B; std::thread t1([a] {a->F(); a->Done();}); std::thread t2([a] {delete a;}); t1.join(); t2.join(); }
  • 32. Atomicity violationstruct ProtectedVector { bool empty() { // Protected by m_ std::lock_guard<std::mutex> g(m_); return v_.empty(); } void pop_back() { // Protected by m_ std::lock_guard<std::mutex> g(m_); v_.pop_back(); } ... private: std::mutex m_; std::vector<int> v_; };
  • 33. Atomicity violation (cont) ProtectedVector v; void Thread1() { if (!v.empty()) v.pop_back(); } void Thread2() { if (!v.empty()) v.pop_back(); }
  • 34. Threading bugs cause memory bugs ● Racey use-after-free ● Race on reference counter ○ double-free ○ leak
  • 35. Async-signal safety ● No real threading ● But similar to races ● malloc is not safe in signal handlers! struct Node { Node *next, *prev; }; Node *head; void PushFront() { Node *n = new Node; n->next = head; n->prev = NULL; if(head) head->prev = n; head = n; } void SignalHandler(...) { assert(!head || !head->prev); }
  • 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]; }
  • 43. Links ● https://en.wikipedia.org/wiki/Buffer_overflow ● https://en.wikipedia.org/wiki/Dangling_pointer ● http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html ● https://code.google.com/p/thread-sanitizer/wiki/AboutRaces ● (*) http://people.freebsd.org/~lstewart/articles/cpumemory.pdf ● (*) https://www.usenix.org/legacy/event/hotpar11/tech/final_files/Boehm.pdf