SlideShare a Scribd company logo
1 of 16
Who am I?
● Sean Young
● Maintainer of Burrow at Monax
● Maintainer of consumer IR and DVB in linux kernel
● Interested in compilers and reverse engineering
Compiling Solidity to WASM
using solang
Solidity/EVM today
 EVM is the Virtual Machine, Solidity the source language
 Simple high-level language inspired by JavaScript
 All arithmetic and memory operations are 256 bit
 Only Solidity supports EVM, no other languages
 string/per-byte processing difficult
 More accurate gas cost
 Not amenable to JIT function foo(string owner) {
if (owner == "root") {
// does not compile
}
if (sha256(owner) == sha256("root")) {
// recommended solution
}
}
WASM
 WASM is a general, low-level virtual machine
 Ability to JIT and great implementations exist
 Arithmetic is 32 and 64 bit
 Memory can be accessed in all the usual ways
 32 bit and 64 floats do exist – not deterministic
 Has fantastic tooling available
Why have Smart Contracts in WASM?
 Support for languages other than Solidity
 Linking Solidity with other languages:
 Cryptography in Smart Contracts
 Re-use existing libraries (e.g. BPM)
 Efficient data structures
 Improving the Solidity Language
 Re-use existing tooling (llvm compiler kit)
 How to do 256 integer arithmetic on 64 bit VM?
Existing Solidity compiler
 Huge C++ project
 Handwritten lexer/parser
 Handwritten optimiser (YUL)
 EVM output is not optimal
What is solang?
 Small, clean implementation of Solidity
 Written in rust
 Generated lexer and parser
 Use llvm:
 Wonderful optimiser
 Writes wasm binary format
 Solves 256 bit integer problem
 Includes linker (e.g. C or rust modules)
 Fraction of the code base
 Proper string processing and string formatting possible
Where is solang today?
 $ cargo install solang
 Hyperledger Labs project
 Incomplete Solidity language support
 Supports intN, uintN, bool, enum types
 Supports if/else, for, while
 Supports contract storage
 ABI compatible with Solidity/EVM
 Supported in Hyperledger Burrow 0.27.0
Running solang with Burrow today
 Install burrow 0.27.0
 Install solang (cargo install solang)
 When deploying contracts, specify –wasm
 Entirely transparent: solang will be used instead solc and
WASM VM rather EVM
burrow deploy –wasm -v Root_0 deploy.yaml
Solang implementation
Traditional compiler construction. The stages are:
1)Lexer and parser produces Abstract Syntax Tree (AST)
2)AST is decorated (resolved) and transformed into Control
Flow Graph (CFG)
3)CFG is mapped to LLVM IR
4)Call LLVM optimiser and generate and link
1) Lexer and parse stage
 Parser is generated from Solidity grammar using lalrpop
 Outputs AST tree, with symbols locations so we can
provide locations on error and warning messages
 Mostly generated
2) Resolve and Control Flow Graph
 Walks the contracts in the AST
 Generates CFG for statements
 Resolves variables, functions
and types (e.g. enums)
# solang –emit-cfg
foo:
x = 100
y = bar * 100
C = y > 100
brcond c, bar1, bar2
bar1:
a = y + 200
return a
bar2:
b = y << 4
return b
3) Translate to LLVM IR
 Intermediate Representation of
code used LLVM
 Mapping from CFG to LLVM IR
should be mostly 1-to-1
 Also generates ABI
encoder/decoder
 solang –emit-llvm
solang –-emit-llvm foo.sol
; ModuleID = 'bar'
target triple = "wasm32-unknown-unknown-wasm"
define i32 @foo(i32, i32) {
entry:
%2= add i32 %0, 100
%3= icmp sgt i32 %1, 0
br i1 %3, label %then, label %else
then: ; preds = %entry
%4= mul i32 %1, 100
%5= add i32 %2, %4
br label %endif
else: ; preds= %entry
%6= add i32 %1, 10
br label %endif
endif: ; preds= %else, %then
%x= phi i32 [ %5, %then ], [ %6, %else ]
reti32 %x
}
4) LLVM generate and link
 Call LLVM optimizating passes
 Call LLVM code generate
 Result: WASM object file
 Need to link into final contract
 Currently done in rust, can be done by LLVM linker too
 solang --no-link
