From 2f0a708a8b1ce60da9ccb8edf0a26dce45e357ba Mon Sep 17 00:00:00 2001 From: Abhinav Anil Sharma Date: Sun, 15 Dec 2024 23:49:50 -0500 Subject: [PATCH] Remove stale file --- .../tools/common/instr_decode_cache.h | 363 ------------------ 1 file changed, 363 deletions(-) delete mode 100644 clients/drcachesim/tools/common/instr_decode_cache.h diff --git a/clients/drcachesim/tools/common/instr_decode_cache.h b/clients/drcachesim/tools/common/instr_decode_cache.h deleted file mode 100644 index 9f9cadcc4a8..00000000000 --- a/clients/drcachesim/tools/common/instr_decode_cache.h +++ /dev/null @@ -1,363 +0,0 @@ -/* ********************************************************** - * Copyright (c) 2024 Google, Inc. All rights reserved. - * **********************************************************/ - -/* - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * * Neither the name of Google, Inc. nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - */ - -/** - * decode_cache.h: Library that supports caching of instruction decode - * information. - */ - -#ifndef _DECODE_CACHE_H_ -#define _DECODE_CACHE_H_ 1 - -#include "dr_api.h" -#include "memref.h" -#include "tracer/raw2trace_shared.h" - -#include -#include -#include - -namespace dynamorio { -namespace drmemtrace { - -/** - * Base class for storing instruction decode info. Users should sub-class this - * base class and implement set_decode_info_derived() to derive and store the decode - * info they need. - */ -class decode_info_base_t { -public: - /** - * Sets the decode info for the provided \p instr which was allocated using the - * provided \p dcontext for the provided \p memref_instr. This is done using - * the set_decode_info_derived() provided by the derived class. Additionally, - * this does other required bookkeeping. - */ - void - set_decode_info(void *dcontext, - const dynamorio::drmemtrace::_memref_instr_t &memref_instr, - instr_t *instr) - { - set_decode_info_derived(dcontext, memref_instr, instr); - is_valid_ = true; - } - /** - * Indicates whether the decode info stored in this object is valid. It won't be - * valid if the object is default-constructed without a subsequent - * set_decode_info() call. When used with \p decode_cache_t, this indicates - * that an invalid instruction was observed at some pc. - */ - bool - is_valid() const - { - return is_valid_; - } - -private: - /** - * Sets the decoding info fields as required by the derived class, based on the - * provided instr_t which was allocated using the provided opaque \p dcontext - * for the provided \p memref_instr. Derived classes must implement this virtual - * function. Note that this cannot be invoked directly as it is private, but - * only through set_decode_info() which does other required bookkeeping. - * - * This is meant for use with \p decode_cache_t, which will invoke - * set_decode_info() for each new decoded instruction. - * - * The responsibility for invoking instr_destroy() on the provided \p instr - * lies with this \p decode_info_base_t object, unless - * \p decode_cache_t was constructed with \p persist_decoded_instrs_ - * set to false, in which case no heap allocation takes place. - */ - virtual void - set_decode_info_derived(void *dcontext, - const dynamorio::drmemtrace::_memref_instr_t &memref_instr, - instr_t *instr) = 0; - - bool is_valid_ = false; -}; - -/** - * Decode info including the full decoded instr_t. This should be used with an - * \p decode_cache_t constructed with \p persist_decoded_instrs_ set to - * true. - */ -class instr_decode_info_t : public decode_info_base_t { -public: - ~instr_decode_info_t(); - instr_t * - get_decoded_instr(); - -private: - void - set_decode_info_derived(void *dcontext, - const dynamorio::drmemtrace::_memref_instr_t &memref_instr, - instr_t *instr) override; - - instr_t *instr_ = nullptr; - void *dcontext_ = nullptr; -}; - -/** - * Base class for \p decode_cache_t. - * - * This is used to allow sharing the static data members among all template instances - * of \p decode_cache_t. - */ -class decode_cache_base_t { -protected: - /** - * Constructor for the base class, intentionally declared as protected so - * \p decode_cache_base_t cannot be instantiated directly but only via - * a derived class. - */ - decode_cache_base_t() = default; - - // XXX: Maybe the ownership and destruction responsibility for the modfile bytes - // should be given to module_mapper_t instead. - struct modfile_bytes_t { - char *bytes = nullptr; - ~modfile_bytes_t() - { - if (bytes != nullptr) { - delete[] bytes; - } - } - }; - - static std::mutex module_mapper_mutex_; - static std::unique_ptr module_mapper_; - static modfile_bytes_t modfile_bytes_; - -public: - // Non-static to allow test sub-classes to override. - virtual std::string - init_module_mapper(const std::string &module_file_path, - const std::string &alt_module_dir) - { - std::lock_guard guard(module_mapper_mutex_); - if (module_mapper_ != nullptr) { - // We want only a single module_mapper_t instance to be - // initialized that is shared among all instances of - // decode_cache_base_t. - return ""; - } - // Legacy trace support where binaries are needed. - // We do not support non-module code for such traces. - file_t modfile; - std::string error = - read_module_file(module_file_path, modfile, modfile_bytes_.bytes); - if (!error.empty()) { - return "Failed to read module file: " + error; - } - dr_close_file(modfile); - module_mapper_ = - module_mapper_t::create(modfile_bytes_.bytes, nullptr, nullptr, nullptr, - nullptr, /*verbose=*/0, alt_module_dir); - module_mapper_->get_loaded_modules(); - error = module_mapper_->get_last_error(); - if (!error.empty()) - return "Failed to load binaries: " + error; - return ""; - } - - static std::string - find_mapped_trace_address(app_pc trace_pc, app_pc &decode_pc) - { - std::lock_guard guard(module_mapper_mutex_); - decode_pc = module_mapper_->find_mapped_trace_address(trace_pc); - if (!module_mapper_->get_last_error().empty()) { - return "Failed to find mapped address for " + to_hex_string(trace_pc) + ": " + - module_mapper_->get_last_error(); - } - return ""; - } -}; - -/** - * A cache to store decode info for instructions per observed app pc. The template arg - * DecodeInfo is a class derived from \p decode_info_base_t which implements the - * set_decode_info_derived() function that derives the required decode info from an - * \p instr_t object when invoked by \p decode_cache_t. This class handles the - * heavylifting of actually producing the decoded \p instr_t. The decoded \p instr_t - * may be made to persist beyond the set_decode_info() calls by constructing the - * \p decode_cache_t object with \p persist_decoded_instrs_ set to true. - * - * This should be used only with traces that have \p OFFLINE_FILE_TYPE_ENCODINGS set - * in their \p TRACE_MARKER_TYPE_FILETYPE marker, as only those traces have instr - * encodings embedded in them. - */ -template -class decode_cache_t : public decode_cache_base_t { - static_assert(std::is_base_of::value, - "DecodeInfo not derived from decode_info_base_t"); - -public: - decode_cache_t(void *dcontext, bool persist_decoded_instrs) - : dcontext_(dcontext) - , persist_decoded_instrs_(persist_decoded_instrs) {}; - virtual ~decode_cache_t() - { - } - - /** - * Returns a pointer to the DecodeInfo available for the instruction at \p pc. - * Returns nullptr if no instruction is known at that \p pc. Returns the - * default-constructed DecodeInfo if there was a decoding error for the - * instruction. - */ - DecodeInfo * - get_decode_info(app_pc pc) - { - auto it = decode_cache_.find(pc); - if (it == decode_cache_.end()) { - return nullptr; - } - return &it->second; - } - /** - * Adds decode info for the given \p memref_instr if it is not yet recorded. - */ - std::string - add_decode_info(const dynamorio::drmemtrace::_memref_instr_t &memref_instr, - DecodeInfo *&decode_info) - { - const app_pc trace_pc = reinterpret_cast(memref_instr.addr); - if (memref_instr.encoding_is_new) { - decode_cache_.erase(trace_pc); - } - auto it = decode_cache_.find(trace_pc); - if (it != decode_cache_.end()) { - decode_info = &it->second; - return ""; - } - decode_cache_[trace_pc] = DecodeInfo(); - decode_info = &decode_cache_[trace_pc]; - instr_t *instr = nullptr; - instr_noalloc_t noalloc; - if (persist_decoded_instrs_) { - instr = instr_create(dcontext_); - } else { - instr_noalloc_init(dcontext_, &noalloc); - instr = instr_from_noalloc(&noalloc); - } - - app_pc decode_pc; - if (!use_module_mapper_) { - decode_pc = const_cast(memref_instr.encoding); - } else { - // Legacy trace support where we need the binaries. - std::string err = find_mapped_trace_address(trace_pc, decode_pc); - if (err != "") - return err; - } - app_pc next_pc = decode_from_copy(dcontext_, decode_pc, trace_pc, instr); - if (next_pc == nullptr || !instr_valid(instr)) { - if (persist_decoded_instrs_) { - instr_destroy(dcontext_, instr); - } - return "decode_from_copy failed"; - } - decode_cache_[trace_pc].set_decode_info(dcontext_, memref_instr, instr); - return ""; - } - // TODO: maybe a good idea to get filetype here. Or alternatively the - // encoding_is_new check. - std::string - use_module_mapper(const std::string &module_file_path, - const std::string &alt_module_dir) - { - use_module_mapper_ = true; - return init_module_mapper(module_file_path, alt_module_dir); - } - -private: - std::unordered_map decode_cache_; - void *dcontext_ = nullptr; - bool persist_decoded_instrs_ = false; - // use_module_mapper_ describes whether we lookup the instr encodings - // from the module map, or alternatively from embedded-encodings in the - // trace. - // - // Note that we store our instr encoding lookup strategy as a non-static - // data member, unlike module_mapper_t which is static and shared between - // all decode_cache_t instances (even of different template types). - // Some analysis tools may deliberately want to look at instr encodings - // from the module mappings, but that strategy does not provide JIT - // encodings which are present only as embedded-encodings in the trace. - // In such a case, other concurrently running analysis tools should still - // be able to see encodings for JIT code. - bool use_module_mapper_ = false; -}; - -/** - * An \p decode_cache_t for testing which uses a \p test_module_mapper_t. - */ -template -class test_decode_cache_t : public decode_cache_t { -public: - using decode_cache_base_t::module_mapper_; - - // The ilist arg is required only for testing the module_mapper_t - // decoding strategy. - test_decode_cache_t(void *dcontext, bool persist_decoded_instrs, - instrlist_t *ilist = nullptr) - : decode_cache_t(dcontext, persist_decoded_instrs) - , dcontext_(dcontext) - , ilist_(ilist) - { - } - - std::string - init_module_mapper(const std::string &unused_module_file_path, - const std::string &unused_alt_module_dir) override - { - if (ilist_ == nullptr) - return "No ilist to init test_module_mapper_t"; - module_mapper_ = - std::unique_ptr(new test_module_mapper_t(ilist_, dcontext_)); - module_mapper_->get_loaded_modules(); - std::string error = module_mapper_->get_last_error(); - if (!error.empty()) - return "Failed to load binaries: " + error; - return ""; - } - -private: - void *dcontext_; - instrlist_t *ilist_; -}; - -} // namespace drmemtrace -} // namespace dynamorio - -#endif /* _INSTR_DECODE_CACHE_H_ */