Compiler-guaranteed Safety in Code-copying Virtual Machines Gregory - - PowerPoint PPT Presentation

compiler guaranteed safety in code copying virtual
SMART_READER_LITE
LIVE PREVIEW

Compiler-guaranteed Safety in Code-copying Virtual Machines Gregory - - PowerPoint PPT Presentation

Background and Motivation Our Design and Implementation Results and Conclusions Compiler-guaranteed Safety in Code-copying Virtual Machines Gregory B. Prokopski Clark Verbrugge School of Computer Science Sable Research Group McGill


slide-1
SLIDE 1

Background and Motivation Our Design and Implementation Results and Conclusions

Compiler-guaranteed Safety in Code-copying Virtual Machines

Gregory B. Prokopski Clark Verbrugge

School of Computer Science Sable Research Group McGill University Montreal, Canada

International Conference on Compiler Construction, 2008

1 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-2
SLIDE 2

Background and Motivation Our Design and Implementation Results and Conclusions

Taxonomy

Virtual Machine Interpreter Compiler code-copying direct-threaded switch- threaded Ahead- Of-Time Just-In-Time

Code-copying technique Interpreter and also a JIT.

2 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-3
SLIDE 3

Background and Motivation Our Design and Implementation Results and Conclusions

Speed Comparison

runtime speed Interpreters naive compilers code- copying switch direct

  • ptimizing

compilers costs

Code-copying technique Bridges the performance gap while keeping costs low. 1.2–3.0 times faster than direct-threading.

3 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-4
SLIDE 4

Background and Motivation Our Design and Implementation Results and Conclusions

Direct-threading vs. Code-copying

ILOAD_0: ILOAD_1: IADD: ISTORE_2: ILOAD_0 ILOAD_1 IADD ISTORE_2 . . . . . . . . . ILOAD_1 ILOAD_0 IADD ISTORE_2

interpreter main loop (direct-threaded) single superinstruction (code-copying) superinstruction ILOAD1_ILOAD0_IADD_ISTORE2

Code-copying technique Reduces number of dispatches and improves branch prediction.

4 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-5
SLIDE 5

Background and Motivation Our Design and Implementation Results and Conclusions

Code-copying Disaster Example