Future: solang linking against C, etc
LLVM LTO linker can link against wasm objects:
C:
clang --target=wasm32 -c -O3 -Wall foo.c
solang contract.sol –link foo.o
Next steps for solang
 Make compatible with eWASM
 More data types: string, bytes, struct, mapping, arrays
 Other language features: modifiers, external calls, imports,
interfaces
 Testcases
 Get involved!
Thanks!
sean.young@monax.io
https://github.com/hyperledger-labs/solang/
https://monax.io/

More Related Content

Similar to Compiling Solidity to WASM using solang

07 140430-ipp-languages used in llvm during compilation
07 140430-ipp-languages used in llvm during compilation07 140430-ipp-languages used in llvm during compilation
07 140430-ipp-languages used in llvm during compilationAdam Husár
 
Web (dis)assembly
Web (dis)assemblyWeb (dis)assembly
Web (dis)assemblyShakacon
 
Raising the Bar on Robotics Code Quality
Raising the Bar on Robotics Code QualityRaising the Bar on Robotics Code Quality
Raising the Bar on Robotics Code QualityThomas Moulard
 
cs262_intro_slides.pptx
cs262_intro_slides.pptxcs262_intro_slides.pptx
cs262_intro_slides.pptxAnaLoreto13
 
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcod...
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcod...elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcod...
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcod...Masashi Yoshimura
 
Porting To Symbian
Porting To SymbianPorting To Symbian
Porting To SymbianMark Wilcox
 
SFO15-500: VIXL
SFO15-500: VIXLSFO15-500: VIXL
SFO15-500: VIXLLinaro
 
Moving NEON to 64 bits
Moving NEON to 64 bitsMoving NEON to 64 bits
Moving NEON to 64 bitsChiou-Nan Chen
 
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to WebAssembly...
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to WebAssembly...elfconv: AOT compiler that translates Linux/AArch64 ELF binary to WebAssembly...
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to WebAssembly...Masashi Yoshimura
 
Experiments in Sharing Java VM Technology with CRuby
Experiments in Sharing Java VM Technology with CRubyExperiments in Sharing Java VM Technology with CRuby
Experiments in Sharing Java VM Technology with CRubyMatthew Gaudet
 
A world to win: WebAssembly for the rest of us
A world to win: WebAssembly for the rest of usA world to win: WebAssembly for the rest of us
A world to win: WebAssembly for the rest of usIgalia
 
Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Javier Tallón
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
AllBits presentation - Lower Level SW Security
AllBits presentation - Lower Level SW SecurityAllBits presentation - Lower Level SW Security
AllBits presentation - Lower Level SW SecurityAllBits BVBA (freelancer)
 
compressed.tracemonkey-pldi-09.pdf
compressed.tracemonkey-pldi-09.pdfcompressed.tracemonkey-pldi-09.pdf
compressed.tracemonkey-pldi-09.pdfGinaMartinezTacuchi
 
compressed.tracemonkey-pldi-09.pdf
compressed.tracemonkey-pldi-09.pdfcompressed.tracemonkey-pldi-09.pdf
compressed.tracemonkey-pldi-09.pdfMaherEmad1
 
Developing Applications for Beagle Bone Black, Raspberry Pi and SoC Single Bo...
Developing Applications for Beagle Bone Black, Raspberry Pi and SoC Single Bo...Developing Applications for Beagle Bone Black, Raspberry Pi and SoC Single Bo...
Developing Applications for Beagle Bone Black, Raspberry Pi and SoC Single Bo...ryancox
 

