Commit d95ebe4
committed
Fix stack extension bug causing HardFault
This patch fixes a bug in the stack extension logic that could cause a HardFault on certain configurations when the stack is reallocated to a new address.
## Background
When the mruby VM's stack runs out, stack_extend_alloc() calls mrb_realloc to grow it.
If reallocation moves the block to a new address, envadjust() adjusts all ci->stack pointers to point into the new allocation.
## The bug
The bug happened under the configuration below:
- MRB_INT64 on MRB_32BIT (`sizeof(mrb_value) == 16` because MRB_NO_BOXING is now mandatory)
- Allocator with 8-byte alignment (eg. PICORB_ALLOC_ALIGN=8 in PicoRuby for Raspi Pico)
The delta was computed via mrb_value* pointer subtraction:
```c
ptrdiff_t delta = newbase - oldbase; // units of sizeof(mrb_value)
```
If :
- Old address: 0x2004c508
- New address: 0x2004c510 (8-byte difference)
The pointer subtraction truncated: 8 / 16 = 0.
envadjust() was misleaded as `delta == 0` and returned early without adjusting any ci->stack pointers.
The stbase was updated to the new address, but all stack pointers still pointed 8 bytes before it.
Every register access was shifted, reading garbage, ultimately causing a HardFault.
## The fix
Byte-level char* calculation instead of mrb_value* calculation:
```c
ptrdiff_t off = (char*)newbase - (char*)oldbase;
// ...
ci->stack = (mrb_value*)((char*)ci->stack + off);
```
This ensures the adjustment is exact regardless of sizeof(mrb_value) and allocator alignment.1 parent b7e3743 commit d95ebe4
1 file changed
+16
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
138 | | - | |
139 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
140 | 149 | | |
141 | 150 | | |
142 | 151 | | |
| 152 | + | |
| 153 | + | |
143 | 154 | | |
144 | 155 | | |
145 | 156 | | |
146 | | - | |
147 | | - | |
| 157 | + | |
148 | 158 | | |
149 | | - | |
| 159 | + | |
150 | 160 | | |
151 | 161 | | |
152 | 162 | | |
| |||
0 commit comments