Control Flow Graph Reconstruction from EVM bytecode

Control Flow Graph Reconstruction from EVM bytecode · 22 May 2024 · 1:04:37

Dr Franck Cassez delivers a talk at the Ethereum Engineering Group on reconstructing control flow graphs from EVM bytecode, a technique for analysing how compiled smart contracts behave. Slides were shared alongside the recording.

Speakers

Topics

From Control Flow Graph Reconstruction from EVM bytecode, hosted by Ethereum Engineering Group.

Transcript

hello and welcome everyone my name is Peter Robinson and this is the ethereum Engineering Group Meetup so today we've got Dr Frank Cass who is going to talk about um call graph analysis in evm bite code um so Frank before you present your slides um do you want to quickly introduce yourself yes thank you Peter uh so uh yeah I'm currently working at movement Labs I've started a week ago so that's very new and I'll describe a bit later on what movement Labs is is doing and my background is in formal verification formal methods and so on so I've been in

the blockchain ecosystem for five years now and I'm trying to apply some rigorous techniques to make the web 3 more secure and today so I'm going to talk about some work related to this topic which is U about the building control flow graphs for a lowlevel code evm bite code cool please um present your slides take it away yeah so apparently I can't share my screen said the host has disabled the participant sh screen yeah all these settings Advance sharing options yeah

have a go now yep so hopefully you should all see my screen I'll zoom in now is it okay you see a yellow background with um perfect yeah yeah during during the talk feel free to interrupt me and ask questions anytime you think it's unclear or you would like to know more I'll be happy to to talk more about this and if someone can monitor the chat I'm going to minimize

the your faces so right uh so first yeah first things first uh what movement labs are doing at the moment so we are trying to build a a move based ZK rollup on ethereum so move is a programming language for smart contracts and it was developed at Facebook in the DM Libra project and since has been let's say taken by a few startups including Aptos sui uh who are actually implementing layer ones or layer twos uh using the move language and the move program so the idea is to bring safer smart contracts execution to ethereum so

if you want to know more you can look up at the movement labs documentation and website there's a lots of things going on and it's a it's a young project so we are still building uh lots of things uh at the moment so don't expect to have something fully fledged but there's lots of very good ideas and we hope we can develop something in the coming semester and release something so for today I'm going to talk um more about U evm bite code so this is about smart contracts execution on ethereum and to motivate the the title of the talk which is building control flow grass for evm bite code so that was what that was not my

main motivation at the beginning I wouldn't have done that if I hadn't need it uh so I'm going to motivate why I ended up doing these kind of things so first uh if you think about how smart contracts are developed and deployed on ethereum you usually write your smart contracts using solidity Viper rust or go or whatever you want and there compilers that can compile to at the end what's on the right hand side evm by code lowlevel code and there's a few tools so you can go even through an intermediate representation there's a few but one of them that's a popular one that the solidity compiler can generate is called U and then from U you can also

compile to EV bite code so there's quite a few options and at the end of the day uh so what what I'm interested in and and what people are usually interest in is you want to make sure that your contracts uh meet some safety requirements somehow that there's no bug in the contracts no bug of a certain type so you can do the reasoning at the solidity level can try to analyze the solidity Code test it or formally reason about it same with Viper rust or go but ultimately when you compile your code what's going to be executed uh is the evm bite code so for instance if there is a bug in the compiler from solidity to M bite code then your AVM bite code will not perform or or realize the tasks

that were specified by the solid code the semantics will not coincide so this is something that you would be interested to do actually to try and there are bugs that in the compilers there are non bugs that are fixed on a regular basis and discovered and fixed on a regular basis so you'd like to be able to reason about the bite code somehow uh to make sure that your bite code is not doing uh silly things or incorrect things and it's hard to reason about bite code because it's lowlevel code it's usually not structured and so on uh so you you really have a lot of issues to deal with reasoning at this low level so I'll show you um in the next few slides what we can do to be

able to reason about the bite code so an example here is I'm going to take my let's say so solidity or or whatever high level representation and I compile it with the solidity compiler and I get some sequences of of bytes which is the evm bite code and I like to be able to reason but formally reason about it so an example would be I'd like to make sure that in the evm bite code you've got a stack machine there's no stack underflow or stack Overflow in in my in my program that's something I'd like to be able to formally prove so not test but formally prove so to do so my strategy is to say it's not only my strategy I should actually say that the

strategy is to take the evm bite code and to try to embed it into a tool that can enable you to do some formal reasoning about the bite code and it turns out that with some of my colleagues some of them are on disc call David dorio and so on we have developed in the last few years an EVN so the semantics of an etherum virtual machine in DNE so we can reason about evm by code uh sequences of of of op codes using these DNE VM and there's a link that you can access to but if you look it up if you Google it you'll find the DNE VM easily so the idea would be to take the evm B code

