From patchwork Wed May 15 15:28:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sayan paul X-Patchwork-Id: 90201 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 36ED33849AC1 for ; Wed, 15 May 2024 15:29:49 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 40A483858430 for ; Wed, 15 May 2024 15:29:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 40A483858430 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 40A483858430 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715786964; cv=none; b=ICvL5FV60C3NE2csh8OlfU7MgvQQXvEkwskZY18mvc6SB8bloZvHamoAqoU36URPUyMLWbp800wtbOYXPz8OtFOPntR43hL90l01PoMVCbv3g/bXQWn0RifECYrYYPDcBnUt/nqNJUGQ6PeUDO8r5tbfHfjHKRs8WP3SrzfpSLk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715786964; c=relaxed/simple; bh=HcTFVYSGDdMenulbA8hKPfCHLITTwv3pAbnxKQ9Gu/4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NaDd85GkkGE/X1N5bPBNfVGQYdJlRTIbvpCLOH5QzpqCrV2TAY0sF5oXpo85zdldC8CDhSZGFG+sKW7ntEZEvDgcjN+KqyACn0dOtI4kGfofvfa5XCftl/ZV79by/UvDhR9fHr43vwZZ2IdmByMigf0Ko8BN+Cpkvw7ovdjUF00= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715786952; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6iwbvvQ7PRuXmRE16IBGnzN7c+vE56pR80YVZZdWtzk=; b=bnRj+7qMzuWATe7zmJECMHHs3hUOO1CGWR2x3bs8gcVqk3Os3RGfP82xyR5/LFFdoxEHag RPPLMdFtVNkKaN60Zm2op2uGBC3wAd1x3XoxO2AysMfAzlIOUIZ3r+XG/2xfalPFmvCgUG ywElifDxn7FncShyrSXs2bMDUPC+84E= Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-675-qkjmPOYKPXSC0J2fmgvjHg-1; Wed, 15 May 2024 11:29:02 -0400 X-MC-Unique: qkjmPOYKPXSC0J2fmgvjHg-1 Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-1ece562f2afso60646775ad.1 for ; Wed, 15 May 2024 08:29:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715786941; x=1716391741; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6iwbvvQ7PRuXmRE16IBGnzN7c+vE56pR80YVZZdWtzk=; b=SFN8aZtH/HXKbJ4sDfe+wh0vAA1y5fsCI3F9AOKrAcvu0qqL7l11nGbxH1IcZO6vOi ygyEXmWwq/pikjOgdG6N/VgJOonYHe5OZ9khMVJepm2Yg3BWgJy0XEpknc+4Ole9Gi0R 1ZjlNefngBVv7CBnyWDlAuPwDQiWmNL78iqJlp4ijub5PGMGUpNu3wEY3j+899cQovwG 3oJzaITlcFOsZgjxWND5NEKggm4C+4CUAia7MiAu1SJg17n2kFs8zfkmhUOBcG5kR6KT 8naRoz8uK6t6F9By3w7SdarQWnt2WfiIOh85VESA/dCNsiWdAq56jsYQ7Bf0mv+3qYFt QTCA== X-Gm-Message-State: AOJu0YwKOsLOI67RBDyox1BZ0vlszHkp09gZRwQD6D2xxntmyO9NwpzD 7M0Kbrj5TkPpsE1VIa4r7c44I1SoV0mSlF6LbAwPQfFtT4/LqyQgHDrRHM6pMJqK9qon0YsK6NF PiiJTMHujNhuAYUJcT1Odv/UC/WH/2j3QEn2F+04budDWRuyx6HHiJ64GM10mzTpAHcITGnU3Y4 Az2xz9iZLIvuUSjkNKFBXQY12BU/CqSfwyT5v21kBT X-Received: by 2002:a17:903:228a:b0:1e5:c131:ca0e with SMTP id d9443c01a7336-1eef9f346cfmr284219085ad.6.1715786941334; Wed, 15 May 2024 08:29:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IESVEXKmYOQWv0OyyaSs4yXnUHqzCuaXj9caYwmfTe6k4n+u3twfAX7bvI36+88evPsJMbBRw== X-Received: by 2002:a17:903:228a:b0:1e5:c131:ca0e with SMTP id d9443c01a7336-1eef9f346cfmr284218745ad.6.1715786940682; Wed, 15 May 2024 08:29:00 -0700 (PDT) Received: from saypaul-thinkpadx1carbongen9.punetw6.csb ([125.16.200.50]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-6340a447429sm11475901a12.18.2024.05.15.08.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 08:29:00 -0700 (PDT) From: sayan paul To: libc-alpha@sourceware.org Cc: Sayan Paul Subject: [PATCH] test/malloc:alternate path due to memory obstruction test that checks if malloc uses mmap when sbrk fails Date: Wed, 15 May 2024 20:58:38 +0530 Message-ID: <20240515152838.89944-1-saypaul@redhat.com> X-Mailer: git-send-email 2.45.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org From: Sayan Paul The test aims to ensure that malloc uses the alternate path to allocate memory when sbrk() or brk() fails.To achieve this, tst-mmap-obstruction-lib creates an obstruction at the current program break, which when loaded using LD_PRELOAD ensures that sbrk() will fail to allocate any additional memory.However the test script:tst-malloc-alternate-path checks if malloc is still returning a valid ptr and we can theb infer that malloc() used mmap() instead of brk() or sbrk() to allocate the memory. --- malloc/Makefile | 9 ++++ malloc/tst-malloc-alternate-path | Bin 0 -> 17000 bytes malloc/tst-malloc-alternate-path.c | 69 +++++++++++++++++++++++++++ malloc/tst-mmap-obstruction-lib.c | 74 +++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100755 malloc/tst-malloc-alternate-path create mode 100644 malloc/tst-malloc-alternate-path.c create mode 100644 malloc/tst-mmap-obstruction-lib.c new file mode 100644 index 0000000000..877d8d14a1 diff --git a/malloc/Makefile b/malloc/Makefile index 77ba1a9109..f0fb98d33e 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -33,6 +33,7 @@ tests := \ tst-interpose-nothread \ tst-interpose-thread \ tst-malloc \ + tst-malloc-alternate-path \ tst-malloc-backtrace \ tst-malloc-check \ tst-malloc-fork-deadlock \ @@ -195,6 +196,7 @@ extra-libs-others = $(extra-libs) extra-test-objs += \ tst-interpose-aux-nothread.o \ tst-interpose-aux-thread.o \ + tst-mmap-obstruction-lib.so \ # extra-test-objs test-extras = \ @@ -202,6 +204,9 @@ test-extras = \ tst-interpose-aux-thread \ # test-extras +modules-names = \ + tst-mmap-obstruction-lib + libmemusage-routines = memusage libmemusage-inhibit-o = $(filter-out .os,$(object-suffixes)) @@ -408,3 +413,7 @@ tst-mallocstate-malloc-check-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so # libc_malloc_debug.so. $(objpfx)tst-mallocstate: $(objpfx)libc_malloc_debug.so $(objpfx)tst-mallocstate-malloc-check: $(objpfx)libc_malloc_debug.so + + +$(objpfx)tst-malloc-alternate-path.out: $(objpfx)tst-mmap-obstruction-lib.so +tst-malloc-alternate-path-ENV = LD_PRELOAD=$(objpfx)tst-mmap-obstruction-lib.so diff --git a/malloc/tst-malloc-alternate-path b/malloc/tst-malloc-alternate-path new file mode 100755 index 0000000000000000000000000000000000000000..19f0dda0bf90f207785cce833f8839bbcde15854 GIT binary patch literal 17000 zcmeHOYitzP6~4RXu@o>11mZv%rYTUU;_m~WzXGH1VXeHwy+G($4xE2&}3y*O;jt$O8tYm>q;@1Xkj5@#HWh>Pe7 z7ul?E%r8@Y%sHQ!6Wm%UpRF|t$6QHw`pJ(QA>0Vzn0G)-B<5HL(ynW6>ciC6OSp_C z!-x#!lldb`EtqqCp8*_oEqUCVaX0Uz`ljoL2<4wSrB^g$(tRz>4VhGZCY>ve)Q_~b z)VDP2g}mM%kV8b!hz5NLh9upI&V|+-2D&#Q9;35fq?}H7~j`-iSEPLgd{J-^U zFHG1kpRD`mYrlPb1^VD|U`+HuLVq4IC}a6!u%VC4k7|H)LH+u_9OhF9q`T=;pjvdd zTHyGorr$V?{^4o#%cjv6L9fB)Cv1MTdbdrZZ<6dAkR8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$CAe?B{$v0Xc*$Etgn18|`?EyuA&O*`j0qp)_w z%GzeCn9Ys?#3Pw_OQ>*l3vRvo$GzUlKyz}IYu683?hxD@puKi3B@dx%XWO<;vvYfg zX$rIBsqJmsdfFj0+p*$V4c{EJ-5HbmmumrAW?icfxa%lB#3fiXWIMw`m(Lx8xuQPotSQ@~|L(v1WYY;gP*0;n{cafL2huDkXKlyO9Y zEib!9w})s94y6Fgl0vA#s1RI+z*aWh54GkY7VLCcfeHpm7CM|0Ss3aaquIr@xiY}a19}(gF0xmIdPg7J^W5@!pALaQ4nWZn6k$)1l*|0IbmGHb?(g2&6 z@ILRQ77)Dn5yJDj$(3GbDD(B0)jd8uKR+C!iHgRK#QpduLH{?HLvZ^isQwAUbN#aj z?{B{adYm&cp669lgpY(25RVayjOY0ct~Wzh#`8SwCN=aR*t|sH%Bp9egmWUs^E}9< z8_rVl-Qi3w+O$5{CJ)i zA;1X_9_NyN{C7ZyIpO%_`xxB9k$C>Bq`Sw@F)(=X8r=sq!jCbKIH~aoLDCOl@5LV{ z{Bd&lDjGe+I9B}_3SK-v@5B}<5B`6E{b^XRC|)Bx=bxWjgr;2Z9juo)f2{i%6tI5A z^LpWoraT@|ti1Qve+evJ{87!2zDG65#4C!G_x}3d1WPS!bh?zEpLt!e-=AgXZvl3J z2VZ_(hmW(7*iT7!WY*sR1CAZ8pP!?7oj9)t$MKKEIPS;yL5IiBcwRR;*?`_Ok5omu zXFT)2gVu}Z=kh7S_j<5cWE}G_fW3HrE^dt}pufM+*GqVP!dTpXes0`Ics@?XXj~vZ zpXw*~vX$}F4P`yo&wMEqux>y8^Q73pjue4K=BuE. */ + +#include +#include +#include +#include +#include + +#define LARGE_SIZE (10 * (1 << 20)) // 10 MB + +static int +do_test (void) +{ + /* Get current program break */ + void *current_brk = sbrk (0); + + /* Try to extend the heap beyond the obstruction using sbrk */ + int *ptr = (int *) sbrk (sizeof (int)); + if (ptr != (void *) -1) + { + fprintf (stderr, "memory allocation can be done using sbrk.\n"); + free (ptr); + return 1; + } + + /* Attempt to allocate memory using malloc */ + void *memptr = malloc (LARGE_SIZE); + if (memptr == NULL) + { + perror ("malloc"); + return 1; + } + + printf ("malloc used alternate path to allocate memory\n"); + + /* Get program break after malloc */ + void *new_brk = sbrk (0); + + /* Check if malloc changed program break */ + if (current_brk != new_brk) + { + fprintf (stderr, "malloc changed program break\n"); + free (memptr); + return 1; + } + + free (memptr); + return 0; +} + +#include diff --git a/malloc/tst-mmap-obstruction-lib.c b/malloc/tst-mmap-obstruction-lib.c new file mode 100644 index 0000000000..61e35c7f55 --- /dev/null +++ b/malloc/tst-mmap-obstruction-lib.c @@ -0,0 +1,74 @@ +/* Module used for creating a memory obstruction using mmap + Copyright (C) 2024 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; see the file COPYING.LIB. If + not, see . */ + +#include +#include +#include +#include +#include +#include +#include + +static void *obstruction_addr = NULL; +static long page_size; + +/* ELF constructor to set up the obstruction */ +static void obstruction_setup (void) __attribute__ ((constructor)); + +/* ELF destructor to teardown the obstruction */ +static void obstruction_teardown (void) __attribute__ ((destructor)); + +static void +obstruction_setup (void) +{ + /* Get the runtime page size */ + page_size = sysconf (_SC_PAGESIZE); + + /* Get current program break */ + void *current_brk = sbrk (0); + + /* Round up to the next page boundary */ + void *next_page_boundary = PTR_ALIGN_UP (current_brk, page_size); + + /* Place a mapping using mmap at the next page boundary */ + obstruction_addr + = mmap (next_page_boundary, page_size, PROT_WRITE | PROT_READ, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); + if (obstruction_addr == MAP_FAILED) + { + perror ("mmap"); + obstruction_addr = NULL; + } + + if (obstruction_addr != next_page_boundary) + { + fprintf (stderr, "memory obstruction not setup correctly!"); + } +} + +/* Function to teardown the obstruction mapping */ +static void +obstruction_teardown (void) +{ + /* Free the obstruction mapping */ + if (obstruction_addr && munmap (obstruction_addr, page_size) == -1) + { + perror ("munmap"); + } +}