From b47d4bbe6ace3bcda8f50f771a5f12d99a4685f5 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 30 Sep 2015 10:54:55 +0930 Subject: [PATCH] ccan: add ccan/mem. Onion test program wants memcheck(). Signed-off-by: Rusty Russell --- ccan/ccan/mem/LICENSE | 1 + ccan/ccan/mem/_info | 30 +++ ccan/ccan/mem/mem.c | 90 +++++++ ccan/ccan/mem/mem.h | 265 +++++++++++++++++++++ ccan/ccan/mem/test/api-memcheck.c | 28 +++ ccan/ccan/mem/test/api.c | 118 +++++++++ ccan/ccan/mem/test/compile_fail-memcheck.c | 23 ++ 7 files changed, 555 insertions(+) create mode 120000 ccan/ccan/mem/LICENSE create mode 100644 ccan/ccan/mem/_info create mode 100644 ccan/ccan/mem/mem.c create mode 100644 ccan/ccan/mem/mem.h create mode 100644 ccan/ccan/mem/test/api-memcheck.c create mode 100644 ccan/ccan/mem/test/api.c create mode 100644 ccan/ccan/mem/test/compile_fail-memcheck.c diff --git a/ccan/ccan/mem/LICENSE b/ccan/ccan/mem/LICENSE new file mode 120000 index 000000000..b7951dabd --- /dev/null +++ b/ccan/ccan/mem/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/ccan/mem/_info b/ccan/ccan/mem/_info new file mode 100644 index 000000000..b95a5754c --- /dev/null +++ b/ccan/ccan/mem/_info @@ -0,0 +1,30 @@ +#include "config.h" +#include +#include + +/** + * mem - Provide mem*() functions if missing from C library + * + * This code implements some string.h mem*() functions if they're not + * already available in the C library. Functions included are: + * memmem() + * + * License: CC0 + */ +int main(int argc, char *argv[]) +{ + /* Expect exactly one argument */ + if (argc != 2) + return 1; + + if (strcmp(argv[1], "depends") == 0) { + printf("ccan/compiler"); + return 0; + } + + if (strcmp(argv[1], "testdepends") == 0) { + return 0; + } + + return 1; +} diff --git a/ccan/ccan/mem/mem.c b/ccan/ccan/mem/mem.c new file mode 100644 index 000000000..5b4b35080 --- /dev/null +++ b/ccan/ccan/mem/mem.c @@ -0,0 +1,90 @@ +/* CC0 (Public domain) - see LICENSE file for details */ + +#include "config.h" + +#include +#include +#include + +#if !HAVE_MEMMEM +void *memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen) +{ + const char *p; + + if (needlelen > haystacklen) + return NULL; + + p = haystack; + + for (p = haystack; + (p + needlelen) <= ((const char *)haystack + haystacklen); + p++) + if (memcmp(p, needle, needlelen) == 0) + return (void *)p; + + return NULL; +} +#endif + +#if !HAVE_MEMRCHR +void *memrchr(const void *s, int c, size_t n) +{ + unsigned char *p = (unsigned char *)s; + + while (n) { + if (p[n-1] == c) + return p + n - 1; + n--; + } + + return NULL; +} +#endif + +void *mempbrkm(const void *data_, size_t len, const void *accept_, size_t accept_len) +{ + const char *data = data_, *accept = accept_; + size_t i, j; + + for (i = 0; i < len; i++) + for (j = 0; j < accept_len; j++) + if (accept[j] == data[i]) + return (void *)&data[i]; + return NULL; +} + +void *memcchr(void const *data, int c, size_t data_len) +{ + char const *p = data; + size_t i; + + for (i = 0; i < data_len; i++) + if (p[i] != c) + return (void *)&p[i]; + + return NULL; +} + +#define MEMSWAP_TMP_SIZE 256 + +void memswap(void *a, void *b, size_t n) +{ + char *ap = a; + char *bp = b; + char tmp[MEMSWAP_TMP_SIZE]; + + assert(!memoverlaps(a, n, b, n)); + + while (n) { + size_t m = n > MEMSWAP_TMP_SIZE ? MEMSWAP_TMP_SIZE : n; + + memcpy(tmp, bp, m); + memcpy(bp, ap, m); + memcpy(ap, tmp, m); + + ap += m; + bp += m; + n -= m; + } +} diff --git a/ccan/ccan/mem/mem.h b/ccan/ccan/mem/mem.h new file mode 100644 index 000000000..68a4aa3c1 --- /dev/null +++ b/ccan/ccan/mem/mem.h @@ -0,0 +1,265 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#ifndef CCAN_MEM_H +#define CCAN_MEM_H + +#include "config.h" +#include + +#include +#include + +#if !HAVE_MEMMEM +PURE_FUNCTION +void *memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen); +#endif + +#if !HAVE_MEMRCHR +PURE_FUNCTION +void *memrchr(const void *s, int c, size_t n); +#endif + +/** + * mempbrkm - locates the first occurrence in @data of any bytes in @accept + * @data: where we search + * @len: length of data in bytes + * @accept: array of bytes we search for + * @accept_len: # of bytes in accept + * + * Returns a pointer to the byte in @data that matches one of the bytes in + * @accept, or NULL if no such byte is found. + * + * Example: + * char otherbytes[] = "Hello \0world"; + * size_t otherbytes_len = sizeof(otherbytes) - 1; + * char *r = mempbrkm(otherbytes, otherbytes_len, "\0b", 2); + * if (r) { + * printf("Found %c\n", *r); + * } else { + * printf("Nada\n"); + * } + * + */ +PURE_FUNCTION +void *mempbrkm(const void *data, size_t len, const void *accept, size_t accept_len); + +/** + * mempbrk - locates the first occurrence in @data of any bytes in @accept + * @data: where we search + * @len: length of data in bytes + * @accept: NUL terminated string containing the bytes we search for + * + * Returns a pointer to the byte in @data that matches one of the bytes in + * @accept, or NULL if no such byte is found. + * + * Example: + * + * r = mempbrk(otherbytes, otherbytes_len, "abcde"); + * if (r) { + * printf("Found %c\n", *r); + * } else { + * printf("Nada\n"); + * } + */ +PURE_FUNCTION +static inline char *mempbrk(const void *data, size_t len, const char *accept) +{ + return mempbrkm(data, len, accept, strlen(accept)); +} + +/** + * memcchr - scan memory until a character does _not_ match @c + * @data: pointer to memory to scan + * @data_len: length of data + * @c: character to scan for + * + * The complement of memchr(). + * + * Returns a pointer to the first character which is _not_ @c. If all memory in + * @data is @c, returns NULL. + * + * Example: + * char somebytes[] = "HI By\0e"; + * size_t bytes_len = sizeof(somebytes) - 1; + * r = memcchr(somebytes, ' ', bytes_len); + * if (r) { + * printf("Found %c after trimming spaces\n", *r); + * } + */ +PURE_FUNCTION +void *memcchr(void const *data, int c, size_t data_len); + +/** + * memeq - Are two byte arrays equal? + * @a: first array + * @al: bytes in first array + * @b: second array + * @bl: bytes in second array + * + * Example: + * if (memeq(somebytes, bytes_len, otherbytes, otherbytes_len)) { + * printf("memory blocks are the same!\n"); + * } + */ +PURE_FUNCTION +static inline bool memeq(const void *a, size_t al, const void *b, size_t bl) +{ + return al == bl && !memcmp(a, b, bl); +} + +/** + * memstarts - determine if @data starts with @prefix + * @data: does this begin with @prefix? + * @data_len: bytes in @data + * @prefix: does @data begin with these bytes? + * @prefix_len: bytes in @prefix + * + * Returns true if @data starts with @prefix, otherwise return false. + * + * Example: + * if (memstarts(somebytes, bytes_len, otherbytes, otherbytes_len)) { + * printf("somebytes starts with otherbytes!\n"); + * } + */ +PURE_FUNCTION +static inline bool memstarts(void const *data, size_t data_len, + void const *prefix, size_t prefix_len) +{ + if (prefix_len > data_len) + return false; + return memeq(data, prefix_len, prefix, prefix_len); +} + +/** + * memeqstr - Is a byte array equal to a NUL terminated string? + * @data: byte array + * @length: length of @data in bytes + * @string: NUL terminated string + * + * The '\0' byte is ignored when checking if @bytes == @string. + * + * Example: + * if (memeqstr(somebytes, bytes_len, "foo")) { + * printf("somebytes == 'foo'!\n"); + * } + */ +PURE_FUNCTION +static inline bool memeqstr(const void *data, size_t length, const char *string) +{ + return memeq(data, length, string, strlen(string)); +} + +/** + * memstarts_str - Does this byte array start with a string prefix? + * @a: byte array + * @al: length in bytes + * @s: string prefix + * + * Example: + * if (memstarts_str(somebytes, bytes_len, "It")) { + * printf("somebytes starts with 'It'\n"); + * } + */ +PURE_FUNCTION +static inline bool memstarts_str(const void *a, size_t al, const char *s) +{ + return memstarts(a, al, s, strlen(s)); +} + +/** + * memends - Does this byte array end with a given byte-array suffix? + * @s: byte array + * @s_len: length in bytes + * @suffix: byte array suffix + * @suffix_len: length of suffix in bytes + * + * Returns true if @suffix appears as a substring at the end of @s, + * false otherwise. + */ +PURE_FUNCTION +static inline bool memends(const void *s, size_t s_len, const void *suffix, size_t suffix_len) +{ + return (s_len >= suffix_len) && (memcmp((const char *)s + s_len - suffix_len, + suffix, suffix_len) == 0); +} + +/** + * memends_str - Does this byte array end with a string suffix? + * @a: byte array + * @al: length in bytes + * @s: string suffix + * + * Example: + * if (memends_str(somebytes, bytes_len, "It")) { + * printf("somebytes ends with with 'It'\n"); + * } + */ +PURE_FUNCTION +static inline bool memends_str(const void *a, size_t al, const char *s) +{ + return memends(a, al, s, strlen(s)); +} + +/** + * memoverlaps - Do two memory ranges overlap? + * @a: pointer to first memory range + * @al: length of first memory range + * @b: pointer to second memory range + * @al: length of second memory range + */ +CONST_FUNCTION +static inline bool memoverlaps(const void *a_, size_t al, + const void *b_, size_t bl) +{ + const char *a = a_; + const char *b = b_; + + return (a < (b + bl)) && (b < (a + al)); +} + +/* + * memswap - Exchange two memory regions + * @a: first region + * @b: second region + * @n: length of the regions + * + * Undefined results if the two memory regions overlap. + */ +void memswap(void *a, void *b, size_t n); + +#if HAVE_VALGRIND_MEMCHECK_H +#include +static inline void *memcheck_(const void *data, size_t len) +{ + VALGRIND_CHECK_MEM_IS_DEFINED(data, len); + return (void *)data; +} +#else +static inline void *memcheck_(const void *data, size_t len) +{ + return (void *)data; +} +#endif + +#if HAVE_TYPEOF +/** + * memcheck - check that a memory region is initialized + * @data: start of region + * @len: length in bytes + * + * When running under valgrind, this causes an error to be printed + * if the entire region is not defined. Otherwise valgrind only + * reports an error when an undefined value is used for a branch, or + * written out. + * + * Example: + * // Search for space, but make sure it's all initialized. + * if (memchr(memcheck(somebytes, bytes_len), ' ', bytes_len)) { + * printf("space was found!\n"); + * } + */ +#define memcheck(data, len) ((__typeof__((data)+0))memcheck_((data), (len))) +#else +#define memcheck(data, len) memcheck_((data), (len)) +#endif +#endif /* CCAN_MEM_H */ diff --git a/ccan/ccan/mem/test/api-memcheck.c b/ccan/ccan/mem/test/api-memcheck.c new file mode 100644 index 000000000..ca8a111c8 --- /dev/null +++ b/ccan/ccan/mem/test/api-memcheck.c @@ -0,0 +1,28 @@ +#include "config.h" + +#include + +#include +#include + +int main(void) +{ + char haystack[] = "abcd\0efgh"; + char *p; + const char *pc; + + /* This is how many tests you plan to run */ + plan_tests(4); + + p = memcheck(haystack, sizeof(haystack)); + ok1(p == haystack); + pc = memcheck(haystack, sizeof(haystack)); + ok1(pc == haystack); + p = memcheck(p, sizeof(haystack)); + ok1(p == haystack); + pc = memcheck(pc, sizeof(haystack)); + ok1(pc == haystack); + + /* This exits depending on whether all tests passed */ + return exit_status(); +} diff --git a/ccan/ccan/mem/test/api.c b/ccan/ccan/mem/test/api.c new file mode 100644 index 000000000..d820a38dd --- /dev/null +++ b/ccan/ccan/mem/test/api.c @@ -0,0 +1,118 @@ +#include "config.h" + +#include + +#include +#include + +#define SWAPSIZE 12 + +int main(void) +{ + char haystack1[] = "abcd\0efgh"; + char haystack2[] = "ab\0ab\0ab\0ab"; + char needle1[] = "ab"; + char needle2[] = "d\0e"; + char scan1[] = "aaaab"; + char scan2[] = "\0\0\0b"; + char tmp1[SWAPSIZE], tmp2[SWAPSIZE]; + + /* This is how many tests you plan to run */ + plan_tests(62); + + ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1); + ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL); + ok1(memmem(haystack1, sizeof(haystack1), needle2, 3) == (haystack1 + 3)); + + ok1(memmem(haystack2, sizeof(haystack2), needle1, sizeof(needle1)) + == haystack2); + ok1(memmem(haystack2, sizeof(haystack2), needle2, 3) == NULL); + + ok1(memrchr(haystack1, 'a', sizeof(haystack1)) == haystack1); + ok1(memrchr(haystack1, 'b', sizeof(haystack1)) == haystack1 + 1); + ok1(memrchr(haystack1, 'c', sizeof(haystack1)) == haystack1 + 2); + ok1(memrchr(haystack1, 'd', sizeof(haystack1)) == haystack1 + 3); + ok1(memrchr(haystack1, 'e', sizeof(haystack1)) == haystack1 + 5); + ok1(memrchr(haystack1, 'f', sizeof(haystack1)) == haystack1 + 6); + ok1(memrchr(haystack1, 'g', sizeof(haystack1)) == haystack1 + 7); + ok1(memrchr(haystack1, 'h', sizeof(haystack1)) == haystack1 + 8); + ok1(memrchr(haystack1, '\0', sizeof(haystack1)) == haystack1 + 9); + ok1(memrchr(haystack1, 'i', sizeof(haystack1)) == NULL); + + ok1(memrchr(haystack2, 'a', sizeof(haystack2)) == haystack2 + 9); + ok1(memrchr(haystack2, 'b', sizeof(haystack2)) == haystack2 + 10); + ok1(memrchr(haystack2, '\0', sizeof(haystack2)) == haystack2 + 11); + + ok1(memrchr(needle1, '\0', 2) == NULL); + +#define S(x) (x), sizeof(x) - 1 + ok1(mempbrkm(S(haystack1), S("\0efgh")) == haystack1 + 4); + ok1(mempbrkm(S(haystack1), S("jklmn")) == NULL); + ok1(mempbrkm(S(haystack1), S("sd\0a")) == haystack1 + 0); + + ok1(mempbrk(haystack1, sizeof(haystack1), "bcd\0a") == haystack1 + 1); + ok1(mempbrk(haystack1, sizeof(haystack1), "\0") == NULL); + + ok1(memcchr(scan1, 'a', sizeof(scan1)) == scan1 + 4); + ok1(memcchr(scan1, 'b', sizeof(scan1)) == scan1); + ok1(memcchr(scan2, '\0', sizeof(scan2)) == scan2 + 3); + ok1(memcchr(scan2, '\0', sizeof(scan2) - 2) == NULL); + + ok1(memeq(haystack1, sizeof(haystack1), haystack1, sizeof(haystack1))); + ok1(!memeq(haystack1, sizeof(haystack1), haystack2, sizeof(haystack2))); + + ok1(memeqstr(scan1, sizeof(scan1) - 1, scan1)); + ok1(!memeqstr(scan1, sizeof(scan1), scan1)); + ok1(!memeqstr(scan1, sizeof(scan1), "aaaa")); + + ok1(memstarts(S("a\0bcdef"), S("a\0bc"))); + ok1(!memstarts(S("a\0bcdef"), S("a\0bcG"))); + ok1(!memstarts(S("a\0bcdef"), S("a\0bcdefg"))); + + ok1(memstarts_str(scan1, sizeof(scan1), scan1)); + ok1(!memstarts_str(scan1, sizeof(scan1), "ab")); + + ok1(memends(S("abcdef"), S("abcdef"))); + ok1(!memends(S("abcdef"), S("abcdefg"))); + ok1(!memends(S("a\0bcdef"), S("a\0b"))); + ok1(memends(S("a\0bcdef"), S("ef"))); + + ok1(memends_str(S("abcdef"), "abcdef")); + ok1(!memends_str(S("abcde\0f"), "d\0f")); + ok1(!memends_str(S("a\0bcdef"), "a")); + ok1(memends_str(S("a\0bcdef"), "ef")); + + ok1(!memoverlaps(haystack1, sizeof(haystack1), + haystack2, sizeof(haystack2))); + ok1(!memoverlaps(haystack2, sizeof(haystack2), + haystack1, sizeof(haystack1))); + ok1(memoverlaps(haystack1, sizeof(haystack1), haystack1, 1)); + ok1(memoverlaps(haystack1, 1, haystack1, sizeof(haystack1))); + ok1(memoverlaps(haystack1, sizeof(haystack1), + haystack1 + sizeof(haystack1) - 1, 1)); + ok1(memoverlaps(haystack1 + sizeof(haystack1) - 1, 1, + haystack1, sizeof(haystack1))); + ok1(!memoverlaps(haystack1, sizeof(haystack1), + haystack1 + sizeof(haystack1), 1)); + ok1(!memoverlaps(haystack1 + sizeof(haystack1), 1, + haystack1, sizeof(haystack1))); + ok1(!memoverlaps(haystack1, sizeof(haystack1), haystack1 - 1, 1)); + ok1(!memoverlaps(haystack1 - 1, 1, haystack1, sizeof(haystack1))); + ok1(memoverlaps(haystack1, 5, haystack1 + 4, 7)); + ok1(!memoverlaps(haystack1, 5, haystack1 + 5, 6)); + ok1(memoverlaps(haystack1 + 4, 7, haystack1, 5)); + ok1(!memoverlaps(haystack1 + 5, 6, haystack1, 5)); + + assert(sizeof(haystack1) <= SWAPSIZE); + assert(sizeof(haystack2) <= SWAPSIZE); + memset(tmp1, 0, sizeof(tmp1)); + memset(tmp2, 0, sizeof(tmp2)); + memcpy(tmp1, haystack1, sizeof(haystack1)); + memcpy(tmp2, haystack2, sizeof(haystack2)); + memswap(tmp1, tmp2, SWAPSIZE); + ok1(memcmp(tmp1, haystack2, sizeof(haystack2)) == 0); + ok1(memcmp(tmp2, haystack1, sizeof(haystack1)) == 0); + + /* This exits depending on whether all tests passed */ + return exit_status(); +} diff --git a/ccan/ccan/mem/test/compile_fail-memcheck.c b/ccan/ccan/mem/test/compile_fail-memcheck.c new file mode 100644 index 000000000..32620f105 --- /dev/null +++ b/ccan/ccan/mem/test/compile_fail-memcheck.c @@ -0,0 +1,23 @@ +#include "config.h" + +#include +#include + +int main(void) +{ + const char *haystack = "abcd\0efgh"; + char *p; + +#ifdef FAIL +#if !HAVE_TYPEOF +#error "Can't fail without typeof" +#else + /* Should catch const discard errors. */ + p = memcheck(haystack, sizeof(haystack)); +#endif +#else + p = memcheck((char *)haystack, sizeof(haystack)); +#endif + + return p == haystack ? 0 : 1; +}