which is a sequence of unstructured instructions and so on and to generate something that can be analyzed using the D EV so that's not trivial so I'm going to show you how it could look like so this is this program for instance it's got a loop and it does some storage and return and so on you compile it with the solidity compiler you get sequencies of byes the bite code and to be able to reason about it what I'd like to produce at the end of the day is a sort of let's call it a proof object it's an instrumented code in daff so it's something that will mimic the execution of the bite code so you see here for instance that I've got a state state number one uh is the image of state zero after doing a push zero so I'm just

applying the semantics of the instructions here but I do it within the DM so these instructions push zero duplicate and so on there are DNE evm instructions and this code is some DNE code and if I can do that so I embed the execution of the bite code into the DM I'm able to reason about the bite code for instance here I'm able to prove that whatever is going to happen starting from a given state with some properties that the the stack has well the capacity of the stack is larger than two if I execute push zero and duplicate one so that's a p zero and duplicate one element then I end up in a normal state of the evm a non eral state so I can

prove properties about the bite code using this Dum right so I'm not going to dive into the Dum there's a few papers and so on you can have a look at but the idea is to try and take some high Lev code get the bite code for it and reason about the B code by embedding it into some DNE code and to do so so I wanted to do these kind of things and actually to produce this kind of code so you see here that the code is is split into a different chunks so there's a sort of a execution of a segment from a few instructions and then we execute another segment of instructions and so so we would like to sort of uh mimic or

retrieve the structure of the initial program if we can and to do so as well we need to be able to to somehow um find or or or get some properties where the where the control flow is going into the bite code because it's not structured bite code it's got jumps and goto statement somehow so we need to recover these kind of things to be able to reason about it so that's why uh in in in as a sort of preliminary work to generate this kind of code that I can use to reason about the bite code I'm going to get some information about the initial bite code and generate What's called the control flow graph from which I can extract this kind of pro object and then I'll be able to reason about

the B code does that make sense or I guess it's yes Frank so so how you choosing the segmentation yes so I'm going to talk about it one yeah yeah thank you for the question yeah so I'm going to get into the details of this in the next few slides so what I'm going to present today is first an algorithm to reconstruct let's say construct a control flow graph and I'll tell you what the control flow graph is an algorithm to do so to demonstrate some tooling that we've been developing in the last year and also a feature that's usually ignored in in this kind of work

is to verify that the graphs that I generate are control flow graphs are semantically correct with respect to the semantics of VM so how do I reconstruct or construct a graph and what is a control flow graph so first thing first so if you compile this code again this U code into bite code you'll get a sequence of bites so this is what's on the left hand side and you can somehow disassemble it to get a more friendly version of it and this more friendly version says that for instance the 5f is the up cut for push Zero 8 Z is the upod for duplicate one and you see on the left hand side is here it's the program counter so the instructions are laid out in

memory uh next to to the previous one so this is the program counter that uh progressing and each address has an instruction associated with it so the idea is in in the code most of the time you execute an instruction and you go to the next one but sometimes there are instruction that are goto instructions or jump instructions there's two in this in this simple Loop program there's a conditional jump so this is this one and the conditional jump is going to use the information that's on the the stack of the evm so the evm is a stack machine so the jump uh e jump I instruction the

conditional jump is going to look up the second topmost symbol or value at the top of the stack and uh depending on whether it's zero or or not zero it's going to jump to the address which is at the top of the stack so it consumes the execution of this operation consumes two um elements at the top of the stack the condition and the target if the condition is through it jumps and if the condition is false it it goes to the next instruction in the code so that's a conditional G so you you will see it in in the graph you would like to see something as you execute the code and then uh if a condition is true you jump if the condition is false you don't jump

so you go to the next instruction U sorry um you go to the next instruction in the code and the jump instruction here is a is a sort of a simple version of a conditional jump It's a non-conditional jump so on a jump instruction you jump uh to the the target address in the code so the PC that is at the top of the stack so this jump here will jump to the address zero X2 let's go back so you see that in my graph I execute the code and then I jump back to ZX2 so the way I interpret the graph on the right hand side is every segment so to answer your question um around every segment is a segment of

instructions well there's no jump in it so there's no um um branching so it's a linear sequence of instructions and the semantics I apply to the graph is when there is an edge from one note of the graph to another you execute you first execute what's in in a no of a graph sequentially so because it's a linear sequence you execute from the top to the bottom and a edge like that would mean that you jump to the next one and you execute from the first instruction to the last and then from this one you execute from the first instruction to the last and then you jump back to the first instruction and so so that's the the meaning of these kind of blocks so in standard control

flow graphs you would have a node per instruction but because here there's no condition or choices there's no branching in in a in a sequence of instructions which is in in what I call a segment I'm going to collapse all the instructions uh that are together into a segment and put them together and and make sure that the B the jumps or the conditional branchings only happen at the boundaries of these segments that's does that answer your question one yeah thanks okay thank okay so what I'd like to do is to build these kind of graphs and and why is it useful because these graphs can actually be consumed by

