Project

General

Profile

Bug #14387 » 0001-thread_pthread.c-make-get_main_stack-portable-on-lin.patch

Patch that fixes the issue - ncopa (Natanael Copa), 03/16/2018 12:59 PM

View differences:

thread_pthread.c
530 530
#   define MAINSTACKADDR_AVAILABLE 0
531 531
# endif
532 532
#endif
533
#if MAINSTACKADDR_AVAILABLE && !defined(get_main_stack)
534
# define get_main_stack(addr, size) get_stack(addr, size)
535
#endif
536 533

  
537 534
#ifdef STACKADDR_AVAILABLE
538 535
/*
......
614 611
    return 0;
615 612
#undef CHECK_ERR
616 613
}
614

  
615
#if defined(__linux__) && !defined(__GLIBC__) && defined(HAVE_GETRLIMIT)
616

  
617
#ifndef PAGE_SIZE
618
#include <unistd.h>
619
#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
620
#endif
621

  
622
static int
623
get_main_stack(void **addr, size_t *size)
624
{
625
    size_t start, end, limit, prevend = 0;
626
    struct rlimit r;
627
    FILE *f;
628
    char buf[PATH_MAX+80], s[8];
629
    int n;
630
    STACK_GROW_DIR_DETECTION;
631

  
632
    f = fopen("/proc/self/maps", "re");
633
    if (!f)
634
        return -1;
635
    n = 0;
636
    while (fgets(buf, sizeof buf, f)) {
637
        n = sscanf(buf, "%zx-%zx %*s %*s %*s %*s %7s", &start, &end, s);
638
        if (n >= 2) {
639
            if (n == 3 && strcmp(s, "[stack]") == 0)
640
                break;
641
            prevend = end;
642
        }
643
        n = 0;
644
    }
645
    fclose(f);
646
    if (n == 0)
647
        return -1;
648

  
649
    limit = 100 << 20; /* 100MB stack limit */
650
    if (getrlimit(RLIMIT_STACK, &r)==0 && r.rlim_cur < limit)
651
        limit = r.rlim_cur & -PAGE_SIZE;
652
    if (limit > end) limit = end;
653
    if (prevend < end - limit) prevend = end - limit;
654
    if (start > prevend) start = prevend;
655
    *addr = IS_STACK_DIR_UPPER() ? (void *)start : (void *)end;
656
    *size = end - start;
657
    return 0;
658
}
659
#else
660
# define get_main_stack(addr, size) get_stack(addr, size)
661
#endif
662

  
617 663
#endif
618 664

  
619 665
static struct {