diff --git a/Makefile b/Makefile index 248b33b..40f8fe9 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,19 @@ DESTDIR ?= /usr/local CPPFLAGS = -D_GNU_SOURCE -Iinclude CFLAGS = -fPIC -g -fvisibility=hidden -Wall -Wextra -Werror -LDFLAGS = -fPIC -shared -Wl,--warn-common +LDFLAGS = -fPIC -shared LIBS = ifeq (,$(shell uname | grep BSD)) # BSDs have dlopen in libc LIBS += -ldl +endif + +ifeq (,$(shell uname | grep Darwin)) + LDFLAGS += -Wl,--warn-common +endif + +ifeq (,$(shell uname | grep 'BSD\|Darwin')) # TODO: why BSD has some undefined syms in libc? LDFLAGS += -Wl,--no-allow-shlib-undefined endif @@ -25,7 +32,6 @@ ifneq (,$(COVERAGE)) endif ifeq (,$(DEBUG)) CFLAGS += -O2 - LDFLAGS += -Wl,-O2 else CFLAGS += -O0 endif diff --git a/README.md b/README.md index e34ef4e..763d90f 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,9 @@ available options are * `start` - check the `start`-th group of 32 leading elements (default 0); a value of `rand` will select random group +Note that on Darwin you need to use `DYLD_INSERT_LIBRARIES` and `DYLD_FORCE_FLAT_NAMESPACE`. +You may also need to disable System Integrity Protection. + # Applying to full distribution You can run full Linux distro under SortChecker: @@ -177,7 +180,7 @@ tested SortChecker on Ubuntu and Fedora. # Known issues * SortChecker is not fully thread-safe yet (should be easy to fix though) -* SortChecker is currently Linux/BSD-only (relies on `LD_PRELOAD`) +* SortChecker supports Linux, BSD and Darwin (relies on `LD_PRELOAD`) # Future plans diff --git a/src/sortchecker.c b/src/sortchecker.c index e6388bc..9e1b366 100644 --- a/src/sortchecker.c +++ b/src/sortchecker.c @@ -25,7 +25,10 @@ EXPORT void qsort(void *data, size_t n, size_t sz, cmp_fun_t cmp); EXPORT void qsort(void *data, size_t n, size_t sz, cmp_fun_t cmp); EXPORT int heapsort(void *data, size_t n, size_t sz, cmp_fun_t cmp); EXPORT int mergesort(void *data, size_t n, size_t sz, cmp_fun_t cmp); +#ifndef __APPLE__ +// TODO: order of qsort_r arguments is different on Darwin EXPORT void qsort_r(void *data, size_t n, size_t sz, cmp_r_fun_t cmp, void *arg); +#endif EXPORT void *dlopen(const char *filename, int flag); EXPORT int dlclose(void *handle); @@ -641,6 +644,7 @@ EXPORT int mergesort(void *data, size_t n, size_t sz, cmp_fun_t cmp) { return sort_common(data, n, sz, cmp, _real, &ctx, /*do_shuffle*/ 0); } +#ifndef __APPLE__ EXPORT void qsort_r(void *data, size_t n, size_t sz, cmp_r_fun_t cmp, void *arg) { MAYBE_INIT; GET_REAL(qsort_r); @@ -657,6 +661,7 @@ EXPORT void qsort_r(void *data, size_t n, size_t sz, cmp_r_fun_t cmp, void *arg) if (!suppress_errors_) check_uniqueness(&ctx, &c, data, n, sz); } +#endif EXPORT void *dlopen(const char *filename, int flag) { GET_REAL(dlopen); diff --git a/tests/check_full_msg.c b/tests/check_full_msg.c index c5504e2..c67115e 100644 --- a/tests/check_full_msg.c +++ b/tests/check_full_msg.c @@ -9,6 +9,8 @@ char aa[] = { 1, 2, 3 }; +// REQUIRE: proc + // Under Asan qsort and callback are intercepted // so addresses are located in libasan (not a.out) // SKIP: asan diff --git a/tests/long_runline.c b/tests/long_runline.c index 1bf0bf7..9cb4754 100644 --- a/tests/long_runline.c +++ b/tests/long_runline.c @@ -9,6 +9,8 @@ char aa[] = { 1, 2, 3 }; +// REQUIRE: proc + // CMDLINE: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 // CHECK: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 int cmp(const void *pa, const void *pb) { diff --git a/tests/sort_r_nonsym_1.c b/tests/sort_r_nonsym_1.c index 61a4441..8d7e1ec 100644 --- a/tests/sort_r_nonsym_1.c +++ b/tests/sort_r_nonsym_1.c @@ -11,7 +11,7 @@ char aa[] = { 1, 2, 3 }; // NetBSD misses qsort_r: https://gnats.netbsd.org/58931 -// SKIP: netbsd +// SKIP: netbsd, mac // CHECK: comparison function is not symmetric int cmp(const void *pa, const void *pb, void *a) { @@ -21,7 +21,11 @@ int cmp(const void *pa, const void *pb, void *a) { int main() { int x = 0; +#ifdef __APPLE__ + qsort_r(aa, sizeof(aa), 1, &x, cmp); +#else qsort_r(aa, sizeof(aa), 1, cmp, &x); +#endif return 0; } diff --git a/tests/test.sh b/tests/test.sh index f64a0af..4e2633c 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -46,6 +46,12 @@ ASAN_OPTIONS=verify_asan_link_order=0:$ASAN_OPTIONS CC=${CC:-gcc} +CFLAGS= +if uname | grep -q Darwin; then + # DYLD_FORCE_FLAT_NAMESPACE is not enough (due to System Integrity Protection?) + CFLAGS="$CFLAGS -flat_namespace" +fi + if readelf -sDW bin/libsortcheck.so | grep -q __asan_init; then if $CC --version | grep -q clang; then ASAN_RT_LIB=libclang_rt.asan-x86_64.so @@ -68,11 +74,17 @@ has_feature() { test -n "$ASAN_PRELOAD" ;; bsd) - uname | grep -q BSD + uname | grep -q 'BSD\|Darwin' + ;; + mac) + uname | grep -q Darwin ;; netbsd) uname | grep -q NetBSD ;; + proc) + test -d /proc && test -r /proc + ;; syslog) test -r /var/log/syslog || which journalctl > /dev/null ;; @@ -88,7 +100,7 @@ for t in tests/*.c; do if has_option $t REQUIRE; then SKIP= - for d in $(get_option $t REQUIRE | sed -e 's/, *//g'); do + for d in $(get_option $t REQUIRE | sed -e 's/, */ /g'); do if ! has_feature $d; then SKIP=1 break @@ -102,7 +114,7 @@ for t in tests/*.c; do if has_option $t SKIP; then SKIP= - for d in $(get_option $t SKIP | sed -e 's/, *//g'); do + for d in $(get_option $t SKIP | sed -e 's/, */ /g'); do if has_feature $d; then SKIP=1 break @@ -115,7 +127,7 @@ for t in tests/*.c; do fi rm -f bin/a.out - if ! $CC $t -Itest $(get_option $t CFLAGS) -o bin/a.out; then + if ! $CC $t -Itest $CFLAGS $(get_option $t CFLAGS) -o bin/a.out; then error "$t: compilation failed" failed=1 fi @@ -134,7 +146,19 @@ for t in tests/*.c; do get_syslog > bin/syslog.bak - if ! LD_PRELOAD=${LD_PRELOAD:+$LD_PRELOAD:}${ASAN_PRELOAD:+$ASAN_PRELOAD:}bin/libsortcheck.so $VALGRIND bin/a.out $ARGS 2>bin/a.out.log; then + set +e + ( + export LD_PRELOAD=${LD_PRELOAD:+$LD_PRELOAD:}${ASAN_PRELOAD:+$ASAN_PRELOAD:}bin/libsortcheck.so + export DYLD_INSERT_LIBRARIES=${DYLD_INSERT_LIBRARIES:+$DYLD_INSERT_LIBRARIES:}${ASAN_PRELOAD:+$ASAN_PRELOAD:}bin/libsortcheck.so + export DYLD_FORCE_FLAT_NAMESPACE=1 + $VALGRIND bin/a.out $ARGS 2>bin/a.out.log + ) + rc=$? + set -e + + cat bin/a.out.log + + if test $rc != 0; then error "$t: test exited with a non-zero exit code" failed=1 elif ! has_option $t CHECK && ! has_option $t CHECK-NOT && test -s bin/a.out.log; then