Static analyzers and so on they encode sequencies of instruction so if you want to know for instance whether you can reach this kind of return statement you would like to analyze this graph and try to check whether this node is reachable for instance um if it's not reachable that should probably be an issue so you'd like to know what's happening you can discover in the graph whether they have loops or not so there's a loop here but you see here that for instance my my initial program has a loop that's going to be taken 10 times so that's the semantics of this 10 here but in the graph it it shows up as a a loop that can be possibly iterated an arbitrary number of times but that's perfectly fine that's

what control prograph should be uh should be they should be over approximating the set of executions of the of the program so that that's the kind of objects I would like to build and then I'll use that to build some Pro object that I can formally analyze with that right so a graph again is going to have segments in each segment it's a linear sequence of instruction and then the boundary of a segment they can be either a jum to another segment or a conditional branching to to different different noes so it's at most two in the set of instructions I'm using I'm not using the new um relative jumps R jumps in the evm where you can Branch to

an arbitrary number of of of of the children but the code I have written actually is ready to accommodate these kind of changes right so let's see uh what I have to do uh so I would say in in this first example that was very easy because if I know the cement of the jump jump I and jump right the target the possible targets of the jumps are just pushed before the instruction is executed so it's easy to construct this graph I just have to track uh what's on the top of the stack and I'll know where the control is going to go in the next uh in the next instruction so for every instruction in segment again I jump to

the next instruction and when I'm at the boundary I have to determine where the the code can go and for jump a conditional one it could go to the next instruction or to this 013 that was pushed previously but it can be a bit more tricky so for instance this is another program Computing the maximum of three numbers and it's got a Max function that computes the max of two numbers but it's called twice so in this code you'll see that actually um the orange part is the in the bite code is the function the max function and the green part is the sort of the main the body of the function this m store and the the letter just declaring variables and so so you see here that I've got a

jump and the jump that occurs here uh actually there's a pop before so it pops something of the stack so it's not easy well it's not obvious where uh the target is for this jump segment and it turns out actually that um because there's two calls to uh to the max function uh when you execute the code this this segments or this section of code will be executed twice or could be executed twice and the target of the jump here will be different depending on the call site so that's what's reflected in this control graph you see that the first jump is here and when it jumps it's going to jump basically after the

computation of Max to the computation of the the value of R in the initial high level code so that's that's here and the last time of the jump is to finish the program so that's the equivalent of finishing the C to Max here and going to storing and returning the result so that's the difficulty in in in reconstructing control flow graphs is that the target of jumps can be dynamically computed so they are they are somewhere but they're not obvious in the code you might need to do some work to retra them so in in the next let's say few slides I'm going to provide Prov a way of resolving this target of jumps and the Assumption I'm

going to make is that all the jump targets that I would need in my code will be on the stack so you can ask me a question later about that I can give some examples as way why this is a well a meaningful assumption so if I assume that all the gem targets are on the stack then I can find a mechanism to build a short flow graph so the the program I showed before so I'm going to unfold the graph as much as I can so I unfold the first segment and here this jump as I mentioned before is known because it jumps to the Target that has been just pushed before Z X1 B so that's the program counter and fold and unfold

until I arrived at a jump and this one is not obvious I don't really know uh where to jump to and sorry uh and to to know this jump so I'm going to work it backwards so if I want to know where to jump here so the jump instruction by definition is going to jump to the element that to the Target address that's at the top of the stack so if I want to know the target address here I'm just working backwards because there's a pop the pop is going to pop one element on top of the stack then uh if I want to know the target of this jump I need at the beginning of this segment to know what's not at the top of the stack but the second most topmost

element of the stack so I will use pick zero for the top element of the stack pick one for the second pick two for the third and so on so if I want to resolve the jump here I need to know pick one at the beginning of this segment and then I can work it backwards as well I can propagate backwards saying okay if I want the the element pick one here uh and it's called by this and I come from this node what element do I at the top of the of the segment it's pi to and so on and I work it backwards and I can resolve it by finding out that in the first segment actually at the end of execution this first segment I need P two and P two is the address 0x0 e so I resolve the first uh jump like that by

working it backwards and then I continue to unfold My Graph until I hit another U non non- trivial jump or dynamic jump and I'm going to do exactly the same thing so work it back and so on and then I can resolve it and I've buil the the full graph so that's the idea so the idea is that if the um the target of jumps are on the stack what I'm going to do is to try and and provide a sort of an abstract semantics when I I'm going somehow to abstractly execute the code in these segments and keep track of some elements in the stack to be able to determine the jumps the target of the jumps at each at each

location so how do I do that so yeah and and one other thing that I should probably mention is you you can see here that I've got different nodes with the same the same code actually it start at the same control location and so on but these nod in the graph are different nodes because they don't lead to the same future so from this node you've got a future in terms of control locations which is given by this set of path right whereas from this node it's a smallest set of path so I need to distinguish these two nodes so the question as well is how do I decide whether when I encounter the same

