1 //===-- sanitizer_thread_arg_retval.cpp -------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between sanitizer tools.
11 // Tracks thread arguments and return value for leak checking.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_thread_arg_retval.h"
16 #include "sanitizer_placement_new.h"
18 namespace __sanitizer
{
20 void ThreadArgRetval::CreateLocked(uptr thread
, bool detached
,
23 Data
& t
= data_
[thread
];
26 t
.detached
= detached
;
30 ThreadArgRetval::Args
ThreadArgRetval::GetArgs(uptr thread
) const {
31 __sanitizer::Lock
lock(&mtx_
);
32 auto t
= data_
.find(thread
);
36 return t
->second
.args
;
39 void ThreadArgRetval::Finish(uptr thread
, void* retval
) {
40 __sanitizer::Lock
lock(&mtx_
);
41 auto t
= data_
.find(thread
);
44 if (t
->second
.detached
) {
45 // Retval of detached thread connot be retrieved.
49 t
->second
.done
= true;
50 t
->second
.args
.arg_retval
= retval
;
53 u32
ThreadArgRetval::BeforeJoin(uptr thread
) const {
54 __sanitizer::Lock
lock(&mtx_
);
55 auto t
= data_
.find(thread
);
57 CHECK(!t
->second
.detached
);
61 void ThreadArgRetval::AfterJoin(uptr thread
, u32 gen
) {
62 __sanitizer::Lock
lock(&mtx_
);
63 auto t
= data_
.find(thread
);
64 if (!t
|| gen
!= t
->second
.gen
) {
65 // Thread was reused and erased by any other event.
68 CHECK(!t
->second
.detached
);
72 void ThreadArgRetval::DetachLocked(uptr thread
) {
74 auto t
= data_
.find(thread
);
76 CHECK(!t
->second
.detached
);
78 // We can't retrive retval after detached thread finished.
82 t
->second
.detached
= true;
85 void ThreadArgRetval::GetAllPtrsLocked(InternalMmapVector
<uptr
>* ptrs
) {
88 data_
.forEach([&](DenseMap
<uptr
, Data
>::value_type
& kv
) -> bool {
89 ptrs
->push_back((uptr
)kv
.second
.args
.arg_retval
);
94 } // namespace __sanitizer