1 1.1 mrg //===-- tsan_interface_java.h -----------------------------------*- C++ -*-===// 2 1.1 mrg // 3 1.3 mrg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.3 mrg // See https://llvm.org/LICENSE.txt for license information. 5 1.3 mrg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 mrg // 7 1.1 mrg //===----------------------------------------------------------------------===// 8 1.1 mrg // 9 1.1 mrg // This file is a part of ThreadSanitizer (TSan), a race detector. 10 1.1 mrg // 11 1.1 mrg // Interface for verification of Java or mixed Java/C++ programs. 12 1.1 mrg // The interface is intended to be used from within a JVM and notify TSan 13 1.1 mrg // about such events like Java locks and GC memory compaction. 14 1.1 mrg // 15 1.1 mrg // For plain memory accesses and function entry/exit a JVM is intended to use 16 1.1 mrg // C++ interfaces: __tsan_readN/writeN and __tsan_func_enter/exit. 17 1.1 mrg // 18 1.1 mrg // For volatile memory accesses and atomic operations JVM is intended to use 19 1.1 mrg // standard atomics API: __tsan_atomicN_load/store/etc. 20 1.1 mrg // 21 1.3 mrg // For usage examples see lit_tests/java_*.cpp 22 1.1 mrg //===----------------------------------------------------------------------===// 23 1.1 mrg #ifndef TSAN_INTERFACE_JAVA_H 24 1.1 mrg #define TSAN_INTERFACE_JAVA_H 25 1.1 mrg 26 1.1 mrg #ifndef INTERFACE_ATTRIBUTE 27 1.1 mrg # define INTERFACE_ATTRIBUTE __attribute__((visibility("default"))) 28 1.1 mrg #endif 29 1.1 mrg 30 1.1 mrg #ifdef __cplusplus 31 1.1 mrg extern "C" { 32 1.1 mrg #endif 33 1.1 mrg 34 1.3 mrg typedef unsigned long jptr; 35 1.1 mrg 36 1.1 mrg // Must be called before any other callback from Java. 37 1.1 mrg void __tsan_java_init(jptr heap_begin, jptr heap_size) INTERFACE_ATTRIBUTE; 38 1.1 mrg // Must be called when the application exits. 39 1.1 mrg // Not necessary the last callback (concurrently running threads are OK). 40 1.1 mrg // Returns exit status or 0 if tsan does not want to override it. 41 1.1 mrg int __tsan_java_fini() INTERFACE_ATTRIBUTE; 42 1.1 mrg 43 1.1 mrg // Callback for memory allocations. 44 1.1 mrg // May be omitted for allocations that are not subject to data races 45 1.1 mrg // nor contain synchronization objects (e.g. String). 46 1.1 mrg void __tsan_java_alloc(jptr ptr, jptr size) INTERFACE_ATTRIBUTE; 47 1.1 mrg // Callback for memory free. 48 1.1 mrg // Can be aggregated for several objects (preferably). 49 1.1 mrg void __tsan_java_free(jptr ptr, jptr size) INTERFACE_ATTRIBUTE; 50 1.1 mrg // Callback for memory move by GC. 51 1.1 mrg // Can be aggregated for several objects (preferably). 52 1.2 mrg // The ranges can overlap. 53 1.1 mrg void __tsan_java_move(jptr src, jptr dst, jptr size) INTERFACE_ATTRIBUTE; 54 1.2 mrg // This function must be called on the finalizer thread 55 1.2 mrg // before executing a batch of finalizers. 56 1.2 mrg // It ensures necessary synchronization between 57 1.2 mrg // java object creation and finalization. 58 1.2 mrg void __tsan_java_finalize() INTERFACE_ATTRIBUTE; 59 1.2 mrg // Finds the first allocated memory block in the [*from_ptr, to) range, saves 60 1.2 mrg // its address in *from_ptr and returns its size. Returns 0 if there are no 61 1.2 mrg // allocated memory blocks in the range. 62 1.2 mrg jptr __tsan_java_find(jptr *from_ptr, jptr to) INTERFACE_ATTRIBUTE; 63 1.1 mrg 64 1.1 mrg // Mutex lock. 65 1.1 mrg // Addr is any unique address associated with the mutex. 66 1.2 mrg // Can be called on recursive reentry. 67 1.1 mrg void __tsan_java_mutex_lock(jptr addr) INTERFACE_ATTRIBUTE; 68 1.1 mrg // Mutex unlock. 69 1.1 mrg void __tsan_java_mutex_unlock(jptr addr) INTERFACE_ATTRIBUTE; 70 1.1 mrg // Mutex read lock. 71 1.1 mrg void __tsan_java_mutex_read_lock(jptr addr) INTERFACE_ATTRIBUTE; 72 1.1 mrg // Mutex read unlock. 73 1.1 mrg void __tsan_java_mutex_read_unlock(jptr addr) INTERFACE_ATTRIBUTE; 74 1.2 mrg // Recursive mutex lock, intended for handling of Object.wait(). 75 1.2 mrg // The 'rec' value must be obtained from the previous 76 1.2 mrg // __tsan_java_mutex_unlock_rec(). 77 1.2 mrg void __tsan_java_mutex_lock_rec(jptr addr, int rec) INTERFACE_ATTRIBUTE; 78 1.2 mrg // Recursive mutex unlock, intended for handling of Object.wait(). 79 1.2 mrg // The return value says how many times this thread called lock() 80 1.2 mrg // w/o a pairing unlock() (i.e. how many recursive levels it unlocked). 81 1.2 mrg // It must be passed back to __tsan_java_mutex_lock_rec() to restore 82 1.2 mrg // the same recursion level. 83 1.2 mrg int __tsan_java_mutex_unlock_rec(jptr addr) INTERFACE_ATTRIBUTE; 84 1.2 mrg 85 1.2 mrg // Raw acquire/release primitives. 86 1.2 mrg // Can be used to establish happens-before edges on volatile/final fields, 87 1.2 mrg // in atomic operations, etc. release_store is the same as release, but it 88 1.2 mrg // breaks release sequence on addr (see C++ standard 1.10/7 for details). 89 1.2 mrg void __tsan_java_acquire(jptr addr) INTERFACE_ATTRIBUTE; 90 1.2 mrg void __tsan_java_release(jptr addr) INTERFACE_ATTRIBUTE; 91 1.2 mrg void __tsan_java_release_store(jptr addr) INTERFACE_ATTRIBUTE; 92 1.1 mrg 93 1.1 mrg #ifdef __cplusplus 94 1.1 mrg } // extern "C" 95 1.1 mrg #endif 96 1.1 mrg 97 1.1 mrg #undef INTERFACE_ATTRIBUTE 98 1.1 mrg 99 1.1 mrg #endif // #ifndef TSAN_INTERFACE_JAVA_H 100