program the same segment I would say in My Graph when I unfold the graph when I encounter the same segment I do I merge them or how do I decide that they should lead to different um Futures so I can't merge mer them so to do so what I'm going to do is to use an abstract semantics again with an abstract stack and um so you you see here for instance in this first segment I'm pushing some values so push one is pushing one bite onto the stack I'm pushing some value I'm some values I'm swapping them and I'm jumping um so to to be able to to keep track of the jumps

uh what I would like to do is to keep track of the elements of of on the stack and um I so what I could do is to record everything that's on the stack right that would be an easy thing uh but that would actually lead to to some issues later on so what I'm going to do is to make sure that when I record what's on the stack and that's why it's an abstract stack it's actually something that could be a Target address and fortunately in the evm that's something you can do because Target addresses uh should be in a given set of addresses that are tagged with an instruction called a jump a jump Des so I know that every time I will have a jump right in

the evm it should be a jump to a jump Des so what I'm going to do here is I'm going to abstractly interpret the code so I'm going to interpret this code and store U on the stack values that are jump destinations so only if I look at my code it's only a few and the other ones uh if if if a value like for instance 0x08 is pushed on the stack I'm going to abstract it away with a question mark because I don't need it well that's probably this question mark because it's not going to be a target of a jump right so that's what I'm going with this abstract stack I'm simulating the computation tracking some

interesting values that can be targets of jumps and if I do so so I start with an empty stack and when I execute this code I end up with an abstract stack which is the top of the stack is on the left so I don't know well I abstract the value it's an unknown or irrelevant value then there's a zero x a zero e value and then 0 X12 value and I do that for all the computation and it turns out that when I've got a jump here for instance the abstract stack if I have defined it properly uh will contain the good value that I have to jump to and you see that for instance from this code

uh I start with a stack which is some value 0 x e two two different values that I don't want to know the value of 0x12 if I execute this code I pop the top one and then the value that's left is 0 x e and that's the target of the next J so I jump to this Target and I do the same for all the the nodes and this way I'm able actually to build a control flow graph so now what about um merging nodes and so on so you see here that these two nodes that I claimed previously were different in the graph because they led to different Futures I can now formally Define why they are

different it's because that's the same code but the context in which they execute the stack is different for the two nodes so I can't merge them and I would not merge them so that's the the ID to generate uh the code the graph sorry to have an abstract stack so I said a couple of seconds ago that um if I would push all of the values for instance on the stack it could lead to some issues and I can quickly let's say mention why so assume I do the same code for instance and sometimes I push some irrelevant value z x so when I say irrelevant values that are not really useful uh to to keep track of the of the program

counter and J Jump destinations I could push two different values like 0x8 and 0x9 and they would be in the stack so even if let's say the future the computations are the same because the stack is 0x e 0x12 and this will lead to the same um program counter sequences if different values were uh in between I would say that these two states maybe two states would be different whereas actually I should be able to merge them to merge them so I'd like to abstract away all of the irrelevant information that will not contribute to the jump targets resolution all right so now what's the

algorithm so I implemented it in in dney so I give you a sketch here but the algorithm is basically to define the State uh of the graph and the state of the graph is a a segment number so if you wish it's a start a PC of a segment and the segments have been determined before and a content of the stack and it can also be an eror state if some error happens I'll show you later on how this can occur but that's an abstract State and the control flow graph algorithm to reconstruct the control flow graph I just do the depth search on these abstract States any question so

far not anything related to does it terminate or yes yeah Frank yeah I um I was a little unsure so you're maintaining um abstract Stacks essentially at the beginning of each block are so do you have multiple Stacks can you have multiple stacks for each at each block if that makes sense like different cont multiple contexts for each block effectively no so you well if you have different if you have if you have different context right a state is the code of a segment and a stack right so you can have multiple so you have multiple nodes or states in the

graph but the stack can be different and the code can be the same but they are so yeah that I agree that's a bit maybe I was not clear enough but you you may what you want to do when you build the graph is basically it's a finite State machine and defines a a language Tres right so in these two cases you can't really say well if you would if you would merge these two nodes your sort of control flow graph would have more path or it would be less precise than distinguishing them so that would still be a good graph but less precise in terms of reasoning so I could merge them but that would not be a good idea if I want to perform some analysis later on

so I need to distinguish it to distinguish them and to do so I use a state which is the context which is the stack which somehow defines the future in terms of which blocks I'm going to which segments I'm going to reach and the code that's going to be executed so by definition right if I have two different context and the same code they will be two different nodes yeah yeah I understand yeah does that make sense or yeah that makes sense would you ever merge them like there are situations where actually you can gen you end up generating uh let's say different nodes but it actually turns out the abstract Stacks by just a coincidence are

