| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 1 | //===-- sanitizer_common.cc -----------------------------------------------===// |
| 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 shared between AddressSanitizer and ThreadSanitizer |
| 11 | // run-time libraries. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "sanitizer_common.h" |
| 15 | #include "sanitizer_libc.h" |
| Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 16 | |
| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 17 | namespace __sanitizer { |
| 18 | |
| 19 | void RawWrite(const char *buffer) { |
| 20 | static const char *kRawWriteError = "RawWrite can't output requested buffer!"; |
| 21 | uptr length = (uptr)internal_strlen(buffer); |
| 22 | if (length != internal_write(2, buffer, length)) { |
| 23 | internal_write(2, kRawWriteError, internal_strlen(kRawWriteError)); |
| 24 | Die(); |
| 25 | } |
| 26 | } |
| 27 | |
| Alexey Samsonov | fe44fbd | 2012-06-07 05:38:26 +0000 | [diff] [blame] | 28 | uptr ReadFileToBuffer(const char *file_name, char **buff, |
| 29 | uptr *buff_size, uptr max_len) { |
| 30 | const uptr kMinFileLen = kPageSize; |
| 31 | uptr read_len = 0; |
| 32 | *buff = 0; |
| 33 | *buff_size = 0; |
| 34 | // The files we usually open are not seekable, so try different buffer sizes. |
| 35 | for (uptr size = kMinFileLen; size <= max_len; size *= 2) { |
| 36 | fd_t fd = internal_open(file_name, /*write*/ false); |
| 37 | if (fd == kInvalidFd) return 0; |
| 38 | UnmapOrDie(*buff, *buff_size); |
| 39 | *buff = (char*)MmapOrDie(size, __FUNCTION__); |
| 40 | *buff_size = size; |
| 41 | // Read up to one page at a time. |
| 42 | read_len = 0; |
| 43 | bool reached_eof = false; |
| 44 | while (read_len + kPageSize <= size) { |
| 45 | uptr just_read = internal_read(fd, *buff + read_len, kPageSize); |
| 46 | if (just_read == 0) { |
| 47 | reached_eof = true; |
| 48 | break; |
| 49 | } |
| 50 | read_len += just_read; |
| 51 | } |
| 52 | internal_close(fd); |
| 53 | if (reached_eof) // We've read the whole file. |
| 54 | break; |
| 55 | } |
| 56 | return read_len; |
| 57 | } |
| 58 | |
| Alexey Samsonov | d77fbba | 2012-07-16 11:27:17 +0000 | [diff] [blame^] | 59 | // We don't want to use std::sort to avoid including <algorithm>, as |
| 60 | // we may end up with two implementation of std::sort - one in instrumented |
| 61 | // code, and the other in runtime. |
| 62 | // qsort() from stdlib won't work as it calls malloc(), which results |
| 63 | // in deadlock in ASan allocator. |
| 64 | // We re-implement in-place sorting w/o recursion as straightforward heapsort. |
| Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 65 | void SortArray(uptr *array, uptr size) { |
| Alexey Samsonov | d77fbba | 2012-07-16 11:27:17 +0000 | [diff] [blame^] | 66 | if (size < 2) |
| 67 | return; |
| 68 | // Stage 1: insert elements to the heap. |
| 69 | for (uptr i = 1; i < size; i++) { |
| 70 | uptr j, p; |
| 71 | for (j = i; j > 0; j = p) { |
| 72 | p = (j - 1) / 2; |
| 73 | if (array[j] > array[p]) |
| 74 | Swap(array[j], array[p]); |
| 75 | else |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | // Stage 2: swap largest element with the last one, |
| 80 | // and sink the new top. |
| 81 | for (uptr i = size - 1; i > 0; i--) { |
| 82 | Swap(array[0], array[i]); |
| 83 | uptr j, max_ind; |
| 84 | for (j = 0; j < i; j = max_ind) { |
| 85 | uptr left = 2 * j + 1; |
| 86 | uptr right = 2 * j + 2; |
| 87 | max_ind = j; |
| 88 | if (left < i && array[left] > array[max_ind]) |
| 89 | max_ind = left; |
| 90 | if (right < i && array[right] > array[max_ind]) |
| 91 | max_ind = right; |
| 92 | if (max_ind != j) |
| 93 | Swap(array[j], array[max_ind]); |
| 94 | else |
| 95 | break; |
| 96 | } |
| 97 | } |
| Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 100 | } // namespace __sanitizer |