k-sanitizers.cc 14.5 KB
Newer Older
Eddie Kohler's avatar
Eddie Kohler committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "kernel.hh"

#define NUMBUFSZ 22

// UNDEFINED BEHAVIOR SANITIZERS

namespace {

struct source_location {
    const char* file;
    uint32_t line;
    uint32_t column;
    inline constexpr bool valid() const {
        return file != nullptr;
    }
};

struct type_descriptor {
    uint16_t kind;
    uint16_t info;
    char name[1];

    inline constexpr bool is_int() const {
        return kind == 0;
    }
    inline constexpr bool is_signed() const {
        return info & 1;
    }
    inline constexpr unsigned bit_width() const {
        return 1 << (info >> 1);
    }
    inline unsigned long value(unsigned long x) const {
        static_assert(sizeof(x) == 8, "unexpected sizeof(unsigned long)");
        if (info >= 14) {
            return ~0UL;
        } else if (info >= 12) {
            return x;
        } else {
            unsigned long mask = ~((1UL << bit_width()) - 1);
            if (is_signed() && (x & (mask >> 1))) {
                return x | mask;
            } else {
                return x & ~mask;
            }
        }
    }
    inline char* unparse_value(char* buf, size_t bufsz,
                                         unsigned long x) const {
        if (kind != 0 || info >= 14) {
            snprintf(buf, bufsz, "???");
        } else if (is_signed()) {
            snprintf(buf, bufsz, "%ld", value(x));
        } else {
            snprintf(buf, bufsz, "%lu", value(x));
        }
        return buf;
    }
};

struct type_mismatch_data {
    source_location location;
62
    const type_descriptor* type;
Eddie Kohler's avatar
Eddie Kohler committed
63
64
65
66
67
68
    unsigned long alignment;
    unsigned char type_check_kind;

    static const char* const type_check_kind_names[];
};

69
70
71
72
73
74
75
struct type_mismatch_data_v1 {
    source_location location;
    const type_descriptor* type;
    unsigned char log_alignment;
    unsigned char type_check_kind;
};

Eddie Kohler's avatar
Eddie Kohler committed
76
77
78
79
80
81
82
83
const char* const type_mismatch_data::type_check_kind_names[] = {
    "load of", "store to", "reference binding to", "member access within",
    "member call on", "constructor call on", "downcast of", "downcast of",
    "upcast of", "cast to virtual base of", "_Nonnull binding to"
};

struct type_data {
    source_location location;
84
    const type_descriptor* type;
Eddie Kohler's avatar
Eddie Kohler committed
85
86
87
88
89
90
91
};
using overflow_data = type_data;
using invalid_value_data = type_data;
using vla_bound_data = type_data;

struct out_of_bounds_data {
    source_location location;
92
93
    const type_descriptor* array_type;
    const type_descriptor* index_type;
Eddie Kohler's avatar
Eddie Kohler committed
94
95
96
97
};

struct shift_out_of_bounds_data {
    source_location location;
98
99
    const type_descriptor* lhs_type;
    const type_descriptor* rhs_type;
Eddie Kohler's avatar
Eddie Kohler committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
};

struct nonnull_arg_data {
    source_location location;
    source_location attr_location;
    int arg_index;
};

}

