Repository navigation
Commit 195c8d1
authored
Keep the Closure in place for libffi across GC compaction (#212)
Closes #211.
## Problem
`Fiddle::Closure` gives libffi the `VALUE` of the Closure object as user
data. The trampoline casts that value back on each call.
`closure_data_type` sets `.dmark = 0` and declares no `.dcompact`.
Nothing keeps the object in place. GC compaction can move the Closure.
After a move, libffi gives back a dead address, and the process stops
with SIGSEGV.
`Fiddle::Closure::BlockCaller` and `Fiddle::Importer#bind` fail in the
same way.
## Change
libffi now receives the `fiddle_closure` struct. That memory comes from
`xmalloc` and does not move.
The struct holds the Closure `VALUE` in a new `self` member:
- `closure_mark` marks the member with `rb_gc_mark_movable`.
- `closure_compact` updates the member with `rb_gc_location`.
- The trampoline reads `((fiddle_closure *)x->ctx)->self`.
The Closure stays movable. The `ffi` gem uses the same method.
## Test
The new test is `test_call_after_compaction` in
`test/fiddle/test_closure.rb`.
The test keeps the Closure in an array. A local variable is pinned by
the conservative machine-stack scan. A pinned Closure does not move, and
then the test cannot fail. A comment in the test records this reason.
| build | result |
|---|---|
| before this change | SIGSEGV at `0x4` |
| after this change | pass |
The full suite after this change: 242 tests, 670 assertions, 0 failures,
0 errors, 3 omissions.
Environment: ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM
[arm64-darwin23].
## Alternative
`rb_gc_mark` also corrects the fault, and the diff is smaller. But it
pins one object for each live Closure. This pull request uses
`rb_gc_mark_movable` to prevent the pin. Please tell me if you prefer
the smaller diff.1 parent e6c49f9 commit 195c8d1
3 files changed
Lines changed: 84 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
57 | 80 | | |
58 | 81 | | |
59 | 82 | | |
60 | | - | |
| 83 | + | |
61 | 84 | | |
62 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
63 | 89 | | |
64 | 90 | | |
65 | 91 | | |
| |||
76 | 102 | | |
77 | 103 | | |
78 | 104 | | |
79 | | - | |
| 105 | + | |
80 | 106 | | |
81 | 107 | | |
82 | 108 | | |
| |||
288 | 314 | | |
289 | 315 | | |
290 | 316 | | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
291 | 321 | | |
292 | 322 | | |
293 | 323 | | |
| |||
318 | 348 | | |
319 | 349 | | |
320 | 350 | | |
321 | | - | |
| 351 | + | |
322 | 352 | | |
323 | | - | |
| 353 | + | |
324 | 354 | | |
325 | 355 | | |
326 | 356 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
| 244 | + | |
244 | 245 | | |
245 | 246 | | |
246 | 247 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
175 | 223 | | |
176 | 224 | | |
177 | 225 | | |
| |||
0 commit comments