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