platform_system_core/libutils/StrongPointer.cpp
Hans Boehm 7f0b2601d3 Add heuristic data race detection to sp<>
Force assignment to read the old pointer value twice, and check
that it didn't change in the interim. Previous experience with
Skia suggests that this has a high probability of correctly detecting
a data race when it occurs, instead of potentially letting the
count associated with the old pointer value get decremented twice,
and corrupting the heap.

This does increase the size of sp assignments, which seem to
commonly get inlined. For the general case, we add a third
comparison and function call.

Some code reformatting to make this consistent with modern conventions
and pass automated checks.

Test: Booted aosp build. Ran libutils tests. Looked at generated code.

Bug: 31227650

Change-Id: Id93a05c6bf10f01ee15ff1bb409611f2058f988f
2017-03-17 17:47:28 -07:00

24 lines
770 B
C++

/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "sp"
#include <log/log.h>
namespace android {
void sp_report_race() { LOG_ALWAYS_FATAL("sp<> assignment detected data race"); }
}