Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: runtime/szrt_asan.c

Issue 2209563002: Subzero: removed loops from ASan access checking (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Removed incorrect assertion Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/runtime/szrt_asan.c - AddressSanitizer Runtime -----*- C -*-===// 1 //===- subzero/runtime/szrt_asan.c - AddressSanitizer Runtime -----*- C -*-===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 /// 9 ///
10 /// \file 10 /// \file
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 do { \ 51 do { \
52 printf(args); \ 52 printf(args); \
53 } while (false); 53 } while (false);
54 #else // !DEBUG 54 #else // !DEBUG
55 #define DUMP(args...) 55 #define DUMP(args...)
56 #endif // DEBUG 56 #endif // DEBUG
57 57
58 static char *shadow_offset = NULL; 58 static char *shadow_offset = NULL;
59 59
60 static void __asan_error(char *, int); 60 static void __asan_error(char *, int);
61 static void __asan_check(char *, int, bool); 61 static void __asan_check(char *, int);
62 static void __asan_get_redzones(char *, char **, char **); 62 static void __asan_get_redzones(char *, char **, char **);
63 63
64 void __asan_init(int, void **, int *); 64 void __asan_init(int, void **, int *);
65 void __asan_check_load(char *, int); 65 void __asan_check_load(char *, int);
66 void __asan_check_store(char *, int); 66 void __asan_check_store(char *, int);
67 void *__asan_malloc(size_t); 67 void *__asan_malloc(size_t);
68 void *__asan_calloc(size_t, size_t); 68 void *__asan_calloc(size_t, size_t);
69 void *__asan_realloc(char *, size_t); 69 void *__asan_realloc(char *, size_t);
70 void __asan_free(char *); 70 void __asan_free(char *);
71 void __asan_poison(char *, int); 71 void __asan_poison(char *, int);
72 void __asan_unpoison(char *, int); 72 void __asan_unpoison(char *, int);
73 73
74 static void __asan_error(char *ptr, int size) { 74 static void __asan_error(char *ptr, int size) {
75 fprintf(stderr, "Illegal access of %d bytes at %p\n", size, ptr); 75 fprintf(stderr, "Illegal access of %d bytes at %p\n", size, ptr);
76 abort(); 76 abort();
77 } 77 }
78 78
79 // check only the first byte of each word unless strict 79 // check only the first byte of each word unless strict
80 static void __asan_check(char *ptr, int size, bool strict) { 80 static void __asan_check(char *ptr, int size) {
81 assert(strict || (uintptr_t)ptr % WORD_SIZE == 0); 81 assert(size == 1 || size == 2 || size == 4 || size == 8);
82 DUMP("%s check %d bytes at %p\n", (strict) ? "strict" : "loose", size, ptr); 82 char *shadow_addr = (char *)MEM2SHADOW(ptr);
83 char *end = ptr + size; 83 DUMP("check %d bytes at %p: %p + %d (%d)\n", size, ptr, shadow_addr,
84 int step = (strict) ? 1 : WORD_SIZE; 84 (uintptr_t)ptr % SHADOW_SCALE, *shadow_addr);
85 for (char *cur = ptr; cur < end; cur += step) { 85 if (size == SHADOW_SCALE) {
86 char shadow = *(char *)MEM2SHADOW(cur); 86 if (*shadow_addr != 0)
87 DUMP("checking %p against %p with shadow %d\n", cur, MEM2SHADOW(cur),
88 shadow);
89 if (shadow != 0 && (shadow < 0 || SHADOW_OFFSET(cur) >= shadow)) {
90 __asan_error(ptr, size); 87 __asan_error(ptr, size);
91 } 88 return;
92 } 89 }
90 if (*shadow_addr != 0 && (char)SHADOW_OFFSET(ptr) + size > *shadow_addr)
91 __asan_error(ptr, size);
93 } 92 }
94 93
95 static void __asan_get_redzones(char *ptr, char **left, char **right) { 94 static void __asan_get_redzones(char *ptr, char **left, char **right) {
96 char *rz_left = ptr - RZ_SIZE; 95 char *rz_left = ptr - RZ_SIZE;
97 char *rz_right = *(char **)rz_left; 96 char *rz_right = *(char **)rz_left;
98 if (left != NULL) 97 if (left != NULL)
99 *left = rz_left; 98 *left = rz_left;
100 if (right != NULL) 99 if (right != NULL)
101 *right = rz_right; 100 *right = rz_right;
102 } 101 }
103 102
104 void __asan_check_load(char *ptr, int size) { 103 void __asan_check_load(char *ptr, int size) {
105 // aligned single word accesses may be widened single byte accesses, but for 104 // aligned single word accesses may be widened single byte accesses, but for
106 // all else use strict check 105 // all else use strict check
107 bool strict = !((uintptr_t)ptr % WORD_SIZE == 0 && size == WORD_SIZE); 106 if (size == WORD_SIZE && (uintptr_t)ptr % WORD_SIZE == 0)
108 __asan_check(ptr, size, strict); 107 size = 1;
108 __asan_check(ptr, size);
109 } 109 }
110 110
111 void __asan_check_store(char *ptr, int size) { 111 void __asan_check_store(char *ptr, int size) {
112 // stores may never be partially out of bounds so use strict check 112 // stores may never be partially out of bounds so use strict check
113 bool strict = true; 113 bool strict = true;
114 __asan_check(ptr, size, strict); 114 __asan_check(ptr, size);
115 } 115 }
116 116
117 void __asan_init(int n_rzs, void **rzs, int *rz_sizes) { 117 void __asan_init(int n_rzs, void **rzs, int *rz_sizes) {
118 // ensure the redzones are large enough to hold metadata 118 // ensure the redzones are large enough to hold metadata
119 assert(RZ_SIZE >= sizeof(void *) && RZ_SIZE >= sizeof(size_t)); 119 assert(RZ_SIZE >= sizeof(void *) && RZ_SIZE >= sizeof(size_t));
120 assert(shadow_offset == NULL); 120 assert(shadow_offset == NULL);
121 size_t length = (IS_32_BIT) ? SHADOW_LENGTH_32 : SHADOW_LENGTH_64; 121 size_t length = (IS_32_BIT) ? SHADOW_LENGTH_32 : SHADOW_LENGTH_64;
122 int prot = PROT_READ | PROT_WRITE; 122 int prot = PROT_READ | PROT_WRITE;
123 int flags = MAP_PRIVATE | MAP_ANONYMOUS; 123 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
124 int fd = -1; 124 int fd = -1;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 assert(size < 2 * RZ_SIZE); 226 assert(size < 2 * RZ_SIZE);
227 DUMP("unpoison %d bytes at %p: %p - %p\n", size, ptr, MEM2SHADOW(ptr), 227 DUMP("unpoison %d bytes at %p: %p - %p\n", size, ptr, MEM2SHADOW(ptr),
228 MEM2SHADOW(end)); 228 MEM2SHADOW(end));
229 *(char *)MEM2SHADOW(ptr) = 0; 229 *(char *)MEM2SHADOW(ptr) = 0;
230 ptr += SHADOW_OFFSET(size); 230 ptr += SHADOW_OFFSET(size);
231 assert(IS_SHADOW_ALIGNED(ptr)); 231 assert(IS_SHADOW_ALIGNED(ptr));
232 for (; ptr != end; ptr += SHADOW_SCALE) { 232 for (; ptr != end; ptr += SHADOW_SCALE) {
233 *(char *)MEM2SHADOW(ptr) = 0; 233 *(char *)MEM2SHADOW(ptr) = 0;
234 } 234 }
235 } 235 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698