From patchwork Tue May 21 15:10:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sayan paul X-Patchwork-Id: 90607 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 A6325384AB40 for ; Tue, 21 May 2024 15:11:32 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 20B033858D1E for ; Tue, 21 May 2024 15:11:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20B033858D1E 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 20B033858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716304269; cv=none; b=dixfiezyOyoD/H+h1GE59lWBF6nvrQQYLkumid3FY8/YbvPb8jkyonC7dAE5xqTgUeEvT7ubjIKxbBBt1WmdrcBtkGpTTNZmXIoGH95p2co9hnkTlSIHRpFGH+g14mh3stGt0bRQb4VY7cbhxkjhp443XJLRgTz8BJr1hyBbblg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716304269; c=relaxed/simple; bh=qKCDRKcQUodjTREyhNU1J9ry+iKICj/imGa9esOytAc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=b19L+xYBMRyJmhWjmv4vcVTMPnxxoRja4sCVWNgchcIXeOjwHorNkBGSsRtt3biHPingScFpVT4MrjJ1N/KDQxMFBLrcfsKIZHyhywLxnLQAc9se+Gj8fSUB1xA720/gslatwarcNcDp2AOhkTFwYQV2JweT5ktImGcGCiffNvM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716304259; 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=7sjdJxl3+eCCud9XDe81adxttOQ6kLQ/Q6epfMVJALA=; b=ONK3Hhe4nuHWLuBZivJxiw0VsiJ4Im/w1a/gt7IoX826U7MhveY4821BzVnm0OqwwGomSg Smz/LfhyoMeQ2BYR8Rm7isGXcYOI1FkLXoFIGKj8omST8poB3hwzGPbi5tKXpK2hFpdsVh bj6n1TxKAt9bmnpk3GNBMoVgfeYqcIw= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-626-9ra4FjkIN2-4mUmn8BckHw-1; Tue, 21 May 2024 11:10:57 -0400 X-MC-Unique: 9ra4FjkIN2-4mUmn8BckHw-1 Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-2b4330e5119so10820625a91.3 for ; Tue, 21 May 2024 08:10:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716304256; x=1716909056; 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=7sjdJxl3+eCCud9XDe81adxttOQ6kLQ/Q6epfMVJALA=; b=hOaOqkwdm+yrAJNIZv/pFKqJKqhDkc/FfRjwqW+fcW3g/QdM9gsCO0XkLunyYUmv8Y FrKC4HTbIyR+sK0mxL20/LCrwI09iAid2jV/BDDnqNPPlMpW2CPafw1u+Sf5oymRt/IN uYftHXVu04UYxXxnpbRLjmPKkBTyCA1Lg45fnJrvqfTrBwpEL8TLk0OEjXJKH3Hl6JEv P5NLh0NtT5Tvyni7JMdDsbMfM5Rh1qmmhEDihxzzFsGGFpaC5qgfAH6JEe4J2teuGbIr YC6GdNExOAKygG82ySb+f8TD+xOylY1kuZKljKyFTW6frVNwXZN4M5zh6k1I/6Runv05 6IOg== X-Gm-Message-State: AOJu0Yz8K/T8kvcCbY6oK2qxafxqPkm4l7l8q+aNdbYY0lTdlEZu9awn VxuZBU+UdWrOAXaVGsEuQlwhQgPpC/6+Seq5C59B0CkcV3D9cBmifgCEuwxSECEGy1AnGbjMFws xgNxvVQ2ea7pBczYFPLE2H0mFSvucQDWqKI0xY5W9QDPNDHY3AL+N+apXzwmf4vJlPbdKXHqxUm s9jvS6+05b5urL+R668XMxSEe+Dor/7cK7ogqUKIOl X-Received: by 2002:a17:90a:bd0f:b0:2b2:c73a:7acc with SMTP id 98e67ed59e1d1-2b6ccedd33cmr29248763a91.37.1716304255696; Tue, 21 May 2024 08:10:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG6nay6EE1itRsoPbJLvXBISpEvadZT74EADghg0logvCL8oAwrIDFu4qvtuJtMzRcRXFwVTA== X-Received: by 2002:a17:90a:bd0f:b0:2b2:c73a:7acc with SMTP id 98e67ed59e1d1-2b6ccedd33cmr29248695a91.37.1716304254629; Tue, 21 May 2024 08:10:54 -0700 (PDT) Received: from saypaul-thinkpadx1carbongen9.punetw6.csb ([125.16.200.50]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2b67105666csm21971655a91.8.2024.05.21.08.10.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 08:10:54 -0700 (PDT) From: sayan paul To: libc-alpha@sourceware.org, tools-patches@redhat.com Cc: sayan paul , zack@owlfolio.org Subject: [PATCH v2] malloc:New test to check malloc alternate path using memory obstruction Date: Tue, 21 May 2024 20:40:05 +0530 Message-ID: <20240521151039.174595-1-saypaul@redhat.com> X-Mailer: git-send-email 2.45.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-8.8 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 The test aims to ensure that malloc uses the alternate path to allocate memory when sbrk() or brk() fails.To achieve this, the test first creates an obstruction at current program break, tests that obstruction with a failing sbrk(),then checks if malloc is still returning a valid ptr thus inferring that malloc() used mmap() instead of brk() or sbrk() to allocate the memory. --- Changes as suggested by Zack: * corrected email address * simplified the approach by creating the obstruction in the test * programme directly --- malloc/Makefile | 2 + malloc/tst-malloc-alternate-path.c | 100 +++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 malloc/tst-malloc-alternate-path.c diff --git a/malloc/Makefile b/malloc/Makefile index 77ba1a9109..ed666e2132 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 \ @@ -408,3 +409,4 @@ 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 + diff --git a/malloc/tst-malloc-alternate-path.c b/malloc/tst-malloc-alternate-path.c new file mode 100644 index 0000000000..fdb0ec5a7c --- /dev/null +++ b/malloc/tst-malloc-alternate-path.c @@ -0,0 +1,100 @@ +/* Test that malloc uses mmap when sbrk or brk fails. + This code returns success when there is an obstruction setup + and sbrk() fails to grow the heap size forcing malloc to use mmap(). + Copyright (C) 2024 Free Software Foundation, Inc. + 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; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include + +#define LARGE_SIZE (10 * (1 << 20)) // 10 MB +static long page_size; + +static int +do_test (void) +{ + /* Get current program break */ + void *current_brk = sbrk (0); + + /* Get the runtime page size */ + page_size = sysconf (_SC_PAGESIZE); + + /* 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 */ + void *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"); + return 1; + } + + if (obstruction_addr != next_page_boundary) + { + fprintf (stderr, "memory obstruction not setup correctly!"); + return 1; + } + + /* 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); + + /* Free the obstruction mapping */ + if (munmap (obstruction_addr, page_size) == -1) + { + perror ("munmap"); + } + + return 0; +} + +#include