identical so yeah yeah yeah so that's good question so I can answer it now G um when I generate the graphs I first generate this graph and this graph is called in in my generator sort of the row or non- minimized graph so I'm going to generate it and then I can apply a minimization algorithm on the graph that I generate and the minimization algorithm is going to preserve the language accepted by the automat yeah okay right so this one is safe in terms of semantics I'm not going to merge at construction time I'm going to merge in a later phase if if I if I find out that these two nodes actually um well let's say when I compute it I don't know because the two states here are

different I don't know whether they will lead to the same future but when I've built the whole graph I can apply standard minimization algorithm to merge the nodes that are encoding the same language until the end and I implemented that as well yeah and so you mentioned termination so I'm guessing you just iterate till you reach a fix point is that what happens here no I to Max to a maximum depth okay because I don't know whether this is going to terminate presumably it should terminate because um I I I totally agree so the proof again I think is in the pudding my assumption is that the the targets are on the stack and I provide a maximum

depth and i r run my depth resarch and at the end of the algorithm I get a result which is yes um the maximum depth was never reached so I could build the graph or know the maximum depth was reached and then I'm going to increase it and at some point it should I'll give you an example where it doesn't work but at some point it should it should be enough to I was going to say I developed something similar to this some years ago I didn't do the exclusion bit which is a really good Innovation and at for some contracts on mainnet it never finished um because it got confused because of how the jump desks were being calculated too complicated for it okay okay I'll show you a CP of things that

are also in the algorithm that I have implemented but that's that's a sort of a well it's it's a euristic right so to answer your question Peter and David's question uh the assumption that the the target of jums are on the stack and I'm going to keep track of some elements in the stack that's a euristic so for instance something that assume I would do this kind of things right I would let's say push um instead of pushing a return address I would push or Target jump I would push a target jump minus one and when I want to retrieve it somehow I would collect it and add one so this wouldn't work in my scheme this is not something that would work because I don't keep track of arithmetic on On

Target addresses but at the same time who would do that uh that would be a cly ID to to offset and and and compute every time and so yeah it works I've tested it on several well dozens of examples including an erc20 the rat if contract the deposit smart contract and so on and it works for these contracts so so Frank with respect to the the merging of nodes yeah if if uh some of these segments do things like swap what is question marks within the stack right yes then I mean you can't really merge those two nodes right it's you're changing the sematics of the program because I'm no no no no I'm

not so I'm applying the sematic front instance to swap here right we swap two elements on the stack so if it's a question two elements are are are question marks yes then there will be if I swap for instance these two elements it will result in the same stack in your representation right but then I suppose you merge that with with another node that's got that same abstract stack right but when you get there the the two elements are in a different order and that affects the yeah okay so yeah okay so that could I agree with you uh there could be some uh weird things happening

but that's a euristic so I'm going to apply my abstract semantics and try to resolve the jumps and that's why in in the lat section of this talk I'm going to verify that my target jumps have been computed correctly okay so okay see if I understand well R just just confirm with me uh I could make some um I mean I could have some let's say some Target gems that would depend on the order of these two elements and if they are swapped uh they would so I don't see them but the fact that they are swapped could influence the target gems later on so I should not say that these two states two states with this

stack should be merged because the actually these two elements should be distinguished somehow that could happen or just if the final output is is one of those elements right then you're changing the semantics of the program is the is the issue yeah well uh so I'm not I don't think I'm changing the semantics of the program but I'm building an abstract semantics that's um uh good enough to compute the targets the jum targets and if I can't find them again in my algorithm right if there is a jump for instance and I can't find the the the the target of a jump on the stack the algorithm will terminate and say I couldn't do it so it's a safe

approach as well it's not going to to try and and uh and continue to apply I can't find a a Target address or it's not a Target address that's a jump destination and so on so I'll stop do you have a formal statement of what your control photograph sort of relates to the actual code yeah so there's I'll show you later on so that's the yeah so I probably what time it is now because I don't see I I'll show another example actually here which is a bit of Contra example but in this example there's something happening which is uh

I push an element and then I iterate some some some Loop but what I'm going to do is that the stack is going to be such that there's the target of the jump here at the top of the stack all the time but the stack is actually growing at each iteration so the target of the jumps is always at the top of the stack but I'm pushing underneath some some values here at zero um and the algorithm I have implemented actually has a test which is a bit more abstract than comparing um two states of the two two two configuration or two abstract types it can compare and it can t some weak precondition and enir of these weakest preconditions for Target of jums that

will enable you to actually build the control FL graph for this program that has a potentially an unbounded stack so I'm not going to talk too much about it but there's a a test with a an enant and a sort of a pumping enant that can be applied and that's applied in the algorithm for this kind of of of programs right so maybe I should give a a demo of the code so first um uh yeah the the graphs that I've shown before they were generated by the uh the program and the interface that we developed that was so I developed the the back end of the of

