Skip to content

Commit e36271d

Browse files
rmacnak-googleCommit Queue
authored andcommitted
Add lint to prevent use of pthread_detach.
TEST=presubmit Change-Id: Ia83acad9c50246af1aa365218354c01cc64e2127 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425142 Reviewed-by: Daco Harkes <[email protected]> Commit-Queue: Ryan Macnak <[email protected]>
1 parent 0064249 commit e36271d

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

runtime/PRESUBMIT.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,42 @@
1414
# problems we disallow the direct use of memcpy. The exceptions are in
1515
# third-party code and in platform/globals.h which uses it to implement
1616
# bit_cast and bit_copy.
17-
def CheckMemcpy(filename):
17+
# pthread_detach (and therefore std::thread::detach) have a bug that can
18+
# result in crashes. https://sourceware.org/bugzilla/show_bug.cgi?id=19951
19+
BANNED_FUNCTIONS = [
20+
['\\bmemcpy\\(', 'memcpy'],
21+
['\\bpthread_detach\\(', 'pthread_detach'],
22+
['\\bdetach\\(', 'std::thread::detach'],
23+
]
24+
25+
26+
def CheckBannedFunctions(filename):
1827
if filename.endswith(os.path.join('platform', 'globals.h')) or \
1928
filename.find('third_party') != -1:
2029
return 0
2130
fh = open(filename, 'r')
2231
content = fh.read()
23-
match = re.search('\\bmemcpy\\b', content)
24-
if match:
25-
offset = match.start()
26-
end_of_line = content.index('\n', offset)
27-
# We allow explicit use of memcpy with an opt-in via NOLINT
28-
if 'NOLINT' not in content[offset:end_of_line]:
29-
line_number = content[0:match.start()].count('\n') + 1
30-
print("%s:%d: use of memcpy is forbidden" % (filename, line_number))
31-
return 1
32+
33+
for banned_function in BANNED_FUNCTIONS:
34+
pattern = banned_function[0]
35+
name = banned_function[1]
36+
match = re.search(pattern, content)
37+
if match:
38+
offset = match.start()
39+
end_of_line = content.index('\n', offset)
40+
# We allow explicit use of memcpy with an opt-in via NOLINT
41+
if 'NOLINT' not in content[offset:end_of_line]:
42+
line_number = content[0:match.start()].count('\n') + 1
43+
print("%s:%d: use of %s is forbidden" %
44+
(filename, line_number, name))
45+
return 1
3246
return 0
3347

3448

3549
def RunLint(input_api, output_api):
3650
result = []
3751
cpplint._cpplint_state.ResetErrorCounts()
38-
memcpy_match_count = 0
52+
banned_match_count = 0
3953
# Find all .cc and .h files in the change list.
4054
for git_file in input_api.AffectedTextFiles():
4155
filename = git_file.AbsoluteLocalPath()
@@ -47,10 +61,10 @@ def RunLint(input_api, output_api):
4761
# Run cpplint on the file.
4862
cpplint.ProcessFile(filename, 1)
4963
# Check for memcpy use.
50-
memcpy_match_count += CheckMemcpy(filename)
64+
banned_match_count += CheckBannedFunctions(filename)
5165

5266
# Report a presubmit error if any of the files had an error.
53-
if cpplint._cpplint_state.error_count > 0 or memcpy_match_count > 0:
67+
if cpplint._cpplint_state.error_count > 0 or banned_match_count > 0:
5468
result = [output_api.PresubmitError('Failed cpplint check.')]
5569
return result
5670

runtime/bin/ffi_test/ffi_test_functions.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ DART_EXPORT void CallFunctionOnNewThreadNonBlocking(int64_t response_id,
13671367
if (result != 0) perror("pthread_attr_destroy");
13681368
#else
13691369
std::thread thread(fn, response_id, 123);
1370-
thread.detach();
1370+
thread.detach(); // NOLINT(not glibc)
13711371
#endif
13721372
}
13731373

0 commit comments

Comments
 (0)