1d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes/* 2d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> 3d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * All rights reserved. 4d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 5d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Redistribution and use in source and binary forms, with or without 6d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * modification, are permitted provided that the following conditions 7d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * are met: 8d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 1. Redistributions of source code must retain the above copyright 9d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * notice, this list of conditions and the following disclaimer. 10d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright 11d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * notice, this list of conditions and the following disclaimer in the 12d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * documentation and/or other materials provided with the distribution. 13d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 3. The name of the author may not be used to endorse or promote products 14d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * derived from this software without specific prior written permission. 15d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 16d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes */ 27d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes 28d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include "tests.h" 29d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <string.h> 30d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <sys/mman.h> 31d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes 32d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesvoid * 33d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughestail_alloc(const size_t size) 34d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes{ 35d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes const size_t page_size = get_page_size(); 36d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes const size_t len = (size + page_size - 1) & -page_size; 37d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes const size_t alloc_size = len + 6 * page_size; 38d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes 39d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes void *p = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, 40d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 41d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes if (MAP_FAILED == p) 42d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes perror_msg_and_fail("mmap(%zu)", alloc_size); 43d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes 44d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes void *start_work = p + 3 * page_size; 45d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes void *tail_guard = start_work + len; 46d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes 47d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes if (munmap(p, page_size) || 48d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes munmap(p + 2 * page_size, page_size) || 49d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes munmap(tail_guard, page_size) || 50d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes munmap(tail_guard + 2 * page_size, page_size)) 51d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes perror_msg_and_fail("munmap"); 52d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes 53d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes memset(start_work, 0xff, len); 54d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes return tail_guard - size; 55d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes} 56d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes 57d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesvoid * 58d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughestail_memdup(const void *p, const size_t size) 59d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes{ 60d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes void *dest = tail_alloc(size); 61d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes memcpy(dest, p, size); 62d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes return dest; 63d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes} 64