/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef SRC_PROTOZERO_FILTERING_MESSAGE_FILTER_H_ #define SRC_PROTOZERO_FILTERING_MESSAGE_FILTER_H_ #include <stdint.h> #include <memory> #include <string> #include <unordered_map> #include "src/protozero/filtering/filter_bytecode_parser.h" #include "src/protozero/filtering/message_tokenizer.h" #include "src/protozero/filtering/string_filter.h" namespace protozero { // A class to filter binary-encoded proto messages using an allow-list of field // ids, also known as "filter bytecode". The filter determines which fields are // allowed to be passed through in output and strips all the other fields. // See go/trace-filtering for full design. // This class takes in input: // 1) The filter bytecode, loaded once via the LoadFilterBytecode() method. // 2) A proto-encoded binary message. The message doesn't have to be contiguous, // it can be passed as an array of arbitrarily chunked fragments. // The FilterMessage*() method returns in output a proto message, stripping out // all unknown fields. If the input is malformed (e.g., unknown proto field wire // types, lengths out of bound) the whole filtering failed and the |error| flag // of the FilteredMessage object is set to true. // The filtering operation is based on rewriting a copy of the message into a // self-allocated buffer, which is then returned in the output. The input buffer // is NOT altered. // Note also that the process of rewriting the protos gets rid of most redundant // varint encoding (if present). So even if all fields are allow-listed, the // output might NOT be bitwise identical to the input (but it will be // semantically equivalent). // Furthermore the enable_field_usage_tracking() method allows to keep track of // a histogram of allowed / denied fields. It slows down filtering and is // intended only on host tools. class MessageFilter { … }; } // namespace protozero #endif // SRC_PROTOZERO_FILTERING_MESSAGE_FILTER_H_