| 1 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build asan && linux && (arm64 || amd64 || loong64 || riscv64 || ppc64le) |
| 6 |
|
| 7 |
package asan |
| 8 |
|
| 9 |
/* |
| 10 |
#cgo CFLAGS: -fsanitize=address |
| 11 |
#cgo LDFLAGS: -fsanitize=address |
| 12 |
|
| 13 |
#include <stdbool.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <sanitizer/asan_interface.h> |
| 16 |
#include <sanitizer/lsan_interface.h> |
| 17 |
|
| 18 |
void __asan_read_go(void *addr, uintptr_t sz, void *sp, void *pc) { |
| 19 |
if (__asan_region_is_poisoned(addr, sz)) { |
| 20 |
__asan_report_error(pc, 0, sp, addr, false, sz); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
void __asan_write_go(void *addr, uintptr_t sz, void *sp, void *pc) { |
| 25 |
if (__asan_region_is_poisoned(addr, sz)) { |
| 26 |
__asan_report_error(pc, 0, sp, addr, true, sz); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
void __asan_unpoison_go(void *addr, uintptr_t sz) { |
| 31 |
__asan_unpoison_memory_region(addr, sz); |
| 32 |
} |
| 33 |
|
| 34 |
void __asan_poison_go(void *addr, uintptr_t sz) { |
| 35 |
__asan_poison_memory_region(addr, sz); |
| 36 |
} |
| 37 |
|
| 38 |
void __lsan_register_root_region_go(void *addr, uintptr_t sz) { |
| 39 |
__lsan_register_root_region(addr, sz); |
| 40 |
} |
| 41 |
|
| 42 |
void __lsan_unregister_root_region_go(void *addr, uintptr_t sz) { |
| 43 |
__lsan_unregister_root_region(addr, sz); |
| 44 |
} |
| 45 |
|
| 46 |
void __lsan_do_leak_check_go(void) { |
| 47 |
__lsan_do_leak_check(); |
| 48 |
} |
| 49 |
|
| 50 |
// Keep in sync with the definition in compiler-rt |
| 51 |
// https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/asan/asan_interface_internal.h#L41 |
| 52 |
// This structure is used to describe the source location of |
| 53 |
// a place where global was defined. |
| 54 |
struct _asan_global_source_location { |
| 55 |
const char *filename; |
| 56 |
int line_no; |
| 57 |
int column_no; |
| 58 |
}; |
| 59 |
|
| 60 |
// Keep in sync with the definition in compiler-rt |
| 61 |
// https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/asan/asan_interface_internal.h#L48 |
| 62 |
// So far, the current implementation is only compatible with the ASan library from version v7 to v9. |
| 63 |
// https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/asan/asan_init_version.h |
| 64 |
// This structure describes an instrumented global variable. |
| 65 |
// |
| 66 |
// TODO: If a later version of the ASan library changes __asan_global or __asan_global_source_location |
| 67 |
// structure, we need to make the same changes. |
| 68 |
struct _asan_global { |
| 69 |
uintptr_t beg; |
| 70 |
uintptr_t size; |
| 71 |
uintptr_t size_with_redzone; |
| 72 |
const char *name; |
| 73 |
const char *module_name; |
| 74 |
uintptr_t has_dynamic_init; |
| 75 |
struct _asan_global_source_location *location; |
| 76 |
uintptr_t odr_indicator; |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
extern void __asan_register_globals(void*, long int); |
| 81 |
|
| 82 |
// Register global variables. |
| 83 |
// The 'globals' is an array of structures describing 'n' globals. |
| 84 |
void __asan_register_globals_go(void *addr, uintptr_t n) { |
| 85 |
struct _asan_global *globals = (struct _asan_global *)(addr); |
| 86 |
__asan_register_globals(globals, n); |
| 87 |
} |
| 88 |
*/ |
| 89 |
import "C" |