extern "C" {

static void handle_overflow(overflow_data* data, unsigned long lhs,
                            unsigned long rhs, char op) {
    char buf1[NUMBUFSZ], buf2[NUMBUFSZ];
    error_printf("!!! %s:%u: %s integer overflow\n"
                 "!!!  %s %c %s cannot be represented in type %s\n",
                 data->location.file, data->location.line,
                 data->type->is_signed() ? "signed" : "unsigned",
                 data->type->unparse_value(buf1, sizeof(buf1), lhs), op,
                 data->type->unparse_value(buf2, sizeof(buf2), rhs),
                 data->type->name);
}

void __ubsan_handle_add_overflow(overflow_data* data,
                                 unsigned long a, unsigned long b) {
    handle_overflow(data, a, b, '+');
}

void __ubsan_handle_sub_overflow(overflow_data* data,
                                 unsigned long a, unsigned long b) {
    handle_overflow(data, a, b, '-');
}

void __ubsan_handle_mul_overflow(overflow_data* data,
                                 unsigned long a, unsigned long b) {
    handle_overflow(data, a, b, '*');
}

void __ubsan_handle_negate_overflow(overflow_data* data,
                                    unsigned long a) {
    char buf[NUMBUFSZ];
    error_printf("!!! %s:%u: %s integer overflow\n"
                 "!!!   -(%s) cannot be represented in type %s\n",
                 data->location.file, data->location.line,
                 data->type->is_signed() ? "signed" : "unsigned",
                 data->type->unparse_value(buf, sizeof(buf), a),
                 data->type->name);
}

void __ubsan_handle_divrem_overflow(overflow_data* data,
                                    unsigned long a, unsigned long b) {
    char buf[NUMBUFSZ];
    if (data->type->is_signed() && long(data->type->value(b)) == -1L) {
        error_printf("!!! %s:%d: division of %s by -1 cannot be represented in type %s\n",
                     data->location.file, data->location.line,
                     data->type->unparse_value(buf, sizeof(buf), a),
                     data->type->name);
    } else {
        error_printf("!!! %s:%d: division by zero\n",
                     data->location.file, data->location.line);
    }
}

void __ubsan_handle_shift_out_of_bounds(shift_out_of_bounds_data* data,
                                        unsigned long a, unsigned long b) {
    char buf1[NUMBUFSZ], buf2[NUMBUFSZ];
    error_printf("!!! %s:%u: shift out of bounds\n",
                 data->location.file, data->location.line);
    if (data->rhs_type->is_signed()
        && long(data->rhs_type->value(b)) < 0) {
        error_printf("!!!   shift amount %s is negative\n",
                     data->rhs_type->unparse_value(buf2, sizeof(buf2), b));
    } else if (data->rhs_type->value(b) >= data->lhs_type->bit_width()) {
        error_printf("!!!   shift amount %s too large for type %s\n",
                     data->rhs_type->unparse_value(buf2, sizeof(buf2), b),
                     data->lhs_type->name);
    } else {
        error_printf("!!!   %s << %s cannot be represented in type %s\n",
                     data->lhs_type->unparse_value(buf1, sizeof(buf1), a),
                     data->rhs_type->unparse_value(buf2, sizeof(buf2), b),
                     data->lhs_type->name);
    }
}

185
186
187
188
189
static void handle_type_mismatch(const source_location& location,
                                 const type_descriptor* type,
                                 unsigned long alignment,
                                 unsigned char type_check_kind,
                                 unsigned long ptr) {
Eddie Kohler's avatar
Eddie Kohler committed
190
191
    if (!ptr) {
        error_printf("!!! %s:%d: %s null pointer of type %s\n",
192
193
194
195
196
197
198
199
                     location.file, location.line,
                     type_mismatch_data::type_check_kind_names[type_check_kind],
                     type->name);
    } else if (alignment && (ptr & (alignment - 1)) != 0) {
        error_printf("!!! %s:%d: %s misaligned address %p for type %s\n",
                     location.file, location.line,
                     type_mismatch_data::type_check_kind_names[type_check_kind],
                     ptr, type->name);
Eddie Kohler's avatar
Eddie Kohler committed
200
201
202
    } else {
        error_printf("!!! %s:%d: %s address %p\n"
                     "!!!   with insufficient space for an object with type %s\n",
203
204
205
                     location.file, location.line,
                     type_mismatch_data::type_check_kind_names[type_check_kind],
                     ptr, type->name);
Eddie Kohler's avatar
Eddie Kohler committed
206
207
208
209
    }
    log_backtrace("!!! ");
}

210
211
212
213
214
215
216
217
218
219
220
221
void __ubsan_handle_type_mismatch(type_mismatch_data* data,
                                  unsigned long ptr) {
    handle_type_mismatch(data->location, data->type, data->alignment,
                         data->type_check_kind, ptr);
}

void __ubsan_handle_type_mismatch_v1(type_mismatch_data_v1* data,
                                     unsigned long ptr) {
    handle_type_mismatch(data->location, data->type, 1UL << data->log_alignment,
                         data->type_check_kind, ptr);
}

Eddie Kohler's avatar
Eddie Kohler committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
void __ubsan_handle_out_of_bounds(out_of_bounds_data* data,
                                  unsigned long index) {
    char buf[NUMBUFSZ];
    error_printf("!!! %s:%d: index %s out of range for type %s\n",
                 data->location.file, data->location.line,
                 data->index_type->unparse_value(buf, sizeof(buf), index),
                 data->array_type->name);
}

void __ubsan_handle_builtin_unreachable(source_location* location) {
    error_printf("!!! %s:%d: execution reached a __builtin_unreachable() call\n",
                 location->file, location->line);
}

void __ubsan_handle_missing_return(source_location* location) {
    error_printf("!!! %s:%d: execution reached the end of a value-returning function\n"
                 "!!!   without returning a value\n",
                 location->file, location->line);
}

void __ubsan_handle_vla_bound_not_positive(vla_bound_data* data,
					   unsigned long bound) {
    char buf[NUMBUFSZ];
    error_printf("!!! %s:%d: variable length array bound evaluates to non-positive %s\n",
                 data->location.file, data->location.line,
                 data->type->unparse_value(buf, sizeof(buf), bound));
}

void __ubsan_handle_load_invalid_value(invalid_value_data* data,
                                       unsigned long val) {
    char buf[NUMBUFSZ];
    error_printf("!!! %s:%d: load value %s is not valid for type %s\n",
                 data->location.file, data->location.line,
                 data->type->unparse_value(buf, sizeof(buf), val),
                 data->type->name);
}

void __ubsan_handle_nonnull_arg(nonnull_arg_data* data) {
    error_printf("!!! %s:%d: null pointer passed as argument %d,\n"
                 "!!!   which is declared to never be null\n",
                 data->location.file, data->location.line,
                 data->arg_index);
    if (data->attr_location.valid()) {
        error_printf("!!! %s:%d: attribute or annotation here\n",
                     data->attr_location.file, data->attr_location.line);
    }
}

void __ubsan_handle_nonnull_return(source_location* location) {
    error_printf("!!! %s:%d: null pointer returned from function\n"
                 "!!!   which is declared to never be null\n",
                 location->file, location->line);
}

276
277
278
279
280
281
282
283
284
285
286
287
288
void __ubsan_handle_pointer_overflow(source_location* location,
                                     uintptr_t base, uintptr_t result) {
    if (((intptr_t) base >= 0) == ((intptr_t) result >= 0)) {
        error_printf("!!! %s:%d: %s of unsigned offset to %p overflowed to %p\n",
                     location->file, location->line,
                     base > result ? "addition" : "subtraction",
                     base, result);
    } else {
        error_printf("!!! %s:%d: pointer index expression with base %p overflowed to %p\n",
                     location->file, location->line, base, result);
    }
}

Eddie Kohler's avatar
Eddie Kohler committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
}


