Path: blob/main/lib/libc/tests/string/memccpy_test.c
39485 views
/*-1* Copyright (c) 2009 David Schultz <[email protected]>2* Copyright (c) 2023, 2024 The FreeBSD Foundation3* All rights reserved.4*5* Portions of this software were developed by Robert Clausecker6* <[email protected]> under sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <sys/cdefs.h>31#include <sys/param.h>32#include <sys/mman.h>33#include <assert.h>34#include <dlfcn.h>35#include <limits.h>36#include <stdio.h>37#include <stdlib.h>38#include <string.h>3940#include <atf-c.h>4142void *(*memccpy_fn)(void *restrict, const void *restrict, int, size_t);4344static char *45makebuf(size_t len, int guard_at_end)46{47char *buf;48size_t alloc_size, page_size;4950page_size = getpagesize();51alloc_size = roundup2(len, page_size) + page_size;5253buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);54assert(buf);55if (guard_at_end) {56assert(mprotect(buf + alloc_size - page_size, page_size, PROT_NONE) == 0);57return (buf + alloc_size - page_size - len);58} else {59assert(mprotect(buf, page_size, PROT_NONE) == 0);60return (buf + page_size);61}62}6364static void65freebuf(char * buf, size_t len, int guard_at_end)66{67size_t alloc_size, page_size;6869page_size = getpagesize();70alloc_size = roundup2(len, page_size) + page_size;7172if (guard_at_end)73munmap(buf + len + page_size - alloc_size, alloc_size);74else75munmap(buf - page_size, alloc_size);76}7778static void79test_memccpy(const char *s, size_t size)80{81char *src, *dst, *expected;82size_t bufsize, x;83int i, j;8485for (i = 0; i <= 1; i++) {86for (j = 0; j <= 1; j++) {87for (bufsize = 0; bufsize <= size + 32; bufsize++) {88dst = makebuf(bufsize, j);89if (bufsize < size) {90src = makebuf(bufsize, i);91memcpy(src, s, bufsize);92expected = NULL;93} else {94src = makebuf(size, i);95memcpy(src, s, size);96expected = dst + size;97}9899memset(dst, 'X', bufsize);100assert(memccpy_fn(dst, src, s[size-1], bufsize) == expected);101assert(memcmp(src, dst, MIN(bufsize, size)) == 0);102for (x = size; x < bufsize; x++)103assert(dst[x] == 'X');104105freebuf(dst, bufsize, j);106freebuf(src, bufsize < size ? bufsize : size, i);107}108}109}110}111112static void113test_sentinel(char *dest, char *src, size_t destlen, size_t srclen)114{115size_t i, effective_len;116void *res, *wantres;117const char *fail = NULL;118char terminator;119120for (i = 0; i < srclen; i++)121/* src will never include (){} */122src[i] = '0' + i;123124/* source sentinels: not to be copied */125src[-1] = '(';126src[srclen] = ')';127128memset(dest, '\xee', destlen);129130/* destination sentinels: not to be touched */131dest[-1] = '{';132dest[destlen] = '}';133134effective_len = srclen < destlen ? srclen : destlen;135wantres = srclen <= destlen ? dest + srclen : NULL;136terminator = src[srclen-1];137res = memccpy_fn(dest, src, terminator, destlen);138139if (dest[-1] != '{')140fail = "start sentinel overwritten";141else if (dest[destlen] != '}')142fail = "end sentinel overwritten";143else if (res != wantres)144fail = "incorrect return value";145else if (destlen > 0 && memcmp(src, dest, effective_len) != 0)146fail = "string not copied correctly";147else for (i = srclen; i < destlen; i++)148if (dest[i] != '\xee') {149fail = "buffer mutilated behind string";150break;151}152153if (fail)154atf_tc_fail_nonfatal("%s\n"155"memccpy(%p \"%s\", %p \"%s\", %u '%c', %zu) = %p (want %p)\n",156fail, dest, dest, src, src, terminator, terminator, destlen, res, wantres);157}158159ATF_TC_WITHOUT_HEAD(null);160ATF_TC_BODY(null, tc)161{162ATF_CHECK_EQ(memccpy_fn(NULL, "foo", 42, 0), NULL);163}164165ATF_TC(zero_extension);166ATF_TC_HEAD(zero_extension, tc)167{168atf_tc_set_md_var(tc, "descr",169"Ensure the upper bits of the terminator are ignored");170}171ATF_TC_BODY(zero_extension, tc)172{173int mask = -1 & ~UCHAR_MAX;174char buf[16];175176memset(buf, 0xcc, sizeof(buf));177ATF_CHECK_EQ(memccpy(buf, "foobar", 'r', sizeof(buf)), buf + sizeof("foobar") - 1);178ATF_CHECK_EQ(memcmp(buf, "foobar", sizeof("foobar") - 1), 0);179180memset(buf, 0xcc, sizeof(buf));181ATF_CHECK_EQ(memccpy(buf, "foobar", mask | 'r', sizeof(buf)), buf + sizeof("foobar") - 1);182ATF_CHECK_EQ(memcmp(buf, "foobar", sizeof("foobar") - 1), 0);183}184185ATF_TC_WITHOUT_HEAD(bounds);186ATF_TC_BODY(bounds, tc)187{188size_t i;189char buf[64];190191for (i = 0; i < sizeof(buf) - 1; i++) {192buf[i] = ' ' + i;193buf[i+1] = '\0';194test_memccpy(buf, i + 1);195}196}197198ATF_TC_WITHOUT_HEAD(alignments);199ATF_TC_BODY(alignments, tc)200{201size_t srcalign, destalign, srclen, destlen;202char src[15+2+64]; /* 15 offsets + 64 max length + sentinels */203char dest[15+2+64]; /* 15 offsets + 64 max length + sentinels */204205for (srcalign = 0; srcalign < 16; srcalign++)206for (destalign = 0; destalign < 16; destalign++)207for (srclen = 1; srclen < 64; srclen++)208for (destlen = 0; destlen < 64; destlen++)209test_sentinel(dest+destalign+1,210src+srcalign+1, destlen, srclen);211}212213ATF_TP_ADD_TCS(tp)214{215void *dl_handle;216217dl_handle = dlopen(NULL, RTLD_LAZY);218memccpy_fn = dlsym(dl_handle, "test_memccpy");219if (memccpy_fn == NULL)220memccpy_fn = memccpy;221222ATF_TP_ADD_TC(tp, null);223ATF_TP_ADD_TC(tp, zero_extension);224ATF_TP_ADD_TC(tp, bounds);225ATF_TP_ADD_TC(tp, alignments);226227return (atf_no_error());228}229230231