1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #include "ChecksumCalculatorThreadInfo.h"
18 
19 #include "host-common/crash_reporter.h"
20 
21 #include <atomic>
22 #include <string>
23 
24 #include <assert.h>
25 #include <stdio.h>
26 
27 namespace {
28 
29 #if TRACE_CHECKSUMHELPER
30 std::atomic<size_t> sNumInstances(0);
31 #endif  // TRACE_CHECKSUMHELPER
32 }
33 
getChecksumCalculatorThreadInfo()34 static ChecksumCalculatorThreadInfo* getChecksumCalculatorThreadInfo() {
35     static thread_local ChecksumCalculatorThreadInfo* tls = new ChecksumCalculatorThreadInfo;
36     return tls;
37 }
38 
ChecksumCalculatorThreadInfo()39 ChecksumCalculatorThreadInfo::ChecksumCalculatorThreadInfo() {
40     LOG_CHECKSUMHELPER("%s: Checksum thread created (%u instances)\n",
41                        __FUNCTION__, (unsigned int)(++sNumInstances));
42 }
43 
~ChecksumCalculatorThreadInfo()44 ChecksumCalculatorThreadInfo::~ChecksumCalculatorThreadInfo() {
45     LOG_CHECKSUMHELPER("%s: GLprotocol destroyed (%u instances)\n",
46                        __FUNCTION__, (unsigned int)(--sNumInstances));
47 }
48 
get()49 ChecksumCalculator& ChecksumCalculatorThreadInfo::get() {
50     return getChecksumCalculatorThreadInfo()->m_protocol;
51 }
52 
setVersion(uint32_t version)53 bool ChecksumCalculatorThreadInfo::setVersion(uint32_t version) {
54     return getChecksumCalculatorThreadInfo()->m_protocol.setVersion(version);
55 }
56 
writeChecksum(ChecksumCalculator * calc,void * buf,size_t bufLen,void * outputChecksum,size_t outputChecksumLen)57 bool ChecksumCalculatorThreadInfo::writeChecksum(ChecksumCalculator* calc,
58                                                  void* buf,
59                                                  size_t bufLen,
60                                                  void* outputChecksum,
61                                                  size_t outputChecksumLen) {
62     calc->addBuffer(buf, bufLen);
63     return calc->writeChecksum(outputChecksum, outputChecksumLen);
64 }
65 
validate(ChecksumCalculator * calc,void * buf,size_t bufLen,void * checksum,size_t checksumLen)66 bool ChecksumCalculatorThreadInfo::validate(ChecksumCalculator* calc,
67                                             void* buf,
68                                             size_t bufLen,
69                                             void* checksum,
70                                             size_t checksumLen) {
71     calc->addBuffer(buf, bufLen);
72     return calc->validate(checksum, checksumLen);
73 }
74 
validOrDie(ChecksumCalculator * calc,void * buf,size_t bufLen,void * checksum,size_t checksumLen,const char * message)75 void ChecksumCalculatorThreadInfo::validOrDie(ChecksumCalculator* calc,
76                                               void* buf,
77                                               size_t bufLen,
78                                               void* checksum,
79                                               size_t checksumLen,
80                                               const char* message) {
81     if (!validate(calc, buf, bufLen, checksum, checksumLen)) {
82         emugl::emugl_crash_reporter(message);
83     }
84 }
85