Source file src/runtime/asan/asan.go

     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  
    17  void __asan_read_go(void *addr, uintptr_t sz, void *sp, void *pc) {
    18  	if (__asan_region_is_poisoned(addr, sz)) {
    19  		__asan_report_error(pc, 0, sp, addr, false, sz);
    20  	}
    21  }
    22  
    23  void __asan_write_go(void *addr, uintptr_t sz, void *sp, void *pc) {
    24  	if (__asan_region_is_poisoned(addr, sz)) {
    25  		__asan_report_error(pc, 0, sp, addr, true, sz);
    26  	}
    27  }
    28  
    29  void __asan_unpoison_go(void *addr, uintptr_t sz) {
    30  	__asan_unpoison_memory_region(addr, sz);
    31  }
    32  
    33  void __asan_poison_go(void *addr, uintptr_t sz) {
    34  	__asan_poison_memory_region(addr, sz);
    35  }
    36  
    37  // Keep in sync with the definition in compiler-rt
    38  // https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/asan/asan_interface_internal.h#L41
    39  // This structure is used to describe the source location of
    40  // a place where global was defined.
    41  struct _asan_global_source_location {
    42  	const char *filename;
    43  	int line_no;
    44  	int column_no;
    45  };
    46  
    47  // Keep in sync with the definition in compiler-rt
    48  // https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/asan/asan_interface_internal.h#L48
    49  // So far, the current implementation is only compatible with the ASan library from version v7 to v9.
    50  // https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/asan/asan_init_version.h
    51  // This structure describes an instrumented global variable.
    52  //
    53  // TODO: If a later version of the ASan library changes __asan_global or __asan_global_source_location
    54  // structure, we need to make the same changes.
    55  struct _asan_global {
    56  	uintptr_t beg;
    57  	uintptr_t size;
    58  	uintptr_t size_with_redzone;
    59  	const char *name;
    60  	const char *module_name;
    61  	uintptr_t has_dynamic_init;
    62  	struct _asan_global_source_location *location;
    63  	uintptr_t odr_indicator;
    64  };
    65  
    66  
    67  extern void __asan_register_globals(void*, long int);
    68  
    69  // Register global variables.
    70  // The 'globals' is an array of structures describing 'n' globals.
    71  void __asan_register_globals_go(void *addr, uintptr_t n) {
    72  	struct _asan_global *globals = (struct _asan_global *)(addr);
    73  	__asan_register_globals(globals, n);
    74  }
    75  */
    76  import "C"
    77  

View as plain text