]> git.saurik.com Git - apple/libsystem.git/blame - enable_asan_mode
Libsystem-1292.100.5.tar.gz
[apple/libsystem.git] / enable_asan_mode
CommitLineData
c06d156a
A
1#!/bin/bash -e
2
3if [ $# -ne 1 ]; then
4 echo "Usage:" 1>&2
5 echo " sudo $0 enable ... enables ASanification of system libraries on your system" 1>&2
6 echo " sudo $0 disable ... reverts the changes and restores the system back to normal" 1>&2
7 echo " $0 status ... prints current mode" 1>&2
8 exit 1
9fi
10
b8ce8438
A
11LIBSYSTEM_B="/usr/lib/libSystem.B.dylib"
12LIBSYSTEM_B_BACKUP="${LIBSYSTEM_B}-asan-mode-backup"
13LIBSYSTEM_B_ASAN="/usr/lib/libSystem.B_asan.dylib"
c06d156a 14
b8ce8438
A
15# If this file is present on the system and we are using a development build
16# then the usual memory resource limits won't be applied.
17# rdar://problem/43520116
18DISABLE_MEM_LIMIT_COOKIE_FILE="/usr/local/share/launchd-disable-memory-limits"
19DISABLE_MEM_LIMIT_COOKIE_CONTENTS="kEnableAsanDisableMemLimit"
c06d156a 20
b8ce8438
A
21function is_mac_os() {
22 if [ $(sw_vers -productName | grep -c '^Mac OS') -ne 0 ]; then
23 # Mac OS.
24 return 0
25 else
26 # Something else.
27 return 1
28 fi
29}
c06d156a 30
b8ce8438
A
31function show_have_asan_mode_on_msg() {
32 echo "Looks like your system already has ASan mode enabled, or you have a custom ${LIBSYSTEM_B} file. Not activating." 1>&2
33}
c06d156a 34
b8ce8438
A
35if ! is_mac_os; then
36 # Check assumption that ${LIBSYSTEM_B} is in the dyld shared cache.
37 if [ "$(dyld_shared_cache_util -list | grep -c "^${LIBSYSTEM_B}$")" -eq 0 ]; then
38 echo "Error: Non macOS platform detected but ${LIBSYSTEM_B} is not in your dyld shared cache." 1>&2
39 exit 1
40 fi
c06d156a 41fi
b8ce8438
A
42
43case "$1" in
44 enable)
45 if [[ $(id -u) != 0 ]]; then echo "Must be run as root." 1>&2; exit 1; fi
46
47 if is_mac_os; then
48 if [ -f ${LIBSYSTEM_B_BACKUP} ]; then
49 if [ "`md5 -q ${LIBSYSTEM_B_BACKUP}`" != "`md5 -q ${LIBSYSTEM_B}`" ]; then
50 show_have_asan_mode_on_msg
51 exit 1
52 fi
53 fi
54 ditto ${LIBSYSTEM_B} ${LIBSYSTEM_B_BACKUP}
55 else
56 # libSystem.B.dylib does not exist on non-macos platforms because it
57 # lives in the dyld cache so we don't have a back up file. Instead
58 # the presence of `libSystem.B.dylib` indicates that we are using ASan mode.
59 if [ -f ${LIBSYSTEM_B} ]; then
60 show_have_asan_mode_on_msg
61 exit 1
62 fi
63 # Disable memory limits because ASan adds memory overhead.
64 echo "${DISABLE_MEM_LIMIT_COOKIE_CONTENTS}" > "${DISABLE_MEM_LIMIT_COOKIE_FILE}"
65 fi
66
67 ditto ${LIBSYSTEM_B_ASAN} ${LIBSYSTEM_B}
68 echo "ASan mode activated. You probably want to reboot now." 1>&2
69 exit 0
70 ;;
71 disable)
72 if [[ $(id -u) != 0 ]]; then echo "Must be run as root." 1>&2; exit 1; fi
73 if is_mac_os; then
74 ditto ${LIBSYSTEM_B_BACKUP} ${LIBSYSTEM_B}
75 else
76 if [ ! -f "${LIBSYSTEM_B}" ]; then
77 echo "ASan mode cannot be disabled if it is not already enabled" 2>&1
78 exit 1
79 fi
80 if [ "`md5 -q ${LIBSYSTEM_B_ASAN}`" != "`md5 -q ${LIBSYSTEM_B}`" ]; then
81 echo "You appear to have a custom ${LIBSYSTEM_B} file. Not deactivating." 1>&2
82 exit 1
83 fi
84 # Safe to do because we've checked this is the ASan version and the normal versions
85 # lives in the dyld shared cache.
86 rm -f ${LIBSYSTEM_B}
87 if [ ! -f "${DISABLE_MEM_LIMIT_COOKIE_FILE}" ] ; then
88 echo "Warning: Can't find \"${DISABLE_MEM_LIMIT_COOKIE_FILE}\" so skipping delete" 2>&1
89 else
90 # Check the contents of the cookie file and only delete if it matches.
91 # If it doesn't match we don't delete because we assume the user wants
92 # to keep the cookie file around.
93 if [ "$(grep -c "^${DISABLE_MEM_LIMIT_COOKIE_CONTENTS}$" "${DISABLE_MEM_LIMIT_COOKIE_FILE}")" -eq 1 ]; then
94 rm -f "${DISABLE_MEM_LIMIT_COOKIE_FILE}"
95 else
96 echo "Warning: Not removing \"${DISABLE_MEM_LIMIT_COOKIE_FILE}\" because it has been modified" 2>&1
97 fi
98 fi
99 fi
100 echo "ASan mode deactivated. You probably want to reboot now." 1>&2
101 exit 0
102 ;;
103 status)
104 if is_mac_os; then
105 if [ ! -f ${LIBSYSTEM_B_BACKUP} ]; then
106 echo "ASan mode is disabled." 1>&2
107 exit 0
108 fi
109
110 if [ "`md5 -q ${LIBSYSTEM_B_BACKUP}`" == "`md5 -q ${LIBSYSTEM_B}`" ]; then
111 echo "ASan mode is disabled." 1>&2
112 exit 0
113 fi
114 else
115 if [ ! -f ${LIBSYSTEM_B} ]; then
116 echo "ASan mode is disabled." 1>&2
117 exit 0
118 fi
119 fi
120
121 if [ "`md5 -q ${LIBSYSTEM_B_ASAN}`" == "`md5 -q ${LIBSYSTEM_B}`" ]; then
122 echo "ASan mode is enabled." 1>&2
123 exit 0
124 fi
125
126 echo "Cannot tell whether ASan mode is enabled or not. You seem to have a custom ${LIBSYSTEM_B} file." 1>&2
127 exit 1
128 ;;
129 *)
130 echo "Invalid argument. Run '$0' for usage instructions." 1>&2
131 exit 1
132 ;;
133esac