| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2020 The Android Open Sourete 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 | #define LOG_TAG "trusty-fuzz-counters" | 
 | 18 |  | 
 | 19 | #include <FuzzerDefs.h> | 
 | 20 |  | 
 | 21 | #include <trusty/fuzz/counters.h> | 
 | 22 |  | 
 | 23 | #include <android-base/logging.h> | 
| Stephen Crane | 6735f84 | 2021-01-07 15:04:14 -0800 | [diff] [blame] | 24 | #include <log/log.h> | 
| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 25 | #include <trusty/coverage/coverage.h> | 
 | 26 | #include <trusty/coverage/tipc.h> | 
 | 27 |  | 
 | 28 | using android::base::ErrnoError; | 
 | 29 | using android::base::Error; | 
 | 30 | using android::base::Result; | 
 | 31 |  | 
 | 32 | /* | 
 | 33 |  * We don't know how many counters the coverage record will contain. So, eyeball | 
 | 34 |  * the size of this section. | 
 | 35 |  */ | 
| Tri Vo | 8ad3865 | 2021-06-24 22:21:41 -0700 | [diff] [blame] | 36 | static const size_t kMaxNumCounters = 0x10000; | 
| Stephen Crane | 6735f84 | 2021-01-07 15:04:14 -0800 | [diff] [blame] | 37 | __attribute__((section("__libfuzzer_extra_counters"))) volatile uint8_t counters[kMaxNumCounters]; | 
| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 38 |  | 
 | 39 | namespace android { | 
 | 40 | namespace trusty { | 
 | 41 | namespace fuzz { | 
 | 42 |  | 
 | 43 | ExtraCounters::ExtraCounters(coverage::CoverageRecord* record) : record_(record) { | 
| Tri Vo | 19eccb4 | 2021-02-10 15:29:57 -0800 | [diff] [blame] | 44 |     if (!record_->IsOpen()) { | 
 | 45 |         return; | 
 | 46 |     } | 
 | 47 |  | 
| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 48 |     assert(fuzzer::ExtraCountersBegin()); | 
 | 49 |     assert(fuzzer::ExtraCountersEnd()); | 
 | 50 |  | 
| Stephen Crane | e962930 | 2020-11-16 18:00:53 -0800 | [diff] [blame] | 51 |     volatile uint8_t* begin = NULL; | 
 | 52 |     volatile uint8_t* end = NULL; | 
 | 53 |     record_->GetRawCounts(&begin, &end); | 
| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 54 |     assert(end - begin <= sizeof(counters)); | 
 | 55 | } | 
 | 56 |  | 
 | 57 | ExtraCounters::~ExtraCounters() { | 
| Tri Vo | 19eccb4 | 2021-02-10 15:29:57 -0800 | [diff] [blame] | 58 |     if (!record_->IsOpen()) { | 
 | 59 |         return; | 
 | 60 |     } | 
 | 61 |  | 
| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 62 |     Flush(); | 
 | 63 | } | 
 | 64 |  | 
 | 65 | void ExtraCounters::Reset() { | 
| Tri Vo | 19eccb4 | 2021-02-10 15:29:57 -0800 | [diff] [blame] | 66 |     if (!record_->IsOpen()) { | 
 | 67 |         return; | 
 | 68 |     } | 
 | 69 |  | 
| Stephen Crane | e962930 | 2020-11-16 18:00:53 -0800 | [diff] [blame] | 70 |     record_->ResetCounts(); | 
| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 71 |     fuzzer::ClearExtraCounters(); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | void ExtraCounters::Flush() { | 
 | 75 |     volatile uint8_t* begin = NULL; | 
 | 76 |     volatile uint8_t* end = NULL; | 
 | 77 |  | 
| Stephen Crane | e962930 | 2020-11-16 18:00:53 -0800 | [diff] [blame] | 78 |     record_->GetRawCounts(&begin, &end); | 
| Stephen Crane | 6735f84 | 2021-01-07 15:04:14 -0800 | [diff] [blame] | 79 |     if (!begin || !end) { | 
 | 80 |         ALOGE("Could not get raw counts from coverage record\n"); | 
 | 81 |         return; | 
 | 82 |     } | 
| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 83 |  | 
 | 84 |     size_t num_counters = end - begin; | 
| Stephen Crane | 6735f84 | 2021-01-07 15:04:14 -0800 | [diff] [blame] | 85 |     if (num_counters > kMaxNumCounters) { | 
 | 86 |         ALOGE("Too many counters (%zu) to fit in the extra counters section!\n", num_counters); | 
 | 87 |         num_counters = kMaxNumCounters; | 
 | 88 |     } | 
| Tri Vo | a67840f | 2020-11-01 13:03:29 -0800 | [diff] [blame] | 89 |     for (size_t i = 0; i < num_counters; i++) { | 
 | 90 |         *(counters + i) = *(begin + i); | 
 | 91 |     } | 
 | 92 | } | 
 | 93 |  | 
 | 94 | }  // namespace fuzz | 
 | 95 | }  // namespace trusty | 
 | 96 | }  // namespace android |