blob: 3fc9f48e74df32a2928a9e0386d75d9099448bd0 [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>
24#include <trusty/coverage/coverage.h>
25#include <trusty/coverage/tipc.h>
26
27using android::base::ErrnoError;
28using android::base::Error;
29using android::base::Result;
30
31/*
32 * We don't know how many counters the coverage record will contain. So, eyeball
33 * the size of this section.
34 */
35__attribute__((section("__libfuzzer_extra_counters"))) volatile uint8_t counters[PAGE_SIZE];
36
37namespace android {
38namespace trusty {
39namespace fuzz {
40
41ExtraCounters::ExtraCounters(coverage::CoverageRecord* record) : record_(record) {
42 assert(fuzzer::ExtraCountersBegin());
43 assert(fuzzer::ExtraCountersEnd());
44
45 uint8_t* begin = NULL;
46 uint8_t* end = NULL;
47 record_->GetRawData((volatile void**)&begin, (volatile void**)&end);
48 assert(end - begin <= sizeof(counters));
49}
50
51ExtraCounters::~ExtraCounters() {
52 Flush();
53}
54
55void ExtraCounters::Reset() {
56 record_->Reset();
57 fuzzer::ClearExtraCounters();
58}
59
60void ExtraCounters::Flush() {
61 volatile uint8_t* begin = NULL;
62 volatile uint8_t* end = NULL;
63
64 record_->GetRawData((volatile void**)&begin, (volatile void**)&end);
65
66 size_t num_counters = end - begin;
67 for (size_t i = 0; i < num_counters; i++) {
68 *(counters + i) = *(begin + i);
69 }
70}
71
72} // namespace fuzz
73} // namespace trusty
74} // namespace android