Commit 0de86e0a

0de86e0ad961Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

fix(analyze,codegen): Array<:ptr> lowers to PtrArray, not IntArray

Closes #492. An array literal whose elements are FFI :ptr returns (e.g. [LibC.malloc(...)]) used to infer as int_array — infer_array_elem_type_from_ids had no et == "ptr" arm and fell through to the int_array default. Codegen then emitted sp_IntArray * storage and sp_IntArray_push(arr, void *) on every push site, failing -Wint-conversion. Storage as mrb_int also happens to work on 64-bit (long-sized pointers fit) but would silently break on a 32-bit target. Reported against a transformer-LM workload with per-head FFI pointer slots.

Three coordinated changes:

  1. analyze: add the et == "ptr" arm in infer_array_elem_type_from_ids, returning ptr_ptr_array for homogeneous-ptr literals (poly_array for mixed). Sibling to the existing obj-type / array-type / hash / proc arms.

  2. runtime: sp_PtrArray_new_noscan() — the default sp_PtrArray_new attaches sp_gc_mark as the per-element scan callback, which reads ((unsigned char*)obj)[-1] to find sp_gc_hdr. Raw external pointers (FFI returns, dlopen handles, ...) don't carry sp_gc_hdr, so the default scan would read undefined bytes and crash at collection time. noscan skips per-element scanning; the array header itself remains GC-tracked.

  3. codegen: a small helper array_literal_arr_type wraps the call to infer_array_elem_type so compile_array_literal picks ptr_ptr_array directly when every element infers as ptr, without modifying the shared infer_array_elem_type_from_ids body (a self-host polyvariance regression on stage-2 codegen when the new arm was added inline — the wrapped form keeps the shared inference function exactly as bootstrap expects). compile_array_literal_from_ids' is_ptr_array_type branch then picks sp_PtrArray_new_noscan() over sp_PtrArray_new() when ptr_array_elem_type == "ptr".

Test fixture pushes / frees pointer slots end-to-end. The -Wint-conversion regression is the codegen failure mode; the harness drops cc stderr, so it surfaces as a missing binary rather than a noisy warning if regressed.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
0de86e0ad961
Parent
f1f48250fdb0

lib/sp_runtime.hmodifiedView file

@@ -555,6 +555,12 @@
555555 static void sp_PtrArray_gc_scan(void*p){sp_PtrArray*a=(sp_PtrArray*)p;if(!a->scan_elem)return;for(mrb_int i=0;i<a->len;i++){if(a->data[i])a->scan_elem(a->data[i]);}}
556556 static sp_PtrArray*sp_PtrArray_new_scan(void(*scan_elem)(void*)){sp_PtrArray*a=(sp_PtrArray*)sp_gc_alloc(sizeof(sp_PtrArray),sp_PtrArray_fin,scan_elem?sp_PtrArray_gc_scan:NULL);a->cap=16;a->data=(void**)malloc(sizeof(void*)*a->cap);a->len=0;a->scan_elem=scan_elem;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(void*)*a->cap;sp_gc_bytes+=sizeof(void*)*a->cap;}return a;}
557557 static sp_PtrArray*sp_PtrArray_new(void){return sp_PtrArray_new_scan(sp_gc_mark);}
558+/* PtrArray for raw external pointers (FFI `:ptr` returns, dlopen
559+ handles, ...). These don't carry sp_gc_hdr -- the default
560+ sp_gc_mark element scan would read undefined bytes and likely
561+ crash at collection time. Skip per-element scanning; the array
562+ header itself is still GC-tracked. */
563+static sp_PtrArray*sp_PtrArray_new_noscan(void){return sp_PtrArray_new_scan(NULL);}
558564 static inline void sp_PtrArray_push(sp_PtrArray*a,void*v){if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(void*)*a->cap;h->size-=sizeof(void*)*a->cap;a->cap=a->cap*2+1;a->data=(void**)realloc(a->data,sizeof(void*)*a->cap);h->size+=sizeof(void*)*a->cap;sp_gc_bytes+=sizeof(void*)*a->cap;}a->data[a->len++]=v;}
559565 static inline void*sp_PtrArray_get(sp_PtrArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}
560566 static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(i<0)i+=a->len;a->data[i]=v;}