Skip to content

Commit 470f063

Browse files
committed
tsan: make all memory mappings testable
Currently we define/compile the mapping for a platform only on that platform. This makes it impossible to unit-test them on a single platform, and even to build test. We have 17 of them and the Go mappings will be tested only after a manual episodic update of the Go runtime. Define all mappings always with unique names. This will allow to unit-test them. No functional changes. Reviewed By: melver Differential Revision: https://reviews.llvm.org/D107734
1 parent 2f6ac22 commit 470f063

File tree

1 file changed

+85
-81
lines changed

1 file changed

+85
-81
lines changed

compiler-rt/lib/tsan/rtl/tsan_platform.h

Lines changed: 85 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@
2323

2424
namespace __tsan {
2525

26-
#if defined(__x86_64__)
27-
#define HAS_48_BIT_ADDRESS_SPACE 1
28-
#elif SANITIZER_IOSSIM // arm64 iOS simulators (order of #if matters)
29-
#define HAS_48_BIT_ADDRESS_SPACE 1
30-
#elif SANITIZER_IOS // arm64 iOS devices (order of #if matters)
31-
#define HAS_48_BIT_ADDRESS_SPACE 0
32-
#elif SANITIZER_MAC // arm64 macOS (order of #if matters)
33-
#define HAS_48_BIT_ADDRESS_SPACE 1
34-
#else
35-
#define HAS_48_BIT_ADDRESS_SPACE 0
36-
#endif
37-
38-
#if !SANITIZER_GO
39-
40-
#if HAS_48_BIT_ADDRESS_SPACE
4126
/*
4227
C/C++ on linux/x86_64 and freebsd/x86_64
4328
0000 0000 1000 - 0080 0000 0000: main binary and/or MAP_32BIT mappings (512GB)
@@ -65,9 +50,8 @@ C/C++ on netbsd/amd64 can reuse the same mapping:
6550
* Stack on NetBSD/amd64 has prereserved 128MB.
6651
* Heap grows downwards (top-down).
6752
* ASLR must be disabled per-process or globally.
68-
6953
*/
70-
struct Mapping {
54+
struct Mapping48AddressSpace {
7155
static const uptr kMetaShadowBeg = 0x300000000000ull;
7256
static const uptr kMetaShadowEnd = 0x340000000000ull;
7357
static const uptr kTraceMemBeg = 0x600000000000ull;
@@ -87,8 +71,6 @@ struct Mapping {
8771
static const uptr kVdsoBeg = 0xf000000000000000ull;
8872
};
8973

90-
#define TSAN_MID_APP_RANGE 1
91-
#elif defined(__mips64)
9274
/*
9375
C/C++ on linux/mips64 (40-bit VMA)
9476
0000 0000 00 - 0100 0000 00: - (4 GB)
@@ -105,7 +87,7 @@ fe00 0000 00 - ff00 0000 00: heap (4 GB)
10587
ff00 0000 00 - ff80 0000 00: - (2 GB)
10688
ff80 0000 00 - ffff ffff ff: modules and main thread stack (<2 GB)
10789
*/
108-
struct Mapping40 {
90+
struct MappingMips64_40 {
10991
static const uptr kMetaShadowBeg = 0x4000000000ull;
11092
static const uptr kMetaShadowEnd = 0x5000000000ull;
11193
static const uptr kTraceMemBeg = 0xb000000000ull;
@@ -125,9 +107,6 @@ struct Mapping40 {
125107
static const uptr kVdsoBeg = 0xfffff00000ull;
126108
};
127109

128-
#define TSAN_MID_APP_RANGE 1
129-
#define TSAN_RUNTIME_VMA 1
130-
#elif defined(__aarch64__) && defined(__APPLE__)
131110
/*
132111
C/C++ on Darwin/iOS/ARM64 (36-bit VMA, 64 GB VM)
133112
0000 0000 00 - 0100 0000 00: - (4 GB)
@@ -141,7 +120,7 @@ C/C++ on Darwin/iOS/ARM64 (36-bit VMA, 64 GB VM)
141120
0f00 0000 00 - 0fc0 0000 00: traces (3 GB)
142121
0fc0 0000 00 - 1000 0000 00: -
143122
*/
144-
struct Mapping {
123+
struct MappingAppleAarch64 {
145124
static const uptr kLoAppMemBeg = 0x0100000000ull;
146125
static const uptr kLoAppMemEnd = 0x0200000000ull;
147126
static const uptr kHeapMemBeg = 0x0200000000ull;
@@ -159,13 +138,6 @@ struct Mapping {
159138
static const uptr kVdsoBeg = 0x7000000000000000ull;
160139
};
161140

162-
#elif defined(__aarch64__) && !defined(__APPLE__)
163-
// AArch64 supports multiple VMA which leads to multiple address transformation
164-
// functions. To support these multiple VMAS transformations and mappings TSAN
165-
// runtime for AArch64 uses an external memory read (vmaSize) to select which
166-
// mapping to use. Although slower, it make a same instrumented binary run on
167-
// multiple kernels.
168-
169141
/*
170142
C/C++ on linux/aarch64 (39-bit VMA)
171143
0000 0010 00 - 0100 0000 00: main binary
@@ -181,7 +153,7 @@ C/C++ on linux/aarch64 (39-bit VMA)
181153
7c00 0000 00 - 7d00 0000 00: heap
182154
7d00 0000 00 - 7fff ffff ff: modules and main thread stack
183155
*/
184-
struct Mapping39 {
156+
struct MappingAarch64_39 {
185157
static const uptr kLoAppMemBeg = 0x0000001000ull;
186158
static const uptr kLoAppMemEnd = 0x0100000000ull;
187159
static const uptr kShadowBeg = 0x0800000000ull;
@@ -216,7 +188,7 @@ C/C++ on linux/aarch64 (42-bit VMA)
216188
3e000 0000 00 - 3f000 0000 00: heap
217189
3f000 0000 00 - 3ffff ffff ff: modules and main thread stack
218190
*/
219-
struct Mapping42 {
191+
struct MappingAarch64_42 {
220192
static const uptr kLoAppMemBeg = 0x00000001000ull;
221193
static const uptr kLoAppMemEnd = 0x01000000000ull;
222194
static const uptr kShadowBeg = 0x10000000000ull;
@@ -236,7 +208,7 @@ struct Mapping42 {
236208
static const uptr kVdsoBeg = 0x37f00000000ull;
237209
};
238210

239-
struct Mapping48 {
211+
struct MappingAarch64_48 {
240212
static const uptr kLoAppMemBeg = 0x0000000001000ull;
241213
static const uptr kLoAppMemEnd = 0x0000200000000ull;
242214
static const uptr kShadowBeg = 0x0002000000000ull;
@@ -256,17 +228,6 @@ struct Mapping48 {
256228
static const uptr kVdsoBeg = 0xffff000000000ull;
257229
};
258230

259-
// Indicates the runtime will define the memory regions at runtime.
260-
#define TSAN_RUNTIME_VMA 1
261-
// Indicates that mapping defines a mid range memory segment.
262-
#define TSAN_MID_APP_RANGE 1
263-
#elif defined(__powerpc64__)
264-
// PPC64 supports multiple VMA which leads to multiple address transformation
265-
// functions. To support these multiple VMAS transformations and mappings TSAN
266-
// runtime for PPC64 uses an external memory read (vmaSize) to select which
267-
// mapping to use. Although slower, it make a same instrumented binary run on
268-
// multiple kernels.
269-
270231
/*
271232
C/C++ on linux/powerpc64 (44-bit VMA)
272233
0000 0000 0100 - 0001 0000 0000: main binary
@@ -281,7 +242,7 @@ C/C++ on linux/powerpc64 (44-bit VMA)
281242
0f50 0000 0000 - 0f60 0000 0000: -
282243
0f60 0000 0000 - 1000 0000 0000: modules and main thread stack
283244
*/
284-
struct Mapping44 {
245+
struct MappingPPC64_44 {
285246
static const uptr kMetaShadowBeg = 0x0b0000000000ull;
286247
static const uptr kMetaShadowEnd = 0x0d0000000000ull;
287248
static const uptr kTraceMemBeg = 0x0d0000000000ull;
@@ -313,7 +274,7 @@ C/C++ on linux/powerpc64 (46-bit VMA)
313274
3e00 0000 0000 - 3e80 0000 0000: -
314275
3e80 0000 0000 - 4000 0000 0000: modules and main thread stack
315276
*/
316-
struct Mapping46 {
277+
struct MappingPPC64_46 {
317278
static const uptr kMetaShadowBeg = 0x100000000000ull;
318279
static const uptr kMetaShadowEnd = 0x200000000000ull;
319280
static const uptr kTraceMemBeg = 0x200000000000ull;
@@ -345,7 +306,7 @@ C/C++ on linux/powerpc64 (47-bit VMA)
345306
7e00 0000 0000 - 7e80 0000 0000: -
346307
7e80 0000 0000 - 8000 0000 0000: modules and main thread stack
347308
*/
348-
struct Mapping47 {
309+
struct MappingPPC64_47 {
349310
static const uptr kMetaShadowBeg = 0x100000000000ull;
350311
static const uptr kMetaShadowEnd = 0x200000000000ull;
351312
static const uptr kTraceMemBeg = 0x200000000000ull;
@@ -363,9 +324,6 @@ struct Mapping47 {
363324
static const uptr kVdsoBeg = 0x7800000000000000ull;
364325
};
365326

366-
// Indicates the runtime will define the memory regions at runtime.
367-
#define TSAN_RUNTIME_VMA 1
368-
#elif defined(__s390x__)
369327
/*
370328
C/C++ on linux/s390x
371329
While the kernel provides a 64-bit address space, we have to restrict ourselves
@@ -380,7 +338,7 @@ a000 0000 0000 - b000 0000 0000: traces - 16TiB (max history * 128k threads)
380338
b000 0000 0000 - be00 0000 0000: -
381339
be00 0000 0000 - c000 0000 0000: heap - 2TiB (max supported by the allocator)
382340
*/
383-
struct Mapping {
341+
struct MappingS390x {
384342
static const uptr kMetaShadowBeg = 0x900000000000ull;
385343
static const uptr kMetaShadowEnd = 0x980000000000ull;
386344
static const uptr kTraceMemBeg = 0xa00000000000ull;
@@ -397,9 +355,6 @@ struct Mapping {
397355
static const uptr kAppMemXor = 0x100000000000ull;
398356
static const uptr kVdsoBeg = 0xfffffffff000ull;
399357
};
400-
#endif
401-
402-
#elif SANITIZER_GO && !SANITIZER_WINDOWS && HAS_48_BIT_ADDRESS_SPACE
403358

404359
/* Go on linux, darwin and freebsd on x86_64
405360
0000 0000 1000 - 0000 1000 0000: executable
@@ -414,7 +369,7 @@ struct Mapping {
414369
6200 0000 0000 - 8000 0000 0000: -
415370
*/
416371

417-
struct Mapping {
372+
struct MappingGo48 {
418373
static const uptr kMetaShadowBeg = 0x300000000000ull;
419374
static const uptr kMetaShadowEnd = 0x400000000000ull;
420375
static const uptr kTraceMemBeg = 0x600000000000ull;
@@ -425,8 +380,6 @@ struct Mapping {
425380
static const uptr kAppMemEnd = 0x00e000000000ull;
426381
};
427382

428-
#elif SANITIZER_GO && SANITIZER_WINDOWS
429-
430383
/* Go on windows
431384
0000 0000 1000 - 0000 1000 0000: executable
432385
0000 1000 0000 - 00f8 0000 0000: -
@@ -439,7 +392,7 @@ struct Mapping {
439392
07d0 0000 0000 - 8000 0000 0000: -
440393
*/
441394

442-
struct Mapping {
395+
struct MappingGoWindows {
443396
static const uptr kMetaShadowBeg = 0x076000000000ull;
444397
static const uptr kMetaShadowEnd = 0x07d000000000ull;
445398
static const uptr kTraceMemBeg = 0x056000000000ull;
@@ -450,10 +403,6 @@ struct Mapping {
450403
static const uptr kAppMemEnd = 0x00e000000000ull;
451404
};
452405

453-
#elif SANITIZER_GO && defined(__powerpc64__)
454-
455-
/* Only Mapping46 and Mapping47 are currently supported for powercp64 on Go. */
456-
457406
/* Go on linux/powerpc64 (46-bit VMA)
458407
0000 0000 1000 - 0000 1000 0000: executable
459408
0000 1000 0000 - 00c0 0000 0000: -
@@ -467,7 +416,7 @@ struct Mapping {
467416
3800 0000 0000 - 4000 0000 0000: -
468417
*/
469418

470-
struct Mapping46 {
419+
struct MappingGoPPC64_46 {
471420
static const uptr kMetaShadowBeg = 0x240000000000ull;
472421
static const uptr kMetaShadowEnd = 0x340000000000ull;
473422
static const uptr kTraceMemBeg = 0x360000000000ull;
@@ -491,7 +440,7 @@ struct Mapping46 {
491440
6200 0000 0000 - 8000 0000 0000: -
492441
*/
493442

494-
struct Mapping47 {
443+
struct MappingGoPPC64_47 {
495444
static const uptr kMetaShadowBeg = 0x300000000000ull;
496445
static const uptr kMetaShadowEnd = 0x400000000000ull;
497446
static const uptr kTraceMemBeg = 0x600000000000ull;
@@ -502,10 +451,6 @@ struct Mapping47 {
502451
static const uptr kAppMemEnd = 0x00e000000000ull;
503452
};
504453

505-
#define TSAN_RUNTIME_VMA 1
506-
507-
#elif SANITIZER_GO && defined(__aarch64__)
508-
509454
/* Go on linux/aarch64 (48-bit VMA) and darwin/aarch64 (47-bit VMA)
510455
0000 0000 1000 - 0000 1000 0000: executable
511456
0000 1000 0000 - 00c0 0000 0000: -
@@ -518,8 +463,7 @@ struct Mapping47 {
518463
6000 0000 0000 - 6200 0000 0000: traces
519464
6200 0000 0000 - 8000 0000 0000: -
520465
*/
521-
522-
struct Mapping {
466+
struct MappingGoAarch64 {
523467
static const uptr kMetaShadowBeg = 0x300000000000ull;
524468
static const uptr kMetaShadowEnd = 0x400000000000ull;
525469
static const uptr kTraceMemBeg = 0x600000000000ull;
@@ -530,10 +474,6 @@ struct Mapping {
530474
static const uptr kAppMemEnd = 0x00e000000000ull;
531475
};
532476

533-
// Indicates the runtime will define the memory regions at runtime.
534-
#define TSAN_RUNTIME_VMA 1
535-
536-
#elif SANITIZER_GO && defined(__mips64)
537477
/*
538478
Go on linux/mips64 (47-bit VMA)
539479
0000 0000 1000 - 0000 1000 0000: executable
@@ -547,7 +487,7 @@ Go on linux/mips64 (47-bit VMA)
547487
6000 0000 0000 - 6200 0000 0000: traces
548488
6200 0000 0000 - 8000 0000 0000: -
549489
*/
550-
struct Mapping47 {
490+
struct MappingGoMips64_47 {
551491
static const uptr kMetaShadowBeg = 0x300000000000ull;
552492
static const uptr kMetaShadowEnd = 0x400000000000ull;
553493
static const uptr kTraceMemBeg = 0x600000000000ull;
@@ -558,9 +498,6 @@ struct Mapping47 {
558498
static const uptr kAppMemEnd = 0x00e000000000ull;
559499
};
560500

561-
#define TSAN_RUNTIME_VMA 1
562-
563-
#elif SANITIZER_GO && defined(__s390x__)
564501
/*
565502
Go on linux/s390x
566503
0000 0000 1000 - 1000 0000 0000: executable and heap - 16 TiB
@@ -571,7 +508,7 @@ Go on linux/s390x
571508
9800 0000 0000 - a000 0000 0000: -
572509
a000 0000 0000 - b000 0000 0000: traces - 16TiB (max history * 128k threads)
573510
*/
574-
struct Mapping {
511+
struct MappingGoS390x {
575512
static const uptr kMetaShadowBeg = 0x900000000000ull;
576513
static const uptr kMetaShadowEnd = 0x980000000000ull;
577514
static const uptr kTraceMemBeg = 0xa00000000000ull;
@@ -582,10 +519,77 @@ struct Mapping {
582519
static const uptr kAppMemEnd = 0x100000000000ull;
583520
};
584521

522+
#if defined(__x86_64__)
523+
# define HAS_48_BIT_ADDRESS_SPACE 1
524+
#elif SANITIZER_IOSSIM // arm64 iOS simulators (order of #if matters)
525+
# define HAS_48_BIT_ADDRESS_SPACE 1
526+
#elif SANITIZER_IOS // arm64 iOS devices (order of #if matters)
527+
# define HAS_48_BIT_ADDRESS_SPACE 0
528+
#elif SANITIZER_MAC // arm64 macOS (order of #if matters)
529+
# define HAS_48_BIT_ADDRESS_SPACE 1
585530
#else
586-
# error "Unknown platform"
531+
# define HAS_48_BIT_ADDRESS_SPACE 0
587532
#endif
588533

534+
#if !SANITIZER_GO
535+
536+
# if HAS_48_BIT_ADDRESS_SPACE
537+
typedef Mapping48AddressSpace Mapping;
538+
# define TSAN_MID_APP_RANGE 1
539+
# elif defined(__mips64)
540+
typedef MappingMips64_40 Mapping40;
541+
# define TSAN_MID_APP_RANGE 1
542+
# define TSAN_RUNTIME_VMA 1
543+
# elif defined(__aarch64__) && defined(__APPLE__)
544+
typedef MappingAppleAarch64 Mapping;
545+
# elif defined(__aarch64__) && !defined(__APPLE__)
546+
// AArch64 supports multiple VMA which leads to multiple address transformation
547+
// functions. To support these multiple VMAS transformations and mappings TSAN
548+
// runtime for AArch64 uses an external memory read (vmaSize) to select which
549+
// mapping to use. Although slower, it make a same instrumented binary run on
550+
// multiple kernels.
551+
typedef MappingAarch64_39 Mapping39;
552+
typedef MappingAarch64_42 Mapping42;
553+
typedef MappingAarch64_48 Mapping48;
554+
// Indicates the runtime will define the memory regions at runtime.
555+
# define TSAN_RUNTIME_VMA 1
556+
// Indicates that mapping defines a mid range memory segment.
557+
# define TSAN_MID_APP_RANGE 1
558+
# elif defined(__powerpc64__)
559+
// PPC64 supports multiple VMA which leads to multiple address transformation
560+
// functions. To support these multiple VMAS transformations and mappings TSAN
561+
// runtime for PPC64 uses an external memory read (vmaSize) to select which
562+
// mapping to use. Although slower, it make a same instrumented binary run on
563+
// multiple kernels.
564+
typedef MappingPPC64_44 Mapping44;
565+
typedef MappingPPC64_46 Mapping46;
566+
typedef MappingPPC64_47 Mapping47;
567+
// Indicates the runtime will define the memory regions at runtime.
568+
# define TSAN_RUNTIME_VMA 1
569+
# elif defined(__s390x__)
570+
typedef MappingS390x Mapping;
571+
# endif
572+
#elif SANITIZER_GO && !SANITIZER_WINDOWS && HAS_48_BIT_ADDRESS_SPACE
573+
typedef MappingGo48 Mapping;
574+
#elif SANITIZER_GO && SANITIZER_WINDOWS
575+
typedef MappingGoWindows Mapping;
576+
#elif SANITIZER_GO && defined(__powerpc64__)
577+
/* Only Mapping46 and Mapping47 are currently supported for powercp64 on Go. */
578+
typedef MappingGoPPC64_46 Mapping46;
579+
typedef MappingGoPPC64_47 Mapping47;
580+
# define TSAN_RUNTIME_VMA 1
581+
#elif SANITIZER_GO && defined(__aarch64__)
582+
typedef MappingGoAarch64 Mapping;
583+
// Indicates the runtime will define the memory regions at runtime.
584+
# define TSAN_RUNTIME_VMA 1
585+
#elif SANITIZER_GO && defined(__mips64)
586+
typedef MappingGoMips64_47 Mapping47;
587+
# define TSAN_RUNTIME_VMA 1
588+
#elif SANITIZER_GO && defined(__s390x__)
589+
typedef MappingGoS390x Mapping;
590+
#else
591+
# error "Unknown platform"
592+
#endif
589593

590594
#ifdef TSAN_RUNTIME_VMA
591595
extern uptr vmaSize;

0 commit comments

Comments
 (0)