Similar to Compiling Solidity to WASM using solang (20)

07 140430-ipp-languages used in llvm during compilation
07 140430-ipp-languages used in llvm during compilation07 140430-ipp-languages used in llvm during compilation
07 140430-ipp-languages used in llvm during compilation
 
Wasm intro
Wasm introWasm intro
Wasm intro
 
Web (dis)assembly
Web (dis)assemblyWeb (dis)assembly
Web (dis)assembly
 
Raising the Bar on Robotics Code Quality
Raising the Bar on Robotics Code QualityRaising the Bar on Robotics Code Quality
Raising the Bar on Robotics Code Quality
 
Embedded C programming session10
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
cs262_intro_slides.pptx
cs262_intro_slides.pptxcs262_intro_slides.pptx
cs262_intro_slides.pptx
 
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcod...
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcod...elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcod...
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to LLVM bitcod...
 
Porting To Symbian
Porting To SymbianPorting To Symbian
Porting To Symbian
 
SFO15-500: VIXL
SFO15-500: VIXLSFO15-500: VIXL
SFO15-500: VIXL
 
Moving NEON to 64 bits
Moving NEON to 64 bitsMoving NEON to 64 bits
Moving NEON to 64 bits
 
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to WebAssembly...
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to WebAssembly...elfconv: AOT compiler that translates Linux/AArch64 ELF binary to WebAssembly...
elfconv: AOT compiler that translates Linux/AArch64 ELF binary to WebAssembly...
 
Experiments in Sharing Java VM Technology with CRuby
Experiments in Sharing Java VM Technology with CRubyExperiments in Sharing Java VM Technology with CRuby
Experiments in Sharing Java VM Technology with CRuby
 
A world to win: WebAssembly for the rest of us
A world to win: WebAssembly for the rest of usA world to win: WebAssembly for the rest of us
A world to win: WebAssembly for the rest of us
 
Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
AllBits presentation - Lower Level SW Security
AllBits presentation - Lower Level SW SecurityAllBits presentation - Lower Level SW Security
AllBits presentation - Lower Level SW Security
 
compressed.tracemonkey-pldi-09.pdf
compressed.tracemonkey-pldi-09.pdfcompressed.tracemonkey-pldi-09.pdf
compressed.tracemonkey-pldi-09.pdf
 
compressed.tracemonkey-pldi-09.pdf
compressed.tracemonkey-pldi-09.pdfcompressed.tracemonkey-pldi-09.pdf
compressed.tracemonkey-pldi-09.pdf
 
Developing Applications for Beagle Bone Black, Raspberry Pi and SoC Single Bo...
Developing Applications for Beagle Bone Black, Raspberry Pi and SoC Single Bo...Developing Applications for Beagle Bone Black, Raspberry Pi and SoC Single Bo...
Developing Applications for Beagle Bone Black, Raspberry Pi and SoC Single Bo...
 

Recently uploaded

%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 

Recently uploaded (20)

