From patchwork Tue May 10 14:38:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 620664 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3r42160rJGz9t3p for ; Wed, 11 May 2016 00:38:17 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=GcQStaBK; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; q=dns; s=default; b= HJKgReuo4V4xOHjW6NbhuC+6mlj6t4L+yvN0GAjte6egQWR/ijRB7q9Wb/3J/jT0 4MHh+JSlhbJy+p43j3OL/fdtH2rntkIBGlHARi42BhCJESLIniVylSr9aWi8qaNO GWJfEGCVflZYy3trgnQkt5IKFBMROUj7YwtiaXb8rFw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; s=default; bh=w0uVpK EBpQ6DhH6KbZ28DPytDXA=; b=GcQStaBKdYWWJnmeOob59gSBySSh+xH8ix9KRQ rVnBXqquJVGH5F17mBF/z35T9fgrBIcBRBcrp+vlvUaC5U+iJEXbCvlCcSPL81J6 gUHmkX6ml+rO/M65kTfab6bpe6/KePknPoFeSjM6gSPvGjUteyqYa1N4nwQz+rCu 9L2Z4= Received: (qmail 54279 invoked by alias); 10 May 2016 14:38:10 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 54249 invoked by uid 89); 10 May 2016 14:38:09 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.0 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=map_failed, MAP_FAILED, Hx-languages-length:4084, our X-HELO: mx1.redhat.com Date: Tue, 10 May 2016 16:38:05 +0200 To: libc-alpha@sourceware.org Subject: [PATCH] tst-rec-dlopen: Use custom malloc instead of hooks User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20160510143805.6AA4141C38D19@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) The hook variables are deprecated. 2016-05-10 Florian Weimer Use custom malloc instead of hooks in tst-rec-dlopen. * dlfcn/tst-rec-dlopen.c (call_function): New variable. (do_test): Do not change malloc hooks. Set and clear call_function as needed. (struct chunk_header): New type. (malloc_internal, malloc, free, malloc_usable_size, realloc) (memalign): New functions. diff --git a/dlfcn/tst-rec-dlopen.c b/dlfcn/tst-rec-dlopen.c index b2a35a7..7acc181 100644 --- a/dlfcn/tst-rec-dlopen.c +++ b/dlfcn/tst-rec-dlopen.c @@ -18,8 +18,10 @@ #include #include -#include #include +#include +#include +#include #define DSO "moddummy1.so" #define FUNC "dummy1" @@ -81,29 +83,13 @@ call_func (const char *dso_name, const char *func_name) } -/* Empty hook that does nothing. */ -void * -custom_malloc_hook (size_t size, const void *caller) -{ - void *result; - /* Restore old hooks. */ - __malloc_hook = old_malloc_hook; - /* First call a function in another library via dlopen. */ - call_func (DSO1, FUNC1); - /* Called recursively. */ - result = malloc (size); - /* Restore new hooks. */ - __malloc_hook = custom_malloc_hook; - return result; -} +/* If true, call another function from malloc. */ +static bool call_function; static int do_test (void) { - /* Save old hook. */ - old_malloc_hook = __malloc_hook; - /* Install new hook. */ - __malloc_hook = custom_malloc_hook; + call_function = true; /* Bug 17702 fixes two things: * A recursive dlopen unmapping the ld.so.cache. @@ -116,8 +102,7 @@ do_test (void) will abort because of the assert described in detail below. */ call_func (DSO, FUNC); - /* Restore old hook. */ - __malloc_hook = old_malloc_hook; + call_function = false; /* The function dummy2() is called by the malloc hook. Check to see that it was called. This ensures the second recursive @@ -141,3 +126,89 @@ do_test (void) #define TEST_FUNCTION do_test () #include "../test-skeleton.c" + +/* The rest of this file contains a minimal malloc implementation. */ + +/* Chunk header for our minimal malloc implementation. */ +struct __attribute__ ((aligned (__alignof__ (max_align_t)))) chunk_header +{ + size_t chunk_size; +}; + +static void * +malloc_internal (size_t size) +{ + size_t total_size = size + sizeof (struct chunk_header); + if (total_size < size) + return NULL; + void *result = mmap (NULL, total_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (result == MAP_FAILED) + return NULL; + return result + sizeof (struct chunk_header); +} + +void * +malloc (size_t size) +{ + /* First call a function in another library via dlopen. */ + if (call_function) + { + call_function = false; + call_func (DSO1, FUNC1); + call_function = true; + } + return malloc_internal (size); +} + +void +free (void *ptr) +{ + /* Do nothing. */ +} + +size_t +malloc_usable_size (void *ptr) +{ + struct chunk_header *chunk = ptr - sizeof (struct chunk_header); + return chunk->chunk_size; +} + +void * +realloc (void *ptr, size_t size) +{ + size_t old_size = malloc_usable_size (ptr); + size_t to_copy; + if (old_size < size) + to_copy = old_size; + else + to_copy = size; + void *newptr = malloc_internal (size); + if (newptr == NULL) + return NULL; + memcpy (newptr, ptr, to_copy); + return newptr; +} + +void * +memalign (size_t alignment, size_t size) +{ + if (alignment > __alignof__ (struct chunk_header)) + { + if (size < size + __alignof__ (struct chunk_header)) + return NULL; + void *result = malloc_internal + (size + __alignof__ (struct chunk_header)); + if (result == NULL) + return NULL; + uintptr_t bits = (uintptr_t) result; + /* Round up to the next multiple of he alignment. */ + bits += __alignof__ (struct chunk_header) - 1; + bits &= ~(__alignof__ (struct chunk_header) - 1); + result = (void *) bits; + struct chunk_header *chunk = result - sizeof (struct chunk_header); + chunk->chunk_size = size; + return result; + } + return malloc_internal (size); +}