les-fuzzer.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Copyright 2021 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "io"
  21. "math/big"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/consensus/ethash"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/core/vm"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. l "github.com/ethereum/go-ethereum/les"
  30. "github.com/ethereum/go-ethereum/params"
  31. "github.com/ethereum/go-ethereum/rlp"
  32. "github.com/ethereum/go-ethereum/trie"
  33. )
  34. var (
  35. bankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  36. bankAddr = crypto.PubkeyToAddress(bankKey.PublicKey)
  37. bankFunds = new(big.Int).Mul(big.NewInt(100), big.NewInt(params.Ether))
  38. testChainLen = 256
  39. testContractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056")
  40. chain *core.BlockChain
  41. addrHashes []common.Hash
  42. txHashes []common.Hash
  43. chtTrie *trie.Trie
  44. bloomTrie *trie.Trie
  45. chtKeys [][]byte
  46. bloomKeys [][]byte
  47. )
  48. func makechain() (bc *core.BlockChain, addrHashes, txHashes []common.Hash) {
  49. db := rawdb.NewMemoryDatabase()
  50. gspec := core.Genesis{
  51. Config: params.TestChainConfig,
  52. Alloc: core.GenesisAlloc{bankAddr: {Balance: bankFunds}},
  53. GasLimit: 100000000,
  54. }
  55. genesis := gspec.MustCommit(db)
  56. signer := types.HomesteadSigner{}
  57. blocks, _ := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, testChainLen,
  58. func(i int, gen *core.BlockGen) {
  59. var (
  60. tx *types.Transaction
  61. addr common.Address
  62. )
  63. nonce := uint64(i)
  64. if i%4 == 0 {
  65. tx, _ = types.SignTx(types.NewContractCreation(nonce, big.NewInt(0), 200000, big.NewInt(0), testContractCode), signer, bankKey)
  66. addr = crypto.CreateAddress(bankAddr, nonce)
  67. } else {
  68. addr = common.BigToAddress(big.NewInt(int64(i)))
  69. tx, _ = types.SignTx(types.NewTransaction(nonce, addr, big.NewInt(10000), params.TxGas, big.NewInt(params.GWei), nil), signer, bankKey)
  70. }
  71. gen.AddTx(tx)
  72. addrHashes = append(addrHashes, crypto.Keccak256Hash(addr[:]))
  73. txHashes = append(txHashes, tx.Hash())
  74. })
  75. bc, _ = core.NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil, nil)
  76. if _, err := bc.InsertChain(blocks); err != nil {
  77. panic(err)
  78. }
  79. return
  80. }
  81. func makeTries() (chtTrie *trie.Trie, bloomTrie *trie.Trie, chtKeys, bloomKeys [][]byte) {
  82. chtTrie, _ = trie.New(common.Hash{}, trie.NewDatabase(rawdb.NewMemoryDatabase()))
  83. bloomTrie, _ = trie.New(common.Hash{}, trie.NewDatabase(rawdb.NewMemoryDatabase()))
  84. for i := 0; i < testChainLen; i++ {
  85. // The element in CHT is <big-endian block number> -> <block hash>
  86. key := make([]byte, 8)
  87. binary.BigEndian.PutUint64(key, uint64(i+1))
  88. chtTrie.Update(key, []byte{0x1, 0xf})
  89. chtKeys = append(chtKeys, key)
  90. // The element in Bloom trie is <2 byte bit index> + <big-endian block number> -> bloom
  91. key2 := make([]byte, 10)
  92. binary.BigEndian.PutUint64(key2[2:], uint64(i+1))
  93. bloomTrie.Update(key2, []byte{0x2, 0xe})
  94. bloomKeys = append(bloomKeys, key2)
  95. }
  96. return
  97. }
  98. func init() {
  99. chain, addrHashes, txHashes = makechain()
  100. chtTrie, bloomTrie, chtKeys, bloomKeys = makeTries()
  101. }
  102. type fuzzer struct {
  103. chain *core.BlockChain
  104. pool *core.TxPool
  105. chainLen int
  106. addr, txs []common.Hash
  107. nonce uint64
  108. chtKeys [][]byte
  109. bloomKeys [][]byte
  110. chtTrie *trie.Trie
  111. bloomTrie *trie.Trie
  112. input io.Reader
  113. exhausted bool
  114. }
  115. func newFuzzer(input []byte) *fuzzer {
  116. return &fuzzer{
  117. chain: chain,
  118. chainLen: testChainLen,
  119. addr: addrHashes,
  120. txs: txHashes,
  121. chtTrie: chtTrie,
  122. bloomTrie: bloomTrie,
  123. chtKeys: chtKeys,
  124. bloomKeys: bloomKeys,
  125. nonce: uint64(len(txHashes)),
  126. pool: core.NewTxPool(core.DefaultTxPoolConfig, params.TestChainConfig, chain),
  127. input: bytes.NewReader(input),
  128. }
  129. }
  130. func (f *fuzzer) read(size int) []byte {
  131. out := make([]byte, size)
  132. if _, err := f.input.Read(out); err != nil {
  133. f.exhausted = true
  134. }
  135. return out
  136. }
  137. func (f *fuzzer) randomByte() byte {
  138. d := f.read(1)
  139. return d[0]
  140. }
  141. func (f *fuzzer) randomBool() bool {
  142. d := f.read(1)
  143. return d[0]&1 == 1
  144. }
  145. func (f *fuzzer) randomInt(max int) int {
  146. if max == 0 {
  147. return 0
  148. }
  149. if max <= 256 {
  150. return int(f.randomByte()) % max
  151. }
  152. var a uint16
  153. if err := binary.Read(f.input, binary.LittleEndian, &a); err != nil {
  154. f.exhausted = true
  155. }
  156. return int(a % uint16(max))
  157. }
  158. func (f *fuzzer) randomX(max int) uint64 {
  159. var a uint16
  160. if err := binary.Read(f.input, binary.LittleEndian, &a); err != nil {
  161. f.exhausted = true
  162. }
  163. if a < 0x8000 {
  164. return uint64(a%uint16(max+1)) - 1
  165. }
  166. return (uint64(1)<<(a%64+1) - 1) & (uint64(a) * 343897772345826595)
  167. }
  168. func (f *fuzzer) randomBlockHash() common.Hash {
  169. h := f.chain.GetCanonicalHash(uint64(f.randomInt(3 * f.chainLen)))
  170. if h != (common.Hash{}) {
  171. return h
  172. }
  173. return common.BytesToHash(f.read(common.HashLength))
  174. }
  175. func (f *fuzzer) randomAddrHash() []byte {
  176. i := f.randomInt(3 * len(f.addr))
  177. if i < len(f.addr) {
  178. return f.addr[i].Bytes()
  179. }
  180. return f.read(common.HashLength)
  181. }
  182. func (f *fuzzer) randomCHTTrieKey() []byte {
  183. i := f.randomInt(3 * len(f.chtKeys))
  184. if i < len(f.chtKeys) {
  185. return f.chtKeys[i]
  186. }
  187. return f.read(8)
  188. }
  189. func (f *fuzzer) randomBloomTrieKey() []byte {
  190. i := f.randomInt(3 * len(f.bloomKeys))
  191. if i < len(f.bloomKeys) {
  192. return f.bloomKeys[i]
  193. }
  194. return f.read(10)
  195. }
  196. func (f *fuzzer) randomTxHash() common.Hash {
  197. i := f.randomInt(3 * len(f.txs))
  198. if i < len(f.txs) {
  199. return f.txs[i]
  200. }
  201. return common.BytesToHash(f.read(common.HashLength))
  202. }
  203. func (f *fuzzer) BlockChain() *core.BlockChain {
  204. return f.chain
  205. }
  206. func (f *fuzzer) TxPool() *core.TxPool {
  207. return f.pool
  208. }
  209. func (f *fuzzer) ArchiveMode() bool {
  210. return false
  211. }
  212. func (f *fuzzer) AddTxsSync() bool {
  213. return false
  214. }
  215. func (f *fuzzer) GetHelperTrie(typ uint, index uint64) *trie.Trie {
  216. if typ == 0 {
  217. return f.chtTrie
  218. } else if typ == 1 {
  219. return f.bloomTrie
  220. }
  221. return nil
  222. }
  223. type dummyMsg struct {
  224. data []byte
  225. }
  226. func (d dummyMsg) Decode(val interface{}) error {
  227. return rlp.DecodeBytes(d.data, val)
  228. }
  229. func (f *fuzzer) doFuzz(msgCode uint64, packet interface{}) {
  230. enc, err := rlp.EncodeToBytes(packet)
  231. if err != nil {
  232. panic(err)
  233. }
  234. version := f.randomInt(3) + 2 // [LES2, LES3, LES4]
  235. peer, closeFn := l.NewFuzzerPeer(version)
  236. defer closeFn()
  237. fn, _, _, err := l.Les3[msgCode].Handle(dummyMsg{enc})
  238. if err != nil {
  239. panic(err)
  240. }
  241. fn(f, peer, func() bool { return true })
  242. }
  243. func Fuzz(input []byte) int {
  244. // We expect some large inputs
  245. if len(input) < 100 {
  246. return -1
  247. }
  248. f := newFuzzer(input)
  249. if f.exhausted {
  250. return -1
  251. }
  252. for !f.exhausted {
  253. switch f.randomInt(8) {
  254. case 0:
  255. req := &l.GetBlockHeadersPacket{
  256. Query: l.GetBlockHeadersData{
  257. Amount: f.randomX(l.MaxHeaderFetch + 1),
  258. Skip: f.randomX(10),
  259. Reverse: f.randomBool(),
  260. },
  261. }
  262. if f.randomBool() {
  263. req.Query.Origin.Hash = f.randomBlockHash()
  264. } else {
  265. req.Query.Origin.Number = uint64(f.randomInt(f.chainLen * 2))
  266. }
  267. f.doFuzz(l.GetBlockHeadersMsg, req)
  268. case 1:
  269. req := &l.GetBlockBodiesPacket{Hashes: make([]common.Hash, f.randomInt(l.MaxBodyFetch+1))}
  270. for i := range req.Hashes {
  271. req.Hashes[i] = f.randomBlockHash()
  272. }
  273. f.doFuzz(l.GetBlockBodiesMsg, req)
  274. case 2:
  275. req := &l.GetCodePacket{Reqs: make([]l.CodeReq, f.randomInt(l.MaxCodeFetch+1))}
  276. for i := range req.Reqs {
  277. req.Reqs[i] = l.CodeReq{
  278. BHash: f.randomBlockHash(),
  279. AccKey: f.randomAddrHash(),
  280. }
  281. }
  282. f.doFuzz(l.GetCodeMsg, req)
  283. case 3:
  284. req := &l.GetReceiptsPacket{Hashes: make([]common.Hash, f.randomInt(l.MaxReceiptFetch+1))}
  285. for i := range req.Hashes {
  286. req.Hashes[i] = f.randomBlockHash()
  287. }
  288. f.doFuzz(l.GetReceiptsMsg, req)
  289. case 4:
  290. req := &l.GetProofsPacket{Reqs: make([]l.ProofReq, f.randomInt(l.MaxProofsFetch+1))}
  291. for i := range req.Reqs {
  292. if f.randomBool() {
  293. req.Reqs[i] = l.ProofReq{
  294. BHash: f.randomBlockHash(),
  295. AccKey: f.randomAddrHash(),
  296. Key: f.randomAddrHash(),
  297. FromLevel: uint(f.randomX(3)),
  298. }
  299. } else {
  300. req.Reqs[i] = l.ProofReq{
  301. BHash: f.randomBlockHash(),
  302. Key: f.randomAddrHash(),
  303. FromLevel: uint(f.randomX(3)),
  304. }
  305. }
  306. }
  307. f.doFuzz(l.GetProofsV2Msg, req)
  308. case 5:
  309. req := &l.GetHelperTrieProofsPacket{Reqs: make([]l.HelperTrieReq, f.randomInt(l.MaxHelperTrieProofsFetch+1))}
  310. for i := range req.Reqs {
  311. switch f.randomInt(3) {
  312. case 0:
  313. // Canonical hash trie
  314. req.Reqs[i] = l.HelperTrieReq{
  315. Type: 0,
  316. TrieIdx: f.randomX(3),
  317. Key: f.randomCHTTrieKey(),
  318. FromLevel: uint(f.randomX(3)),
  319. AuxReq: uint(2),
  320. }
  321. case 1:
  322. // Bloom trie
  323. req.Reqs[i] = l.HelperTrieReq{
  324. Type: 1,
  325. TrieIdx: f.randomX(3),
  326. Key: f.randomBloomTrieKey(),
  327. FromLevel: uint(f.randomX(3)),
  328. AuxReq: 0,
  329. }
  330. default:
  331. // Random trie
  332. req.Reqs[i] = l.HelperTrieReq{
  333. Type: 2,
  334. TrieIdx: f.randomX(3),
  335. Key: f.randomCHTTrieKey(),
  336. FromLevel: uint(f.randomX(3)),
  337. AuxReq: 0,
  338. }
  339. }
  340. }
  341. f.doFuzz(l.GetHelperTrieProofsMsg, req)
  342. case 6:
  343. req := &l.SendTxPacket{Txs: make([]*types.Transaction, f.randomInt(l.MaxTxSend+1))}
  344. signer := types.HomesteadSigner{}
  345. for i := range req.Txs {
  346. var nonce uint64
  347. if f.randomBool() {
  348. nonce = uint64(f.randomByte())
  349. } else {
  350. nonce = f.nonce
  351. f.nonce += 1
  352. }
  353. req.Txs[i], _ = types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(10000), params.TxGas, big.NewInt(1000000000*int64(f.randomByte())), nil), signer, bankKey)
  354. }
  355. f.doFuzz(l.SendTxV2Msg, req)
  356. case 7:
  357. req := &l.GetTxStatusPacket{Hashes: make([]common.Hash, f.randomInt(l.MaxTxStatus+1))}
  358. for i := range req.Hashes {
  359. req.Hashes[i] = f.randomTxHash()
  360. }
  361. f.doFuzz(l.GetTxStatusMsg, req)
  362. }
  363. }
  364. return 0
  365. }