Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions compiler-rt/lib/memprof/memprof_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ INTERCEPTOR(long long, atoll, const char *nptr) {
return result;
}

#if SANITIZER_INTERCEPT_MEMCCPY
INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, usize size) {
void *ctx;
MEMPROF_INTERCEPTOR_ENTER(ctx, memccpy);
if (UNLIKELY(!memprof_inited))
return internal_memccpy(dest, src, c, size);
ENSURE_MEMPROF_INITED();
void *res = REAL(memccpy)(dest, src, c, size);
if (res != nullptr)
size = static_cast<char *>(res) - static_cast<char *>(dest);
MEMPROF_READ_RANGE(src, size);
MEMPROF_WRITE_RANGE(dest, size);
return res;
}
#endif

// ---------------------- InitializeMemprofInterceptors ---------------- {{{1
namespace __memprof {
void InitializeMemprofInterceptors() {
Expand Down Expand Up @@ -333,6 +349,10 @@ void InitializeMemprofInterceptors() {
MEMPROF_INTERCEPT_FUNC(pthread_create);
MEMPROF_INTERCEPT_FUNC(pthread_join);

#if SANITIZER_INTERCEPT_MEMCCPY
MEMPROF_INTERCEPT_FUNC(memccpy);
#endif

InitializePlatformInterceptors();

VReport(1, "MemProfiler: libc interceptors initialized\n");
Expand Down
13 changes: 13 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_libc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ int internal_memcmp(const void* s1, const void* s2, uptr n) {
return 0;
}

void *internal_memccpy(void *dest, const void *src, int c, uptr n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have a test for this. Unfortunately, I don't see any internal_* functions being tested in compiler-rt/test right now, but it would make sense to start with this one.

char *d = (char *)dest;
const char *s = (const char *)src;
uptr i = 0;
for (; i < n; ++i) {
d[i] = s[i];
if (s[i] == c) break;
}
if (n > 0 && i < n)
return d + i + 1;
return nullptr;
}

extern "C" {
SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memcpy(void *dest,
const void *src,
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ s64 internal_atoll(const char *nptr);
void *internal_memchr(const void *s, int c, uptr n);
void *internal_memrchr(const void *s, int c, uptr n);
int internal_memcmp(const void* s1, const void* s2, uptr n);
void *internal_memccpy(void *d, const void *s, int c, uptr n);
ALWAYS_INLINE void *internal_memcpy(void *dest, const void *src, uptr n) {
return __sanitizer_internal_memcpy(dest, src, n);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
#define SANITIZER_INTERCEPT_MEMMEM (SI_POSIX && !SI_MAC_DEPLOYMENT_BELOW_10_7)
#define SANITIZER_INTERCEPT_MEMCHR SI_NOT_FUCHSIA
#define SANITIZER_INTERCEPT_MEMRCHR (SI_FREEBSD || SI_LINUX || SI_NETBSD)
#define SANITIZER_INTERCEPT_MEMCCPY (SI_FREEBSD || SI_LINUX || SI_NETBSD)

#define SANITIZER_INTERCEPT_READ SI_POSIX
#define SANITIZER_INTERCEPT_PREAD SI_POSIX
Expand Down
17 changes: 17 additions & 0 deletions compiler-rt/test/memprof/TestCases/memprof_memccpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %clangxx_memprof -O0 %s -o %t
// %env_memprof_opts=print_text=true:log_path=stdout %run %t | FileCheck %s

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char *p = strdup("memccpy");
char *d = (char *)malloc(5);
void *r = memccpy(d, p, 'c', 8);
int cmp = memcmp(r, "memc", 4);
free(d);
free(p);
return cmp;
}
// CHECK: Memory allocation stack id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other allocations in this code without the memccpy interception, does this test fail without that support? If not, can the checking be tightened up?

Loading