runtime_errors.sol 2.9 KB

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