the system which is Computing the control flow graphs in a format that can be displayed so I use the dot format I've written the code in dhne so I can show you why and well what are the benefits later on but one of the benefits is that I can generate artifacts of the control flow graph generator in different languages so we can use the Java one the python the C JavaScript and one of my former colleagues Aon at M he developed a simple interface just to demo how it works so we call it B spectum uh so if I can move Zoom window I can try to show you how it

Works um I should have a b Spectre here so um I've already preloaded it I would say but um but just to confirm you can see now the the screen with the bite Spectre logo and yes I assume yes so um the algorithm generates the graph so you put the code in there you submit it and it generates the graph and we also annotated the semantics of the different instructions so if you want to learn about what an instruction in doing so this one has a

tip tool tip which is it's pushing white by this one is a jump destination uh and it's it links to the uh the semantics of the instruction in the dhne evm so that's also something if you want to have a more precise explanation of what an instruction is doing uh you can you can do that as well so you can try it if you want it's still working it's a bit slow because we have a slow server we haven't paid too much for it so why but it's working and I've got a few other examples that I can demo at the end if there is some time for but for now so

let's say I'll go back to um to the question that Ron asked uh it could be that I have abstracted away some some elements in my stack and the control flow graph is not great or it could just be right um when I generate the graph for instance I print out a DOT graph and so on or a program and and and I I've got some errors in my algorithm so what I would like to do is to to to verify somehow that my compiler from evm by code to control flow graphs is correct or not so I'm not going to there's a few options you could um uh let's say uh write a

verified compiler from EV and by code to control flow graphs but I'm going to do something else which is to have a verifying compiler so I'm going to write the compiler from evm by code to control flow graphs and then this control prog graphs I'm going to provide a way of checking that they are correct and to do so I need first to provide the definition of correctness so that's the thing I want to do so I've got this uh these programs I've computed the graph and resolve resolve some some jump targets right I'd like to know whether um this is okay so when I said jump jumps to this destination this jump jumps to this

destination is it actually the case in in the code so what does it mean to for a control flow graph to be correct so you can actually formally Define it so if you're given a program right and you execute it so on you range over all possible inputs on the program and you execute this program so you get a set of traces you you and you collect every time you execute you collect sequences of program counters that you can generate from an initial input running the code so you get all these executions and traces is the the projection of these executions on the program counters you can do the same for the control flow graph you get the control

flow graph you get all the path in the control flow graph and you define the traces of this control FL graph to be all the sequences of program counts that you can generate following a path in the control FL graph so the correctness of the control flow graph in terms of safety so if I establish some safety properties on the graph for instance that some code is not reachable and so on I'd like them to be um satisfied on the concrete program so if I want this uh let's say this ability to translate some safety properties from the control flow graph to the the actual program what I want to do is to make sure that the traces of the program all of the traces of the program in terms of sequences of instructions are

represented in the control progr so this is also easy to achieve you would argue that if I take every block of instruction and I put a loop on it so I do some random stuff I said every instruction can be F by any other this will satisfy this predicate this yeah this but a control flow graph to be useful should be a bit more precise right so that's the idea you could have this simple graph with all of the instructions looping around but that's not a very useful one you won't be able to prove anything it it will satisfy this property but will not be very useful so I want something that contains all of the traces that can be uh generated by the initial program but is is not too Co so that I can reason

about it so how do I verify this Criterion that the traces of my program are included in the traces of the control program so I'm going to do it inductively what I'm going to do is to say I'll take this block of instruction I'm going to show that the the for instance the uh the the the initial state of a program and the initial state of my control FL graph are in a certain relation and every time I can do uh I can I can execute this block this segment I end up uh at the Target address that's defined by the next block and I'm going to apply the semantics of EVN to check that for every input State when I execute this block the target of

the jump is the out the the the first the start address of this segment and I will do that for all the pair of edges and then I provide an inductive proof that every time I execute the code uh in the blocks in the segments uh it can be simulated by the code in the control flow graph and that's that's a proof of Trace inclusion in that case so how does it work in practice so I'm going to take the code of this segment and uncode it into uh the DNE evm somehow in practice it's not I'm not using the actual semantics I'm using an semantics but let's say it's the semantics of the evm so I'm going to take this code and write

it in the D evm as a sequence of instructions uh and tracking the state after so I start in a given State and I compute the next state it's a push one push one push one push one so a few push one so I'm using the the push n with n equals one in the D and I just swap swap three uh and so on and I want to check that at the end in the last state of this segment after the jump the program counter is actually 0x1b so to do so I could I could write an assert statement here which is assert that the PC uh in S8 is a z x1b but what I want also to be able

to show is that I can execute the code of the next block without any uh let's say um underflow or overflow in the stack so what I'm going to do is to call the execution of next block in the program to say after you've computed this uh uh effect of the segment you should continue so there's a continuation you should continue to segment one sorry segment three not number one to execute the code and this code has actually some some preconditions uh to be satisfied so the meaning here is that I start my execution is the the initial state of of the evm so there's no real condition on the stack the stack is empty well it's

