sanitizer_libc.h revision 251134734f71a7e21a7bd17bc7b5821f07c803b2
116e0075746b21ed866ec3be21ef0d1e46f0efed5Kostya Serebryany//===-- sanitizer_libc.h ----------------------------------------*- C++ -*-===// 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 11b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// run-time libraries. 12b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// These tools can not use some of the libc functions directly because those 13b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany// functions are intercepted. Instead, we implement a tiny subset of libc here. 14a92c0cb873b67babb1aa4bc9aaa5c3b4d6c2772eAlexey Samsonov// NOTE: This file may be included into user code. 15b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany//===----------------------------------------------------------------------===// 169aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany#ifndef SANITIZER_LIBC_H 179aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany#define SANITIZER_LIBC_H 18b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany 19a92c0cb873b67babb1aa4bc9aaa5c3b4d6c2772eAlexey Samsonov// ----------- ATTENTION ------------- 20a92c0cb873b67babb1aa4bc9aaa5c3b4d6c2772eAlexey Samsonov// This header should NOT include any other headers from sanitizer runtime. 21a92c0cb873b67babb1aa4bc9aaa5c3b4d6c2772eAlexey Samsonov#include "sanitizer_interface_defs.h" 223836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov 23b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryanynamespace __sanitizer { 24b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany 253836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov// internal_X() is a custom implementation of X() for use in RTL. 261f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov 271f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov// String functions 28c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonovs64 internal_atoll(const char *nptr); 291f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonovvoid *internal_memchr(const void *s, int c, uptr n); 30327c1c17d9557ed8b197f732c8a070fd6ba821b5Alexey Samsonovint internal_memcmp(const void* s1, const void* s2, uptr n); 31f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonovvoid *internal_memcpy(void *dest, const void *src, uptr n); 324fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonov// Should not be used in performance-critical places. 334fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonovvoid *internal_memset(void *s, int c, uptr n); 3488207ab15125e2f1e9b3d541b735b2b8aba9b6d9Alexey Samsonovchar* internal_strchr(const char *s, int c); 35c0d78c1de1f2607c874020d27b72cf989c5ce092Alexey Samsonovint internal_strcmp(const char *s1, const char *s2); 36251134734f71a7e21a7bd17bc7b5821f07c803b2Alexey Samsonovuptr internal_strcspn(const char *s, const char *reject); 37f7667cc84cdd8923c0b6c7cfc92b7bd5692ce18cAlexey Samsonovchar *internal_strdup(const char *s); 38230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonovuptr internal_strlen(const char *s); 39c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonovchar *internal_strncat(char *dst, const char *src, uptr n); 408cd0df78c317106be477c5496f481af0563a5208Alexey Samsonovint internal_strncmp(const char *s1, const char *s2, uptr n); 413836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonovchar *internal_strncpy(char *dst, const char *src, uptr n); 42c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonovuptr internal_strnlen(const char *s, uptr maxlen); 434fac1482179c6bfd84fa129560caa43f341c7336Alexey Samsonovchar *internal_strrchr(const char *s, int c); 44c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov// This is O(N^2), but we are not using it in hot places. 45c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonovchar *internal_strstr(const char *haystack, const char *needle); 46c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov// Works only for base=10 and doesn't set errno. 47c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonovs64 internal_simple_strtoll(const char *nptr, char **endptr, int base); 483836ff2733d40e1182e301ef7de3eff9469777aeAlexey Samsonov 491f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov// Memory 50ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonovvoid *internal_mmap(void *addr, uptr length, int prot, int flags, 51ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov int fd, u64 offset); 521f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonovint internal_munmap(void *addr, uptr length); 53c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonov 541f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov// I/O 55c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonovtypedef int fd_t; 56a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonovconst fd_t kInvalidFd = -1; 57a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonovint internal_close(fd_t fd); 58c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonovfd_t internal_open(const char *filename, bool write); 59a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonovuptr internal_read(fd_t fd, void *buf, uptr count); 60a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonovuptr internal_write(fd_t fd, const void *buf, uptr count); 618e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonovuptr internal_filesize(fd_t fd); // -1 on error. 628e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonovint internal_dup2(int oldfd, int newfd); 63de08c02aa3007a590bfb7d43f31d5b1a0e7d337cAlexey Samsonovint internal_snprintf(char *buffer, uptr length, const char *format, ...); 64ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov 650969bcf2c936126b1f6e636b978aade8fc207437Alexey Samsonov// Threading 660969bcf2c936126b1f6e636b978aade8fc207437Alexey Samsonovint internal_sched_yield(); 670969bcf2c936126b1f6e636b978aade8fc207437Alexey Samsonov 68b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany} // namespace __sanitizer 69b3cedf98a3c8545da2234c2d35cb5d687984035fKostya Serebryany 709aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany#endif // SANITIZER_LIBC_H 71