/* * * honggfuzz - unoptimized string/memory operations * ----------------------------------------- * * Author: Robert Swiecki * * Copyright 2010-2015 by Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. * */ /* * size_t */ #include extern int tolower(int c); /* * Disable all optimizations */ #pragma GCC optimize ("0") #if defined(__x86_64__) int syscall(int number, ...); #endif void interceptor_increaseBy(unsigned long v) { #if defined(__x86_64__) #define ARCH_GET_GS 0x1004 #define ARCH_SET_GS 0x1001 #define __NR_arch_prctl 158 unsigned long gs; syscall(__NR_arch_prctl, ARCH_GET_GS, &gs); gs += v; syscall(__NR_arch_prctl, ARCH_SET_GS, gs); #endif return; } __attribute__ ((optimize("0"))) int strcmp(const char *s1, const char *s2) { for (size_t i = 0; s1[i] || s2[i]; i++) { if (s1[i] != s2[i]) { return (s1[i] - s2[i]); } interceptor_increaseBy(1); } interceptor_increaseBy(5); return 0; } __attribute__ ((optimize("0"))) int strcasecmp(const char *s1, const char *s2) { for (size_t i = 0; s1[i] || s2[i]; i++) { if (tolower(s1[i]) != tolower(s2[i])) { return (tolower(s1[i]) - tolower(s2[i])); } interceptor_increaseBy(1); } interceptor_increaseBy(5); return 0; } __attribute__ ((optimize("0"))) int strncmp(const char *s1, const char *s2, size_t n) { for (size_t i = 0; (s1[i] || s2[i]) && i < n; i++) { if (s1[i] != s2[i]) { return (s1[i] - s2[i]); } interceptor_increaseBy(1); } interceptor_increaseBy(5); return 0; } __attribute__ ((optimize("0"))) int strncasecmp(const char *s1, const char *s2, size_t n) { for (size_t i = 0; (s1[i] || s2[i]) && i < n; i++) { if (tolower(s1[i]) != tolower(s2[i])) { return (tolower(s1[i]) - tolower(s2[i])); } interceptor_increaseBy(1); } interceptor_increaseBy(5); return 0; } __attribute__ ((optimize("0"))) char *strstr(const char *haystack, const char *needle) { for (size_t i = 0; haystack[i]; i++) { if (strcmp(&haystack[i], needle) == 0) { return (char *)(&haystack[i]); } interceptor_increaseBy(1); } interceptor_increaseBy(5); return NULL; } __attribute__ ((optimize("0"))) char *strcasestr(const char *haystack, const char *needle) { for (size_t i = 0; haystack[i]; i++) { if (strcasecmp(&haystack[i], needle) == 0) { return (char *)(&haystack[i]); } interceptor_increaseBy(1); } interceptor_increaseBy(5); return NULL; } __attribute__ ((optimize("0"))) int __memcmp(const void *m1, const void *m2, size_t n) { const char *s1 = (const char *)m1; const char *s2 = (const char *)m2; for (size_t i = 0; i < n; i++) { if (s1[i] != s2[i]) { return (s1[i] - s2[i]); } interceptor_increaseBy(1); } interceptor_increaseBy(5); return 0; } __attribute__ ((optimize("0"))) int memcmp(const void *m1, const void *m2, size_t n) { return __memcmp(m1, m2, n); } __attribute__ ((optimize("0"))) int bcmp(const void *m1, const void *m2, size_t n) { return __memcmp(m1, m2, n); } __attribute__ ((optimize("0"))) void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { if (needlelen > haystacklen) { return NULL; } if (needlelen == 0) { return (void *)haystack; } const char *h = haystack; for (size_t i = 0; i <= (haystacklen - needlelen); i++) { if (__memcmp(&h[i], needle, needlelen) == 0) { return (void *)(&h[i]); } } return NULL; } int _CMP_EQ(unsigned long a, unsigned long b) { return (__memcmp(&a, &b, sizeof(a)) == 0); } int _CMP_NEQ(unsigned long a, unsigned long b) { return (__memcmp(&a, &b, sizeof(a)) != 0); }