SlideShare a Scribd company logo
1 of 22
Download to read offline
elfconv: AOT compiler that translates Linux/AArch64 ELF binary
to LLVM bitcode targeting WebAssembly
Masashi Yoshimura, NTT
2024/02/04
repo: https://github.com/yomaytk/elfconv
1
• WebAssembly (WASM) is virtual machine instruction set
• ✅ portable
– enables to run apps on both browsers and servers without modification
• ✅ secure
– highly isolated from the host kernel on the server by WASI.
• WASI is an API that provides access to several OS-like features (filesystems,
sockets, …).
• WASI is implemented by WASI runtimes (wasmtime, WasmEdge, …).
– memory isolation with harvard architecture
• architecture that physically separates memory for instructions and data.
What is WebAssembly? Why using that?
2
• ❌ limitation in the capability of apps
– can jump to only the instructions that are determinable at compile time
• cannot indirectly jump to the instructions generated in the data memory at runtime
– WASI implementation doesn’t cover all POSIX APIs (e.g. fork, exec)
What is WebAssembly? Why using that?
3
Many programming languages support WASM (e.g. C, C++, Rust, Go, …).
However, it isn’t easy to build WASM in some cases as follows.
Case 1. The programming language that you want to use doesn’t completely
support WASM
Case 2. binaries are available, but the source codes of the binaries are not
available
– e.g.) The source code is not available under lisence
Case 3. Time-consuming to building the environment
– e.g.) you might be not able to build the dependent libraries because they are not
maintained and so on.
challenging in building WASM
4
• TinyEMU: https://bellard.org/tinyemu/
– Author: Fabrice Bellard
– x86 and RISC-V emulator available on the browser
– Linux kernel can run on the browser
• container2wasm: https://github.com/ktock/container2wasm
– Author: Kohei Tokunaga, NTT
– enables to run Linux kernel and container runtimes with emulators compiled
to WASM (e.g. TinyEMU)
– can run containers without modification on the browser and WASI runtimes
But, emulators possibly incur large performance overheads…
Existing projects that run Linux binaries on WASM
AOT compile Linux binaries to WASM!
5
• compiles Linux ELF binary to LLVM bitcode
• existing compilers (e.g. emscripten) compile LLVM bitcode and the object of
Linux syscalls emulation to WASM
• elfconv is successor to myAOT: https://github.com/AkihiroSuda/myaot
– Author: Akihiro Suda, NTT
– An experimental AOT-ish compiler (Linux/riscv32 ELF → Linux/x86_64 ELF, Mach-O, WASM, ...)
elfconv: AOT compiler from Linux/ELF to WASM
6
Demo
7
• elfconv-Lifter
– parse ELF binary, map every ELF section, etc…
• remill (elfconv-Backend) : https://github.com/lifting-bits/remill
– library for lifting machine code to LLVM bitcode
How it works? (ELF -> LLVM bitcode)
8
• convert a function to a LLVM IR function (e.g. _func1 -> @_func1_lift)
– But, need to extract every function from ELF
How it works? (remill)
9
• convert a CPU instruction to a LLVM IR block (e.g. mov x2, x0 -> 1_mov)
How it works? (remill)
10
• convert a CPU instruction to a LLVM IR block
– PC calculation, Operand calculation
– call the function of the instruction-specific operation
How it works? (remill)
11
• The code of WASM can indirectly jump to only the code that is determinable at
compile time.
• currently, not support setjmp and longjmp.
How it works? (indirect jump)
12
• statically link LLVM bitcode and elfconv-Runtime
• elfconv-Runtime
– mapped memory (stack, heap), Linux system calls emulation
How it works? (LLVM bitcode -> WASM)
13
• libc implementation: emscripten, wasi-libc, etc…
Case 1. use libc function if it exists (e.g. write)
How it works? (Linux syscalls emulation)
14
• libc implementation: emscripten, wasi-libc, etc…
Case 2. pseudo-implement the syscall if it doesn’t exist (e.g. brk)
How it works? (Linux syscalls emulation)
not use brk (unsigned long brk)
15
• target sample ELF binary: prime number calculator
– compute all prime numbers less than the input integer
• Test: ELF/aarch64 -> LLVM bitcode -> ELF/x86_64 (not WASM)
– current system calls emulation for WASI runtimes is insufficient, so we
use x86_64 as the output binary for benchmark tests.
• comparison : QEMU emulation aarch64 to x86_64
16
Performance
QEMU emulation vs. binary AOT compilation
Case 1. input integer : 10,000,000
• QEMU : 9.437s
• elfconv : 8.353s
Case 2. input integer : 50,000,000
• QEMU : 1m30.014s
• elfconv : 1m18.972s
17
Performance
Case 1. input integer : 10,000,000
• QEMU : 9.437s
• elfconv : 8.353s
Case 2. input integer : 50,000,000
• QEMU : 1m30.014s
• elfconv : 1m18.972s
18
Performance
1.13 times faster
1.14 times faster
• output other binary formats
– support WASM, ELF/x86-64 now
Future works
19
• compile ELF of other CPU architectures
– support aarch64 now
Future works
20
• append system calls emulation
– implement a part of system calls now
– Some system calls (e.g. fork, exec) are difficult to implement when targeting
WASM
• support dynamic linking
– support only static linking now
• performance analysis of WASM target
• make LLVM bitcode more efficient
Future works
21
repo: https://github.com/yomaytk/elfconv
Questions?, and I would like to get your opinions!
22

