sanitizer_libc.cc revision 4fac1482179c6bfd84fa129560caa43f341c7336
1603c4be006d8c53905d736bf1f19a49f5ce98276Alexey Samsonov//===-- sanitizer_libc.cc -------------------------------------------------===// 2b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// 3b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// The LLVM Compiler Infrastructure 4b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// 5b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// This file is distributed under the University of Illinois Open Source 6b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// License. See LICENSE.TXT for details. 7b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// 8b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//===----------------------------------------------------------------------===// 9b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// 10b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// This file is shared between AddressSanitizer and ThreadSanitizer 1116e0075746b21ed866ec3be21ef0d1e46f0efed5Kostya Serebryany// run-time libraries. See sanitizer_libc.h for details. 12b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//===----------------------------------------------------------------------===// 13f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov#include "sanitizer_common.h" 1416e0075746b21ed866ec3be21ef0d1e46f0efed5Kostya Serebryany#include "sanitizer_libc.h" 15b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany 16b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryanynamespace __sanitizer { 17b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany 18b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryanyvoid MiniLibcStub() { 19b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany} 20b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany 211f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonovvoid *internal_memchr(const void *s, int c, uptr n) { 221f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov const char* t = (char*)s; 231f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov for (uptr i = 0; i < n; ++i, ++t) 241f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov if (*t == c) 251f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov return (void*)t; 261f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov return 0; 271f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov} 281f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov 29f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonovvoid *internal_memcpy(void *dest, const void *src, uptr n) { 30f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov char *d = (char*)dest; 31f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov char *s = (char*)src; 32f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov for (uptr i = 0; i < n; ++i) 33f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov d[i] = s[i]; 34f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov return dest; 35f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov} 36f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov 374fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonovvoid *internal_memset(void* s, int c, uptr n) { 384fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov // The next line prevents Clang from making a call to memset() instead of the 394fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov // loop below. 404fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov // FIXME: building the runtime with -ffreestanding is a better idea. However 414fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov // there currently are linktime problems due to PR12396. 424fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov char volatile *t = (char*)s; 434fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov for (uptr i = 0; i < n; ++i, ++t) { 444fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov *t = c; 454fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov } 464fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov return s; 474fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov} 484fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov 49f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonovchar* internal_strdup(const char *s) { 50f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov uptr len = internal_strlen(s); 51f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov char *s2 = (char*)InternalAlloc(len + 1); 52f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov internal_memcpy(s2, s, len); 53f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov s2[len] = 0; 54f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov return s2; 55f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov} 56f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonov 57c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonovint internal_strcmp(const char *s1, const char *s2) { 58c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov while (true) { 59c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov unsigned c1 = *s1; 60c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov unsigned c2 = *s2; 61c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov if (c1 != c2) return (c1 < c2) ? -1 : 1; 62c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov if (c1 == 0) break; 63c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov s1++; 64c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov s2++; 65c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov } 66c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov return 0; 67c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov} 68c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonov 694fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonovchar *internal_strrchr(const char *s, int c) { 704fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov const char *res = 0; 714fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov for (uptr i = 0; s[i]; i++) { 724fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov if (s[i] == c) res = s + i; 734fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov } 744fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov return (char*)res; 754fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov} 764fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov 77230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonovuptr internal_strlen(const char *s) { 78230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov uptr i = 0; 79230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov while (s[i]) i++; 80230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov return i; 81230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov} 82230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov 833836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonovchar *internal_strncpy(char *dst, const char *src, uptr n) { 843836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov uptr i; 853836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov for (i = 0; i < n && src[i]; i++) 863836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov dst[i] = src[i]; 873836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov for (; i < n; i++) 883836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov dst[i] = '\0'; 893836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov return dst; 903836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov} 913836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov 92b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany} // namespace __sanitizer 93