// ADDRESS SANITIZER

static volatile signed char* volatile asan_pagemap;
static signed char* asan_pagemap_storage;
static size_t asan_pagemap_sz;
static int asan_global_status;
static std::atomic<int> asan_enabled;

static void asan_access(unsigned long addr, size_t sz, bool write) {
    proc* p = current();
    int& status = p ? p->sanitizer_status_ : asan_global_status;
    volatile signed char* pagemap = asan_pagemap;

    if (addr < HIGHMEM_BASE || !pagemap || status) {
        return;
    }

    size_t lp, rp;
    if (addr >= KTEXT_BASE) {
        lp = (addr - KTEXT_BASE) / PAGESIZE;
        rp = (addr + sz - 1 - KTEXT_BASE) / PAGESIZE;
    } else {
        lp = (addr - HIGHMEM_BASE) / PAGESIZE;
        rp = (addr + sz - 1 - HIGHMEM_BASE) / PAGESIZE;
    }

    while (lp <= rp && lp < asan_pagemap_sz && pagemap[lp] >= 0) {
        ++lp;
    }

    if (lp <= rp && lp < PA_IOHIGHMIN / PAGESIZE && !status) {
        ++status;

        uintptr_t lpaddr = HIGHMEM_BASE + lp * PAGESIZE;
        if (addr >= KTEXT_BASE) {
            lpaddr += KTEXT_BASE - HIGHMEM_BASE;
        }
        uintptr_t xaddr = max(addr, lpaddr);
        auto type = lp < asan_pagemap_sz ? "poisoned" : "nonexistent";
        error_printf("!!! invalid %s of %s address %p\n",
                     write ? "write" : "read", type, xaddr);
        log_backtrace("!!! ");

        --status;
    }
}

