From patchwork Tue Apr 17 13:59:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 899312 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-476476-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=golang.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="ZM3nKG3e"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40QRhh5r16z9s0n for ; Wed, 18 Apr 2018 00:00:02 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=VRKkGb6tZ/fdSNU5Ec2ncV7t52ol0tRjUG0FfH6H1dHnqB MuVdefmRkBtKB+qbX5lYFoU32VqOwJu+PgU39jWx/FcyeEuH+zudWjbI+VPIEL8y tRQnb/aLZ93ouEh9JjWnVvK7sB605cpkaXFZJN6hyavVEV2e/ZKJAknj9pUcs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=liQNwxXBltEClM4aFS6M4N87nbM=; b=ZM3nKG3eQ2iAOfSf2Q1m aeN7gCOxjRO9AU1I23abbLdFbAAYGSARFTdn/dV50z/y4+3iscl7cFyIdbUJAOjO XKFgr51Pp9ZY4ys0XUuVycxAI4MoEklqOjbQNw1PSLu7wLL+tnZhlsvSq3UpOnaH Nib2lYJA+In2GejKQbQT5k0= Received: (qmail 15913 invoked by alias); 17 Apr 2018 13:59:55 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 15904 invoked by uid 89); 17 Apr 2018 13:59:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-9.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS, UNWANTED_LANGUAGE_BODY autolearn=ham version=3.3.2 spammy=H*r:10.80.245 X-HELO: mail-wr0-f182.google.com Received: from mail-wr0-f182.google.com (HELO mail-wr0-f182.google.com) (209.85.128.182) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 17 Apr 2018 13:59:48 +0000 Received: by mail-wr0-f182.google.com with SMTP id u11so35826422wri.12 for ; Tue, 17 Apr 2018 06:59:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=2PQt5uQqg3QIm1xcmq6RvOXXtfxAcHce4R6SgSA1cfM=; b=UOjrMnKMpmb+FBTiw4m7KmJ08MUEbb9bVcl9JAta3S6ZKB2R9d6WeXNAhf79dUkS/z iBRgzDKvCM2Wg7MGIGeZO+8Mr8OxNdTR0DqNvQdo/kT4HXzDuAQ81pmom9FZcQkc+7TK 0A3nS0eIqimABc0PNUP+WvtJYNcxAN5wGMk+fMjyFT2bKNiMNwliiIt5oPhXgfX6FCmZ iO0N8fAoic2iBxi7EicFluZyXj72pTWNiWm+fM9fQMqzzwLdlbUuL39/lKqQ7ASHNtd1 KjuuDtQ8VFcakXQt9aycYTj6S+tMG60oG4/nzHAmNW+xONNHDm9rBwMgs5f2SPubVSe8 v9DQ== X-Gm-Message-State: ALQs6tBWNMNm3LoWGnr7beGHo9bgVOHexxjb2mtgz6hwRighr7PImLCa WPiXiudzQrLkOntOM/Z+rFAAru1HUzai5Wprhk2ZSE0M X-Google-Smtp-Source: AIpwx48Zc0ZUfsQaf0V5CMH9idwbacaXnAAF2oYKhQM5iOzeQFl1xdrF54Py7rB5GNlaXJcc6VFoZK3KirV/zAqwWSY= X-Received: by 10.80.174.252 with SMTP id f57mr3276254edd.72.1523973586255; Tue, 17 Apr 2018 06:59:46 -0700 (PDT) MIME-Version: 1.0 Received: by 10.80.245.189 with HTTP; Tue, 17 Apr 2018 06:59:45 -0700 (PDT) From: Ian Lance Taylor Date: Tue, 17 Apr 2018 06:59:45 -0700 Message-ID: Subject: libbacktrace patch committed: Call munmap after memory test To: gcc-patches , gofrontend-dev@googlegroups.com The bug report https://github.com/ianlancetaylor/libbacktrace/issues/13 points out that when backtrace_full checks whether memory is available, it doesn't necessarily release that memory. It will stay on the free list, so libbacktrace will use more and more memory over time. This patch fixes that problem by explicitly calling munmap. Bootstrapped and ran libbacktrace and Go tests on x86_64-pc-linux-gnu. Committed to mainline. Ian 2018-04-17 Ian Lance Taylor * backtrace.c (backtrace_full): When testing whether we can allocate memory, call mmap directly, and munmap the memory. Index: backtrace.c =================================================================== --- backtrace.c (revision 259359) +++ backtrace.c (working copy) @@ -32,12 +32,26 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" +#include #include +#if !BACKTRACE_USES_MALLOC +#include +#endif + #include "unwind.h" #include "backtrace.h" +#include "backtrace-supported.h" #include "internal.h" +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif + +#ifndef MAP_FAILED +#define MAP_FAILED ((void *)-1) +#endif + /* The main backtrace_full routine. */ /* Data passed through _Unwind_Backtrace. */ @@ -104,7 +118,6 @@ backtrace_full (struct backtrace_state * backtrace_error_callback error_callback, void *data) { struct backtrace_data bdata; - void *p; bdata.skip = skip + 1; bdata.state = state; @@ -113,16 +126,25 @@ backtrace_full (struct backtrace_state * bdata.data = data; bdata.ret = 0; - /* If we can't allocate any memory at all, don't try to produce - file/line information. */ - p = backtrace_alloc (state, 4096, NULL, NULL); - if (p == NULL) - bdata.can_alloc = 0; - else - { - backtrace_free (state, p, 4096, NULL, NULL); - bdata.can_alloc = 1; - } +#if !BACKTRACE_USES_MALLOC + { + size_t pagesize; + void *page; + + /* If we can't allocate any memory at all, don't try to produce + file/line information. */ + pagesize = getpagesize (); + page = mmap (NULL, pagesize, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (page == MAP_FAILED) + bdata.can_alloc = 0; + else + { + munmap (page, pagesize); + bdata.can_alloc = 1; + } + } +#endif _Unwind_Backtrace (unwind, &bdata); return bdata.ret;