Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Possible SIGSEGV durning aset memory tracking

Đang mở
#2,130 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, postgresql
Lĩnh vực
backend

Hướng nghiên cứu

The crash path runs through src/backend/utils/mmgr/aset.c: AllocSetContextCreateInternal, AllocSetDelete and AllocSetTransferAccounting all cast the parent context to AllocSet without checking its type. Reproduce with the test extension linked in the issue, starting from the backtrace through MemoryContextSetParent in mcxt.c. Done means the reproduction no longer crashes and the parent-type checks are in place, though the issue leaves open whether to add validation or move accounting into mcxt.c, which is a design decision for maintainers.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Memory context allocation mostly tracked at aset.c and the code relied on the assumption that context tree is an AllocSet context tree, previously that was correct, but now the next query:

-- table test contains only i int column
select * from test order by i;

may allocate a Generation context (from tuplesort_begin_batch) in the context tree and this commit adds additional context validation.
But seems it's not enough:

Suppose that at some point there would be Generation context under the AccountingRoot (aset) context and over leaf Aset context - there would be performed an invalid type cast with further invalid writes which leads to SIGSEGV. Such cases may seems insubstantial, but postgres code base may mix context in further releases.

Here is a test extension which leads to SIGSEGV:

#include "postgres.h"

#include "access/htup_details.h"
#include "commands/async.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "lib/stringinfo.h"
#include "utils/rel.h"
#include "utils/memutils.h"
#include "utils/syscache.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(test_ctx);

Datum
test_ctx(PG_FUNCTION_ARGS)
{
	MemoryContext newAset = AllocSetContextCreate(CurrentMemoryContext,
												 "test preparent aset",
												 ALLOCSET_DEFAULT_SIZES);

	// create and remove context, so further context would be created
	// from freelist
	MemoryContext deleteAse = AllocSetContextCreate(CurrentMemoryContext,
												 "to drop aset",
												 ALLOCSET_DEFAULT_SIZES);
	MemoryContextDelete(deleteAse);

	MemoryContext newGen = GenerationContextCreate(newAset, "test mid",
													SLAB_DEFAULT_BLOCK_SIZE,
													SLAB_DEFAULT_BLOCK_SIZE,
													SLAB_DEFAULT_BLOCK_SIZE
	);

	MemoryContext asetLast = AllocSetContextCreate(newGen,
												"last aset",
												 ALLOCSET_DEFAULT_SIZES);

	MemoryContextDelete(newGen);
	elog(INFO, "last ctx addr %p", (void*)asetLast);
	PG_RETURN_VOID();
}

And here is a link to full test extension

The backtrace

#0  __pthread_kill_implementation (threadid=<optimized out>, signo=11, no_tid=0) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (threadid=<optimized out>, signo=11) at ./nptl/pthread_kill.c:89
#2  __GI___pthread_kill (threadid=<optimized out>, signo=signo@entry=11) at ./nptl/pthread_kill.c:100
#3  0x00007e41fb245b7e in __GI_raise (sig=11) at ../sysdeps/posix/raise.c:26
#4  0x00005865986bae91 in StandardHandlerForSigillSigsegvSigbus_OnMainThread (processName=0x586598e5dabb "Master process", postgres_signal_arg=11) at elog.c:5547
#5  0x0000586598487a83 in CdbProgramErrorHandler (postgres_signal_arg=11) at postgres.c:3990
#6  0x0000586598c8e953 in wrapper_handler (postgres_signal_arg=11) at pqsignal.c:90
#7  <signal handler called>
#8  AllocSetTransferAccounting (context=0x5865b8655080, new_parent=0x0) at aset.c:1874
#9  0x000058659870899e in MemoryContextSetParent (context=0x5865b8655080, new_parent=0x0) at mcxt.c:596
#10 0x00005865987086f5 in MemoryContextDeleteImpl (context=0x5865b8655080, sfile=0x586598ec3e7f "mcxt.c", func=0x0, sline=497) at mcxt.c:468
#11 0x000058659870879d in MemoryContextDeleteChildren (context=0x5865b864f040) at mcxt.c:497
#12 0x00005865987086c8 in MemoryContextDeleteImpl (context=0x5865b864f040, sfile=0x7e41fbdcf021 "ctx.c", func=0x0, sline=40) 

The problem occurs on the debug build with --enable-cassert

For now I suppose the problem should be closed by additional validation, but seems more correct way is to move the logic of memory tracking from aset to mctx and also extend Generation context methods to support tracking.

Ngôn ngữ chính
C
Star
1.4k
Fork
260
Merge trung bình
4 ngày 19 giờ
Pull request đã merge (30 ngày)
54

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/cloudberry

Tất cả issue của apache/cloudberry

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.