• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <optional>
20 #include <string>
21 
22 #include <android-base/result.h>
23 #include <android-base/unique_fd.h>
24 #include <stdint.h>
25 #include <trusty/coverage/tipc.h>
26 
27 namespace android {
28 namespace trusty {
29 namespace coverage {
30 
31 using android::base::Result;
32 using android::base::unique_fd;
33 
34 class CoverageRecord {
35   public:
36     /**
37      * Create a coverage record interface. Coverage will not be written to a
38      * sancov output file on completion.
39      */
40     CoverageRecord(std::string tipc_dev, struct uuid* uuid);
41 
42     /**
43      * Create a coverage record interface. On destruction, write this coverage
44      * to the given sancov filename.
45      */
46     CoverageRecord(std::string tipc_dev, struct uuid* uuid, std::string module_name);
47 
48     ~CoverageRecord();
49     Result<void> Open();
50     bool IsOpen();
51     void ResetFullRecord();
52     void ResetCounts();
53     void ResetPCs();
54     void GetRawData(volatile void** begin, volatile void** end);
55     void GetRawCounts(volatile uint8_t** begin, volatile uint8_t** end);
56     void GetRawPCs(volatile uintptr_t** begin, volatile uintptr_t** end);
57     uint64_t TotalEdgeCounts();
58 
59     /**
60      * Save the current set of observed PCs to the given filename.
61      * The resulting .sancov file can be parsed via the LLVM sancov tool to see
62      * coverage statistics and visualize coverage.
63      */
64     Result<void> SaveSancovFile(const std::string& filename);
65 
66   private:
67     Result<void> Rpc(coverage_client_req* req, int req_fd, coverage_client_resp* resp);
68 
69     Result<std::pair<size_t, size_t>> GetRegionBounds(uint32_t region_type);
70 
71     std::string tipc_dev_;
72     unique_fd coverage_srv_fd_;
73     struct uuid uuid_;
74     std::optional<std::string> sancov_filename_;
75     size_t record_len_;
76     volatile void* shm_;
77     size_t shm_len_;
78 };
79 
80 }  // namespace coverage
81 }  // namespace trusty
82 }  // namespace android
83