Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sclang: PyrGC: Fix GC segfault caused by uncollected temporary function objects by placing an upper limit on un-scanned objects. #6261

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lang/LangSource/GC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ HOT void PyrGC::Collect() {
if (mNumToScan > 0) {
// post("->Collect ns %d ng %d s %d\n", mNumToScan, mNumGrey, mScans);
// DumpInfo();

// This operation can easily overflow if mNumToScan is large causing undefined behaviour.
// Right shifting a negative is also implementation defined and has led to bugs that only appear on certain
// operating systems and hardware.
mNumToScan += mNumToScan >> 3;

// post("->Collect2 ns %d ng %d s %d\n", mNumToScan, mNumGrey, mScans);
Expand Down
15 changes: 13 additions & 2 deletions lang/LangSource/GC.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,20 @@ inline void PyrGC::ToGrey2(PyrObjectHdr* obj) {
}

inline PyrObject* PyrGC::Allocate(size_t inNumBytes, int32 sizeclass, bool inRunCollection) {
if (inRunCollection && mNumToScan >= kScanThreshold)
// When allocating a large number of objects, mNumToScan (int32) can overflow.
// This usually happens when creating temporary frames, which might not be collected until later.
// This is designed to force collection when there might be a large number of temporary objects.
// The threshold is somewhat arbitrary and could be made larger, but it is better to be safe than fast,
// and sclang is not designed nor optimized to process large amounts of data.
const bool aboveTemporaryObjectLimit = mNumToScan > 5'000'000;

// All allocations should set inRunCollection to true, but some types of methods do not as a part of an
// optimization. Don't scan an insignificant amount of memory, wait until above kScanThreshold.
const bool shouldCollect = inRunCollection && mNumToScan >= kScanThreshold;

if (aboveTemporaryObjectLimit || shouldCollect) {
Collect();
else {
} else {
if (inRunCollection)
mUncollectedAllocations = 0;
else
Expand Down