]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc
MFV r305100: Update amd from am-utils 6.1.5 to 6.2.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_mac.cc
1 //===-- sanitizer_mac.cc --------------------------------------------------===//
2 //
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 various sanitizers' runtime libraries and
11 // implements OSX-specific functions.
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_platform.h"
15 #if SANITIZER_MAC
16 #include "sanitizer_mac.h"
17
18 // Use 64-bit inodes in file operations. ASan does not support OS X 10.5, so
19 // the clients will most certainly use 64-bit ones as well.
20 #ifndef _DARWIN_USE_64_BIT_INODE
21 #define _DARWIN_USE_64_BIT_INODE 1
22 #endif
23 #include <stdio.h>
24
25 #include "sanitizer_common.h"
26 #include "sanitizer_flags.h"
27 #include "sanitizer_internal_defs.h"
28 #include "sanitizer_libc.h"
29 #include "sanitizer_placement_new.h"
30 #include "sanitizer_platform_limits_posix.h"
31 #include "sanitizer_procmaps.h"
32
33 #if !SANITIZER_IOS
34 #include <crt_externs.h>  // for _NSGetEnviron
35 #else
36 extern char **environ;
37 #endif
38
39 #if defined(__has_include) && __has_include(<os/trace.h>)
40 #define SANITIZER_OS_TRACE 1
41 #include <os/trace.h>
42 #else
43 #define SANITIZER_OS_TRACE 0
44 #endif
45
46 #if !SANITIZER_IOS
47 #include <crt_externs.h>  // for _NSGetArgv and _NSGetEnviron
48 #else
49 extern "C" {
50   extern char ***_NSGetArgv(void);
51 }
52 #endif
53
54 #include <asl.h>
55 #include <dlfcn.h>  // for dladdr()
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <libkern/OSAtomic.h>
59 #include <mach-o/dyld.h>
60 #include <mach/mach.h>
61 #include <mach/vm_statistics.h>
62 #include <pthread.h>
63 #include <sched.h>
64 #include <signal.h>
65 #include <stdlib.h>
66 #include <sys/mman.h>
67 #include <sys/resource.h>
68 #include <sys/stat.h>
69 #include <sys/sysctl.h>
70 #include <sys/types.h>
71 #include <unistd.h>
72 #include <util.h>
73
74 namespace __sanitizer {
75
76 #include "sanitizer_syscall_generic.inc"
77
78 // ---------------------- sanitizer_libc.h
79 uptr internal_mmap(void *addr, size_t length, int prot, int flags,
80                    int fd, u64 offset) {
81   if (fd == -1) fd = VM_MAKE_TAG(VM_MEMORY_ANALYSIS_TOOL);
82   return (uptr)mmap(addr, length, prot, flags, fd, offset);
83 }
84
85 uptr internal_munmap(void *addr, uptr length) {
86   return munmap(addr, length);
87 }
88
89 int internal_mprotect(void *addr, uptr length, int prot) {
90   return mprotect(addr, length, prot);
91 }
92
93 uptr internal_close(fd_t fd) {
94   return close(fd);
95 }
96
97 uptr internal_open(const char *filename, int flags) {
98   return open(filename, flags);
99 }
100
101 uptr internal_open(const char *filename, int flags, u32 mode) {
102   return open(filename, flags, mode);
103 }
104
105 uptr internal_read(fd_t fd, void *buf, uptr count) {
106   return read(fd, buf, count);
107 }
108
109 uptr internal_write(fd_t fd, const void *buf, uptr count) {
110   return write(fd, buf, count);
111 }
112
113 uptr internal_stat(const char *path, void *buf) {
114   return stat(path, (struct stat *)buf);
115 }
116
117 uptr internal_lstat(const char *path, void *buf) {
118   return lstat(path, (struct stat *)buf);
119 }
120
121 uptr internal_fstat(fd_t fd, void *buf) {
122   return fstat(fd, (struct stat *)buf);
123 }
124
125 uptr internal_filesize(fd_t fd) {
126   struct stat st;
127   if (internal_fstat(fd, &st))
128     return -1;
129   return (uptr)st.st_size;
130 }
131
132 uptr internal_dup2(int oldfd, int newfd) {
133   return dup2(oldfd, newfd);
134 }
135
136 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
137   return readlink(path, buf, bufsize);
138 }
139
140 uptr internal_unlink(const char *path) {
141   return unlink(path);
142 }
143
144 uptr internal_sched_yield() {
145   return sched_yield();
146 }
147
148 void internal__exit(int exitcode) {
149   _exit(exitcode);
150 }
151
152 uptr internal_getpid() {
153   return getpid();
154 }
155
156 int internal_sigaction(int signum, const void *act, void *oldact) {
157   return sigaction(signum,
158                    (struct sigaction *)act, (struct sigaction *)oldact);
159 }
160
161 void internal_sigfillset(__sanitizer_sigset_t *set) { sigfillset(set); }
162
163 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
164                           __sanitizer_sigset_t *oldset) {
165   return sigprocmask(how, set, oldset);
166 }
167
168 // Doesn't call pthread_atfork() handlers.
169 extern "C" pid_t __fork(void);
170
171 int internal_fork() {
172   return __fork();
173 }
174
175 int internal_forkpty(int *amaster) {
176   int master, slave;
177   if (openpty(&master, &slave, nullptr, nullptr, nullptr) == -1) return -1;
178   int pid = __fork();
179   if (pid == -1) {
180     close(master);
181     close(slave);
182     return -1;
183   }
184   if (pid == 0) {
185     close(master);
186     CHECK_EQ(login_tty(slave), 0);
187   } else {
188     *amaster = master;
189     close(slave);
190   }
191   return pid;
192 }
193
194 uptr internal_rename(const char *oldpath, const char *newpath) {
195   return rename(oldpath, newpath);
196 }
197
198 uptr internal_ftruncate(fd_t fd, uptr size) {
199   return ftruncate(fd, size);
200 }
201
202 // ----------------- sanitizer_common.h
203 bool FileExists(const char *filename) {
204   struct stat st;
205   if (stat(filename, &st))
206     return false;
207   // Sanity check: filename is a regular file.
208   return S_ISREG(st.st_mode);
209 }
210
211 uptr GetTid() {
212   return reinterpret_cast<uptr>(pthread_self());
213 }
214
215 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
216                                 uptr *stack_bottom) {
217   CHECK(stack_top);
218   CHECK(stack_bottom);
219   uptr stacksize = pthread_get_stacksize_np(pthread_self());
220   // pthread_get_stacksize_np() returns an incorrect stack size for the main
221   // thread on Mavericks. See
222   // https://github.com/google/sanitizers/issues/261
223   if ((GetMacosVersion() >= MACOS_VERSION_MAVERICKS) && at_initialization &&
224       stacksize == (1 << 19))  {
225     struct rlimit rl;
226     CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
227     // Most often rl.rlim_cur will be the desired 8M.
228     if (rl.rlim_cur < kMaxThreadStackSize) {
229       stacksize = rl.rlim_cur;
230     } else {
231       stacksize = kMaxThreadStackSize;
232     }
233   }
234   void *stackaddr = pthread_get_stackaddr_np(pthread_self());
235   *stack_top = (uptr)stackaddr;
236   *stack_bottom = *stack_top - stacksize;
237 }
238
239 char **GetEnviron() {
240 #if !SANITIZER_IOS
241   char ***env_ptr = _NSGetEnviron();
242   if (!env_ptr) {
243     Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is "
244            "called after libSystem_initializer().\n");
245     CHECK(env_ptr);
246   }
247   char **environ = *env_ptr;
248 #endif
249   CHECK(environ);
250   return environ;
251 }
252
253 const char *GetEnv(const char *name) {
254   char **env = GetEnviron();
255   uptr name_len = internal_strlen(name);
256   while (*env != 0) {
257     uptr len = internal_strlen(*env);
258     if (len > name_len) {
259       const char *p = *env;
260       if (!internal_memcmp(p, name, name_len) &&
261           p[name_len] == '=') {  // Match.
262         return *env + name_len + 1;  // String starting after =.
263       }
264     }
265     env++;
266   }
267   return 0;
268 }
269
270 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
271   CHECK_LE(kMaxPathLength, buf_len);
272
273   // On OS X the executable path is saved to the stack by dyld. Reading it
274   // from there is much faster than calling dladdr, especially for large
275   // binaries with symbols.
276   InternalScopedString exe_path(kMaxPathLength);
277   uint32_t size = exe_path.size();
278   if (_NSGetExecutablePath(exe_path.data(), &size) == 0 &&
279       realpath(exe_path.data(), buf) != 0) {
280     return internal_strlen(buf);
281   }
282   return 0;
283 }
284
285 uptr ReadLongProcessName(/*out*/char *buf, uptr buf_len) {
286   return ReadBinaryName(buf, buf_len);
287 }
288
289 void ReExec() {
290   UNIMPLEMENTED();
291 }
292
293 uptr GetPageSize() {
294   return sysconf(_SC_PAGESIZE);
295 }
296
297 BlockingMutex::BlockingMutex() {
298   internal_memset(this, 0, sizeof(*this));
299 }
300
301 void BlockingMutex::Lock() {
302   CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
303   CHECK_EQ(OS_SPINLOCK_INIT, 0);
304   CHECK_NE(owner_, (uptr)pthread_self());
305   OSSpinLockLock((OSSpinLock*)&opaque_storage_);
306   CHECK(!owner_);
307   owner_ = (uptr)pthread_self();
308 }
309
310 void BlockingMutex::Unlock() {
311   CHECK(owner_ == (uptr)pthread_self());
312   owner_ = 0;
313   OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
314 }
315
316 void BlockingMutex::CheckLocked() {
317   CHECK_EQ((uptr)pthread_self(), owner_);
318 }
319
320 u64 NanoTime() {
321   return 0;
322 }
323
324 uptr GetTlsSize() {
325   return 0;
326 }
327
328 void InitTlsSize() {
329 }
330
331 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
332                           uptr *tls_addr, uptr *tls_size) {
333 #ifndef SANITIZER_GO
334   uptr stack_top, stack_bottom;
335   GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
336   *stk_addr = stack_bottom;
337   *stk_size = stack_top - stack_bottom;
338   *tls_addr = 0;
339   *tls_size = 0;
340 #else
341   *stk_addr = 0;
342   *stk_size = 0;
343   *tls_addr = 0;
344   *tls_size = 0;
345 #endif
346 }
347
348 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
349                       string_predicate_t filter) {
350   MemoryMappingLayout memory_mapping(false);
351   return memory_mapping.DumpListOfModules(modules, max_modules, filter);
352 }
353
354 bool IsDeadlySignal(int signum) {
355   return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv;
356 }
357
358 MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED;
359
360 MacosVersion GetMacosVersionInternal() {
361   int mib[2] = { CTL_KERN, KERN_OSRELEASE };
362   char version[100];
363   uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]);
364   for (uptr i = 0; i < maxlen; i++) version[i] = '\0';
365   // Get the version length.
366   CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1);
367   CHECK_LT(len, maxlen);
368   CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1);
369   switch (version[0]) {
370     case '9': return MACOS_VERSION_LEOPARD;
371     case '1': {
372       switch (version[1]) {
373         case '0': return MACOS_VERSION_SNOW_LEOPARD;
374         case '1': return MACOS_VERSION_LION;
375         case '2': return MACOS_VERSION_MOUNTAIN_LION;
376         case '3': return MACOS_VERSION_MAVERICKS;
377         case '4': return MACOS_VERSION_YOSEMITE;
378         default:
379           if (IsDigit(version[1]))
380             return MACOS_VERSION_UNKNOWN_NEWER;
381           else
382             return MACOS_VERSION_UNKNOWN;
383       }
384     }
385     default: return MACOS_VERSION_UNKNOWN;
386   }
387 }
388
389 MacosVersion GetMacosVersion() {
390   atomic_uint32_t *cache =
391       reinterpret_cast<atomic_uint32_t*>(&cached_macos_version);
392   MacosVersion result =
393       static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire));
394   if (result == MACOS_VERSION_UNINITIALIZED) {
395     result = GetMacosVersionInternal();
396     atomic_store(cache, result, memory_order_release);
397   }
398   return result;
399 }
400
401 uptr GetRSS() {
402   struct task_basic_info info;
403   unsigned count = TASK_BASIC_INFO_COUNT;
404   kern_return_t result =
405       task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count);
406   if (UNLIKELY(result != KERN_SUCCESS)) {
407     Report("Cannot get task info. Error: %d\n", result);
408     Die();
409   }
410   return info.resident_size;
411 }
412
413 void *internal_start_thread(void(*func)(void *arg), void *arg) {
414   // Start the thread with signals blocked, otherwise it can steal user signals.
415   __sanitizer_sigset_t set, old;
416   internal_sigfillset(&set);
417   internal_sigprocmask(SIG_SETMASK, &set, &old);
418   pthread_t th;
419   pthread_create(&th, 0, (void*(*)(void *arg))func, arg);
420   internal_sigprocmask(SIG_SETMASK, &old, 0);
421   return th;
422 }
423
424 void internal_join_thread(void *th) { pthread_join((pthread_t)th, 0); }
425
426 static BlockingMutex syslog_lock(LINKER_INITIALIZED);
427
428 void WriteOneLineToSyslog(const char *s) {
429   syslog_lock.CheckLocked();
430   asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", s);
431 }
432
433 void LogMessageOnPrintf(const char *str) {
434   // Log all printf output to CrashLog.
435   if (common_flags()->abort_on_error)
436     CRAppendCrashLogMessage(str);
437 }
438
439 void LogFullErrorReport(const char *buffer) {
440   // Log with os_trace. This will make it into the crash log.
441 #if SANITIZER_OS_TRACE
442   if (GetMacosVersion() >= MACOS_VERSION_YOSEMITE) {
443     // os_trace requires the message (format parameter) to be a string literal.
444     if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
445                          sizeof("AddressSanitizer") - 1) == 0)
446       os_trace("Address Sanitizer reported a failure.");
447     else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
448                               sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
449       os_trace("Undefined Behavior Sanitizer reported a failure.");
450     else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
451                               sizeof("ThreadSanitizer") - 1) == 0)
452       os_trace("Thread Sanitizer reported a failure.");
453     else
454       os_trace("Sanitizer tool reported a failure.");
455
456     if (common_flags()->log_to_syslog)
457       os_trace("Consult syslog for more information.");
458   }
459 #endif
460
461   // Log to syslog.
462   // The logging on OS X may call pthread_create so we need the threading
463   // environment to be fully initialized. Also, this should never be called when
464   // holding the thread registry lock since that may result in a deadlock. If
465   // the reporting thread holds the thread registry mutex, and asl_log waits
466   // for GCD to dispatch a new thread, the process will deadlock, because the
467   // pthread_create wrapper needs to acquire the lock as well.
468   BlockingMutexLock l(&syslog_lock);
469   if (common_flags()->log_to_syslog)
470     WriteToSyslog(buffer);
471
472   // The report is added to CrashLog as part of logging all of Printf output.
473 }
474
475 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
476   ucontext_t *ucontext = (ucontext_t*)context;
477 # if defined(__aarch64__)
478   *pc = ucontext->uc_mcontext->__ss.__pc;
479 #   if defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
480   *bp = ucontext->uc_mcontext->__ss.__fp;
481 #   else
482   *bp = ucontext->uc_mcontext->__ss.__lr;
483 #   endif
484   *sp = ucontext->uc_mcontext->__ss.__sp;
485 # elif defined(__x86_64__)
486   *pc = ucontext->uc_mcontext->__ss.__rip;
487   *bp = ucontext->uc_mcontext->__ss.__rbp;
488   *sp = ucontext->uc_mcontext->__ss.__rsp;
489 # elif defined(__arm__)
490   *pc = ucontext->uc_mcontext->__ss.__pc;
491   *bp = ucontext->uc_mcontext->__ss.__r[7];
492   *sp = ucontext->uc_mcontext->__ss.__sp;
493 # elif defined(__i386__)
494   *pc = ucontext->uc_mcontext->__ss.__eip;
495   *bp = ucontext->uc_mcontext->__ss.__ebp;
496   *sp = ucontext->uc_mcontext->__ss.__esp;
497 # else
498 # error "Unknown architecture"
499 # endif
500 }
501
502 static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES";
503 LowLevelAllocator allocator_for_env;
504
505 // Change the value of the env var |name|, leaking the original value.
506 // If |name_value| is NULL, the variable is deleted from the environment,
507 // otherwise the corresponding "NAME=value" string is replaced with
508 // |name_value|.
509 void LeakyResetEnv(const char *name, const char *name_value) {
510   char **env = GetEnviron();
511   uptr name_len = internal_strlen(name);
512   while (*env != 0) {
513     uptr len = internal_strlen(*env);
514     if (len > name_len) {
515       const char *p = *env;
516       if (!internal_memcmp(p, name, name_len) && p[name_len] == '=') {
517         // Match.
518         if (name_value) {
519           // Replace the old value with the new one.
520           *env = const_cast<char*>(name_value);
521         } else {
522           // Shift the subsequent pointers back.
523           char **del = env;
524           do {
525             del[0] = del[1];
526           } while (*del++);
527         }
528       }
529     }
530     env++;
531   }
532 }
533
534 static bool reexec_disabled = false;
535
536 void DisableReexec() {
537   reexec_disabled = true;
538 }
539
540 extern "C" double dyldVersionNumber;
541 static const double kMinDyldVersionWithAutoInterposition = 360.0;
542
543 bool DyldNeedsEnvVariable() {
544   // If running on OS X 10.11+ or iOS 9.0+, dyld will interpose even if
545   // DYLD_INSERT_LIBRARIES is not set. However, checking OS version via
546   // GetMacosVersion() doesn't work for the simulator. Let's instead check
547   // `dyldVersionNumber`, which is exported by dyld, against a known version
548   // number from the first OS release where this appeared.
549   return dyldVersionNumber < kMinDyldVersionWithAutoInterposition;
550 }
551
552 void MaybeReexec() {
553   if (reexec_disabled) return;
554
555   // Make sure the dynamic runtime library is preloaded so that the
556   // wrappers work. If it is not, set DYLD_INSERT_LIBRARIES and re-exec
557   // ourselves.
558   Dl_info info;
559   CHECK(dladdr((void*)((uptr)&__sanitizer_report_error_summary), &info));
560   char *dyld_insert_libraries =
561       const_cast<char*>(GetEnv(kDyldInsertLibraries));
562   uptr old_env_len = dyld_insert_libraries ?
563       internal_strlen(dyld_insert_libraries) : 0;
564   uptr fname_len = internal_strlen(info.dli_fname);
565   const char *dylib_name = StripModuleName(info.dli_fname);
566   uptr dylib_name_len = internal_strlen(dylib_name);
567
568   bool lib_is_in_env = dyld_insert_libraries &&
569                        internal_strstr(dyld_insert_libraries, dylib_name);
570   if (DyldNeedsEnvVariable() && !lib_is_in_env) {
571     // DYLD_INSERT_LIBRARIES is not set or does not contain the runtime
572     // library.
573     InternalScopedString program_name(1024);
574     uint32_t buf_size = program_name.size();
575     _NSGetExecutablePath(program_name.data(), &buf_size);
576     char *new_env = const_cast<char*>(info.dli_fname);
577     if (dyld_insert_libraries) {
578       // Append the runtime dylib name to the existing value of
579       // DYLD_INSERT_LIBRARIES.
580       new_env = (char*)allocator_for_env.Allocate(old_env_len + fname_len + 2);
581       internal_strncpy(new_env, dyld_insert_libraries, old_env_len);
582       new_env[old_env_len] = ':';
583       // Copy fname_len and add a trailing zero.
584       internal_strncpy(new_env + old_env_len + 1, info.dli_fname,
585                        fname_len + 1);
586       // Ok to use setenv() since the wrappers don't depend on the value of
587       // asan_inited.
588       setenv(kDyldInsertLibraries, new_env, /*overwrite*/1);
589     } else {
590       // Set DYLD_INSERT_LIBRARIES equal to the runtime dylib name.
591       setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0);
592     }
593     VReport(1, "exec()-ing the program with\n");
594     VReport(1, "%s=%s\n", kDyldInsertLibraries, new_env);
595     VReport(1, "to enable wrappers.\n");
596     execv(program_name.data(), *_NSGetArgv());
597
598     // We get here only if execv() failed.
599     Report("ERROR: The process is launched without DYLD_INSERT_LIBRARIES, "
600            "which is required for the sanitizer to work. We tried to set the "
601            "environment variable and re-execute itself, but execv() failed, "
602            "possibly because of sandbox restrictions. Make sure to launch the "
603            "executable with:\n%s=%s\n", kDyldInsertLibraries, new_env);
604     CHECK("execv failed" && 0);
605   }
606
607   if (!lib_is_in_env)
608     return;
609
610   // DYLD_INSERT_LIBRARIES is set and contains the runtime library. Let's remove
611   // the dylib from the environment variable, because interceptors are installed
612   // and we don't want our children to inherit the variable.
613
614   uptr env_name_len = internal_strlen(kDyldInsertLibraries);
615   // Allocate memory to hold the previous env var name, its value, the '='
616   // sign and the '\0' char.
617   char *new_env = (char*)allocator_for_env.Allocate(
618       old_env_len + 2 + env_name_len);
619   CHECK(new_env);
620   internal_memset(new_env, '\0', old_env_len + 2 + env_name_len);
621   internal_strncpy(new_env, kDyldInsertLibraries, env_name_len);
622   new_env[env_name_len] = '=';
623   char *new_env_pos = new_env + env_name_len + 1;
624
625   // Iterate over colon-separated pieces of |dyld_insert_libraries|.
626   char *piece_start = dyld_insert_libraries;
627   char *piece_end = NULL;
628   char *old_env_end = dyld_insert_libraries + old_env_len;
629   do {
630     if (piece_start[0] == ':') piece_start++;
631     piece_end = internal_strchr(piece_start, ':');
632     if (!piece_end) piece_end = dyld_insert_libraries + old_env_len;
633     if ((uptr)(piece_start - dyld_insert_libraries) > old_env_len) break;
634     uptr piece_len = piece_end - piece_start;
635
636     char *filename_start =
637         (char *)internal_memrchr(piece_start, '/', piece_len);
638     uptr filename_len = piece_len;
639     if (filename_start) {
640       filename_start += 1;
641       filename_len = piece_len - (filename_start - piece_start);
642     } else {
643       filename_start = piece_start;
644     }
645
646     // If the current piece isn't the runtime library name,
647     // append it to new_env.
648     if ((dylib_name_len != filename_len) ||
649         (internal_memcmp(filename_start, dylib_name, dylib_name_len) != 0)) {
650       if (new_env_pos != new_env + env_name_len + 1) {
651         new_env_pos[0] = ':';
652         new_env_pos++;
653       }
654       internal_strncpy(new_env_pos, piece_start, piece_len);
655       new_env_pos += piece_len;
656     }
657     // Move on to the next piece.
658     piece_start = piece_end;
659   } while (piece_start < old_env_end);
660
661   // Can't use setenv() here, because it requires the allocator to be
662   // initialized.
663   // FIXME: instead of filtering DYLD_INSERT_LIBRARIES here, do it in
664   // a separate function called after InitializeAllocator().
665   if (new_env_pos == new_env + env_name_len + 1) new_env = NULL;
666   LeakyResetEnv(kDyldInsertLibraries, new_env);
667 }
668
669 }  // namespace __sanitizer
670
671 #endif  // SANITIZER_MAC