Skip to content

Commit 19c4d66

Browse files
committed
[ASan] Use dynamic shadow on 32-bit iOS and simulators
The VM layout on iOS is not stable between releases. On 64-bit iOS and its derivatives we use a dynamic shadow offset that enables ASan to search for a valid location for the shadow heap on process launch rather than hardcode it. This commit extends that approach for 32-bit iOS plus derivatives and their simulators. rdar://50645192 rdar://51200372 rdar://51767702 Reviewed By: delcypher Differential Revision: https://reviews.llvm.org/D63586 llvm-svn: 364105
1 parent 67d1f8a commit 19c4d66

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

compiler-rt/lib/asan/asan_mapping.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ static const u64 kDefaultShadowOffset32 = 1ULL << 29; // 0x20000000
160160
static const u64 kDefaultShadowOffset64 = 1ULL << 44;
161161
static const u64 kDefaultShort64bitShadowOffset =
162162
0x7FFFFFFF & (~0xFFFULL << kDefaultShadowScale); // < 2G.
163-
static const u64 kIosShadowOffset32 = 1ULL << 30; // 0x40000000
164-
static const u64 kIosShadowOffset64 = 0x120200000;
165-
static const u64 kIosSimShadowOffset32 = 1ULL << 30;
166-
static const u64 kIosSimShadowOffset64 = kDefaultShadowOffset64;
167163
static const u64 kAArch64_ShadowOffset64 = 1ULL << 36;
168164
static const u64 kMIPS32_ShadowOffset32 = 0x0aaa0000;
169165
static const u64 kMIPS64_ShadowOffset64 = 1ULL << 37;
@@ -201,23 +197,15 @@ static const u64 kMyriadCacheBitMask32 = 0x40000000ULL;
201197
# elif SANITIZER_WINDOWS
202198
# define SHADOW_OFFSET kWindowsShadowOffset32
203199
# elif SANITIZER_IOS
204-
# if SANITIZER_IOSSIM
205-
# define SHADOW_OFFSET kIosSimShadowOffset32
206-
# else
207-
# define SHADOW_OFFSET kIosShadowOffset32
208-
# endif
200+
# define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
209201
# elif SANITIZER_MYRIAD2
210202
# define SHADOW_OFFSET kMyriadShadowOffset32
211203
# else
212204
# define SHADOW_OFFSET kDefaultShadowOffset32
213205
# endif
214206
#else
215207
# if SANITIZER_IOS
216-
# if SANITIZER_IOSSIM
217-
# define SHADOW_OFFSET kIosSimShadowOffset64
218-
# else
219-
# define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
220-
# endif
208+
# define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
221209
# elif defined(__aarch64__)
222210
# define SHADOW_OFFSET kAArch64_ShadowOffset64
223211
# elif defined(__powerpc64__)

compiler-rt/lib/sanitizer_common/sanitizer_mac.cc

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ char **GetArgv() {
912912
return *_NSGetArgv();
913913
}
914914

915-
#if defined(__aarch64__) && SANITIZER_IOS && !SANITIZER_IOSSIM
915+
#if SANITIZER_IOS
916916
// The task_vm_info struct is normally provided by the macOS SDK, but we need
917917
// fields only available in 10.12+. Declare the struct manually to be able to
918918
// build against older SDKs.
@@ -943,33 +943,37 @@ struct __sanitizer_task_vm_info {
943943
#define __SANITIZER_TASK_VM_INFO_COUNT ((mach_msg_type_number_t) \
944944
(sizeof(__sanitizer_task_vm_info) / sizeof(natural_t)))
945945

