Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ICOOOLPS'16: Sulong - Execution of LLVM-Based L...

ICOOOLPS'16: Sulong - Execution of LLVM-Based Languages on the JVM

Talk at ICOOOLPS 2016

Manuel Rigger

July 18, 2016
Tweet

More Decks by Manuel Rigger

Other Decks in Research

Transcript

  1. Sulong - Execution of LLVM-Based Languages on the JVM Manuel

    Rigger Matthias Grimmer Hanspeter Mössenböck Johannes Kepler University Linz ICOOOLPS 18. July, 2016
  2. Java Platform JVM [1] Rose. "Bytecodes meet combinators: invokedynamic on

    the JVM." [2] Würthinger et al. "One VM to rule them all." 2
  3. Disadvantages • Slow • Transitions between Java and native code

    • Conversions/marshaling • Language boundaries are compilation boundaries • Difficult to use • boiler plate code • Breaks Java‘s safety guarantees Java Native Interface Native side JVM 4
  4. System Overview LLVM IR Interpreter Truffle LLVM IR Clang C

    C++ GCC Fortran Other LLVM frontend ... JVM + Graal tooling 6
  5. System Overview LLVM IR Interpreter Truffle LLVM IR Clang C

    C++ GCC Fortran Other LLVM frontend ... JVM + Graal tooling Use LLVM front ends and LLVM IR 6
  6. System Overview LLVM IR Interpreter • Only component that has

    to be implemented LLVM IR Interpreter Truffle LLVM IR Clang C C++ GCC Fortran Other LLVM frontend ... JVM + Graal tooling 6
  7. System Overview Use Truffle and Graal • Language Interoperability •

    Dynamic compilation LLVM IR Interpreter Truffle LLVM IR Clang C C++ GCC Fortran Other LLVM frontend ... JVM + Graal tooling 6
  8. 7

  9. Static + Dynamic Optimizations Can we combine static with dynamic

    optimizations to top peak performance of static compilers? 7
  10. Static + Dynamic Optimizations LLVM: advanced static optimizations Truffle and

    Graal: speculative optimizations based on profiling Compile- time Link- time Run- time 8
  11. LLVM IR Interpreter Truffle LLVM IR JVM+Graal opt Optimization Pipeline

    Profiling-based optimizations • Polymorphic inline caches for function pointer calls • Function inlining Static LLVM optimizations 9
  12. LLVM IR Interpreter Truffle LLVM IR JVM+Graal opt Optimization Pipeline

    Profiling-based optimizations • Polymorphic inline caches for function pointer calls • Function inlining Compilation of the AST Static LLVM optimizations 9
  13. Dynamic Optimization: Example 10 void bubble_sort(int *numbers, int count, (*compare)(int

    a, int b)) { for (int i = 0; i < count; i++) { for (int j = 0; j < count - 1; j++) { if (compare(numbers[j], numbers[j+1]) > 0) { swap(&numbers[j], &numbers[j+1]); } } } } int ascending(int a, int b){ return a - b; } int descending(int a, int b){ return b - a; }
  14. Native Interface We can use Sulong as an alternative to

    JNI and to implement other language‘s native interfaces 13 Static + Dynamic Optimizations
  15. Sulong as an Alternative to JNI Use Sulong as a

    Java library! Java C/C++/ Fortran Sulong 14
  16. Truffle as a Multi Language Runtime [4] Grimmer, et al.

    "High-performance cross-language interoperability in a multi-language runtime." JVM Truffle Language Interoperability 16
  17. Truffle as a Multi Language Runtime #include<stdio.h> struct complex {

    double r; double i; }; int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } main.c complex.js 17
  18. Truffle as a Multi Language Runtime #include<stdio.h> struct complex {

    double r; double i; }; int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } add(a, b) main.c complex.js 17
  19. Truffle as a Multi Language Runtime #include<stdio.h> struct complex {

    double r; double i; }; int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } add(a, b) a->r b->r a->i b->i main.c complex.js 17
  20. Case Studies: Sulong to Implement NFIs 18 program.c vm.h Program

    JRuby+Truffle Written against Interop call Interop call vm.c [5] Grimmer, et al. "Dynamically composing languages in a modular way: supporting C extensions for dynamic languages."
  21. Native Interface Static + Dynamic Optimizations Memory Safety Can we

    provide memory safety for C/C++ and Fortran? 19
  22. Memory Errors in C int *arr = malloc(4 * sizeof(int))

    20 [6] Szekeres, et al. "Sok: Eternal war in memory."
  23. Memory Errors in C int *arr = malloc(4 * sizeof(int))

    … = arr[5] arr[5] = … 20 [6] Szekeres, et al. "Sok: Eternal war in memory."
  24. Memory Errors in C int *arr = malloc(4 * sizeof(int))

    … = arr[5] arr[5] = … Spatial memory safety error 20 [6] Szekeres, et al. "Sok: Eternal war in memory."
  25. Memory Errors in C int *arr = malloc(4 * sizeof(int))

    … = arr[5] arr[5] = … free(arr); … = arr[0] arr[0] = … Spatial memory safety error 20 [6] Szekeres, et al. "Sok: Eternal war in memory."
  26. Memory Errors in C int *arr = malloc(4 * sizeof(int))

    … = arr[5] arr[5] = … free(arr); … = arr[0] arr[0] = … Spatial memory safety error Temporal memory safety error 20 [6] Szekeres, et al. "Sok: Eternal war in memory."
  27. Memory Errors in C int *arr = malloc(4 * sizeof(int))

    … = arr[5] arr[5] = … free(arr); … = arr[0] arr[0] = … Spatial memory safety error Temporal memory safety error 20 • Segmentation faults • (Silent) data corruption • Reading of sensitive data • Code injection [6] Szekeres, et al. "Sok: Eternal war in memory."
  28. Current Sulong implementation • Relies on unmanaged memory (sun.misc.Unsafe) •

    https://github.com/graalvm/sulong • Goal: use managed Java objects 21
  29. Prevent Spatial Errors … = arr[5] arr[5] = … 22

    (arr[5] ≈ arr + sizeof(int) * 5) ManagedAddress offset=20 data I32Array elementSize=4 contents {1, 2, 3, 4}
  30. Prevent Spatial Errors … = arr[5] arr[5] = … contents[20

    / 4]  ArrayOutOfBoundsException 22 (arr[5] ≈ arr + sizeof(int) * 5) ManagedAddress offset=20 data I32Array elementSize=4 contents {1, 2, 3, 4}
  31. Prevent Temporal Errors free(arr); … = arr[0] arr[0] = …

    23 ManagedAddress offset=0 data I32Array elementSize=4 contents=null
  32. Prevent Temporal Errors free(arr); … = arr[0] arr[0] = …

    contents[0]  NullPointerException 23 ManagedAddress offset=0 data I32Array elementSize=4 contents=null
  33. Limitations • Warm-up and start-up • Current limitations: multithreading, 80

    bit floats, inline assembler support • Support of all library functions • Conundrum between memory safety and programmer freedom 24
  34. Summary 25 Native Interface Static + Dynamic Optimizations Memory Safety

    JRuby+Truffle FastR Graal.JS C/C++/ Fortran … = arr[5] arr[5] = … contents[20 / 4]  ArrayOutOfBoundsException ManagedAddress offset=20 data I32Array elementSize=4 contents {1, 2, 3, 4}
  35. References [1] Rose, John R. "Bytecodes meet combinators: invokedynamic on

    the JVM." Proceedings of the Third Workshop on Virtual Machines and Intermediate Languages. ACM, 2009. [2] Würthinger, Thomas, et al. "One VM to rule them all." Proceedings of the 2013 ACM international symposium on New ideas, new paradigms, and reflections on programming & software. ACM, 2013. [3] Lattner, Chris, and Vikram Adve. "LLVM: A compilation framework for lifelong program analysis & transformation." Code Generation and Optimization, 2004. CGO 2004. International Symposium on. IEEE, 2004. [4] Grimmer, Matthias, et al. "High-performance cross-language interoperability in a multi-language runtime." Proceedings of the 11th Symposium on Dynamic Languages. ACM, 2015. [5] Grimmer, Matthias, et al. "Dynamically composing languages in a modular way: supporting C extensions for dynamic languages." Proceedings of the 14th International Conference on Modularity. ACM, 2015. [6] Szekeres, Laszlo, et al. "Sok: Eternal war in memory." Security and Privacy (SP), 2013 IEEE Symposium on. IEEE, 2013. 26