%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Compiling Solidity to WASM using solang

  • 1. Who am I? ● Sean Young ● Maintainer of Burrow at Monax ● Maintainer of consumer IR and DVB in linux kernel ● Interested in compilers and reverse engineering Compiling Solidity to WASM using solang
  • 2. Solidity/EVM today  EVM is the Virtual Machine, Solidity the source language  Simple high-level language inspired by JavaScript  All arithmetic and memory operations are 256 bit  Only Solidity supports EVM, no other languages  string/per-byte processing difficult  More accurate gas cost  Not amenable to JIT function foo(string owner) { if (owner == "root") { // does not compile } if (sha256(owner) == sha256("root")) { // recommended solution } }
  • 3. WASM  WASM is a general, low-level virtual machine  Ability to JIT and great implementations exist  Arithmetic is 32 and 64 bit  Memory can be accessed in all the usual ways  32 bit and 64 floats do exist – not deterministic  Has fantastic tooling available
  • 4. Why have Smart Contracts in WASM?  Support for languages other than Solidity  Linking Solidity with other languages:  Cryptography in Smart Contracts  Re-use existing libraries (e.g. BPM)  Efficient data structures  Improving the Solidity Language  Re-use existing tooling (llvm compiler kit)  How to do 256 integer arithmetic on 64 bit VM?
  • 5. Existing Solidity compiler  Huge C++ project  Handwritten lexer/parser  Handwritten optimiser (YUL)  EVM output is not optimal
  • 6. What is solang?  Small, clean implementation of Solidity  Written in rust  Generated lexer and parser  Use llvm:  Wonderful optimiser  Writes wasm binary format  Solves 256 bit integer problem  Includes linker (e.g. C or rust modules)  Fraction of the code base  Proper string processing and string formatting possible
  • 7. Where is solang today?  $ cargo install solang  Hyperledger Labs project  Incomplete Solidity language support  Supports intN, uintN, bool, enum types  Supports if/else, for, while  Supports contract storage  ABI compatible with Solidity/EVM  Supported in Hyperledger Burrow 0.27.0
  • 8. Running solang with Burrow today  Install burrow 0.27.0  Install solang (cargo install solang)  When deploying contracts, specify –wasm  Entirely transparent: solang will be used instead solc and WASM VM rather EVM burrow deploy –wasm -v Root_0 deploy.yaml
  • 9. Solang implementation Traditional compiler construction. The stages are: 1)Lexer and parser produces Abstract Syntax Tree (AST) 2)AST is decorated (resolved) and transformed into Control Flow Graph (CFG) 3)CFG is mapped to LLVM IR 4)Call LLVM optimiser and generate and link
  • 10. 1) Lexer and parse stage  Parser is generated from Solidity grammar using lalrpop  Outputs AST tree, with symbols locations so we can provide locations on error and warning messages  Mostly generated
  • 11. 2) Resolve and Control Flow Graph  Walks the contracts in the AST  Generates CFG for statements  Resolves variables, functions and types (e.g. enums) # solang –emit-cfg foo: x = 100 y = bar * 100 C = y > 100 brcond c, bar1, bar2 bar1: a = y + 200 return a bar2: b = y << 4 return b
  • 12. 3) Translate to LLVM IR  Intermediate Representation of code used LLVM  Mapping from CFG to LLVM IR should be mostly 1-to-1  Also generates ABI encoder/decoder  solang –emit-llvm solang –-emit-llvm foo.sol ; ModuleID = 'bar' target triple = "wasm32-unknown-unknown-wasm" define i32 @foo(i32, i32) { entry: %2= add i32 %0, 100 %3= icmp sgt i32 %1, 0 br i1 %3, label %then, label %else then: ; preds = %entry %4= mul i32 %1, 100 %5= add i32 %2, %4 br label %endif else: ; preds= %entry %6= add i32 %1, 10 br label %endif endif: ; preds= %else, %then %x= phi i32 [ %5, %then ], [ %6, %else ] reti32 %x }
  • 13. 4) LLVM generate and link  Call LLVM optimizating passes  Call LLVM code generate  Result: WASM object file  Need to link into final contract  Currently done in rust, can be done by LLVM linker too  solang --no-link
  • 14. Future: solang linking against C, etc LLVM LTO linker can link against wasm objects: C: clang --target=wasm32 -c -O3 -Wall foo.c solang contract.sol –link foo.o
  • 15. Next steps for solang  Make compatible with eWASM  More data types: string, bytes, struct, mapping, arrays  Other language features: modifiers, external calls, imports, interfaces  Testcases  Get involved!

Editor's Notes

  1. Straw poll: Solidity, Kafka, elliptic curve diffie helman?
  2. Straw poll: Solidity, Kafka, elliptic curve diffie helman?