Change from custom Lock to std::mutex.

[ Merge of http://go/wvgerrit/67884 ]

Now that we can use C++11, we should use the cross-platform std::mutex
type, not the custom pthread version.

Bug: 111850982
Test: WV unit/integration tests
Change-Id: If2fde2836826c5184609e6b1f3a6511206bd4594
This commit is contained in:
Rahul Frias
2018-12-13 10:07:21 -08:00
parent 65c64292b7
commit 0e28104cff
22 changed files with 151 additions and 163 deletions

View File

@@ -110,7 +110,6 @@ LOCAL_SRC_FILES := \
$(UTIL_SRC_DIR)/clock.cpp \
$(UTIL_SRC_DIR)/file_store.cpp \
$(UTIL_SRC_DIR)/file_utils.cpp \
$(UTIL_SRC_DIR)/lock.cpp \
$(UTIL_SRC_DIR)/log.cpp \
$(SRC_DIR)/properties_android.cpp \
$(SRC_DIR)/timer.cpp \

View File

@@ -6,6 +6,7 @@
#define WVCDM_CORE_CDM_ENGINE_H_
#include <memory>
#include <mutex>
#include <string>
#include <vector>
@@ -16,7 +17,6 @@
#include "disallow_copy_and_assign.h"
#include "file_store.h"
#include "initialization_data.h"
#include "lock.h"
#include "metrics_collections.h"
#include "oemcrypto_adapter.h"
#include "service_certificate.h"
@@ -338,7 +338,7 @@ class CdmEngine {
int64_t last_usage_information_update_time_;
// Protect release_key_sets_ from non-thread-safe operations.
Lock release_key_sets_lock_;
std::mutex release_key_sets_lock_;
// TODO(rfrias): Replace with two sets of locks, one to protect
// the CdmSessionMap and a per-session lock to control access to
@@ -349,7 +349,7 @@ class CdmEngine {
// The layer above the CDM implementation is expected to handle thread
// synchronization to make sure other functions that access sessions do not
// occur simultaneously with OpenSession or CloseSession.
Lock session_map_lock_;
std::mutex session_map_lock_;
CORE_DISALLOW_COPY_AND_ASSIGN(CdmEngine);
};

View File

@@ -11,7 +11,6 @@
#include "cdm_session.h"
#include "disallow_copy_and_assign.h"
#include "lock.h"
#include "wv_cdm_types.h"
namespace wvcdm {

View File

@@ -7,13 +7,13 @@
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "OEMCryptoCENC.h"
#include "disallow_copy_and_assign.h"
#include "key_session.h"
#include "lock.h"
#include "metrics_collections.h"
#include "oemcrypto_adapter.h"
#include "timer_metric.h"
@@ -278,7 +278,7 @@ class CryptoSession {
static const size_t kAes128BlockSize = 16; // Block size for AES_CBC_128
static const size_t kSignatureSize = 32; // size for HMAC-SHA256 signature
static Lock crypto_lock_;
static std::mutex crypto_lock_;
static bool initialized_;
static int session_count_;

View File

@@ -11,7 +11,6 @@
#include "cdm_client_property_set.h"
#include "disallow_copy_and_assign.h"
#include "lock.h"
#include "wv_cdm_types.h"
#if defined(UNIT_TEST)

View File

@@ -6,6 +6,7 @@
#define WVCDM_CORE_USAGE_TABLE_HEADER_H_
#include <memory>
#include <mutex>
#include <string>
#include <vector>
@@ -13,7 +14,6 @@
#include "device_files.h"
#include "disallow_copy_and_assign.h"
#include "file_store.h"
#include "lock.h"
#include "metrics_collections.h"
#include "wv_cdm_types.h"
@@ -123,7 +123,7 @@ class UsageTableHeader {
// Synchonizes access to the Usage Table Header and bookkeeping
// data-structures
Lock usage_table_header_lock_;
std::mutex usage_table_header_lock_;
metrics::CryptoMetrics alternate_crypto_metrics_;

View File

@@ -87,7 +87,7 @@ CdmEngine::CdmEngine(FileSystem* file_system, const std::string& spoid)
CdmEngine::~CdmEngine() {
usage_session_.reset();
AutoLock lock(session_map_lock_);
std::unique_lock<std::mutex> lock(session_map_lock_);
session_map_.Terminate();
}
@@ -147,7 +147,7 @@ CdmResponseType CdmEngine::OpenSession(
CdmSessionId id = new_session->session_id();
LOGI("CdmEngine::OpenSession: %s", id.c_str());
AutoLock lock(session_map_lock_);
std::unique_lock<std::mutex> lock(session_map_lock_);
session_map_.Add(id, new_session.release());
if (session_id) *session_id = id;
return NO_ERROR;
@@ -167,7 +167,7 @@ CdmResponseType CdmEngine::OpenKeySetSession(
// resources (CryptoSession etc).
bool key_set_in_use = false;
{
AutoLock lock(release_key_sets_lock_);
std::unique_lock<std::mutex> lock(release_key_sets_lock_);
key_set_in_use =
release_key_sets_.find(key_set_id) != release_key_sets_.end();
}
@@ -180,7 +180,7 @@ CdmResponseType CdmEngine::OpenKeySetSession(
if (sts != NO_ERROR) return sts;
AutoLock lock(release_key_sets_lock_);
std::unique_lock<std::mutex> lock(release_key_sets_lock_);
release_key_sets_[key_set_id] = std::make_pair(session_id,
clock_.GetCurrentTime() + kReleaseSessionTimeToLive);
@@ -189,7 +189,7 @@ CdmResponseType CdmEngine::OpenKeySetSession(
CdmResponseType CdmEngine::CloseSession(const CdmSessionId& session_id) {
LOGI("CdmEngine::CloseSession: %s", session_id.c_str());
AutoLock lock(session_map_lock_);
std::unique_lock<std::mutex> lock(session_map_lock_);
if (!session_map_.CloseSession(session_id)) {
LOGE("CdmEngine::CloseSession: session not found = %s", session_id.c_str());
return SESSION_NOT_FOUND_1;
@@ -202,7 +202,7 @@ CdmResponseType CdmEngine::CloseKeySetSession(const CdmKeySetId& key_set_id) {
CdmSessionId session_id;
{
AutoLock lock(release_key_sets_lock_);
std::unique_lock<std::mutex> lock(release_key_sets_lock_);
CdmReleaseKeySetMap::iterator iter = release_key_sets_.find(key_set_id);
if (iter == release_key_sets_.end()) {
LOGE("CdmEngine::CloseKeySetSession: key set id not found = %s",
@@ -214,7 +214,7 @@ CdmResponseType CdmEngine::CloseKeySetSession(const CdmKeySetId& key_set_id) {
CdmResponseType sts = CloseSession(session_id);
AutoLock lock(release_key_sets_lock_);
std::unique_lock<std::mutex> lock(release_key_sets_lock_);
CdmReleaseKeySetMap::iterator iter = release_key_sets_.find(key_set_id);
if (iter != release_key_sets_.end()) {
release_key_sets_.erase(iter);
@@ -223,7 +223,7 @@ CdmResponseType CdmEngine::CloseKeySetSession(const CdmKeySetId& key_set_id) {
}
bool CdmEngine::IsOpenSession(const CdmSessionId& session_id) {
AutoLock lock(session_map_lock_);
std::unique_lock<std::mutex> lock(session_map_lock_);
return session_map_.Exists(session_id);
}
@@ -251,7 +251,7 @@ CdmResponseType CdmEngine::GenerateKeyRequest(
return INVALID_SESSION_ID;
}
AutoLock lock(release_key_sets_lock_);
std::unique_lock<std::mutex> lock(release_key_sets_lock_);
CdmReleaseKeySetMap::iterator iter = release_key_sets_.find(key_set_id);
if (iter == release_key_sets_.end()) {
LOGE("CdmEngine::GenerateKeyRequest: key set ID not found = %s",
@@ -328,7 +328,7 @@ CdmResponseType CdmEngine::AddKey(const CdmSessionId& session_id,
return EMPTY_KEYSET_ID_ENG_3;
}
AutoLock lock(release_key_sets_lock_);
std::unique_lock<std::mutex> lock(release_key_sets_lock_);
CdmReleaseKeySetMap::iterator iter = release_key_sets_.find(*key_set_id);
if (iter == release_key_sets_.end()) {
LOGE("CdmEngine::AddKey: key set id not found = %s", key_set_id->c_str());
@@ -1525,7 +1525,7 @@ CdmResponseType CdmEngine::Decrypt(const CdmSessionId& session_id,
// else we must be level 1 direct and we don't need to return a buffer.
}
AutoLock lock(session_map_lock_);
std::unique_lock<std::mutex> lock(session_map_lock_);
std::shared_ptr<CdmSession> session;
if (session_id.empty()) {
CdmSessionList sessions;
@@ -1646,7 +1646,7 @@ bool CdmEngine::FindSessionForKey(const KeyId& key_id,
uint32_t session_sharing_id = Properties::GetSessionSharingId(*session_id);
AutoLock lock(session_map_lock_);
std::unique_lock<std::mutex> lock(session_map_lock_);
CdmSessionList sessions;
session_map_.GetSessionList(sessions);
@@ -1703,7 +1703,7 @@ void CdmEngine::OnTimerEvent() {
bool is_usage_update_needed = false;
{
AutoLock lock(session_map_lock_);
std::unique_lock<std::mutex> lock(session_map_lock_);
CdmSessionList sessions;
session_map_.GetSessionList(sessions);
@@ -1798,7 +1798,7 @@ void CdmEngine::CloseExpiredReleaseSessions() {
std::set<CdmSessionId> close_session_set;
{
AutoLock lock(release_key_sets_lock_);
std::unique_lock<std::mutex> lock(release_key_sets_lock_);
for (CdmReleaseKeySetMap::iterator iter = release_key_sets_.begin();
iter != release_key_sets_.end();) {
if (iter->second.second < current_time) {

View File

@@ -75,7 +75,7 @@ void DeleteX509Stack(STACK_OF(X509)* stack) {
}
namespace wvcdm {
Lock CryptoSession::crypto_lock_;
std::mutex CryptoSession::crypto_lock_;
bool CryptoSession::initialized_ = false;
int CryptoSession::session_count_ = 0;
uint64_t CryptoSession::request_id_index_ = 0;
@@ -212,7 +212,7 @@ CdmResponseType CryptoSession::GetProvisioningMethod(
void CryptoSession::Init() {
LOGV("CryptoSession::Init");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
session_count_ += 1;
if (!initialized_) {
std::string sandbox_id;
@@ -235,7 +235,7 @@ void CryptoSession::Init() {
void CryptoSession::Terminate() {
LOGV("CryptoSession::Terminate: initialized_=%d, session_count_=%d",
initialized_, session_count_);
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (session_count_ > 0) {
session_count_ -= 1;
} else {
@@ -322,7 +322,7 @@ bool CryptoSession::GetProvisioningToken(std::string* token) {
return false;
}
LOGV("CryptoSession::GetProvisioningToken: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!initialized_) {
metrics_->crypto_session_get_token_.Increment(false);
@@ -380,7 +380,7 @@ bool CryptoSession::GetInternalDeviceUniqueId(std::string* device_id) {
}
LOGV("CryptoSession::GetInternalDeviceUniqueId: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!initialized_) {
return false;
}
@@ -473,7 +473,7 @@ bool CryptoSession::GetSystemId(uint32_t* system_id) {
}
LOGV("CryptoSession::GetSystemId: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!initialized_ || !open_) {
return false;
}
@@ -642,7 +642,7 @@ bool CryptoSession::GetProvisioningId(std::string* provisioning_id) {
{
LOGV("CryptoSession::GetProvisioningId: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!initialized_) {
return false;
}
@@ -663,7 +663,7 @@ bool CryptoSession::GetProvisioningId(std::string* provisioning_id) {
} else {
OEMCryptoResult sts;
LOGV("CryptoSession::GetProvisioningId: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
M_TIME(
sts = OEMCrypto_GetKeyData(buf, &buf_size, requested_security_level_),
metrics_, oemcrypto_get_key_data_, sts, metrics::Pow2Bucket(buf_size));
@@ -688,7 +688,7 @@ CdmResponseType CryptoSession::Open(SecurityLevel requested_security_level) {
requested_security_level == kLevel3
? QUERY_VALUE_SECURITY_LEVEL_L3.c_str()
: QUERY_VALUE_SECURITY_LEVEL_DEFAULT.c_str());
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!initialized_) return UNKNOWN_ERROR;
if (open_) return NO_ERROR;
}
@@ -699,7 +699,7 @@ CdmResponseType CryptoSession::Open(SecurityLevel requested_security_level) {
if (result != NO_ERROR) return result;
LOGV("CryptoSession::Open: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
OEMCrypto_SESSION sid;
requested_security_level_ = requested_security_level;
OEMCryptoResult sts = OEMCrypto_OpenSession(&sid, requested_security_level);
@@ -753,10 +753,10 @@ CdmResponseType CryptoSession::Open(SecurityLevel requested_security_level) {
// Ignore errors since we do not know when a session is opened,
// if it is intended to be used for offline/usage session related
// or otherwise.
crypto_lock_.Release();
auto_lock.unlock();
bool is_usage_table_header_inited =
(*header)->Init(security_level, this);
crypto_lock_.Acquire();
auto_lock.lock();
if (!is_usage_table_header_inited) {
delete *header;
*header = NULL;
@@ -783,7 +783,7 @@ void CryptoSession::Close() {
OEMCryptoResult close_sts;
bool update_usage_table = false;
{
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!open_) return;
close_sts = OEMCrypto_CloseSession(oec_session_id_);
@@ -799,7 +799,7 @@ void CryptoSession::Close() {
bool CryptoSession::GenerateRequestId(std::string* req_id_str) {
LOGV("CryptoSession::GenerateRequestId: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!req_id_str) {
LOGE("CryptoSession::GenerateRequestId: No output destination provided.");
return false;
@@ -816,7 +816,7 @@ bool CryptoSession::PrepareRequest(const std::string& message,
bool is_provisioning,
std::string* signature) {
LOGV("CryptoSession::PrepareRequest: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!signature) {
LOGE("CryptoSession::PrepareRequest : No output destination provided.");
@@ -837,7 +837,7 @@ bool CryptoSession::PrepareRequest(const std::string& message,
bool CryptoSession::PrepareRenewalRequest(const std::string& message,
std::string* signature) {
LOGV("CryptoSession::PrepareRenewalRequest: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!signature) {
LOGE(
@@ -862,7 +862,7 @@ CdmResponseType CryptoSession::LoadKeys(
CdmResponseType result = KEY_ADDED;
{
LOGV("CryptoSession::LoadKeys: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (key_type == kLicenseKeyTypeEntitlement &&
key_session_->Type() != KeySession::kEntitlement) {
@@ -901,7 +901,7 @@ CdmResponseType CryptoSession::LoadKeys(
CdmResponseType CryptoSession::LoadEntitledContentKeys(
const std::vector<CryptoKey>& key_array) {
LOGV("CryptoSession::LoadEntitledContentKeys: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
OEMCryptoResult sts = key_session_->LoadEntitledContentKeys(key_array);
@@ -920,7 +920,7 @@ CdmResponseType CryptoSession::LoadEntitledContentKeys(
bool CryptoSession::LoadCertificatePrivateKey(std::string& wrapped_key) {
LOGV("CryptoSession::LoadCertificatePrivateKey: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
// Call OEMCrypto_GetOEMPublicCertificate before OEMCrypto_LoadDeviceRSAKey
// so it caches the OEMCrypto Public Key and then throw away result
@@ -950,7 +950,7 @@ bool CryptoSession::RefreshKeys(const std::string& message,
const std::string& signature, int num_keys,
const CryptoKey* key_array) {
LOGV("CryptoSession::RefreshKeys: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
const uint8_t* msg = reinterpret_cast<const uint8_t*>(message.data());
std::vector<OEMCrypto_KeyRefreshObject> load_key_array(num_keys);
@@ -1155,7 +1155,7 @@ CdmResponseType CryptoSession::Decrypt(const CdmDecryptionParameters& params) {
pattern_descriptor.encrypt = params.pattern_descriptor.encrypt_blocks;
pattern_descriptor.skip = params.pattern_descriptor.skip_blocks;
pattern_descriptor.offset = 0; // Deprecated field
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
// Check if key needs to be selected
if (params.is_encrypted) {
CdmResponseType result = SelectKey(*params.key_id, params.cipher_mode);
@@ -1216,7 +1216,7 @@ bool CryptoSession::UsageInformationSupport(bool* has_support) {
CdmResponseType CryptoSession::UpdateUsageInformation() {
LOGV("CryptoSession::UpdateUsageInformation: id=%lu",
oec_session_id_);
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (!initialized_) return UNKNOWN_ERROR;
if (usage_table_header_ != NULL) {
@@ -1237,7 +1237,7 @@ CdmResponseType CryptoSession::DeactivateUsageInformation(
const std::string& provider_session_token) {
LOGV("DeactivateUsageInformation: id=%lu", oec_session_id_);
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
uint8_t* pst = reinterpret_cast<uint8_t*>(
const_cast<char*>(provider_session_token.data()));
@@ -1269,7 +1269,7 @@ CdmResponseType CryptoSession::GenerateUsageReport(
return UNKNOWN_ERROR;
}
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
uint8_t* pst = reinterpret_cast<uint8_t*>(
const_cast<char*>(provider_session_token.data()));
@@ -1355,7 +1355,7 @@ CdmResponseType CryptoSession::ReleaseUsageInformation(
const std::string& provider_session_token) {
LOGV("ReleaseUsageInformation: id=%lu", oec_session_id_);
{
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (usage_table_header_ != NULL) {
LOGW("ReleaseUsageInformation: deprecated for OEMCrypto v13+");
return NO_ERROR;
@@ -1387,7 +1387,7 @@ CdmResponseType CryptoSession::DeleteUsageInformation(
LOGV("CryptoSession::DeleteUsageInformation");
OEMCryptoResult status;
{
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
status = OEMCrypto_ForceDeleteUsageEntry(
reinterpret_cast<const uint8_t*>(provider_session_token.c_str()),
provider_session_token.length());
@@ -1409,7 +1409,7 @@ CdmResponseType CryptoSession::DeleteMultipleUsageInformation(
LOGV("CryptoSession::DeleteMultipleUsageInformation");
CdmResponseType response = NO_ERROR;
{
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
for (size_t i = 0; i < provider_session_tokens.size(); ++i) {
OEMCryptoResult status = OEMCrypto_ForceDeleteUsageEntry(
reinterpret_cast<const uint8_t*>(provider_session_tokens[i].c_str()),
@@ -1432,7 +1432,7 @@ CdmResponseType CryptoSession::DeleteAllUsageReports() {
LOGV("DeleteAllUsageReports");
OEMCryptoResult status;
{
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
status = OEMCrypto_DeleteOldUsageTable();
metrics_->oemcrypto_delete_usage_table_.Increment(status);
if (OEMCrypto_SUCCESS != status) {
@@ -1461,7 +1461,7 @@ bool CryptoSession::GenerateNonce(uint32_t* nonce) {
}
LOGV("CryptoSession::GenerateNonce: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
OEMCryptoResult result = OEMCrypto_GenerateNonce(oec_session_id_, nonce);
metrics_->oemcrypto_generate_nonce_.Increment(result);
@@ -1831,7 +1831,7 @@ CdmResponseType CryptoSession::GenericEncrypt(const std::string& in_buffer,
out_buffer->resize(in_buffer.size());
}
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
// TODO(jfore): We need to select a key with a cipher mode and algorithm
// doesn't seem to fit. Is it ok to just use a default value here?
// Or do we need to pass it in?
@@ -1884,7 +1884,7 @@ CdmResponseType CryptoSession::GenericDecrypt(const std::string& in_buffer,
out_buffer->resize(in_buffer.size());
}
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
// TODO(jfore): We need to select a key with a cipher mode and algorithm
// doesn't seem to fit. Is it ok to just use a default value here?
// Or do we need to pass it in?
@@ -1934,7 +1934,7 @@ CdmResponseType CryptoSession::GenericSign(const std::string& message,
OEMCryptoResult sts;
size_t length = signature->size();
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
// TODO(jfore): We need to select a key with a cipher mode and algorithm
// doesn't seem to fit. Is it ok to just use a default value here?
// Or do we need to pass it in?
@@ -1988,7 +1988,7 @@ CdmResponseType CryptoSession::GenericVerify(const std::string& message,
return INVALID_PARAMETERS_ENG_16;
}
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
// TODO(jfore): We need to select a key with a cipher mode and algorithm
// doesn't seem to fit. Is it ok to just use a default value here?
// Or do we need to pass it in?
@@ -2276,7 +2276,7 @@ bool CryptoSession::CreateOldUsageEntry(
const std::string& server_mac_key, const std::string& client_mac_key,
const std::string& provider_session_token) {
LOGV("CreateOldUsageEntry: Lock");
AutoLock auto_lock(crypto_lock_);
std::unique_lock<std::mutex> auto_lock(crypto_lock_);
if (server_mac_key.size() < MAC_KEY_SIZE ||
client_mac_key.size() < MAC_KEY_SIZE) {

View File

@@ -18,14 +18,16 @@
#include <sys/mman.h>
#include <unistd.h>
#include <condition_variable>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include "file_store.h"
#include "level3.h"
#include "lock.h"
#include "log.h"
#include "metrics_collections.h"
#include "properties.h"
@@ -382,35 +384,25 @@ class WatchDog {
public:
// Created by main thread.
WatchDog(std::vector<uint8_t> sandbox_id) {
pthread_mutex_init(&mutex_, NULL);
pthread_cond_init(&condition_, NULL);
status_ = OEMCrypto_SUCCESS;
gave_up_ = false;
sandbox_id_ = sandbox_id;
}
// Deleted by either thread.
~WatchDog() {
pthread_cond_destroy(&condition_);
}
~WatchDog() {}
// Starts worker thread.
void StartThread() {
running_ = true;
if(pthread_create(&thread_, NULL, RunWatchDog, this)) {
LOGE("Could not create watch dog thread.");
status_ = OEMCrypto_ERROR_INIT_FAILED;
running_ = false;
return;
}
thread_ = std::thread(&RunWatchDog, this);
}
// Function called by new worker thread in pthread_create.
static void *RunWatchDog(void *watcher) {
// Function called by new worker thread.
static void RunWatchDog(void *watcher) {
WatchDog* dog = reinterpret_cast<WatchDog *>(watcher);
dog->DoInit();
dog->SignalDoneAndCleanUp();
return NULL;
}
// Called by worker thread.
@@ -475,50 +467,48 @@ class WatchDog {
// Called by worker thread after DoInit has finshed.
void SignalDoneAndCleanUp() {
pthread_mutex_lock(&mutex_);
running_ = false;
pthread_cond_signal(&condition_);
// If the main thread gave up, it won't delete this, so we must.
bool should_delete = gave_up_;
pthread_mutex_unlock(&mutex_);
bool should_delete;
{
std::unique_lock<std::mutex> lock(mutex_);
running_ = false;
condition_.notify_all();
// If the main thread gave up, it won't delete this, so we must.
should_delete = gave_up_;
}
// https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
if (should_delete) delete this;
}
// Called by main thread to wait for worker thread.
OEMCryptoResult WaitForStatusAndCleanUp() {
pthread_mutex_lock(&mutex_);
struct timespec time_to_giveup;
clock_gettime(CLOCK_REALTIME, &time_to_giveup);
time_to_giveup.tv_sec += 120; // wait 2 minutes.
if (running_) {
pthread_cond_timedwait(&condition_, &mutex_, &time_to_giveup);
bool should_delete;
OEMCryptoResult status;
{
std::unique_lock<std::mutex> lock(mutex_);
if (running_) {
condition_.wait_for(lock, std::chrono::minutes(2));
}
if (running_) {
gave_up_ = true;
status_ = OEMCrypto_ERROR_INIT_FAILED;
LOGE("XXX WATCH DOG ERROR XXX");
// HACK: this normally just returns an error. However, we are using it
// as a signal to dump debugging information.
Level3_GetOEMPublicCertificate(0, NULL, NULL);
SaveFailureInformation();
// This is controversial. The argument for an abort here is that if we
// do not abort, we will suck all the life out of the user's battery.
// By saving information to the file system, above, we can still track
// metrics.
abort();
}
// If we gave up waiting for init thread, we should not delete the mutex
// out from under it.
should_delete = !gave_up_;
status = status_;
}
if (running_) {
gave_up_ = true;
status_ = OEMCrypto_ERROR_INIT_FAILED;
LOGE("XXX WATCH DOG ERROR XXX");
// HACK: this normally just returns an error. However, we are using it
// as a signal to dump debugging information.
Level3_GetOEMPublicCertificate(0, NULL, NULL);
SaveFailureInformation();
// This tells the worker thread to clean up after itself. It is not
// really needed since we are going to abort. However, if somebody
// removes the "abort()" below, then this is needed.
pthread_detach(thread_);
// This is controversial. The argument for an abort here is that if we
// do not abort, we will suck all the life out of the user's battery. By
// saving information to the file system, above, we can still track
// metrics.
abort();
}
// If we gave up waiting for init thread, we should not delete the mutex
// out from under it.
bool should_delete = !gave_up_;
OEMCryptoResult status = status_;
pthread_mutex_unlock(&mutex_);
if (should_delete) {
pthread_join(thread_, NULL);
thread_.join();
delete this;
}
return status;
@@ -528,9 +518,9 @@ class WatchDog {
private:
OEMCryptoResult status_;
pthread_t thread_;
pthread_mutex_t mutex_;
pthread_cond_t condition_;
std::thread thread_;
std::mutex mutex_;
std::condition_variable condition_;
bool running_;
bool gave_up_;
std::vector<uint8_t> sandbox_id_;
@@ -952,7 +942,7 @@ class Adapter {
}
LevelSession GetSession(OEMCrypto_SESSION session) {
wvcdm::AutoLock auto_lock(session_map_lock_);
std::unique_lock<std::mutex> auto_lock(session_map_lock_);
map_iterator pair = session_map_.find(session);
if (pair == session_map_.end()) {
return LevelSession();
@@ -974,7 +964,7 @@ class Adapter {
*session = new_session.session + kLevel3Offset;
}
if (result == OEMCrypto_SUCCESS) {
wvcdm::AutoLock auto_lock(session_map_lock_);
std::unique_lock<std::mutex> auto_lock(session_map_lock_);
// Make sure session is not already in my list of sessions.
while (session_map_.find(*session) != session_map_.end()) {
(*session)++;
@@ -986,7 +976,7 @@ class Adapter {
}
OEMCryptoResult CloseSession(OEMCrypto_SESSION session) {
wvcdm::AutoLock auto_lock(session_map_lock_);
std::unique_lock<std::mutex> auto_lock(session_map_lock_);
map_iterator pair = session_map_.find(session);
if (pair == session_map_.end()) {
return OEMCrypto_ERROR_INVALID_SESSION;
@@ -1004,7 +994,7 @@ class Adapter {
struct FunctionPointers level1_;
struct FunctionPointers level3_;
std::map<OEMCrypto_SESSION, LevelSession> session_map_;
wvcdm::Lock session_map_lock_;
std::mutex session_map_lock_;
std::vector<uint8_t> sandbox_id_;
// This is just for debugging the map between session ids.
// If we add this to the level 3 session id, then the external session

View File

@@ -161,7 +161,7 @@ CdmResponseType UsageTableHeader::AddEntry(
if (status != NO_ERROR) return status;
AutoLock auto_lock(usage_table_header_lock_);
std::unique_lock<std::mutex> auto_lock(usage_table_header_lock_);
if (*usage_entry_number < usage_entry_info_.size()) {
LOGE("UsageTableHeader::AddEntry: new entry %d smaller than table size: %d",
*usage_entry_number, usage_entry_info_.size());
@@ -198,7 +198,7 @@ CdmResponseType UsageTableHeader::LoadEntry(CryptoSession* crypto_session,
uint32_t usage_entry_number) {
{
LOGV("UsageTableHeader::LoadEntry: Lock");
AutoLock auto_lock(usage_table_header_lock_);
std::unique_lock<std::mutex> auto_lock(usage_table_header_lock_);
if (usage_entry_number >= usage_entry_info_.size()) {
LOGE(
@@ -234,7 +234,7 @@ CdmResponseType UsageTableHeader::LoadEntry(CryptoSession* crypto_session,
CdmResponseType UsageTableHeader::UpdateEntry(CryptoSession* crypto_session,
CdmUsageEntry* usage_entry) {
LOGV("UsageTableHeader::UpdateEntryL: Lock");
AutoLock auto_lock(usage_table_header_lock_);
std::unique_lock<std::mutex> auto_lock(usage_table_header_lock_);
CdmResponseType status =
crypto_session->UpdateUsageEntry(&usage_table_header_, usage_entry);
@@ -248,7 +248,7 @@ CdmResponseType UsageTableHeader::DeleteEntry(uint32_t usage_entry_number,
DeviceFiles* handle,
metrics::CryptoMetrics* metrics) {
LOGV("UsageTableHeader::DeleteEntry: Lock");
AutoLock auto_lock(usage_table_header_lock_);
std::unique_lock<std::mutex> auto_lock(usage_table_header_lock_);
if (usage_entry_number >= usage_entry_info_.size()) {
LOGE("UsageTableHeader::DeleteEntry: usage entry number %d larger than "
"usage entry size %d", usage_entry_number, usage_entry_info_.size());

View File

@@ -7,13 +7,13 @@
#include <map>
#include <memory>
#include <mutex>
#include <utils/RefBase.h>
#include "cdm_identifier.h"
#include "disallow_copy_and_assign.h"
#include "file_store.h"
#include "lock.h"
#include "metrics.pb.h"
#include "timer.h"
#include "wv_cdm_types.h"
@@ -165,14 +165,14 @@ class WvContentDecryptionModule : public android::RefBase, public TimerHandler {
void DisablePolicyTimer();
void OnTimerEvent();
static Lock session_sharing_id_generation_lock_;
Lock policy_timer_lock_;
static std::mutex session_sharing_id_generation_lock_;
std::mutex policy_timer_lock_;
Timer policy_timer_;
// instance variables
// This manages the lifetime of the CDM instances.
std::map<CdmIdentifier, CdmInfo> cdms_;
Lock cdms_lock_;
std::mutex cdms_lock_;
// This contains weak pointers to the CDM instances contained in |cdms_|.
std::map<std::string, CdmEngine*> cdm_by_session_id_;

View File

@@ -8,12 +8,12 @@
#include <cstdarg>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "attribute_handler.h"
#include "field_tuples.h"
#include "lock.h"
namespace wvcdm {
namespace metrics {
@@ -51,7 +51,7 @@ class BaseCounterMetric {
* across multiple threads preventing the caller from worrying about
* locking.
*/
Lock internal_lock_;
std::mutex internal_lock_;
};
// The CounterMetric class is used to track a counter such as the number of

View File

@@ -7,13 +7,13 @@
#include <cstdarg>
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include "OEMCryptoCENC.h"
#include "attribute_handler.h"
#include "distribution.h"
#include "lock.h"
#include "log.h"
#include "metrics.pb.h"
#include "pow2bucket.h"
@@ -50,7 +50,7 @@ class BaseEventMetric {
* This locks the internal state of the event metric to ensure safety across
* multiple threads preventing the caller from worrying about locking.
*/
Lock internal_lock_;
std::mutex internal_lock_;
};
// The EventMetric class is used to capture statistics about an event such as

View File

@@ -8,6 +8,7 @@
#include <stddef.h>
#include <stdint.h>
#include <mutex>
#include <ostream>
#include "OEMCryptoCENC.h"
@@ -313,7 +314,7 @@ class OemCryptoDynamicAdapterMetrics {
void Clear();
private:
mutable Lock adapter_lock_;
mutable std::mutex adapter_lock_;
ValueMetric<OEMCryptoInitializationMode> oemcrypto_initialization_mode_;
ValueMetric<uint32_t> oemcrypto_l1_api_version_;
ValueMetric<uint32_t> oemcrypto_l1_min_api_version_;
@@ -405,7 +406,7 @@ class EngineMetrics {
cdm_engine_unprovision_;
private:
mutable Lock session_metrics_lock_;
mutable std::mutex session_metrics_lock_;
std::vector<metrics::SessionMetrics *> session_metrics_list_;
// This is used to populate the engine lifespan metric
metrics::TimerMetric life_span_internal_;

View File

@@ -12,7 +12,7 @@ namespace metrics {
void BaseCounterMetric::Increment(const std::string &counter_key,
int64_t value) {
AutoLock lock(internal_lock_);
std::unique_lock<std::mutex> lock(internal_lock_);
if (value_map_.find(counter_key) == value_map_.end()) {
value_map_[counter_key] = value;

View File

@@ -10,7 +10,7 @@ namespace wvcdm {
namespace metrics {
BaseEventMetric::~BaseEventMetric() {
AutoLock lock(internal_lock_);
std::unique_lock<std::mutex> lock(internal_lock_);
for (std::map<std::string, Distribution *>::iterator it = value_map_.begin();
it != value_map_.end(); it++) {
@@ -19,7 +19,7 @@ BaseEventMetric::~BaseEventMetric() {
}
void BaseEventMetric::Record(const std::string &key, double value) {
AutoLock lock(internal_lock_);
std::unique_lock<std::mutex> lock(internal_lock_);
Distribution *distribution;

View File

@@ -186,23 +186,23 @@ OemCryptoDynamicAdapterMetrics::OemCryptoDynamicAdapterMetrics()
void OemCryptoDynamicAdapterMetrics::SetInitializationMode(
OEMCryptoInitializationMode mode) {
AutoLock lock(adapter_lock_);
std::unique_lock<std::mutex> lock(adapter_lock_);
oemcrypto_initialization_mode_.Record(mode);
}
void OemCryptoDynamicAdapterMetrics::SetL1ApiVersion(uint32_t version) {
AutoLock lock(adapter_lock_);
std::unique_lock<std::mutex> lock(adapter_lock_);
oemcrypto_l1_api_version_.Record(version);
}
void OemCryptoDynamicAdapterMetrics::SetL1MinApiVersion(uint32_t version) {
AutoLock lock(adapter_lock_);
std::unique_lock<std::mutex> lock(adapter_lock_);
oemcrypto_l1_min_api_version_.Record(version);
}
void OemCryptoDynamicAdapterMetrics::Serialize(
WvCdmMetrics::EngineMetrics *engine_metrics) const {
AutoLock lock(adapter_lock_);
std::unique_lock<std::mutex> lock(adapter_lock_);
engine_metrics->set_allocated_oemcrypto_initialization_mode(
oemcrypto_initialization_mode_.ToProto());
@@ -213,7 +213,7 @@ void OemCryptoDynamicAdapterMetrics::Serialize(
}
void OemCryptoDynamicAdapterMetrics::Clear() {
AutoLock lock(adapter_lock_);
std::unique_lock<std::mutex> lock(adapter_lock_);
oemcrypto_initialization_mode_.Clear();
oemcrypto_l1_api_version_.Clear();
@@ -235,7 +235,7 @@ EngineMetrics::EngineMetrics() {
}
EngineMetrics::~EngineMetrics() {
AutoLock lock(session_metrics_lock_);
std::unique_lock<std::mutex> lock(session_metrics_lock_);
std::vector<SessionMetrics *>::iterator i;
if (!session_metrics_list_.empty()) {
LOGV("EngineMetrics::~EngineMetrics. Session count: %d",
@@ -249,14 +249,14 @@ EngineMetrics::~EngineMetrics() {
}
SessionMetrics *EngineMetrics::AddSession() {
AutoLock lock(session_metrics_lock_);
std::unique_lock<std::mutex> lock(session_metrics_lock_);
SessionMetrics *metrics = new SessionMetrics();
session_metrics_list_.push_back(metrics);
return metrics;
}
void EngineMetrics::RemoveSession(wvcdm::CdmSessionId session_id) {
AutoLock lock(session_metrics_lock_);
std::unique_lock<std::mutex> lock(session_metrics_lock_);
session_metrics_list_.erase(
std::remove_if(session_metrics_list_.begin(), session_metrics_list_.end(),
CompareSessionIds(session_id)),
@@ -264,7 +264,7 @@ void EngineMetrics::RemoveSession(wvcdm::CdmSessionId session_id) {
}
void EngineMetrics::Serialize(WvCdmMetrics *wv_metrics) const {
AutoLock lock(session_metrics_lock_);
std::unique_lock<std::mutex> lock(session_metrics_lock_);
WvCdmMetrics::EngineMetrics *engine_metrics =
wv_metrics->mutable_engine_metrics();
// Serialize the most recent metrics from the OemCyrpto dynamic adapter.

View File

@@ -21,7 +21,7 @@ const int kCdmPolicyTimerDurationSeconds = 1;
namespace wvcdm {
Lock WvContentDecryptionModule::session_sharing_id_generation_lock_;
std::mutex WvContentDecryptionModule::session_sharing_id_generation_lock_;
WvContentDecryptionModule::WvContentDecryptionModule() {}
@@ -51,7 +51,7 @@ CdmResponseType WvContentDecryptionModule::OpenSession(
const CdmIdentifier& identifier, WvCdmEventListener* event_listener,
CdmSessionId* session_id) {
if (property_set && property_set->is_session_sharing_enabled()) {
AutoLock auto_lock(session_sharing_id_generation_lock_);
std::unique_lock<std::mutex> auto_lock(session_sharing_id_generation_lock_);
if (property_set->session_sharing_id() == 0)
property_set->set_session_sharing_id(GenerateSessionSharingId());
}
@@ -72,7 +72,7 @@ CdmResponseType WvContentDecryptionModule::CloseSession(
CdmEngine* cdm_engine = GetCdmForSessionId(session_id);
// TODO(rfrias): Avoid reusing the error codes from CdmEngine.
if (!cdm_engine) return SESSION_NOT_FOUND_1;
AutoLock auto_lock(cdms_lock_);
std::unique_lock<std::mutex> auto_lock(cdms_lock_);
CdmResponseType sts = cdm_engine->CloseSession(session_id);
cdm_engine->GetMetrics()->cdm_engine_close_session_.Increment(sts);
if (sts == NO_ERROR) {
@@ -366,7 +366,7 @@ CdmResponseType WvContentDecryptionModule::GetMetrics(
if (!metrics) {
return PARAMETER_NULL;
}
AutoLock auto_lock(cdms_lock_);
std::unique_lock<std::mutex> auto_lock(cdms_lock_);
auto it = cdms_.find(identifier);
if (it == cdms_.end()) {
LOGE("WVContentDecryptionModule::GetMetrics. cdm_identifier not found");
@@ -382,7 +382,7 @@ WvContentDecryptionModule::CdmInfo::CdmInfo()
CdmEngine* WvContentDecryptionModule::EnsureCdmForIdentifier(
const CdmIdentifier& identifier) {
AutoLock auto_lock(cdms_lock_);
std::unique_lock<std::mutex> auto_lock(cdms_lock_);
if (cdms_.find(identifier) == cdms_.end()) {
// Accessing the map entry will create a new instance using the default
// constructor. We then need to provide it with two pieces of info: The
@@ -411,7 +411,7 @@ CdmEngine* WvContentDecryptionModule::GetCdmForSessionId(
}
void WvContentDecryptionModule::CloseAllCdms() {
AutoLock auto_lock(cdms_lock_);
std::unique_lock<std::mutex> auto_lock(cdms_lock_);
for (auto it = cdms_.begin(); it != cdms_.end();) {
it = cdms_.erase(it);
@@ -426,7 +426,7 @@ CdmResponseType WvContentDecryptionModule::CloseCdm(
// Acquire the cdms_lock_ first, in its own scope.
bool cdms_empty = false;
{
AutoLock auto_lock(cdms_lock_);
std::unique_lock<std::mutex> auto_lock(cdms_lock_);
auto it = cdms_.find(cdm_identifier);
if (it == cdms_.end()) {
LOGE("WVContentDecryptionModule::Close. cdm_identifier not found.");
@@ -453,20 +453,20 @@ CdmResponseType WvContentDecryptionModule::CloseCdm(
}
void WvContentDecryptionModule::EnablePolicyTimer() {
AutoLock auto_lock(policy_timer_lock_);
std::unique_lock<std::mutex> auto_lock(policy_timer_lock_);
if (!policy_timer_.IsRunning())
policy_timer_.Start(this, kCdmPolicyTimerDurationSeconds);
}
void WvContentDecryptionModule::DisablePolicyTimer() {
AutoLock auto_lock(policy_timer_lock_);
std::unique_lock<std::mutex> auto_lock(policy_timer_lock_);
if (policy_timer_.IsRunning()) {
policy_timer_.Stop();
}
}
void WvContentDecryptionModule::OnTimerEvent() {
AutoLock auto_lock(cdms_lock_);
std::unique_lock<std::mutex> auto_lock(cdms_lock_);
for (auto it = cdms_.begin(); it != cdms_.end(); ++it) {
it->second.cdm_engine->OnTimerEvent();
}

View File

@@ -38,7 +38,7 @@ CryptoEngine::CryptoEngine(std::unique_ptr<wvcdm::FileSystem>&& file_system)
}
CryptoEngine::~CryptoEngine() {
wvcdm::AutoLock lock(session_table_lock_);
std::unique_lock<std::mutex> lock(session_table_lock_);
ActiveSessions::iterator it;
for (it = sessions_.begin(); it != sessions_.end(); ++it) {
delete it->second;
@@ -53,7 +53,7 @@ bool CryptoEngine::Initialize() {
}
SessionId CryptoEngine::OpenSession() {
wvcdm::AutoLock lock(session_table_lock_);
std::unique_lock<std::mutex> lock(session_table_lock_);
static OEMCrypto_SESSION unique_id = 1;
SessionId id = ++unique_id;
sessions_[id] = MakeSession(id);
@@ -68,7 +68,7 @@ UsageTable* CryptoEngine::MakeUsageTable() { return new UsageTable(this); }
bool CryptoEngine::DestroySession(SessionId sid) {
SessionContext* sctx = FindSession(sid);
wvcdm::AutoLock lock(session_table_lock_);
std::unique_lock<std::mutex> lock(session_table_lock_);
if (sctx) {
sessions_.erase(sid);
delete sctx;
@@ -79,7 +79,7 @@ bool CryptoEngine::DestroySession(SessionId sid) {
}
SessionContext* CryptoEngine::FindSession(SessionId sid) {
wvcdm::AutoLock lock(session_table_lock_);
std::unique_lock<std::mutex> lock(session_table_lock_);
ActiveSessions::iterator it = sessions_.find(sid);
if (it != sessions_.end()) {
return it->second;

View File

@@ -11,13 +11,13 @@
#include <time.h>
#include <map>
#include <memory>
#include <mutex>
#include <vector>
#include <openssl/rsa.h>
#include "OEMCryptoCENC.h"
#include "file_store.h"
#include "lock.h"
#include "oemcrypto_auth_ref.h"
#include "oemcrypto_key_ref.h"
#include "oemcrypto_rsa_key_shared.h"
@@ -201,7 +201,7 @@ class CryptoEngine {
uint8_t* destination_;
ActiveSessions sessions_;
AuthenticationRoot root_of_trust_;
wvcdm::Lock session_table_lock_;
std::mutex session_table_lock_;
std::unique_ptr<wvcdm::FileSystem> file_system_;
std::unique_ptr<UsageTable> usage_table_;

View File

@@ -166,7 +166,7 @@ OldUsageTable::OldUsageTable(CryptoEngine *ce) {
}
OldUsageTableEntry *OldUsageTable::FindEntry(const std::vector<uint8_t> &pst) {
wvcdm::AutoLock lock(lock_);
std::unique_lock<std::mutex> lock(lock_);
return FindEntryLocked(pst);
}
@@ -192,13 +192,13 @@ OldUsageTableEntry *OldUsageTable::CreateEntry(
return NULL;
}
OldUsageTableEntry *entry = new OldUsageTableEntry(this, pst_hash);
wvcdm::AutoLock lock(lock_);
std::unique_lock<std::mutex> lock(lock_);
table_[pst_hash] = entry;
return entry;
}
void OldUsageTable::Clear() {
wvcdm::AutoLock lock(lock_);
std::unique_lock<std::mutex> lock(lock_);
for (EntryMap::iterator i = table_.begin(); i != table_.end(); ++i) {
if (i->second) delete i->second;
}

View File

@@ -12,11 +12,11 @@
#include <stdint.h>
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include "OEMCryptoCENC.h"
#include "lock.h"
#include "oemcrypto_types.h"
#include "openssl/sha.h"
@@ -89,7 +89,7 @@ class OldUsageTable {
typedef std::map<std::vector<uint8_t>, OldUsageTableEntry *> EntryMap;
EntryMap table_;
wvcdm::Lock lock_;
std::mutex lock_;
int64_t generation_;
CryptoEngine *ce_;