Skip to content

Commit c288107

Browse files
[IAST] Use recursive_mutex instead of CS and mutex (#7890)
## Summary of changes Replaced custom mutex guard with `std::lock_guard`, using `std::recursive_mutex` instead of `CRITICAL_SECTION` in windows and `std::mutex` with railings in Linux ## Reason for change Some locks have been spotted in smoke test wich could be cause by the lack of thread recursive lock in the `std::mutex` ## Implementation details ## Test coverage ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: Where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. MergeQueue is NOT enabled in this repository. If you have write access to the repo, the PR has 1-2 approvals (see above), and all of the required checks have passed, you can use the Squash and Merge button to merge the PR. If you don't have write access, or you need help, reach out in the #apm-dotnet channel in Slack. -->
1 parent f273b96 commit c288107

File tree

4 files changed

+4
-113
lines changed

4 files changed

+4
-113
lines changed

tracer/build/_build/Build.Shared.Steps.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ partial class Build
310310
// See also the ValidateNativeProfilerGlibcCompatibility Nuke task and the checks
311311
// in shared/src/Datadog.Trace.ClrProfiler.Native/cor_profiler.cpp#L1279
312312
var expectedGlibcVersion = IsArm64
313-
? new Version(2, 18)
313+
? new Version(2, 17)
314314
: new Version(2, 17);
315315

316316
var nativeLibHelper = new NativeValidationHelper(Nm, IsAlpine, BuildProjectDirectory);

tracer/src/Datadog.Tracer.Native/iast/hardcoded_secrets_method_analyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int HardcodedSecretsMethodAnalyzer::GetUserStrings(int arrSize, UserStringIntero
6666
{
6767
CSGUARD(_cs);
6868
_deliveredUserStrings.clear();
69-
_deliveredUserStrings.reserve(fmin(arrSize, _userStrings.size()));
69+
_deliveredUserStrings.reserve((size_t)fmin(arrSize, _userStrings.size()));
7070
int x = 0;
7171
while (x < arrSize && _userStrings.size() > 0)
7272
{

tracer/src/Datadog.Tracer.Native/iast/iast_util.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -17,87 +17,6 @@ extern char** environ;
1717

1818
namespace iast
1919
{
20-
#ifndef _WIN32
21-
static thread_local std::unordered_map<void *, bool> locked;
22-
#endif
23-
CS::CS()
24-
{
25-
#ifdef _WIN32
26-
InitializeCriticalSection(&cs);
27-
#else
28-
locked[this] = false;
29-
#endif
30-
}
31-
CS::~CS()
32-
{
33-
#ifdef _WIN32
34-
DeleteCriticalSection(&cs);
35-
#else
36-
locked.erase(this); // To avoid uncontrolled map growth
37-
#endif
38-
}
39-
bool CS::Lock()
40-
{
41-
#ifdef _WIN32
42-
EnterCriticalSection(&cs);
43-
#else
44-
if (locked[this])
45-
{
46-
return false;
47-
}
48-
cs.lock();
49-
locked[this] = true;
50-
#endif
51-
return true;
52-
}
53-
void CS::Unlock()
54-
{
55-
#ifdef _WIN32
56-
LeaveCriticalSection(&cs);
57-
#else
58-
if (locked[this])
59-
{
60-
cs.unlock();
61-
locked[this] = false;
62-
}
63-
#endif
64-
}
65-
66-
CSGuard::CSGuard(CS* cs) : refs(0)
67-
{
68-
this->cs = cs;
69-
Enter();
70-
}
71-
CSGuard::~CSGuard()
72-
{
73-
Leave();
74-
cs = nullptr;
75-
}
76-
void CSGuard::Enter()
77-
{
78-
if (std::atomic_fetch_add(&refs, 1) == 0)
79-
{
80-
if (!cs->Lock())
81-
{
82-
cs = nullptr;
83-
}
84-
}
85-
}
86-
void CSGuard::Leave()
87-
{
88-
if (cs && refs > 0 && std::atomic_fetch_sub(&refs, 1) == 1)
89-
{
90-
try
91-
{
92-
cs->Unlock();
93-
}
94-
catch (...)
95-
{
96-
/* Ignored */
97-
}
98-
}
99-
}
100-
10120
MatchResult IsMatch(const std::string& exp, const std::string& value)
10221
{
10322
return IsMatch(ToWSTRING(exp), ToWSTRING(value));

tracer/src/Datadog.Tracer.Native/iast/iast_util.h

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -271,36 +271,8 @@ namespace iast
271271
}
272272
};
273273

274-
class CS
275-
{
276-
private:
277-
#ifdef _WIN32
278-
CRITICAL_SECTION cs;
279-
#else
280-
std::mutex cs;
281-
#endif
282-
public:
283-
CS();
284-
virtual ~CS();
285-
bool Lock();
286-
void Unlock();
287-
};
288-
289-
class CSGuard
290-
{
291-
CS* cs;
292-
std::atomic<int> refs;
293-
294-
public:
295-
CSGuard(CS* cs);
296-
virtual ~CSGuard();
297-
void Enter();
298-
void Leave();
299-
};
300-
301-
#define CSGUARD(x) CSGuard __csGuard_##x(&x);
302-
#define CSENTER(x) __csGuard_##x.Enter();
303-
#define CSLEAVE(x) __csGuard_##x.Leave();
274+
#define CS std::recursive_mutex
275+
#define CSGUARD(x) std::lock_guard<std::recursive_mutex> __csGuard_##x(x);
304276

305277
/////////////////////// Enum Utils /////////////////////////////
306278
#ifndef BEGIN_ENUM_PARSE

0 commit comments

Comments
 (0)