BCODE_START: if (...) { // then part } else { // else part }

How it happens?

Direct-threading - one label

5 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-6
SLIDE 6

Background and Motivation Our Design and Implementation Results and Conclusions

Code-copying Disaster Example

BCODE_START: if (...) { // then part } else { // else part } BCODE_END:

How it happens?

Direct-threading - one label Code-copying - two bracketing labels

5 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-7
SLIDE 7

Background and Motivation Our Design and Implementation Results and Conclusions

Code-copying Disaster Example

BCODE_START: if (...) { // then part } BCODE_END: . . . { // else part }

How it happens?

Direct-threading - one label Code-copying - two bracketing labels Optimizations move basic blocks

5 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-8
SLIDE 8

Background and Motivation Our Design and Implementation Results and Conclusions

Code-copying Disaster Example

BCODE_START: if (...) { // then part } BCODE_END: . . . // else ???

How it happens?

Direct-threading - one label Code-copying - two bracketing labels Optimizations move basic blocks Incomplete copied code

5 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-9
SLIDE 9

Background and Motivation Our Design and Implementation Results and Conclusions

Code-copying Disaster Example

BCODE_START: if (...) { // then part } BCODE_END: . . . // else ???

How it happens?

Direct-threading - one label Code-copying - two bracketing labels Optimizations move basic blocks Incomplete copied code CRASH!!!

5 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-10
SLIDE 10

Background and Motivation Our Design and Implementation Results and Conclusions

Code-copying Disaster Example

BCODE_START: if (...) { // then part } BCODE_END: . . . // else ???

How it happens?

Direct-threading - one label Code-copying - two bracketing labels Optimizations move basic blocks Incomplete copied code CRASH!!!

Problems always arise when a compiler uses relative addressing to reach outside a bytecode.

5 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-11
SLIDE 11

Background and Motivation Our Design and Implementation Results and Conclusions

Motivation

Code-copying

Easy, cheap to implement Great performance Not reliable (with modern compilers) - current approaches:

Ignore the problem. Hand-check the assembly. Trial and error testing. Approximate runtime checks.

6 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-12
SLIDE 12

Background and Motivation Our Design and Implementation Results and Conclusions

Outline

1

Background and Motivation Interpreters vs. Compilers Gap Code-copying and Its (Lack of) Safety

2

Our Design and Implementation Copied Code Tracking Verification

3

Results and Conclusions Performance Compiler Maintainability Impact

7 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-13
SLIDE 13

Background and Motivation Our Design and Implementation Results and Conclusions

Copyable Code - What Is It?

Copyable Code ”Chunk” Requirements

Contiguous in memory between two labels Control flow ”top” to ”bottom” Jumps to outside and calls are absolute Jumps within chunk are relative Consistent registers use at entry and exit

8 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-14
SLIDE 14

Background and Motivation Our Design and Implementation Results and Conclusions

Solution overview

Optimizing compiler (GCC) enhancement

Programmer-friendly #pragma Track copyable code ”chunks” Dozens of passes — Do not touch! Selective restore of code properties Final code verification

9 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-15
SLIDE 15

Background and Motivation Our Design and Implementation Results and Conclusions

Solution overview

Inserted new passes

Identify basic blocks of copyable code chunks Enforce absolute jumps and calls ⇒ Run existing optimizations Basic block order fixup ⇒ Legacy existing optimizations Copyable code verification

10 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-16
SLIDE 16

Background and Motivation Our Design and Implementation Results and Conclusions

Pragma Handling

. . . BCODE_START: if (...) { . . . } else { . . . } BCODE_END: . . .

11 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-17
SLIDE 17

Background and Motivation Our Design and Implementation Results and Conclusions

Pragma Handling

. . . #pragma copyable start BCODE_START: if (...) { . . . } else { . . . } #pragma copyable end BCODE_END: . . .

11 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-18
SLIDE 18

Background and Motivation Our Design and Implementation Results and Conclusions

Pragma Handling

a r e a = 5 flags = START a r e a = 5

area = 0

a r e a = 5 a r e a = 5 flags = TARGET

area = 0

Basic blocks Statements stream . . . #pragma copyable start BCODE_START: if (...) { . . . } else { . . . } #pragma copyable end BCODE_END: . . .

First and past-last basic blocks are marked as Start and Target.

11 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-19
SLIDE 19

Background and Motivation Our Design and Implementation Results and Conclusions

Enforcing Absolute Gotos and Calls

BCODE_START: . . . if (ptr==NULL) goto NPE_handler; . . . BCODE_END:

Need to correct relative addressing within ”chunks”.

External assembler decides on addressing mode — not GCC. Needed an architecture-agnostic solution.

12 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-20
SLIDE 20

Background and Motivation Our Design and Implementation Results and Conclusions

Enforcing Absolute Gotos and Calls

BCODE_START: . . . if (ptr==NULL) goto NPE_handler; . . . BCODE_END: { void *target = &NPE_handler; __asm__ __volatile__ ( "" : "=r" (target) : "0" (target) : "memory"); goto *target; }

Need to correct relative addressing within ”chunks”.

External assembler decides on addressing mode — not GCC. Needed an architecture-agnostic solution. Goto to the outside of chunk is forced into a computed goto. Each call is forced into call via function pointer.

12 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-21
SLIDE 21

Background and Motivation Our Design and Implementation Results and Conclusions

Compiler Runs Largerly Unaffected

Once Start and Target basic blocks are marked and absolute addressing enforced all optimizations are performed as usual. A lot of work to modify several dozens of passes — don’t! Start and Target block are never removed or duplicated. Able to find all copyable code of each chunk via CFG.

Traverse CFG from Start until Target or computed goto is reached. No heuristics.

13 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-22
SLIDE 22

Background and Motivation Our Design and Implementation Results and Conclusions

Ensuring Copyable Code Contiguity

area = 5 flags = TARGET BB2 area = 0 BB3 area = 5 flags = START BB1 area = 0 BB4 area = 0 BB5 area = 0 BB6

1.

1 4 6 3 5 2

1

Compiler moved basic blocks.

14 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-23
SLIDE 23

Background and Motivation Our Design and Implementation Results and Conclusions

Ensuring Copyable Code Contiguity

area = 5 flags = TARGET BB2 area = 0 BB3 area = 5 flags = START BB1 area = 5 BB4 area = 0 BB5 area = 5 BB6

2.

1 4 6 3 5 2

1

Compiler moved basic blocks.

2

Follow CFG to find blocks of each chunk.

14 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-24
SLIDE 24

Background and Motivation Our Design and Implementation Results and Conclusions

Ensuring Copyable Code Contiguity

area = 5 flags = TARGET BB2 area = 0 BB3 area = 5 flags = START BB1 area = 5 BB4 area = 0 BB5 area = 5 BB6

2.

1 4 6 3 5 2

area = 5 BB4 area = 5 BB6 area = 5 flags = START BB1 area = 5 flags = TARGET BB2 area = 0 BB3 area = 0 BB5

3. 1

Compiler moved basic blocks.

2

Follow CFG to find blocks of each chunk.

3

Reorder basic blocks, deoptimize to ensure chunk contiguity.

14 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-25
SLIDE 25

Background and Motivation Our Design and Implementation Results and Conclusions

Final Verification Pass

CFG is discarded at some point. Some legacy optimization code is ran after. Need to be sure of the final result. Insert special RTL ”notes” to mark Start and Target. When the code is final verify all properties. This way ensure safety of the final result.

15 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-26
SLIDE 26

Background and Motivation Our Design and Implementation Results and Conclusions

Brief Design Summary

Enables safe code-copying. Avoided modifying dozens of passes. Very maintainable. Easy to use. Portable.

16 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-27
SLIDE 27

Background and Motivation Our Design and Implementation Results and Conclusions

Performance Comparison

Comparable or faster than unsafe code-copying of SableVM JVM

17 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-28
SLIDE 28

Background and Motivation Our Design and Implementation Results and Conclusions

Compiler Maintainability Impact

Metric # Data structures modified 4 Fields added to data structures 6 Data structures added 3 Functions added to existing files 4 Function calls/hooks inserted 8 Code lines added or modified 139 Code lines in new files 1500

Minimal impact in terms of source modified. Update GCC 3.4 to 4.2 (2 years of development) took only a few hours.

18 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-29
SLIDE 29

Background and Motivation Our Design and Implementation Results and Conclusions

Conclusions and Future Work

Presented an industry compiler extension supporting copyable code generation. Easy to use by VM programmers. Easy to maintain in the compiler. Provides safety guarantees for copied code execution in a VM. Provides comparable performance to unsafe copied code execution. Expected future application to other VMs and other architectures.

19 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines

slide-30
SLIDE 30

Background and Motivation Our Design and Implementation Results and Conclusions

Conclusions and Future Work

Presented an industry compiler extension supporting copyable code generation. Easy to use by VM programmers. Easy to maintain in the compiler. Provides safety guarantees for copied code execution in a VM. Provides comparable performance to unsafe copied code execution. Expected future application to other VMs and other architectures.

Questions?

19 / 19 Compiler-guaranteed Safety in Code-copying, Virtual Machines