Skip to content

Commit 01f2e37

Browse files
committed
[compiler-rt][memprof] memccpy interception
1 parent c6c48b4 commit 01f2e37

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

compiler-rt/lib/memprof/memprof_interceptors.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ INTERCEPTOR(long long, atoll, const char *nptr) {
306306
return result;
307307
}
308308

309+
#if SANITIZER_INTERCEPT_MEMCCPY
310+
INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, usize size) {
311+
void *ctx;
312+
MEMPROF_INTERCEPTOR_ENTER(ctx, memccpy);
313+
if (UNLIKELY(!memprof_inited))
314+
return internal_memccpy(dest, src, c, size);
315+
ENSURE_MEMPROF_INITED();
316+
void *res = REAL(memccpy)(dest, src, c, size);
317+
if (res != nullptr)
318+
size = static_cast<char *>(res) - static_cast<char *>(dest);
319+
MEMPROF_READ_RANGE(src, size);
320+
MEMPROF_WRITE_RANGE(dest, size);
321+
return res;
322+
}
323+
#endif
324+
309325
// ---------------------- InitializeMemprofInterceptors ---------------- {{{1
310326
namespace __memprof {
311327
void InitializeMemprofInterceptors() {
@@ -333,6 +349,10 @@ void InitializeMemprofInterceptors() {
333349
MEMPROF_INTERCEPT_FUNC(pthread_create);
334350
MEMPROF_INTERCEPT_FUNC(pthread_join);
335351

352+
#if SANITIZER_INTERCEPT_MEMCCPY
353+
MEMPROF_INTERCEPT_FUNC(memccpy);
354+
#endif
355+
336356
InitializePlatformInterceptors();
337357

338358
VReport(1, "MemProfiler: libc interceptors initialized\n");

compiler-rt/lib/sanitizer_common/sanitizer_libc.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ int internal_memcmp(const void* s1, const void* s2, uptr n) {
4949
return 0;
5050
}
5151

52+
void *internal_memccpy(void *dest, const void *src, int c, uptr n) {
53+
char *d = (char *)dest;
54+
const char *s = (const char *)src;
55+
uptr i = 0;
56+
for (; i < n && s[i] != c; ++i) d[i] = s[i];
57+
if (n > 0 && i < n - 1)
58+
return d + i + 1;
59+
return nullptr;
60+
}
61+
5262
extern "C" {
5363
SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memcpy(void *dest,
5464
const void *src,

compiler-rt/lib/sanitizer_common/sanitizer_libc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ s64 internal_atoll(const char *nptr);
4141
void *internal_memchr(const void *s, int c, uptr n);
4242
void *internal_memrchr(const void *s, int c, uptr n);
4343
int internal_memcmp(const void* s1, const void* s2, uptr n);
44+
void *internal_memccpy(void *d, const void *s, int c, uptr n);
4445
ALWAYS_INLINE void *internal_memcpy(void *dest, const void *src, uptr n) {
4546
return __sanitizer_internal_memcpy(dest, src, n);
4647
}

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
198198
#define SANITIZER_INTERCEPT_MEMMEM (SI_POSIX && !SI_MAC_DEPLOYMENT_BELOW_10_7)
199199
#define SANITIZER_INTERCEPT_MEMCHR SI_NOT_FUCHSIA
200200
#define SANITIZER_INTERCEPT_MEMRCHR (SI_FREEBSD || SI_LINUX || SI_NETBSD)
201+
#define SANITIZER_INTERCEPT_MEMCCPY (SI_FREEBSD || SI_LINUX || SI_NETBSD)
201202

202203
#define SANITIZER_INTERCEPT_READ SI_POSIX
203204
#define SANITIZER_INTERCEPT_PREAD SI_POSIX
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clangxx_memprof -O0 %s -o %t
2+
// %env_memprof_opts=print_text=true:log_path=stdout %run %t | FileCheck %s
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
int main() {
9+
char *p = strdup("memccpy");
10+
char *d = (char *)malloc(4);
11+
void *r = memccpy(d, p, 'c', 8);
12+
int cmp = memcmp(r, "mem", 3);
13+
free(d);
14+
free(p);
15+
return cmp;
16+
}
17+
// CHECK: Memory allocation stack id

0 commit comments

Comments
 (0)