946-
uptr GetTaskInfoMaxAddress() {
946+
static uptr GetTaskInfoMaxAddress() {
947947
__sanitizer_task_vm_info vm_info = {} /* zero initialize */;
948948
mach_msg_type_number_t count = __SANITIZER_TASK_VM_INFO_COUNT;
949949
int err = task_info(mach_task_self(), TASK_VM_INFO, (int *)&vm_info, &count);
950-
if (err == 0 && vm_info.max_address != 0) {
951-
return vm_info.max_address - 1;
952-
} else {
953-
// xnu cannot provide vm address limit
954-
return 0x200000000 - 1;
955-
}
950+
return err ? 0 : vm_info.max_address;
956951
}
957-
#endif
958952

959953
uptr GetMaxUserVirtualAddress() {
960-
#if SANITIZER_WORDSIZE == 64
961-
# if defined(__aarch64__) && SANITIZER_IOS && !SANITIZER_IOSSIM
962-
// Get the maximum VM address
963954
static uptr max_vm = GetTaskInfoMaxAddress();
964-
CHECK(max_vm);
965-
return max_vm;
955+
if (max_vm != 0)
956+
return max_vm - 1;
957+
958+
// xnu cannot provide vm address limit
959+
# if SANITIZER_WORDSIZE == 32
960+
return 0xffe00000 - 1;
966961
# else
967-
return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
962+
return 0x200000000 - 1;
968963
# endif
969-
#else // SANITIZER_WORDSIZE == 32
964+
}
965+
966+
#else // !SANITIZER_IOS
967+
968+
uptr GetMaxUserVirtualAddress() {
969+
# if SANITIZER_WORDSIZE == 64
970+
return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
971+
# else // SANITIZER_WORDSIZE == 32
972+
static_assert(SANITIZER_WORDSIZE == 32, "Wrong wordsize");
970973
return (1ULL << 32) - 1; // 0xffffffff;
971-
#endif // SANITIZER_WORDSIZE
974+
# endif
972975
}
976+
#endif
973977

974978
uptr GetMaxVirtualAddress() {
975979
return GetMaxUserVirtualAddress();

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
9494
static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
9595
static const uint64_t kDynamicShadowSentinel =
9696
std::numeric_limits<uint64_t>::max();
97-
static const uint64_t kIOSShadowOffset32 = 1ULL << 30;
98-
static const uint64_t kIOSSimShadowOffset32 = 1ULL << 30;
99-
static const uint64_t kIOSSimShadowOffset64 = kDefaultShadowOffset64;
10097
static const uint64_t kSmallX86_64ShadowOffsetBase = 0x7FFFFFFF; // < 2G.
10198
static const uint64_t kSmallX86_64ShadowOffsetAlignMask = ~0xFFFULL;
10299
static const uint64_t kLinuxKasan_ShadowOffset64 = 0xdffffc0000000000;
@@ -428,7 +425,6 @@ static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
428425
bool IsPPC64 = TargetTriple.getArch() == Triple::ppc64 ||
429426
TargetTriple.getArch() == Triple::ppc64le;
430427
bool IsSystemZ = TargetTriple.getArch() == Triple::systemz;
431-
bool IsX86 = TargetTriple.getArch() == Triple::x86;
432428
bool IsX86_64 = TargetTriple.getArch() == Triple::x86_64;
433429
bool IsMIPS32 = TargetTriple.isMIPS32();
434430
bool IsMIPS64 = TargetTriple.isMIPS64();
@@ -455,8 +451,7 @@ static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
455451
else if (IsNetBSD)
456452
Mapping.Offset = kNetBSD_ShadowOffset32;
457453
else if (IsIOS)
458-
// If we're targeting iOS and x86, the binary is built for iOS simulator.
459-
Mapping.Offset = IsX86 ? kIOSSimShadowOffset32 : kIOSShadowOffset32;
454+
Mapping.Offset = kDynamicShadowSentinel;
460455
else if (IsWindows)
461456
Mapping.Offset = kWindowsShadowOffset32;
462457
else if (IsMyriad) {
@@ -495,10 +490,7 @@ static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
495490
} else if (IsMIPS64)
496491
Mapping.Offset = kMIPS64_ShadowOffset64;
497492
else if (IsIOS)
498-
// If we're targeting iOS and x86, the binary is built for iOS simulator.
499-
// We are using dynamic shadow offset on the 64-bit devices.
500-
Mapping.Offset =
501-
IsX86_64 ? kIOSSimShadowOffset64 : kDynamicShadowSentinel;
493+
Mapping.Offset = kDynamicShadowSentinel;
502494
else if (IsAArch64)
503495
Mapping.Offset = kAArch64_ShadowOffset64;
504496
else
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; Test using dynamic shadow address on darwin
2+
;
3+
; RUN: opt -asan -asan-module -mtriple=arm64_32-apple-watchos --data-layout="e-m:o-p:32:32-i64:64-i128:128-n32:64-S128" -S < %s | FileCheck %s --check-prefixes=CHECK,CHECK-DYNAMIC -DPTR_SIZE=32
4+
; RUN: opt -asan -asan-module -mtriple=armv7k-apple-watchos --data-layout="e-m:o-p:32:32-Fi8-i64:64-a:0:32-n32-S128" -S < %s | FileCheck %s --check-prefixes=CHECK,CHECK-DYNAMIC -DPTR_SIZE=32
5+
; RUN: opt -asan -asan-module -mtriple=arm64-apple-ios --data-layout="e-m:o-i64:64-i128:128-n32:64-S128" -S < %s | FileCheck %s --check-prefixes=CHECK,CHECK-DYNAMIC -DPTR_SIZE=64
6+
; RUN: opt -asan -asan-module -mtriple=armv7s-apple-ios --data-layout="e-m:o-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32" -S < %s | FileCheck %s --check-prefixes=CHECK,CHECK-DYNAMIC -DPTR_SIZE=32
7+
; RUN: opt -asan -asan-module -mtriple=i386-apple-watchos-simulator --data-layout="e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128" -S < %s | FileCheck %s --check-prefixes=CHECK,CHECK-DYNAMIC -DPTR_SIZE=32
8+
; RUN: opt -asan -asan-module -mtriple=i386-apple-ios-simulator --data-layout="e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128" -S < %s | FileCheck %s --check-prefixes=CHECK,CHECK-DYNAMIC -DPTR_SIZE=32
9+
; RUN: opt -asan -asan-module -mtriple=x86_64-apple-ios-simulator --data-layout="e-m:o-i64:64-f80:128-n8:16:32:64-S128" -S < %s | FileCheck %s --check-prefixes=CHECK,CHECK-DYNAMIC -DPTR_SIZE=64
10+
;
11+
; // macOS does not use dynamic shadow placement
12+
; RUN: opt -asan -asan-module -mtriple=x86_64-apple-macosx --data-layout="e-m:o-i64:64-f80:128-n8:16:32:64-S128" -S < %s | FileCheck %s --check-prefixes=CHECK,CHECK-NONDYNAMIC -DPTR_SIZE=64
13+
14+
define i32 @test_load(i32* %a) sanitize_address {
15+
; First instrumentation in the function must be to load the dynamic shadow
16+
; address into a local variable.
17+
; CHECK-LABEL: @test_load
18+
; CHECK: entry:
19+
; CHECK-DYNAMIC-NEXT: %[[SHADOW:[^ ]*]] = load i[[PTR_SIZE]], i[[PTR_SIZE]]* @__asan_shadow_memory_dynamic_address
20+
; CHECK-NONDYNAMIC-NOT: __asan_shadow_memory_dynamic_address
21+
22+
; Shadow address is loaded and added into the whole offset computation.
23+
; CHECK-DYNAMIC: add i[[PTR_SIZE]] %{{.*}}, %[[SHADOW]]
24+
25+
entry:
26+
%tmp1 = load i32, i32* %a, align 4
27+
ret i32 %tmp1
28+
}

0 commit comments

Comments
 (0)