blob: ae4e938f4af9164fec7dc2202ccf29fdef222643 [file] [log] [blame]
Kostya Serebryany16e00752012-05-31 13:42:53 +00001//===-- sanitizer_libc.h ----------------------------------------*- C++ -*-===//
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries.
12// These tools can not use some of the libc functions directly because those
13// functions are intercepted. Instead, we implement a tiny subset of libc here.
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070014// FIXME: Some of functions declared in this file are in fact POSIX, not libc.
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000015//===----------------------------------------------------------------------===//
Kostya Serebryany9aead372012-05-31 14:11:07 +000016#ifndef SANITIZER_LIBC_H
17#define SANITIZER_LIBC_H
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000018
Alexey Samsonova92c0cb2012-06-19 08:52:02 +000019// ----------- ATTENTION -------------
20// This header should NOT include any other headers from sanitizer runtime.
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000021#include "sanitizer_internal_defs.h"
Alexey Samsonov3836ff22012-06-04 10:30:16 +000022
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000023namespace __sanitizer {
24
Alexey Samsonov3836ff22012-06-04 10:30:16 +000025// internal_X() is a custom implementation of X() for use in RTL.
Alexey Samsonov1f11d312012-06-05 09:49:25 +000026
27// String functions
Alexey Samsonovc9256972012-06-15 13:09:52 +000028s64 internal_atoll(const char *nptr);
Alexey Samsonov1f11d312012-06-05 09:49:25 +000029void *internal_memchr(const void *s, int c, uptr n);
Stephen Hines86277eb2015-03-23 12:06:32 -070030void *internal_memrchr(const void *s, int c, uptr n);
Alexey Samsonov327c1c12012-06-14 14:04:54 +000031int internal_memcmp(const void* s1, const void* s2, uptr n);
Alexey Samsonovf7667cc2012-06-07 11:54:08 +000032void *internal_memcpy(void *dest, const void *src, uptr n);
Alexander Potapenkof1673e62012-10-15 15:34:41 +000033void *internal_memmove(void *dest, const void *src, uptr n);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070034// Set [s, s + n) to 0. Both s and n should be 16-aligned.
35void internal_bzero_aligned16(void *s, uptr n);
Alexey Samsonov4fac1482012-06-08 14:11:12 +000036// Should not be used in performance-critical places.
37void *internal_memset(void *s, int c, uptr n);
Alexey Samsonov88207ab2012-06-15 12:24:07 +000038char* internal_strchr(const char *s, int c);
Alexey Samsonov72870db2013-09-03 13:09:28 +000039char *internal_strchrnul(const char *s, int c);
Alexey Samsonovc0d78c12012-06-04 13:27:49 +000040int internal_strcmp(const char *s1, const char *s2);
Alexey Samsonov25113472012-08-21 09:26:26 +000041uptr internal_strcspn(const char *s, const char *reject);
Alexey Samsonovf7667cc2012-06-07 11:54:08 +000042char *internal_strdup(const char *s);
Stephen Hines86277eb2015-03-23 12:06:32 -070043char *internal_strndup(const char *s, uptr n);
Alexey Samsonov230c3be2012-06-06 09:26:25 +000044uptr internal_strlen(const char *s);
Alexey Samsonovc9256972012-06-15 13:09:52 +000045char *internal_strncat(char *dst, const char *src, uptr n);
Alexey Samsonov8cd0df72012-06-18 14:34:59 +000046int internal_strncmp(const char *s1, const char *s2, uptr n);
Alexey Samsonov3836ff22012-06-04 10:30:16 +000047char *internal_strncpy(char *dst, const char *src, uptr n);
Alexey Samsonovc9256972012-06-15 13:09:52 +000048uptr internal_strnlen(const char *s, uptr maxlen);
Alexey Samsonov4fac1482012-06-08 14:11:12 +000049char *internal_strrchr(const char *s, int c);
Alexey Samsonovc9256972012-06-15 13:09:52 +000050// This is O(N^2), but we are not using it in hot places.
51char *internal_strstr(const char *haystack, const char *needle);
52// Works only for base=10 and doesn't set errno.
53s64 internal_simple_strtoll(const char *nptr, char **endptr, int base);
Alexey Samsonovf8822472013-02-20 13:54:32 +000054int internal_snprintf(char *buffer, uptr length, const char *format, ...);
Alexey Samsonov3836ff22012-06-04 10:30:16 +000055
Kostya Serebryanyeb280932012-12-28 15:24:16 +000056// Return true if all bytes in [mem, mem+size) are zero.
57// Optimized for the case when the result is true.
58bool mem_is_zero(const char *mem, uptr size);
59
Alexey Samsonov1f11d312012-06-05 09:49:25 +000060// I/O
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070061const fd_t kInvalidFd = (fd_t)-1;
Alexey Samsonovf3457cb2012-11-02 09:38:47 +000062const fd_t kStdinFd = 0;
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070063const fd_t kStdoutFd = (fd_t)1;
64const fd_t kStderrFd = (fd_t)2;
Alexey Samsonovee7cc442013-02-01 15:58:46 +000065
Stephen Hines2d1fdb22014-05-28 23:58:16 -070066uptr internal_ftruncate(fd_t fd, uptr size);
Alexey Samsonovf8822472013-02-20 13:54:32 +000067
68// OS
Alexey Samsonovf8822472013-02-20 13:54:32 +000069void NORETURN internal__exit(int exitcode);
Kostya Serebryany6fb47af2013-02-27 11:22:40 +000070
Peter Collingbourne0b694fc2013-05-17 16:56:53 +000071uptr internal_getpid();
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000072uptr internal_getppid();
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000073
Alexey Samsonov0969bcf2012-06-18 08:44:30 +000074// Threading
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000075uptr internal_sched_yield();
76
77// Error handling
78bool internal_iserror(uptr retval, int *rverrno = 0);
Alexey Samsonov0969bcf2012-06-18 08:44:30 +000079
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000080} // namespace __sanitizer
81
Kostya Serebryany9aead372012-05-31 14:11:07 +000082#endif // SANITIZER_LIBC_H