tsan_interface_java.h 4.2 KB

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