mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-18 19:16:43 +00:00
[Sanitizer] add sanitizer_posix.cc. Move more various functions into sanitizer_libc: sscanf, munmap, memchr
llvm-svn: 157994
This commit is contained in:
parent
9354688dc5
commit
7ac77d6b29
@ -249,14 +249,6 @@ char* internal_strchr(const char *s, int c) {
|
||||
}
|
||||
}
|
||||
|
||||
void* internal_memchr(const void* s, int c, uptr n) {
|
||||
const char* t = (char*)s;
|
||||
for (uptr i = 0; i < n; ++i, ++t)
|
||||
if (*t == c)
|
||||
return (void*)t;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int internal_memcmp(const void* s1, const void* s2, uptr n) {
|
||||
const char* t1 = (char*)s1;
|
||||
const char* t2 = (char*)s2;
|
||||
|
@ -35,7 +35,6 @@ s64 internal_atoll(const char *nptr);
|
||||
uptr internal_strlen(const char *s);
|
||||
uptr internal_strnlen(const char *s, uptr maxlen);
|
||||
char* internal_strchr(const char *s, int c);
|
||||
void* internal_memchr(const void* s, int c, uptr n);
|
||||
void* internal_memset(void *s, int c, uptr n);
|
||||
int internal_memcmp(const void* s1, const void* s2, uptr n);
|
||||
char *internal_strstr(const char *haystack, const char *needle);
|
||||
|
@ -194,7 +194,6 @@ uptr ReadFileToBuffer(const char *file_name, char **buff,
|
||||
void RawWrite(const char *buffer);
|
||||
int SNPrintf(char *buffer, uptr length, const char *format, ...);
|
||||
void Printf(const char *format, ...);
|
||||
int SScanf(const char *str, const char *format, ...);
|
||||
void Report(const char *format, ...);
|
||||
|
||||
// Don't use std::min and std::max, to minimize dependency on libstdc++.
|
||||
|
@ -103,7 +103,7 @@ void *AsanMprotect(uptr fixed_addr, uptr size) {
|
||||
|
||||
void AsanUnmapOrDie(void *addr, uptr size) {
|
||||
if (!addr || !size) return;
|
||||
int res = syscall(__NR_munmap, addr, size);
|
||||
int res = internal_munmap(addr, size);
|
||||
if (res != 0) {
|
||||
Report("Failed to unmap\n");
|
||||
AsanDie();
|
||||
@ -171,10 +171,10 @@ bool AsanProcMaps::Next(uptr *start, uptr *end,
|
||||
char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
|
||||
if (next_line == 0)
|
||||
next_line = last;
|
||||
if (SScanf(current_,
|
||||
"%lx-%lx %4s %lx %x:%x %ld %n",
|
||||
start, end, flags, offset, &major, &minor,
|
||||
&inode, &consumed) != 7)
|
||||
if (internal_sscanf(current_,
|
||||
"%lx-%lx %4s %lx %x:%x %ld %n",
|
||||
start, end, flags, offset, &major, &minor,
|
||||
&inode, &consumed) != 7)
|
||||
return false;
|
||||
current_ += consumed;
|
||||
// Skip spaces.
|
||||
|
@ -127,7 +127,7 @@ void *AsanMprotect(uptr fixed_addr, uptr size) {
|
||||
|
||||
void AsanUnmapOrDie(void *addr, uptr size) {
|
||||
if (!addr || !size) return;
|
||||
int res = munmap(addr, size);
|
||||
int res = internal_munmap(addr, size);
|
||||
if (res != 0) {
|
||||
Report("Failed to unmap\n");
|
||||
AsanDie();
|
||||
|
@ -193,17 +193,4 @@ void Report(const char *format, ...) {
|
||||
RawWrite(buffer);
|
||||
}
|
||||
|
||||
int SScanf(const char *str, const char *format, ...) {
|
||||
#ifndef _WIN32
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
int res = vsscanf(str, format, args);
|
||||
va_end(args);
|
||||
return res;
|
||||
#else
|
||||
UNIMPLEMENTED();
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace __asan
|
||||
|
@ -18,6 +18,14 @@ namespace __sanitizer {
|
||||
void MiniLibcStub() {
|
||||
}
|
||||
|
||||
void *internal_memchr(const void *s, int c, uptr n) {
|
||||
const char* t = (char*)s;
|
||||
for (uptr i = 0; i < n; ++i, ++t)
|
||||
if (*t == c)
|
||||
return (void*)t;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int internal_strcmp(const char *s1, const char *s2) {
|
||||
while (true) {
|
||||
unsigned c1 = *s1;
|
||||
|
@ -26,18 +26,25 @@ namespace __sanitizer {
|
||||
void MiniLibcStub();
|
||||
|
||||
// internal_X() is a custom implementation of X() for use in RTL.
|
||||
|
||||
// String functions
|
||||
void *internal_memchr(const void *s, int c, uptr n);
|
||||
int internal_strcmp(const char *s1, const char *s2);
|
||||
char *internal_strncpy(char *dst, const char *src, uptr n);
|
||||
|
||||
// Memory
|
||||
void *internal_mmap(void *addr, uptr length, int prot, int flags,
|
||||
int fd, u64 offset);
|
||||
int internal_munmap(void *addr, uptr length);
|
||||
|
||||
// I/O
|
||||
typedef int fd_t;
|
||||
const fd_t kInvalidFd = -1;
|
||||
int internal_close(fd_t fd);
|
||||
fd_t internal_open(const char *filename, bool write);
|
||||
uptr internal_read(fd_t fd, void *buf, uptr count);
|
||||
uptr internal_write(fd_t fd, const void *buf, uptr count);
|
||||
int internal_sscanf(const char *str, const char *format, ...);
|
||||
|
||||
} // namespace __sanitizer
|
||||
|
||||
|
@ -34,6 +34,10 @@ void *internal_mmap(void *addr, uptr length, int prot, int flags,
|
||||
#endif
|
||||
}
|
||||
|
||||
int internal_munmap(void *addr, uptr length) {
|
||||
return syscall(__NR_munmap, addr, length);
|
||||
}
|
||||
|
||||
int internal_close(fd_t fd) {
|
||||
return syscall(__NR_close, fd);
|
||||
}
|
||||
|
@ -30,6 +30,10 @@ void *internal_mmap(void *addr, size_t length, int prot, int flags,
|
||||
return mmap(addr, length, prot, flags, fd, offset);
|
||||
}
|
||||
|
||||
int internal_munmap(void *addr, uptr length) {
|
||||
return munmap(addr, length);
|
||||
}
|
||||
|
||||
int internal_close(fd_t fd) {
|
||||
return close(fd);
|
||||
}
|
||||
|
34
compiler-rt/lib/sanitizer_common/sanitizer_posix.cc
Normal file
34
compiler-rt/lib/sanitizer_common/sanitizer_posix.cc
Normal file
@ -0,0 +1,34 @@
|
||||
//===-- sanitizer_posix.cc ------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file is shared between AddressSanitizer and ThreadSanitizer
|
||||
// run-time libraries and implements POSIX-specific functions from
|
||||
// sanitizer_libc.h.
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
|
||||
#include "sanitizer_defs.h"
|
||||
#include "sanitizer_libc.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace __sanitizer {
|
||||
|
||||
int internal_sscanf(const char *str, const char *format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
int res = vsscanf(str, format, args);
|
||||
va_end(args);
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace __sanitizer
|
||||
|
||||
#endif // __linux__ || __APPLE_
|
@ -29,6 +29,11 @@ void *internal_mmap(void *addr, uptr length, int prot, int flags,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int internal_munmap(void *addr, uptr length) {
|
||||
UNIMPLEMENTED_WIN();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int internal_close(fd_t fd) {
|
||||
UNIMPLEMENTED_WIN();
|
||||
return 0;
|
||||
@ -58,6 +63,11 @@ uptr internal_write(fd_t fd, const void *buf, uptr count) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int internal_sscanf(const char *str, const char *format, ...) {
|
||||
UNIMPLEMENTED_WIN();
|
||||
return -1;
|
||||
}
|
||||
|
||||
} // namespace __sanitizer
|
||||
|
||||
#endif // _WIN32
|
||||
|
Loading…
x
Reference in New Issue
Block a user