More Related Content

Similar to elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcode targeting WebAssembly_(FOSDEM'2024).pdf

Evolution of containers to kubernetes
Evolution of containers to kubernetesEvolution of containers to kubernetes
Evolution of containers to kubernetesKrishna-Kumar
 
A Comparison of .NET Framework vs. Java Virtual Machine
A Comparison of .NET Framework vs. Java Virtual MachineA Comparison of .NET Framework vs. Java Virtual Machine
A Comparison of .NET Framework vs. Java Virtual MachineAbdelrahman Hosny
 
Intel Briefing Notes
Intel Briefing NotesIntel Briefing Notes
Intel Briefing NotesGraham Lee
 
Bridging the Semantic Gap in Virtualized Environment
Bridging the Semantic Gap in Virtualized EnvironmentBridging the Semantic Gap in Virtualized Environment
Bridging the Semantic Gap in Virtualized EnvironmentAndy Lee
 
[Defcon24] Introduction to the Witchcraft Compiler Collection
[Defcon24] Introduction to the Witchcraft Compiler Collection[Defcon24] Introduction to the Witchcraft Compiler Collection
[Defcon24] Introduction to the Witchcraft Compiler CollectionMoabi.com
 
Lightweight Virtualization in Linux
Lightweight Virtualization in LinuxLightweight Virtualization in Linux
Lightweight Virtualization in LinuxSadegh Dorri N.
 
Web (dis)assembly
Web (dis)assemblyWeb (dis)assembly
Web (dis)assemblyShakacon
 
Let's Try Every CRI Runtime Available for Kubernetes
Let's Try Every CRI Runtime Available for KubernetesLet's Try Every CRI Runtime Available for Kubernetes
Let's Try Every CRI Runtime Available for KubernetesPhil Estes
 
Linux containers and docker
Linux containers and dockerLinux containers and docker
Linux containers and dockerFabio Fumarola
 
Running Applications on the NetBSD Rump Kernel by Justin Cormack
Running Applications on the NetBSD Rump Kernel by Justin Cormack Running Applications on the NetBSD Rump Kernel by Justin Cormack
Running Applications on the NetBSD Rump Kernel by Justin Cormack eurobsdcon
 
Containerization Is More than the New Virtualization
Containerization Is More than the New VirtualizationContainerization Is More than the New Virtualization
Containerization Is More than the New VirtualizationC4Media
 
Containers and OpenStack: Marc Van Hoof, Kumulus: Containers and OpenStack
Containers and OpenStack: Marc Van Hoof, Kumulus: Containers and OpenStackContainers and OpenStack: Marc Van Hoof, Kumulus: Containers and OpenStack
Containers and OpenStack: Marc Van Hoof, Kumulus: Containers and OpenStackOpenStack
 
Noah - Robust and Flexible Operating System Compatibility Architecture - Cont...
Noah - Robust and Flexible Operating System Compatibility Architecture - Cont...Noah - Robust and Flexible Operating System Compatibility Architecture - Cont...
Noah - Robust and Flexible Operating System Compatibility Architecture - Cont...Takaya Saeki
 
Containers #101 Meetup: Containers and OpenStack
Containers #101 Meetup: Containers and OpenStackContainers #101 Meetup: Containers and OpenStack
Containers #101 Meetup: Containers and OpenStackCodefresh
 
Containers #101 Meetup: Containers & OpenStack
Containers #101 Meetup: Containers & OpenStack Containers #101 Meetup: Containers & OpenStack
Containers #101 Meetup: Containers & OpenStack Brittany Ingram
 

Similar to elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcode targeting WebAssembly_(FOSDEM'2024).pdf (20)

64-bit ARM Unikernels on uKVM
64-bit ARM Unikernels on uKVM64-bit ARM Unikernels on uKVM
64-bit ARM Unikernels on uKVM
 
Evolution of containers to kubernetes
Evolution of containers to kubernetesEvolution of containers to kubernetes
Evolution of containers to kubernetes
 
A2O Core implementation on FPGA
A2O Core implementation on FPGAA2O Core implementation on FPGA
A2O Core implementation on FPGA
 
A Comparison of .NET Framework vs. Java Virtual Machine
A Comparison of .NET Framework vs. Java Virtual MachineA Comparison of .NET Framework vs. Java Virtual Machine
A Comparison of .NET Framework vs. Java Virtual Machine
 
Intel Briefing Notes
Intel Briefing NotesIntel Briefing Notes
Intel Briefing Notes
 
Bridging the Semantic Gap in Virtualized Environment
Bridging the Semantic Gap in Virtualized EnvironmentBridging the Semantic Gap in Virtualized Environment
Bridging the Semantic Gap in Virtualized Environment
 
[Defcon24] Introduction to the Witchcraft Compiler Collection
[Defcon24] Introduction to the Witchcraft Compiler Collection[Defcon24] Introduction to the Witchcraft Compiler Collection
[Defcon24] Introduction to the Witchcraft Compiler Collection
 
Lightweight Virtualization in Linux
Lightweight Virtualization in LinuxLightweight Virtualization in Linux
Lightweight Virtualization in Linux
 
Web (dis)assembly
Web (dis)assemblyWeb (dis)assembly
Web (dis)assembly
 
Let's Try Every CRI Runtime Available for Kubernetes
Let's Try Every CRI Runtime Available for KubernetesLet's Try Every CRI Runtime Available for Kubernetes
Let's Try Every CRI Runtime Available for Kubernetes
 
Linux containers and docker
Linux containers and dockerLinux containers and docker
Linux containers and docker
 
Running Applications on the NetBSD Rump Kernel by Justin Cormack
Running Applications on the NetBSD Rump Kernel by Justin Cormack Running Applications on the NetBSD Rump Kernel by Justin Cormack
Running Applications on the NetBSD Rump Kernel by Justin Cormack
 
olibc: Another C Library optimized for Embedded Linux
olibc: Another C Library optimized for Embedded Linuxolibc: Another C Library optimized for Embedded Linux
olibc: Another C Library optimized for Embedded Linux
 
A Robust and Flexible Operating System Compatibility Architecture
A Robust and Flexible Operating System Compatibility ArchitectureA Robust and Flexible Operating System Compatibility Architecture
A Robust and Flexible Operating System Compatibility Architecture
 
Containerization Is More than the New Virtualization
Containerization Is More than the New VirtualizationContainerization Is More than the New Virtualization
Containerization Is More than the New Virtualization
 
Containers and OpenStack: Marc Van Hoof, Kumulus: Containers and OpenStack
Containers and OpenStack: Marc Van Hoof, Kumulus: Containers and OpenStackContainers and OpenStack: Marc Van Hoof, Kumulus: Containers and OpenStack
Containers and OpenStack: Marc Van Hoof, Kumulus: Containers and OpenStack
 
Noah - Robust and Flexible Operating System Compatibility Architecture - Cont...
Noah - Robust and Flexible Operating System Compatibility Architecture - Cont...Noah - Robust and Flexible Operating System Compatibility Architecture - Cont...
Noah - Robust and Flexible Operating System Compatibility Architecture - Cont...
 
Containers #101 Meetup: Containers and OpenStack
Containers #101 Meetup: Containers and OpenStackContainers #101 Meetup: Containers and OpenStack
Containers #101 Meetup: Containers and OpenStack
 
Containers #101 Meetup: Containers & OpenStack
Containers #101 Meetup: Containers & OpenStack Containers #101 Meetup: Containers & OpenStack
Containers #101 Meetup: Containers & OpenStack
 
Erlang on OSv
Erlang on OSvErlang on OSv
Erlang on OSv
 

Recently uploaded

chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 

Recently uploaded (20)

chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 

elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcode targeting WebAssembly_(FOSDEM'2024).pdf

  • 1. elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcode targeting WebAssembly Masashi Yoshimura, NTT 2024/02/04 repo: https://github.com/yomaytk/elfconv 1
  • 2. • WebAssembly (WASM) is virtual machine instruction set • ✅ portable – enables to run apps on both browsers and servers without modification • ✅ secure – highly isolated from the host kernel on the server by WASI. • WASI is an API that provides access to several OS-like features (filesystems, sockets, …). • WASI is implemented by WASI runtimes (wasmtime, WasmEdge, …). – memory isolation with harvard architecture • architecture that physically separates memory for instructions and data. What is WebAssembly? Why using that? 2
  • 3. • ❌ limitation in the capability of apps – can jump to only the instructions that are determinable at compile time • cannot indirectly jump to the instructions generated in the data memory at runtime – WASI implementation doesn’t cover all POSIX APIs (e.g. fork, exec) What is WebAssembly? Why using that? 3
  • 4. Many programming languages support WASM (e.g. C, C++, Rust, Go, …). However, it isn’t easy to build WASM in some cases as follows. Case 1. The programming language that you want to use doesn’t completely support WASM Case 2. binaries are available, but the source codes of the binaries are not available – e.g.) The source code is not available under lisence Case 3. Time-consuming to building the environment – e.g.) you might be not able to build the dependent libraries because they are not maintained and so on. challenging in building WASM 4
  • 5. • TinyEMU: https://bellard.org/tinyemu/ – Author: Fabrice Bellard – x86 and RISC-V emulator available on the browser – Linux kernel can run on the browser • container2wasm: https://github.com/ktock/container2wasm – Author: Kohei Tokunaga, NTT – enables to run Linux kernel and container runtimes with emulators compiled to WASM (e.g. TinyEMU) – can run containers without modification on the browser and WASI runtimes But, emulators possibly incur large performance overheads… Existing projects that run Linux binaries on WASM AOT compile Linux binaries to WASM! 5
  • 6. • compiles Linux ELF binary to LLVM bitcode • existing compilers (e.g. emscripten) compile LLVM bitcode and the object of Linux syscalls emulation to WASM • elfconv is successor to myAOT: https://github.com/AkihiroSuda/myaot – Author: Akihiro Suda, NTT – An experimental AOT-ish compiler (Linux/riscv32 ELF → Linux/x86_64 ELF, Mach-O, WASM, ...) elfconv: AOT compiler from Linux/ELF to WASM 6
  • 8. • elfconv-Lifter – parse ELF binary, map every ELF section, etc… • remill (elfconv-Backend) : https://github.com/lifting-bits/remill – library for lifting machine code to LLVM bitcode How it works? (ELF -> LLVM bitcode) 8
  • 9. • convert a function to a LLVM IR function (e.g. _func1 -> @_func1_lift) – But, need to extract every function from ELF How it works? (remill) 9
  • 10. • convert a CPU instruction to a LLVM IR block (e.g. mov x2, x0 -> 1_mov) How it works? (remill) 10
  • 11. • convert a CPU instruction to a LLVM IR block – PC calculation, Operand calculation – call the function of the instruction-specific operation How it works? (remill) 11
  • 12. • The code of WASM can indirectly jump to only the code that is determinable at compile time. • currently, not support setjmp and longjmp. How it works? (indirect jump) 12
  • 13. • statically link LLVM bitcode and elfconv-Runtime • elfconv-Runtime – mapped memory (stack, heap), Linux system calls emulation How it works? (LLVM bitcode -> WASM) 13
  • 14. • libc implementation: emscripten, wasi-libc, etc… Case 1. use libc function if it exists (e.g. write) How it works? (Linux syscalls emulation) 14
  • 15. • libc implementation: emscripten, wasi-libc, etc… Case 2. pseudo-implement the syscall if it doesn’t exist (e.g. brk) How it works? (Linux syscalls emulation) not use brk (unsigned long brk) 15
  • 16. • target sample ELF binary: prime number calculator – compute all prime numbers less than the input integer • Test: ELF/aarch64 -> LLVM bitcode -> ELF/x86_64 (not WASM) – current system calls emulation for WASI runtimes is insufficient, so we use x86_64 as the output binary for benchmark tests. • comparison : QEMU emulation aarch64 to x86_64 16 Performance QEMU emulation vs. binary AOT compilation
  • 17. Case 1. input integer : 10,000,000 • QEMU : 9.437s • elfconv : 8.353s Case 2. input integer : 50,000,000 • QEMU : 1m30.014s • elfconv : 1m18.972s 17 Performance
  • 18. Case 1. input integer : 10,000,000 • QEMU : 9.437s • elfconv : 8.353s Case 2. input integer : 50,000,000 • QEMU : 1m30.014s • elfconv : 1m18.972s 18 Performance 1.13 times faster 1.14 times faster
  • 19. • output other binary formats – support WASM, ELF/x86-64 now Future works 19
  • 20. • compile ELF of other CPU architectures – support aarch64 now Future works 20
  • 21. • append system calls emulation – implement a part of system calls now – Some system calls (e.g. fork, exec) are difficult to implement when targeting WASM • support dynamic linking – support only static linking now • performance analysis of WASM target • make LLVM bitcode more efficient Future works 21 repo: https://github.com/yomaytk/elfconv
  • 22. Questions?, and I would like to get your opinions! 22