1 valgrind a framework for heavyweight dynamic binary instrumentation nicholas nethercote —...

27
1 Valgrind Valgrind A Framework for A Framework for Heavyweight Dynamic Heavyweight Dynamic Binary Instrumentation Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

Upload: leonard-warner

Post on 31-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

1

ValgrindValgrind A Framework for A Framework for

Heavyweight Dynamic Heavyweight Dynamic Binary InstrumentationBinary Instrumentation

Nicholas Nethercote — National ICT Australia

Julian Seward — OpenWorks LLP

Page 2: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

2

FAQ #1FAQ #1

• How do you pronounce “Valgrind”?

• “Val-grinned”, not “Val-grined”

• Don’t feel bad: almost everyone gets it wrong at first

Page 3: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

3

DBA toolsDBA tools

• Program analysis tools are useful– Bug detectors– Profilers– Visualizers

• Dynamic binary analysis (DBA) tools– Analyse a program’s machine code at run-

time– Augment original code with analysis code

Page 4: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

4

Building DBA toolsBuilding DBA tools

• Dynamic binary instrumentation (DBI)– Add analysis code to the original machine code at

run-time– No preparation, 100% coverage

• DBI frameworks – Pin, DynamoRIO, Valgrind, etc.

Tool FrameworkTool

plug-in+ =

Page 5: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

5

Prior workPrior work

• Potential of DBI has not been fully exploited– Tools get less attention than frameworks– Complex tools are more interesting than simple tools

Well-studied Not well-studied

Framework performance

Instrumentation capabilities

Simple tools Complex tools

Page 6: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

6

Shadow value toolsShadow value tools

Page 7: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

7

Shadow value tools Shadow value tools (I)(I)

• Shadow every value with another value that describes it– Tool stores and propagates shadow values in parallel

Tool(s) Shadow values help find...

Memcheck Uses of undefined values

Annelid Array bounds violations

Hobbes Run-time type errors

TaintCheck, LIFT, TaintTrace

Uses of untrusted values

“Secret tracker” Leaked secrets

DynCompB Invariants

Redux Dynamic dataflow graphs

security

bugs

properties

Page 8: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

8

MemcheckMemcheck

• Shadow values: defined or undefined

• 30 undefined value bugs found in OpenOffice

Original operation Shadow operation

int* p = malloc(4) sh(p) = undefined

R1 = 0x12345678 sh(R1) = defined

R1 = R2 sh(R1) = sh(R2)

R1 = R2 + R3 sh(R1) = addsh(R2, R3)

if R1==0 then goto L

complain if sh(R1) is undefined

Page 9: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

9

Shadow value tools Shadow value tools (II)(II)

• All shadow value tools work in the same basic way

• Shadow value tools are heavyweight tools– Tool’s data + ops are as complex as the original

programs’s

• Shadow value tools are hard to implement– Multiplex real and shadow registers onto register file– Squeeze real and shadow memory into address

space– Instrument most instructions and system calls

Page 10: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

10

Valgrind basicsValgrind basics

Page 11: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

11

ValgrindValgrind• Software

– Free software (GPL)– {x86, x86-64, PPC}/Linux, PPC/AIX

• Users – Development: Firefox, OpenOffice, KDE, GNOME,

MySQL, Perl, Python, PHP, Samba, RenderMan, Unreal Tournament, NASA, CERN

– Research: Cambridge, MIT, Berkeley, CMU, Cornell, UNM, ANU, Melbourne, TU Muenchen, TU Graz

• Design– Heavyweight tools are well supported– Lightweight tools are slow

Page 12: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

12

Two unusual features of Two unusual features of ValgrindValgrind

Page 13: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

13

#1: Code #1: Code representationrepresentation

C&A

Copy-and-annotate

analysiscode

asmout

descriptionsasmin

annotate

interleave

instrumentcopy

D&R

Disassemble-and-resynthesize(Valgrind)

instrument

IR

asmout

asmindisassemble

resynthesize

Page 14: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

14

Pros and cons of Pros and cons of D&RD&R

• Cons: Lightweight tools– Framework design and implementation effort– Code translation cost, code quality

• Pros: Heavyweight tools– Analysis code as expressive as original code– Tight interleaving of original code and analysis

code– Obvious when things go wrong!

wrongbehaviour wrong

analysis

correctbehaviour

baddescriptions

bad IR

D&R C&A

Page 15: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

15

Other IR featuresOther IR features

• Writing complex inline analysis code is easy

Feature Benefit

First-class shadow registers

As expressive as normal registers

Typed, SSA Catches instrumentation errors

RISC-like Fewer cases to handle

Infinitely many temporaries

Never have to find a spare register

Page 16: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

16

#2: Thread #2: Thread serialisationserialisation

• Shadow memory: memory accesses no longer atomic– Uni-processors: thread switches may intervene– Multi-processors: real/shadow accesses may

be reordered

• Simple solution: serialise thread execution!– Tools can ignore the issue– Great for uni-processors, slow for multi-

processors...

Page 17: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

17

PerformancePerformance

Page 18: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

18

SPEC2000 SPEC2000 PerformancePerformance

(*) LIFT limitations:– No FP or SIMD programs– No multi-threaded programs– 32-bit x86 code on 64-bit x86 machines only

Valgrind, no-instrumentation

4.3x

Pin/DynRIO, no-instrumentation

~1.5x

Memcheck 22.1x (7--58x)

Most other shadow value tools

10--180x

LIFT 3.6x (*)

Page 19: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

19

Post-performancePost-performance

• Only Valgrind allows robust shadow value tools– All robust ones built with Valgrind or from

scratch

• Perception: “Valgrind is slow”– Too simplistic– Beware apples-to-oranges comparisons– Different frameworks have different

strengths

Page 20: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

20

Future of DBIFuture of DBI

Page 21: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

21

The futureThe future

• Interesting tools!– Memcheck changed many C/C++

programmer’s lives– Tools don’t arise in a vacuum

• What do you want to know about program execution?– Think big! – Don’t worry about being practical at first

Page 22: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

22

If you remember nothing If you remember nothing else...else...

Page 23: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

23

Take-home Take-home messagesmessages

• Heavyweight tools are interesting• Each DBI framework has its pros and

cons• Valgrind supports heavyweight tools

well

www.valgrind.org

Page 24: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

24

(Extra slides)(Extra slides)

Page 25: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

25

The past: The past: performanceperformance

• Influenced by Dynamo: dynamic binary optimizer

• Everyone in research focuses on performance– No PLDI paper ever got rejected for focusing on

performance“The subjective issues are important — ease of use and robustness, but performance is the item which would be most interesting for the audience.” (my italics)

• Slow tools are ok, if sufficiently useful

Page 26: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

26

Shadow value Shadow value requirementsrequirements

• Requirements:– (1) Shadow all state– (2) Instrument operations that involve state– (3) Produce extra output without disturbing

execution

Page 27: 1 Valgrind A Framework for Heavyweight Dynamic Binary Instrumentation Nicholas Nethercote — National ICT Australia Julian Seward — OpenWorks LLP

27

RobustnessRobustness• Q. How many programs can Valgrind run?

– A. A lot

• Valgrind is robust, Valgrind tools can be

• SPEC2000 is not a good stress test!– Reviewer: “If the authors want to claim that

their tool is to be used in real projects, then they would need to evaluate their tools using the reference inputs for the SPEC CPU2K benchmarks.”