compiler guaranteed safety in code copying virtual
play

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


  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 Compiler-guaranteed Safety in Code-copying, Virtual Machines 1 / 19

  2. Background and Motivation Our Design and Implementation Results and Conclusions Taxonomy Virtual Machine Interpreter Compiler switch- Ahead- code-copying threaded Of-Time Just-In-Time direct-threaded Code-copying technique Interpreter and also a JIT. Compiler-guaranteed Safety in Code-copying, Virtual Machines 2 / 19

  3. Background and Motivation Our Design and Implementation Results and Conclusions Speed Comparison costs optimizing compilers naive compilers code- Interpreters direct copying runtime switch speed Code-copying technique Bridges the performance gap while keeping costs low. 1.2–3.0 times faster than direct-threading. Compiler-guaranteed Safety in Code-copying, Virtual Machines 3 / 19

  4. Background and Motivation Our Design and Implementation Results and Conclusions Direct-threading vs. Code-copying interpreter main loop single superinstruction (direct-threaded) (code-copying) IADD: IADD ILOAD_1 . . . ILOAD_0: ILOAD_0 ILOAD_0 IADD ILOAD_1: ISTORE_2 ILOAD_1 . . . ISTORE_2: superinstruction ILOAD1_ILOAD0_IADD_ISTORE2 ISTORE_2 . . . Code-copying technique Reduces number of dispatches and improves branch prediction. Compiler-guaranteed Safety in Code-copying, Virtual Machines 4 / 19

  5. Background and Motivation Our Design and Implementation Results and Conclusions Code-copying Disaster Example BCODE_START: How it happens? if (...) { Direct-threading - one label // then part } else { // else part } Compiler-guaranteed Safety in Code-copying, Virtual Machines 5 / 19

  6. Background and Motivation Our Design and Implementation Results and Conclusions Code-copying Disaster Example BCODE_START: How it happens? if (...) { Direct-threading - one label // then part Code-copying - two bracketing } labels else { // else part } BCODE_END: Compiler-guaranteed Safety in Code-copying, Virtual Machines 5 / 19

  7. Background and Motivation Our Design and Implementation Results and Conclusions Code-copying Disaster Example BCODE_START: How it happens? if (...) { Direct-threading - one label // then part Code-copying - two bracketing } labels BCODE_END: . . . Optimizations move basic { blocks // else part } Compiler-guaranteed Safety in Code-copying, Virtual Machines 5 / 19

  8. Background and Motivation Our Design and Implementation Results and Conclusions Code-copying Disaster Example BCODE_START: How it happens? if (...) { Direct-threading - one label // then part Code-copying - two bracketing } labels BCODE_END: Optimizations move basic . . . blocks // else ??? Incomplete copied code Compiler-guaranteed Safety in Code-copying, Virtual Machines 5 / 19

  9. Background and Motivation Our Design and Implementation Results and Conclusions Code-copying Disaster Example BCODE_START: How it happens? if (...) { Direct-threading - one label // then part Code-copying - two bracketing } labels BCODE_END: Optimizations move basic . . . blocks // else ??? Incomplete copied code CRASH!!! Compiler-guaranteed Safety in Code-copying, Virtual Machines 5 / 19

  10. Background and Motivation Our Design and Implementation Results and Conclusions Code-copying Disaster Example BCODE_START: How it happens? if (...) { Direct-threading - one label // then part Code-copying - two bracketing } labels BCODE_END: Optimizations move basic . . . blocks // else ??? Incomplete copied code CRASH!!! Problems always arise when a compiler uses relative addressing to reach outside a bytecode. Compiler-guaranteed Safety in Code-copying, Virtual Machines 5 / 19

  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. Compiler-guaranteed Safety in Code-copying, Virtual Machines 6 / 19

  12. Background and Motivation Our Design and Implementation Results and Conclusions Outline Background and Motivation 1 Interpreters vs. Compilers Gap Code-copying and Its (Lack of) Safety Our Design and Implementation 2 Copied Code Tracking Verification Results and Conclusions 3 Performance Compiler Maintainability Impact Compiler-guaranteed Safety in Code-copying, Virtual Machines 7 / 19

  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 Compiler-guaranteed Safety in Code-copying, Virtual Machines 8 / 19

  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 Compiler-guaranteed Safety in Code-copying, Virtual Machines 9 / 19

  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 Compiler-guaranteed Safety in Code-copying, Virtual Machines 10 / 19

  16. Background and Motivation Our Design and Implementation Results and Conclusions Pragma Handling . . . BCODE_START: if (...) { . . . } else { . . . } BCODE_END: . . . Compiler-guaranteed Safety in Code-copying, Virtual Machines 11 / 19

  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: . . . Compiler-guaranteed Safety in Code-copying, Virtual Machines 11 / 19

  18. Background and Motivation Our Design and Implementation Results and Conclusions Pragma Handling Basic blocks Statements stream . . . area = 0 #pragma copyable start BCODE_START: a r e a = 5 if (...) { flags = START . . . } else { a r e a = 5 . . . } a r e a = 5 #pragma copyable end a r e a = 5 BCODE_END: flags = TARGET . . . area = 0 First and past-last basic blocks are marked as Start and Target. Compiler-guaranteed Safety in Code-copying, Virtual Machines 11 / 19

  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. Compiler-guaranteed Safety in Code-copying, Virtual Machines 12 / 19

  20. Background and Motivation Our Design and Implementation Results and Conclusions Enforcing Absolute Gotos and Calls { void *target = &NPE_handler; BCODE_START: __asm__ __volatile__ ( "" : . . . if (ptr==NULL) "=r" (target) : goto NPE_handler; "0" (target) : . . . "memory"); BCODE_END: 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. Compiler-guaranteed Safety in Code-copying, Virtual Machines 12 / 19

  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. Compiler-guaranteed Safety in Code-copying, Virtual Machines 13 / 19

  22. Background and Motivation Our Design and Implementation Results and Conclusions Ensuring Copyable Code Contiguity 1. area = 5 1 BB1 flags = START area = 5 BB2 4 6 flags = TARGET area = 0 BB3 2 area = 0 BB4 3 area = 0 BB5 area = 0 5 BB6 Compiler moved basic blocks. 1 Compiler-guaranteed Safety in Code-copying, Virtual Machines 14 / 19

  23. Background and Motivation Our Design and Implementation Results and Conclusions Ensuring Copyable Code Contiguity 2. area = 5 1 BB1 flags = START area = 5 4 6 BB2 flags = TARGET area = 0 BB3 2 area = 5 BB4 3 area = 0 BB5 5 area = 5 BB6 Compiler moved basic blocks. 1 Follow CFG to find blocks of each chunk. 2 Compiler-guaranteed Safety in Code-copying, Virtual Machines 14 / 19

Recommend


More recommend