extern "C" {
void __asan_load1_noabort(unsigned long addr) {
    asan_access(addr, 1, false);
}
void __asan_load2_noabort(unsigned long addr) {
    asan_access(addr, 2, false);
}
void __asan_load4_noabort(unsigned long addr) {
    asan_access(addr, 4, false);
}
void __asan_load8_noabort(unsigned long addr) {
    asan_access(addr, 8, false);
}
void __asan_loadN_noabort(unsigned long addr, size_t sz) {
    asan_access(addr, sz, false);
}

void __asan_store1_noabort(unsigned long addr) {
    asan_access(addr, 1, true);
}
void __asan_store2_noabort(unsigned long addr) {
    asan_access(addr, 2, true);
}
void __asan_store4_noabort(unsigned long addr) {
    asan_access(addr, 4, true);
}
void __asan_store8_noabort(unsigned long addr) {
    asan_access(addr, 8, true);
}
void __asan_storeN_noabort(unsigned long addr, size_t sz) {
    asan_access(addr, sz, true);
}

void __asan_handle_no_return() {
}
void __asan_before_dynamic_init(const char* module_name) {
}
void __asan_after_dynamic_init() {
}
}


// external sanitizer interface

void init_sanitizers() {
    assert(!asan_pagemap_sz);

    // reserve enough pagemap space to cover all allocatable
    // physical memory plus all kernel-accessible memory
    size_t top = MEMSIZE_PHYSICAL;
    for (auto& it : physical_ranges) {
        if (it.type() == mem_kernel || it.type() == mem_available) {
            top = round_up(it.last(), PAGESIZE);
        }
    }
    asan_pagemap_sz = round_up(top / PAGESIZE, PAGESIZE);

    // reserve the memory
    uintptr_t asan_pagemap_top_pa = 128 << 20; // 128MB
    uintptr_t asan_pagemap_pa = asan_pagemap_top_pa - asan_pagemap_sz;
    physical_ranges.set(asan_pagemap_pa, asan_pagemap_top_pa, mem_reserved);

    // initialize storage
    asan_pagemap_storage = pa2ka<signed char*>(asan_pagemap_pa);
    signed char* s = const_cast<signed char*>(asan_pagemap_storage);
    memset(s, 0, MEMSIZE_PHYSICAL / PAGESIZE);
    memset(s + (MEMSIZE_PHYSICAL / PAGESIZE), 255,
           asan_pagemap_sz - (MEMSIZE_PHYSICAL / PAGESIZE));
    for (auto& it : physical_ranges) {
        if (it.type() == mem_kernel || it.type() == mem_available) {
            memset(s + it.first() / PAGESIZE, 0,
                   (round_up(it.last(), PAGESIZE) / PAGESIZE)
                   - it.first() / PAGESIZE);
        }
    }
}

void disable_asan() {
    if (--asan_enabled == 0) {
        asan_pagemap = nullptr;
    }
}

void enable_asan() {
    if (++asan_enabled == 1) {
        asan_pagemap = asan_pagemap_storage;
    }
}

void asan_mark_memory(unsigned long pa, size_t sz, bool poisoned) {
    size_t lp = pa / PAGESIZE;
    size_t rp = (pa + sz - 1) / PAGESIZE;
    while (lp <= rp && lp < asan_pagemap_sz) {
        asan_pagemap_storage[lp] = poisoned ? -1 : 0;
        ++lp;
    }
}