Alexey Samsonov | 3b2f9f4 | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_mman.cc ------------------------------------------------------===// |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 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 a part of ThreadSanitizer (TSan), a race detector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Alexey Samsonov | 91e1a7e | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 13 | #include "sanitizer_common/sanitizer_common.h" |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 14 | #include "sanitizer_common/sanitizer_placement_new.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 15 | #include "tsan_mman.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 16 | #include "tsan_rtl.h" |
| 17 | #include "tsan_report.h" |
| 18 | #include "tsan_flags.h" |
| 19 | |
| 20 | namespace __tsan { |
| 21 | |
Dmitry Vyukov | 191f2f7 | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 22 | static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64); |
| 23 | Allocator *allocator() { |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 24 | return reinterpret_cast<Allocator*>(&allocator_placeholder); |
| 25 | } |
| 26 | |
| 27 | void InitializeAllocator() { |
| 28 | allocator()->Init(); |
| 29 | } |
| 30 | |
| 31 | void AlloctorThreadFinish(ThreadState *thr) { |
| 32 | allocator()->SwallowCache(&thr->alloc_cache); |
| 33 | } |
| 34 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 35 | static void SignalUnsafeCall(ThreadState *thr, uptr pc) { |
| 36 | if (!thr->in_signal_handler || !flags()->report_signal_unsafe) |
| 37 | return; |
| 38 | StackTrace stack; |
| 39 | stack.ObtainCurrent(thr, pc); |
| 40 | ScopedReport rep(ReportTypeSignalUnsafe); |
| 41 | rep.AddStack(&stack); |
Dmitry Vyukov | 665ce2a | 2012-05-14 15:28:03 +0000 | [diff] [blame] | 42 | OutputReport(rep, rep.GetReport()->stacks[0]); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 45 | void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align) { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 46 | CHECK_GT(thr->in_rtl, 0); |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 47 | void *p = allocator()->Allocate(&thr->alloc_cache, sz, align); |
| 48 | if (p == 0) |
Dmitry Vyukov | f64046c | 2012-05-18 09:41:52 +0000 | [diff] [blame] | 49 | return 0; |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 50 | MBlock *b = (MBlock*)allocator()->GetMetaData(p); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 51 | b->size = sz; |
Dmitry Vyukov | 191f2f7 | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 52 | b->alloc_tid = thr->unique_id; |
Dmitry Vyukov | 318f777 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 53 | b->alloc_stack_id = CurrentStackId(thr, pc); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 54 | if (CTX() && CTX()->initialized) { |
Dmitry Vyukov | 1c0b3c6 | 2012-08-15 17:27:20 +0000 | [diff] [blame] | 55 | MemoryRangeImitateWrite(thr, pc, (uptr)p, sz); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 56 | } |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 57 | DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 58 | SignalUnsafeCall(thr, pc); |
| 59 | return p; |
| 60 | } |
| 61 | |
| 62 | void user_free(ThreadState *thr, uptr pc, void *p) { |
| 63 | CHECK_GT(thr->in_rtl, 0); |
| 64 | CHECK_NE(p, (void*)0); |
| 65 | DPrintf("#%d: free(%p)\n", thr->tid, p); |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 66 | MBlock *b = (MBlock*)allocator()->GetMetaData(p); |
Dmitry Vyukov | 1c0b3c6 | 2012-08-15 17:27:20 +0000 | [diff] [blame] | 67 | if (b->head) { |
| 68 | Lock l(&b->mtx); |
| 69 | for (SyncVar *s = b->head; s;) { |
| 70 | SyncVar *res = s; |
| 71 | s = s->next; |
| 72 | StatInc(thr, StatSyncDestroyed); |
| 73 | res->mtx.Lock(); |
| 74 | res->mtx.Unlock(); |
| 75 | DestroyAndFree(res); |
| 76 | } |
| 77 | b->head = 0; |
| 78 | } |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 79 | if (CTX() && CTX()->initialized && thr->in_rtl == 1) { |
| 80 | MemoryRangeFreed(thr, pc, (uptr)p, b->size); |
| 81 | } |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 82 | allocator()->Deallocate(&thr->alloc_cache, p); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 83 | SignalUnsafeCall(thr, pc); |
| 84 | } |
| 85 | |
| 86 | void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) { |
| 87 | CHECK_GT(thr->in_rtl, 0); |
| 88 | void *p2 = 0; |
| 89 | // FIXME: Handle "shrinking" more efficiently, |
| 90 | // it seems that some software actually does this. |
| 91 | if (sz) { |
| 92 | p2 = user_alloc(thr, pc, sz); |
Dmitry Vyukov | 9d2229f | 2012-05-21 06:46:27 +0000 | [diff] [blame] | 93 | if (p2 == 0) |
| 94 | return 0; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 95 | if (p) { |
| 96 | MBlock *b = user_mblock(thr, p); |
| 97 | internal_memcpy(p2, p, min(b->size, sz)); |
| 98 | } |
| 99 | } |
| 100 | if (p) { |
| 101 | user_free(thr, pc, p); |
| 102 | } |
| 103 | return p2; |
| 104 | } |
| 105 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 106 | MBlock *user_mblock(ThreadState *thr, void *p) { |
Dmitry Vyukov | 191f2f7 | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 107 | // CHECK_GT(thr->in_rtl, 0); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 108 | CHECK_NE(p, (void*)0); |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 109 | return (MBlock*)allocator()->GetMetaData(p); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 112 | void *internal_alloc(MBlockType typ, uptr sz) { |
| 113 | ThreadState *thr = cur_thread(); |
| 114 | CHECK_GT(thr->in_rtl, 0); |
Dmitry Vyukov | de1fd1c | 2012-06-22 11:08:55 +0000 | [diff] [blame] | 115 | if (thr->nomalloc) { |
| 116 | thr->nomalloc = 0; // CHECK calls internal_malloc(). |
| 117 | CHECK(0); |
| 118 | } |
Alexey Samsonov | 91e1a7e | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 119 | return InternalAlloc(sz); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void internal_free(void *p) { |
| 123 | ThreadState *thr = cur_thread(); |
| 124 | CHECK_GT(thr->in_rtl, 0); |
Dmitry Vyukov | de1fd1c | 2012-06-22 11:08:55 +0000 | [diff] [blame] | 125 | if (thr->nomalloc) { |
| 126 | thr->nomalloc = 0; // CHECK calls internal_malloc(). |
| 127 | CHECK(0); |
| 128 | } |
Alexey Samsonov | 91e1a7e | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 129 | InternalFree(p); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace __tsan |