Commit c435236
fix: prevent EVM memory size overflow crash for extreme memory requests (#9887)
fix: prevent EVM memory size overflow crash for large memory requests
The memory validation in CheckMemoryAccessViolation was checking against
long.MaxValue, but .NET arrays are limited to int.MaxValue. When memory
requests exceeded int.MaxValue but were less than long.MaxValue, the
word-aligned size calculation would overflow when cast to int, causing
ArrayPool.Rent() to allocate an incorrectly sized array and subsequent
Array.Copy operations to crash.
This fix changes the limit from long.MaxValue to (int.MaxValue - WordSize + 1)
to ensure that after word alignment, the resulting size still fits in int.
Root cause:
- Memory size validation checked: totalSize > long.MaxValue
- But .NET arrays require: (int)Size to be valid
- Word alignment adds up to 31 bytes: Size = totalSize + (32 - totalSize % 32)
- When totalSize > int.MaxValue - 31, (int)Size overflows
Example crash scenario:
1. Contract requests 4GB (0xffffffff) memory via DELEGATECALL
2. Validation passes (4GB < long.MaxValue)
3. Word-aligned Size = 0x100000000 (exceeds int.MaxValue)
4. (int)Size = 0 (overflow)
5. ArrayPool.Rent(0) returns tiny array
6. Array.Copy crashes with "Destination array was not long enough"
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude <[email protected]>1 parent d7ad74e commit c435236
File tree
2 files changed
+53
-3
lines changed- src/Nethermind
- Nethermind.Evm.Test
- Nethermind.Evm
2 files changed
+53
-3
lines changedLines changed: 46 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| |||
114 | 115 | | |
115 | 116 | | |
116 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
117 | 162 | | |
118 | 163 | | |
119 | 164 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
88 | 93 | | |
89 | 94 | | |
90 | 95 | | |
91 | 96 | | |
92 | 97 | | |
93 | 98 | | |
94 | 99 | | |
95 | | - | |
| 100 | + | |
96 | 101 | | |
97 | 102 | | |
98 | 103 | | |
| |||
0 commit comments