Skip to content

Commit

Permalink
Improve error message for segv in read_barrier_handler
Browse files Browse the repository at this point in the history
If the page_body is a null pointer, then read_barrier_handler will
crash with an unrelated message. This commit improves the error message.

Before:

test.rb:1: [BUG] Couldn't unprotect page 0x0000000000000000, errno: Cannot allocate memory

After:

test.rb:1: [BUG] read_barrier_handler: segmentation fault at 0x14
  • Loading branch information
peterzhu2118 committed Jul 7, 2022
1 parent d6c9862 commit f368598
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions gc.c
Expand Up @@ -5210,18 +5210,27 @@ static void invalidate_moved_page(rb_objspace_t *objspace, struct heap_page *pag

#if GC_CAN_COMPILE_COMPACTION
static void
read_barrier_handler(uintptr_t address)
read_barrier_handler(uintptr_t original_address)
{
VALUE obj;
rb_objspace_t * objspace = &rb_objspace;

address -= address % BASE_SLOT_SIZE;
/* Calculate address aligned to slots. */
uintptr_t address = original_address - (original_address % BASE_SLOT_SIZE);

obj = (VALUE)address;

struct heap_page_body *page_body = GET_PAGE_BODY(obj);

/* If the page_body is NULL, then mprotect cannot handle it and will crash
* with "Cannot allocate memory". */
if (page_body == NULL) {
rb_bug("read_barrier_handler: segmentation fault at 0x%lx", original_address);
}

RB_VM_LOCK_ENTER();
{
unlock_page_body(objspace, GET_PAGE_BODY(obj));
unlock_page_body(objspace, page_body);

objspace->profile.read_barrier_faults++;

Expand Down

0 comments on commit f368598

Please sign in to comment.