FunTAL: Reasonably Mixing a Functional Language with Assembly Daniel Patterson, Jamie Perconti, Christos Dimoulas, Amal Ahmed Irene Yoon
FunTAL: Reasonably Mixing a Functional
Language with Assembly
Daniel Patterson, Jamie Perconti, Christos Dimoulas, Amal Ahmed
Irene Yoon
Mixed Language Programs
High-level LanguageHigh-level Language
Low-level Language
Mixed Language Programs
High-level Language
Inline assembly
Mixed Language Programs
High-level Language
Inline assembly
+ hardware access
+ fine-tune performance
Mixed Language Programs
• no safety guarantees
• low-level code can
• alter control flow
• mutate inaccessible values
• introduce security vulnerabilities
Mixed Language Programs
High-level Language
Assembly
JITThis portion can be
compiled!
Mixed Language Programs
High-level Language
Assembly
JITThis portion can be
compiled!
Verification of correctness?
Mixed Language Programs
High-level Language
Assembly
Verification of correctness?
=
Need to prove contextual equivalence
Goals
• Safely mix high-level and low-level code
• Prove contextual equivalence of mixed programs
FunTAL
• Multi-language
• Embeds assembly in typed functional language
• Embeds typed functional language in assembly
FunTAL Goals
• Appropriate granularity for comparison
• Keep low-level language low and high-level language high
• Mix high-level and low-level language
• Prove (contextual) equivalence
Fun + TAL
Fun : Functional Language
• Simply Typed Lambda Calculus
• (iso-) recursive types
Fun + Assembly
Assembly
Assembly
• Inherently non-compositional
Assembly
• Inherently non-compositional
direct jump
Assembly
• Inherently non-compositional
_______ _______ _______ _______ _______ _______
call stack
direct jump
Mixing Fun
direct jump
_______ _______ _______ _______ _______ _______
call stack
Fun
compositional
Mixing Fun
Fun
compositional
• Need to constrain non-compositional behavior
Fun + (compositional) TAL
TAL : Typed Assembly Language
• Types at assembly level
• Stack-based TAL = STAL
[Morrisett, Crary, Glew, Walker ’98]
TAL : Typed Assembly Language
• Types at assembly level
• Stack-based TAL = STAL
[Morrisett, Crary, Glew, Walker ’98]
HeapTyping
TAL : Typed Assembly Language
• Types at assembly level
• Stack-based TAL = STAL
[Morrisett, Crary, Glew, Walker ’98]
HeapTyping
Type Env
TAL : Typed Assembly Language
• Types at assembly level
• Stack-based TAL = STAL
[Morrisett, Crary, Glew, Walker ’98]
HeapTyping
Type EnvRegister File
Typing
TAL : Typed Assembly Language
• Types at assembly level
• Stack-based TAL = STAL
[Morrisett, Crary, Glew, Walker ’98]
HeapTyping
Type EnvRegister File
Typing
Stack Typing
TAL : Typed Assembly Language
• Types at assembly level
• Stack-based TAL = STAL
[Morrisett, Crary, Glew, Walker ’98]
HeapTyping
Type EnvRegister File
Typing
Stack Typing
instr; … ; jmp l
Fun + (compositional) TAL
type parameter register stack
typing preconditions
type parameter register stack
typing preconditions
type parameter register stack
typing preconditions
type parameter register stack
typing preconditions
TAL in Fun
Fun
TAL
_________________ _________________
Granularity
_________________ _________________
assembly components
_________________ _________________
assembly components
_________________ _________________
instr. sequence
(basic block)
localheap
fragment
assembly components
TAL Components
• Represent one or more basic blocks
• Local heap fragment - locations to code blocks used in jumps
• Similar granularity to expressions in Fun
instr. sequence
localheap
fragment
Fun + (compositional, TAL component-based)
FunTAL Goals
• Appropriate granularity for comparison
• Keep low-level language low and high-level language high
• Mix high-level and low-level language
• Prove (contextual) equivalence
_________________ _________________
_________________ _________________ Input Output
type parameter register stack
typing preconditions
return addr l l
Return Marker
• return marker specifies register/stack position where return continuation is stored
Result Type
• Must provide components with return continuations
• Called f/ assembly jump: jump to next instr.
• Called f/ high level code: contain halt
End / Halt
Polymorphic return marker
Polymorphic return markerI know
the future!!!
Polymorphic return marker
caller instantiates where control flow should go next
FunTAL Goals
• Appropriate granularity for comparison
• Keep low-level language low and high-level language high
• Mix high-level and low-level language
• Prove (contextual) equivalence
jmp
call
bz
Component Based Jumps
• intra-component jump jmp
• inter-component jump ret
• inter-component jump call
jmp (intra-component)
“current component will terminate by jumping to a same TAL component”
=> same return marker
jmp (intra-component)
“current component will terminate by jumping to a same TAL component”
=> same return marker
ret (intra-component)
“returning from a component(jumping back to different component)”
ret (intra-component)
“returning from a component (jumping back to different component)”
=> no restrictions on return marker
call (inter-component)
“current component will terminate by halting”
call (inter-component)
• STAL stack-tail polymorphism : “callee-saved” stack tail
“current component will terminate by halting”
“current component will terminate by jumping to another TAL component”
call (inter-component)
FunTAL Goals
• Appropriate granularity for comparison
• Keep low-level language low and high-level language high
• Mix high-level and low-level language
• Prove (contextual) equivalence
def fn(x): _________________ _________________
def fn(x): _________________ _________________
need multi-language!
Multi-Language• Syntax of both languages combined
• Boundary terms added to mediate interactions
•
[Matthews-Findler `07]
Fun TAL (e) TAL Fun (e)
Multi-Language• Syntax of both languages combined
• Boundary terms added to mediate interactions
•
[Matthews-Findler `07]
Fun TAL (e) TAL Fun (e)
Multi-Language• Syntax of both languages combined
• Boundary terms added to mediate interactions
•
[Matthews-Findler `07]
translation meta-functionreduce
Fun TAL (e) TAL Fun (e)
Multi-Language FunTAL
Multi-Language FunTAL
Multi-Language FunTAL
Multi-Language FunTAL
Fun in TAL
def fn(x): _________________ _________________
Import (Fun in TAL)
FunTAL Goals
• Appropriate granularity for comparison
• Keep low-level language low and high-level language high
• Mix high-level and low-level language
• Prove (contextual) equivalence
Mixed Language Programs
High-level Language
Assembly
Verification of correctness?
=
Need to prove contextual equivalence
Logical Relation for Multi-Language
https://dbp.io/talks/2017/funtal-pldi.pdf
Equivalence in Fun
“related inputs result in related outputs”
https://dbp.io/talks/2017/funtal-pldi.pdf
Equivalence in TAL
https://dbp.io/talks/2017/funtal-pldi.pdf
Future Work
• Verification of JIT transformations
• Compositional compiler correctness
• Return markers for slightly higher-level languages
Goals
• Safely mix high-level and low-level code
• Prove contextual equivalence of mixed programs