rlp_fuzzer.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 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 rlp
  17. import (
  18. "bytes"
  19. "fmt"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. "github.com/ethereum/go-ethereum/rlp"
  22. )
  23. func decodeEncode(input []byte, val interface{}, i int) {
  24. if err := rlp.DecodeBytes(input, val); err == nil {
  25. output, err := rlp.EncodeToBytes(val)
  26. if err != nil {
  27. panic(err)
  28. }
  29. if !bytes.Equal(input, output) {
  30. panic(fmt.Sprintf("case %d: encode-decode is not equal, \ninput : %x\noutput: %x", i, input, output))
  31. }
  32. }
  33. }
  34. func Fuzz(input []byte) int {
  35. if len(input) == 0 {
  36. return 0
  37. }
  38. if len(input) > 500*1024 {
  39. return 0
  40. }
  41. var i int
  42. {
  43. rlp.Split(input)
  44. }
  45. {
  46. if elems, _, err := rlp.SplitList(input); err == nil {
  47. rlp.CountValues(elems)
  48. }
  49. }
  50. {
  51. rlp.NewStream(bytes.NewReader(input), 0).Decode(new(interface{}))
  52. }
  53. {
  54. decodeEncode(input, new(interface{}), i)
  55. i++
  56. }
  57. {
  58. var v struct {
  59. Int uint
  60. String string
  61. Bytes []byte
  62. }
  63. decodeEncode(input, &v, i)
  64. i++
  65. }
  66. {
  67. type Types struct {
  68. Bool bool
  69. Raw rlp.RawValue
  70. Slice []*Types
  71. Iface []interface{}
  72. }
  73. var v Types
  74. decodeEncode(input, &v, i)
  75. i++
  76. }
  77. {
  78. type AllTypes struct {
  79. Int uint
  80. String string
  81. Bytes []byte
  82. Bool bool
  83. Raw rlp.RawValue
  84. Slice []*AllTypes
  85. Array [3]*AllTypes
  86. Iface []interface{}
  87. }
  88. var v AllTypes
  89. decodeEncode(input, &v, i)
  90. i++
  91. }
  92. {
  93. decodeEncode(input, [10]byte{}, i)
  94. i++
  95. }
  96. {
  97. var v struct {
  98. Byte [10]byte
  99. Rool [10]bool
  100. }
  101. decodeEncode(input, &v, i)
  102. i++
  103. }
  104. {
  105. var h types.Header
  106. decodeEncode(input, &h, i)
  107. i++
  108. var b types.Block
  109. decodeEncode(input, &b, i)
  110. i++
  111. var t types.Transaction
  112. decodeEncode(input, &t, i)
  113. i++
  114. var txs types.Transactions
  115. decodeEncode(input, &txs, i)
  116. i++
  117. var rs types.Receipts
  118. decodeEncode(input, &rs, i)
  119. }
  120. return 1
  121. }