blob: 1e863acafd6061328cf30c7996be8d44d7a18259 [file] [log] [blame]
Tri Voa67840f2020-11-01 13:03:29 -08001/*
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 Crane6735f842021-01-07 15:04:14 -080024#include <log/log.h>
Tri Voa67840f2020-11-01 13:03:29 -080025#include <trusty/coverage/coverage.h>
26#include <trusty/coverage/tipc.h>
27
28using android::base::ErrnoError;
29using android::base::Error;
30using 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 */
Stephen Crane6735f842021-01-07 15:04:14 -080036static const size_t kMaxNumCounters = 0x4000;
37__attribute__((section("__libfuzzer_extra_counters"))) volatile uint8_t counters[kMaxNumCounters];
Tri Voa67840f2020-11-01 13:03:29 -080038
39namespace android {
40namespace trusty {
41namespace fuzz {
42
43ExtraCounters::ExtraCounters(coverage::CoverageRecord* record) : record_(record) {
44 assert(fuzzer::ExtraCountersBegin());
45 assert(fuzzer::ExtraCountersEnd());
46
Stephen Cranee9629302020-11-16 18:00:53 -080047 volatile uint8_t* begin = NULL;
48 volatile uint8_t* end = NULL;
49 record_->GetRawCounts(&begin, &end);
Tri Voa67840f2020-11-01 13:03:29 -080050 assert(end - begin <= sizeof(counters));
51}
52
53ExtraCounters::~ExtraCounters() {
54 Flush();
55}
56
57void ExtraCounters::Reset() {
Stephen Cranee9629302020-11-16 18:00:53 -080058 record_->ResetCounts();
Tri Voa67840f2020-11-01 13:03:29 -080059 fuzzer::ClearExtraCounters();
60}
61
62void ExtraCounters::Flush() {
63 volatile uint8_t* begin = NULL;
64 volatile uint8_t* end = NULL;
65
Stephen Cranee9629302020-11-16 18:00:53 -080066 record_->GetRawCounts(&begin, &end);
Stephen Crane6735f842021-01-07 15:04:14 -080067 if (!begin || !end) {
68 ALOGE("Could not get raw counts from coverage record\n");
69 return;
70 }
Tri Voa67840f2020-11-01 13:03:29 -080071
72 size_t num_counters = end - begin;
Stephen Crane6735f842021-01-07 15:04:14 -080073 if (num_counters > kMaxNumCounters) {
74 ALOGE("Too many counters (%zu) to fit in the extra counters section!\n", num_counters);
75 num_counters = kMaxNumCounters;
76 }
Tri Voa67840f2020-11-01 13:03:29 -080077 for (size_t i = 0; i < num_counters; i++) {
78 *(counters + i) = *(begin + i);
79 }
80}
81
82} // namespace fuzz
83} // namespace trusty
84} // namespace android