got more than zero operan it can have it should be zero but more than zero is enough um the PC should be zero and I should be able to execute this code and I'm going to verify this in DNE so in the semantics of each instruction in the DNE evm there are also pre and post conditions that must be satisfied so if I'm able to verify this code in the DNE evm uh it means that there's no stack underflow stack Overflow and so on that all these instructions can go until the end and when I call this uh when I go to the next node to be able to to verify the program I'll have to verify the preconditions that are imposed by the next note including that the PC is actually 0x1b which is where this node

starts and that some requirements on the stack are satisfied as well the stack has more than five elements and some elements in the stack are actually some values that are keeping track of the program counter so I do that for each uh each Edge in the graph and what I obtain is a pro object and I'm going to verify this Pro object with DNE and if it verifies the control flow graph is semantically correct in the sense that every block every execution of a block from a an initial state will lead to the next block um and because I have proved it for all of the input State I have an inductive proof that all of the traces

of the program are actually included in the traces of the control Str graph right so it's a simple check I generate this object the sequence of code for every block with a call to the next block and I verify this dni program and if it's verified then the control flow graph is a over approximation of sequences of of instructions that can be generated by the initial program the concrete program right so what does it guarantee when I verify again this this Pro object it guarantees that all the germ targets

have been properly computed so I have checked that they are properly computed and it also guarantees so in my setting here is there's no stack on the flow I can do no stack on overflow as well but I have simplified the semantics here so I can check that the code in the EV never has a stack and the flu exception so if you're curious about it as well so I I'll give a list of some blog posts that I've written about it I'm not actually using the actual semantics of the DM but I'm using a more abstract semantics and I have Pro that this more abstract semantics refines the semantics of the EV so what are the I'll check if there

are any questions at that stage before I go to the Limit ation any questions so I'll finish by some limitations non limitations so there are some I've tried the dozens of programs so from simple to and they are in the GitHub repository of of the tool as well so I can do of course a large code like evm well erc20 deposit contract wrapped e and even larger code uh the strategy I have adopted Works only if the target of the jumps are on the stack so there's a

program for instance this one sorry it's a program that was generated by the Viper compiler where some jumps some Target of jumps are actually pushed in memory so for this one this is what the my program is doing it says I couldn't determine the target of of the jump because it's not not on the stack so it will abort and not compute the result there's another one that was given to me at some point it took me a while to figure out why it was not working so it's a Fibonacci program so it computes the Fibonacci series but it's a non-tail recursive version so it Stacks calls on the stack and unstack them when it reaches the the last element so it turns

out that for this one it's not a failure of the of the tool but the control flow graph for this program it's a non-regular language so it cannot be represented by a finite automaton and in the setting that I have it will reach the maximum depth in my depth search and and say that the maximum depth has been reached and will not produce finalize the final complete control flow graph for this program all right so what have we achieved so I would say um there's a robust and verifiable control prograph reconstruction algorithm so in terms of robust I have implemented the code in

DNE so I think there's quite a lot of checks in the code that makes the code reliable there's no let's say division by zero out of bound and so on there's also some um some verification that I can talk about later on but I think we almost out of time but um the Control Pro graphs that are generated can be automatically verified so I can automatically generate the the proof object and verified with DNE so if you want to know more the back end the tool with the back end is at this address the the sort of the interface the B Spector interface is at the B Spectre um URL and there's two blog posts detailing what I've been talking

about today uh on the control flow graph reconstructions and how to verify them with the details of the semantics and so on that you can read about as well so just to I think it's the some of the features of the graph of the program um think most of them are are easy but there's one for instance which is quite useful in terms of when you want to verify you can choose the size of the segments as well so you can reduce them to be able to prove smaller segments using the DNE VM sometimes it it's sensitive to the size of the segments if you have long sequences of instruction it can be hard to prove um properties about them so you can decide

to limit them to let's say 10 or 12 instructions and the generator will split the code into smaller chunks uh it can generate some statistics about the code as well and so on print the different segments and a few other things and the things that I been doing but it's not finished yet so proving functional properties so beyond um uh stack Overflow and stack underflows of the code so I've got a few examples using for instance the maximum the function Max that computes the Max and so on you can actually instrument the proof object to prove that it actually computes the maximum of three numbers uh but it's probably a better

idea to try to prove some sort of refinement between some higher level code like U and evm bite code and do the proof at the level of the U code and show that the evm B code refines the U code or the U code simulates evm bite code few things that we haven't done yet so my colleague Aon he he designed this simple user interface for B Spector to demonstrate how the tool works but it's not polished enough and it's there somezing effects that are not working properly so we need to improve on that side as well and I was planning also to use the the the proof objects to be able to compute some bounds on the on the gas costs so it's not trivial because the gas costs when you execute a program it

