Academia.eduAcademia.edu

Fully abstract compilation to JavaScript

2013, Proceedings of the 40th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '13

Abstract

Many tools allow programmers to develop applications in highlevel languages and deploy them in web browsers via compilation to JavaScript. While practical and widely used, these compilers are ad hoc: no guarantee is provided on their correctness for whole programs, nor their security for programs executed within arbitrary JavaScript contexts. This paper presents a compiler with such guarantees. We compile an ML-like language with higher-order functions and references to JavaScript, while preserving all source program properties. Relying on type-based invariants and applicative bisimilarity, we show full abstraction: two programs are equivalent in all source contexts if and only if their wrapped translations are equivalent in all JavaScript contexts. We evaluate our compiler on sample programs, including a series of secure libraries.

Key takeaways

  • Disclaimer As usual, full abstraction holds only within our formal semantics of JavaScript, and various side channels may still exist in JavaScript implementations, based, for instance, on stack or heap exhaustion, or timing analysis.
  • Our wrappers are JavaScript functions indexed by source types t. They come in pairs: a 'down' wrapper, written ↓ t, takes a light translation of a source value v:t and exports it safely to the context; an 'up' wrapper, written ↑t, takes any JavaScript value supplied by the context and attempts to extract from it a value that is a light translation of some source v:t; this may fail.
  • To handle this, we represent JavaScript functions as js values Fun o f, constructed from a function object o:dyn and a closure f. All functions in JavaScript receive an implicit this parameter and, following λ JS, a single argument object with a field for each of their explicit arguments.
  • Formally, our compiler can be viewed as the translation from f to JavaScript ( §4) composed with the embedding of JavaScript into js ( §5).
  • Performance The benefits of running secure JavaScript come at a price, as any interactions with untrusted code are mediated by wrappers.