runtime_errors.sol 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import 'solana';
  2. contract RuntimeErrors {
  3. bytes b = hex"0000_00fa";
  4. uint256[] arr;
  5. Creature public c;
  6. constructor() {}
  7. function print_test(int8 num) public returns (int8) {
  8. print("Hello world!");
  9. require(num > 10, "sesa");
  10. assert(num > 10);
  11. int8 ovf = num + 120;
  12. print("x = {}".format(ovf));
  13. return ovf;
  14. }
  15. function math_overflow(int8 num) public returns (int8) {
  16. int8 ovf = num + 120;
  17. print("x = {}".format(ovf));
  18. return ovf;
  19. }
  20. function require_test(int256 num) public returns (int8) {
  21. require(num > 10, "sesa");
  22. return 0;
  23. }
  24. // assert failure
  25. function assert_test(int256 num) public returns (int8) {
  26. assert(num > 10);
  27. return 0;
  28. }
  29. // storage index out of bounds
  30. function set_storage_bytes() public returns (bytes) {
  31. bytes sesa = new bytes(1);
  32. b[5] = sesa[0];
  33. return sesa;
  34. }
  35. // storage array index out of bounds
  36. function get_storage_bytes() public returns (bytes) {
  37. bytes sesa = new bytes(1);
  38. sesa[0] = b[5];
  39. return sesa;
  40. }
  41. // value transfer failure
  42. function transfer_abort() public {
  43. address a = address(0);
  44. payable(a).transfer(1e15);
  45. }
  46. // pop from empty storage array
  47. function pop_empty_storage() public {
  48. arr.pop();
  49. }
  50. // external call failed
  51. function call_ext(Creature e) public {
  52. e.say_my_name();
  53. }
  54. // contract creation failed (contract was deplyed with no value)
  55. function create_child(address child_contract_addr, address payer) public {
  56. c = new Creature{address: child_contract_addr}(payer);
  57. c.say_my_name();
  58. }
  59. function i_will_revert() public {
  60. revert();
  61. }
  62. function write_integer_failure(uint256 buf_size) public {
  63. bytes smol_buf = new bytes(buf_size);
  64. smol_buf.writeUint32LE(350, 20);
  65. }
  66. function write_bytes_failure(uint256 buf_size) public {
  67. bytes data = new bytes(10);
  68. bytes smol_buf = new bytes(buf_size);
  69. smol_buf.writeBytes(data, 0);
  70. }
  71. function read_integer_failure(uint32 offset) public {
  72. bytes smol_buf = new bytes(1);
  73. smol_buf.readUint16LE(offset);
  74. }
  75. // truncated type overflows
  76. function trunc_failure(uint256 input) public returns (uint256[]) {
  77. uint256[] a = new uint256[](input);
  78. return a;
  79. }
  80. function out_of_bounds(uint256 input) public returns (uint256) {
  81. uint256[] a = new uint256[](input);
  82. return a[20];
  83. }
  84. function invalid_instruction() public {
  85. assembly {
  86. invalid()
  87. }
  88. }
  89. function byte_cast_failure(uint256 num) public returns (bytes) {
  90. bytes smol_buf = new bytes(num);
  91. bytes32 b32 = bytes32(smol_buf);
  92. return b32;
  93. }
  94. }
  95. @program_id("Cre7AzxtwSxXwU2jekYtCAQ57DkBhY9SjGDLdcrwhAo6")
  96. contract Creature {
  97. @payer(payer)
  98. @space(511 + 7)
  99. constructor(address payer) {
  100. print("In child constructor");
  101. }
  102. function say_my_name() public pure returns (string memory) {
  103. print("say_my_name");
  104. return "child_contract";
  105. }
  106. }
  107. contract calle_contract {
  108. constructor() {}
  109. function calle_contract_func() public {
  110. revert();
  111. }
  112. }