can depend on memory expansion and stuff like that so Computing the gas cus is actually an exercise in defining enir on memory sizes and that can already be an interesting topic to to to to try and investigating and I think I'll stop there because I'm over time over time du to lots of good questions so yeah yeah yeah thank you for that yeah so that was a very interesting talk Frank and um I thinkk you yeah interesting um interesting thoughts did you get to integrate in those um the

next slides or next talk slides yes yes yeah okay first yes merch Yes sold a t-shirt night um hadn't sold one for a few weeks and then suddenly there was a NE a sale just yesterday anyway next slide um so the the forthcoming talks has been shaken up a bit um so um hopefully um that's not mucked up too many people schedules but um in two weeks I'm going to be talking about um Unis swap V3 liquidity pools and try to really um get into how should people think about them because I know I know we've talked about these um constant

curve things and but I mean that's a really highly mathematical high level view but you know if you're actually going to put a couple of thousand dollar into a liquidity pool how would you set it up you know what should you be looking for which which coins would you really put it in for which coins are safe you know what should you be you know how how should all that work so but I'm going to give people a bit of a a feel for my understanding and I would I'm not a guru on the topic but I think at least got some ideas um Matthew is going to give us a deep dive on layer zero so that was meant to be today but it's been pushed out by a month um I going to talk about random number Generation Um and talk about the issues

that you can have and um how you can do it so it's cure um for um blockchains and rollups um Andreas Rey is going to come along and talk about um another perspective on L2 standardization and David is going to tell us about how Ai and blockchain really intersect uh which is going to be super interesting because I'm yeah I'm interested to see what what's happening um so David just a question the the group that you're working for is that public or can we not even mention that stealth group that's a fine question Peter I have absolutely no idea I'll find out

for you yeah you should out before August 21 so David is working on steal group that may have something to do with AI and web 3 but um yeah um all right and um there's one more slide um okay um so if you're here today there is a YouTube channel where you can catch up with the recording if you're watching this on YouTube you can join um and ask tricky questions live um and um which is that Meetup Group link um you can have um discuss with the um speakers and everyone else um by joining slack and sometimes there is example code in

which case you should go to that link there at the bottom so are there any last talks talks last questions for Frank so I mean I have a question if no one else does um so I'm interested in that um sort of what you alluded to at the end there which is actually using the gener or the deconstructed graph to do some analysis with a D evm um I'm wondering how like how much progress you've made on that myself first so uh I've got um a prototype generator and I would say uh

it's not pushed yet or it's not uh completely terminated well it's not complete yet but uh I had to add a couple of things like what I was mentioning for instance splitting the the large segments into smaller chunks because the DM when you go over well my experiments were about 30 to 40 depending on the instructions it can't prove anything about the sequence of linear instructions it's too large so I implemented this thing to chunk the coding into smaller pieces as well um and I had to implement as well some uh you might have seen it's a bit probably tricky but um um in the code there's

um uh yeah there's some gas happening so when we do some proofs we need to make sure that the depth of the execution is bounded it can be arbitrary large but bounded otherwise the because you got got continuations that can call previous you may have infinite Loops so I had to implement this thing as well in DNE to be able to reason about arbitrary uh deep uh executions but of finite sizes and then otherwise the code is um it's almost ready to go it's the same code that I'm using for here I'm using an eate which is a bit of um it's a simplified evm State uh but uh so I've

got a sort of a simplified semantics to prove that my graph is correct but I can replace this e state by an evm State and start to reason about some code so I've done that again for a simple program like the Computing the maximum of two or three numbers uh but my my opinion is that it's not um I would say um it's not very easy to reason about bite code directly so that's why I also started this spread on and coding the semantics of fuel in and it's usually much easier to prove a sort of a refinement of a chunk of code so refinement meaning that you start with these values and at the end you get the values and you relate them to the U

code which is a high level code so do the proofs at the U code level and show that the evm by code refines the your code so that's what I was probably trying to do but I can Pro some simple properties of the bite code cool sounds nice um I posted a question in chat it's not relevant to the topic can Frank comment on the Move languages where is it uh the second question comment on S FL after FL which

one do you prefer uh I'm unable to comment on so movement Labs will have their own version of move probably some some twixs in the semantics that's the thing oh I see yeah yeah thanks I I reckon an interesting talk for maybe three months time is you the lowdown on the difference between mov and the evm and um oh there are different things and so yeah to have a talk on move yeah yeah I think it would be interesting like to understand understand you know what what's the difference and you know what why would someone um have you know why did someone

come up with move VM and you know in what way is it does it make sense that you know someone would do that because I think it's interesting to be able to see what other EO systems are doing to understand how they relate yeah that's cool yeah yeah all right look thank you for your time Frank um thank you everyone thank you very much coming along thank you everyone for your questions all right talk later byebye thank you byebye thank you

Related talks