pass typeid(T) when extending struct arrays so they finalize as structs#5161
Open
badnikhil wants to merge 1 commit into
Open
pass typeid(T) when extending struct arrays so they finalize as structs#5161badnikhil wants to merge 1 commit into
badnikhil wants to merge 1 commit into
Conversation
_d_arraysetlengthT_ set FINALIZE without passing the TypeInfo, so the GC never promoted the block to STRUCTFINAL and finalized struct data as a class on sweep -> segfault in rt_finalize2. mirrors dmd #22990.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5150. Extending an array of structs that have a destructor (e.g. arr.length = n) lowers to d_arraysetlengthT in core/internal/array/capacity.d, which sets BlkAttr.FINALIZE on the allocated block but never passes typeid(T) to GC.malloc. Without the TypeInfo, the conservative GC's adjustAttrs cannot promote the block to BlkAttr.STRUCTFINAL, so when the block is later collected the GC finalizes it as a class , reading a vtable/ClassInfo pointer out of what is really struct data — and segfaults in rt_finalize2 during the sweep typically reached from gc_term at shutdown).
This passes typeid(T) to both GC.malloc calls in d_arraysetlengthT (the initial allocation and the reallocation path), guarded by version (D_TypeInfo) to match the existing pattern already used in construction.d and utils.d. With the TypeInfo present, adjustAttrs sets STRUCTFINAL and the block is finalized correctly as a struct. This is a direct port of the upstream DMD fix dlang/dmd#22990 (original report dlang/dmd#22884); LDC 1.42 ships the pre-fix druntime, which is why the regression surfaced there.
Verified with the module from the issue (ddn.adam, built with ldc2 -unittest -main): it segfaults in rt_finalize2 on every run before this change and exits cleanly after, using an otherwise identical toolchain that differs only by this patch.