Skip to content

core/vm: improve jumpdest analysis#14582

Merged
karalabe merged 6 commits into
ethereum:masterfrom
holiman:jumpdest_improv
Nov 15, 2017
Merged

core/vm: improve jumpdest analysis#14582
karalabe merged 6 commits into
ethereum:masterfrom
holiman:jumpdest_improv

Conversation

@holiman
Copy link
Copy Markdown
Contributor

@holiman holiman commented Jun 5, 2017

This PR improves jumpdest analysis by making the worst-case more spread out, instead of being always worst-case at a slack-space filled with JUMPDEST.

Instead of mapping out valid JUMPDEST locations in a bitmap, we map out data-sections in the bitmap. When the jump occurs, it is validated that

  • The byte at that place is JUMPDEST
  • The pc at that place is not within a data-section.

The big 'cost' for building the bitmap is when setting bits. Due to the nature of PUSH, it is not possible to have a whole sector of set bits - they can max be set 32 bits in sequence. However, this PR further optimizes large PUSH sections by setting 8 bits at a time in the bitmap.

The new worst case is most likely PUSH1, but even for that case, only every second bit will be set, instead of every bit.

@fjl fjl requested a review from Arachnid June 6, 2017 07:36
Comment thread core/vm/analysis.go Outdated

type bitvec struct {
m []byte
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use type bitvec []byte

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment about the layout of bitvec, i.e. explain what's in the slice.

Comment thread core/vm/analysis.go
func (bits *bitvec) addOneByte(pos uint64) {
bits.m[pos/8] |= 0xFF >> (pos % 8)
bits.m[pos/8+1] |= ^(0xFF >> (pos % 8))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming could be better. I thinkaddone can be set and addOneByte can be set8 or something like it. It's confusing to say 'byte' because bitvec is []byte.

Comment thread core/vm/analysis.go Outdated

// jumpdests creates a map that contains an entry for each
// PC location that is a JUMPDEST instruction.
func jumpdests(code []byte) []byte {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now misnamed because it doesn't compute jumpdest locations anymore. You should rename it and fix the comment.

Comment thread core/vm/analysis.go Outdated
//The map is 4 bytes longer than necessary, in case the code
// ends with a PUSH32, the algorithm will push zeroes onto the
// bitvector outside the bounds of the actual code.
m := make([]byte, len(code)/8+1+4)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use make(bitvec, ...) if you change the type to non-struct.

Comment thread core/vm/analysis.go Outdated
d[codehash] = m
}
return (m[udest/8] & (1 << (udest % 8))) != 0
return OpCode(code[udest]) == JUMPDEST && (m[udest/8]&(0x80>>(udest%8))) == 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the bitvec lookup a method, maybe it could be called get.

@fjl
Copy link
Copy Markdown
Contributor

fjl commented Jul 31, 2017

@holiman Do you still want this?

@holiman
Copy link
Copy Markdown
Contributor Author

holiman commented Aug 1, 2017 via email

@holiman
Copy link
Copy Markdown
Contributor Author

holiman commented Aug 14, 2017

I've addressed the concerns now. I don't want to push on this one, I'll leave it totally up to you guys to decide if it's worth it.

Copy link
Copy Markdown
Contributor

@fjl fjl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the remaining comment.

@holiman
Copy link
Copy Markdown
Contributor Author

holiman commented Sep 8, 2017

Done

@fjl
Copy link
Copy Markdown
Contributor

fjl commented Sep 10, 2017

@Arachnid your turn

@fjl fjl changed the title Improved jumpdest analysis core/vm: improve jumpdest analysis Sep 10, 2017
Comment thread core/vm/analysis_test.go
@@ -0,0 +1,37 @@
package vm
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copyright header

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@karalabe karalabe added this to the 1.7.3 milestone Nov 15, 2017
@karalabe karalabe merged commit bce5d83 into ethereum:master Nov 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants