Coverage for /Syzygy/agent/asan/reporters/exported_function.h

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
100.0%33330.C++source

Line-by-line coverage:

   1    :  // Copyright 2016 Google Inc. All Rights Reserved.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  
  15    :  #ifndef SYZYGY_AGENT_ASAN_REPORTERS_EXPORTED_FUNCTION_H_
  16    :  #define SYZYGY_AGENT_ASAN_REPORTERS_EXPORTED_FUNCTION_H_
  17    :  
  18    :  #include "base/callback.h"
  19    :  #include "base/logging.h"
  20    :  
  21    :  namespace agent {
  22    :  namespace asan {
  23    :  namespace reporters {
  24    :  
  25    :  // A templated class representing an exported funtion with a given name and
  26    :  // signature. The instantiator must defined the static |name_| variable.
  27    :  // Instances of these are used for injecting callbacks to be used instead of
  28    :  // exported functions, allowing testing of a reporter that depends on exported
  29    :  // functions. See ExportedFunction<> below for instantiation details.
  30    :  //
  31    :  // There might be multiple export functions with the same signature, but
  32    :  // different names. In this case the user should provide a distinct ID for the
  33    :  // type definition to ensure they don't collide.
  34    :  //
  35    :  // @tparam I The unique integer ID of the function.
  36    :  // @tparam F The function type.
  37    :  // @tparam R The function return type.
  38    :  // @tparam A The function argument types.
  39    :  template<int I, typename F, typename R, typename... A>
  40    :  class ExportedFunctionImpl {
  41    :   public:
  42    :    using Type = F*;
  43    :    using CallbackType = base::Callback<F>;
  44    :  
  45  E :    ExportedFunctionImpl() : function_(nullptr) {}
  46    :  
  47    :    // Constructor with an explicit function pointer.
  48    :    explicit ExportedFunctionImpl(Type* function)
  49    :        : function_(function) {
  50    :    }
  51    :  
  52    :    // Constructor with an explicit callback.
  53    :    explicit ExportedFunctionImpl(const CallbackType& callback)
  54    :        : callback_(callback) {
  55    :    }
  56    :  
  57    :    // Implicit copy constructor.
  58    :    ExportedFunctionImpl(const ExportedFunctionImpl& rhs)  // NOLINT
  59  E :        : function_(rhs.function_), callback_(rhs.callback_) {
  60  E :    }
  61    :  
  62    :    // Assignment operator.
  63    :    ExportedFunctionImpl& operator=(const ExportedFunctionImpl& rhs) {
  64    :      function_ = rhs.function_;
  65    :      callback_ = rhs.callback_;
  66    :      return *this;
  67    :    }
  68    :  
  69    :    // @returns the name of the export.
  70  E :    static const char* name() { return name_; }
  71    :  
  72    :    // Looks up the export, sets the function pointer and clears any callback.
  73    :    // @returns true if the export was found, false otherwise.
  74  E :    bool Lookup() {
  75  E :      callback_.Reset();
  76  E :      HMODULE exe_hmodule = ::GetModuleHandle(NULL);
  77  E :      function_ = reinterpret_cast<Type>(
  78    :          ::GetProcAddress(exe_hmodule, name()));
  79  E :      return function_ != nullptr;
  80  E :    }
  81    :  
  82    :    // Explicitly sets the function. Clears the callback.
  83    :    // @param function The function pointer to be set.
  84  E :    void set_function(Type function) {
  85  E :      callback_.Reset();
  86  E :      function_ = function;
  87  E :    }
  88    :  
  89    :    // Explicitly sets the callback. Clears the function pointer.
  90    :    // @param callback The callback to be set.
  91  E :    void set_callback(const CallbackType& callback) {
  92  E :      function_ = nullptr;
  93  E :      callback_ = callback;
  94  E :    }
  95    :  
  96    :    // Clears this function.
  97  E :    void Reset() {
  98  E :      function_ = nullptr;
  99  E :      callback_.Reset();
 100  E :    }
 101    :  
 102  E :    bool IsValid() const {
 103  E :      return function_ != nullptr || !callback_.is_null();
 104  E :    }
 105    :  
 106    :    // Invokes the configured function or callback.
 107  E :    R Run(A... args) {
 108  E :      DCHECK(IsValid());
 109  E :      if (function_ != nullptr)
 110  E :        return (*function_)(args...);
 111  E :      return callback_.Run(args...);
 112  E :    }
 113    :  
 114    :    // Accessor for the underlying function.
 115  E :    Type function() const { return function_; }
 116  E :    CallbackType callback() const { return callback_; }
 117    :  
 118    :   private:
 119    :    // The name of the export.
 120    :    static const char* name_;
 121    :  
 122    :    // The function itself.
 123    :    Type function_;
 124    :  
 125    :    // An equivalent callback.
 126    :    CallbackType callback_;
 127    :  };
 128    :  
 129    :  // Template magic for cleaner instantiation of ExportedFunction.
 130    :  template<typename FunctionType, int TypeId = 0> class ExportedFunction;
 131    :  template<typename R, typename... A>
 132    :  class ExportedFunction<R __cdecl(A...), 0> : public ExportedFunctionImpl<
 133    :      0, R __cdecl(A...), R, A...> {};
 134    :  template<int I, typename R, typename... A>
 135    :  class ExportedFunction<R __cdecl(A...), I> : public ExportedFunctionImpl<
 136    :      I, R __cdecl(A...), R, A...> {};
 137    :  
 138    :  }  // namespace reporters
 139    :  }  // namespace asan
 140    :  }  // namespace agent
 141    :  
 142    :  #endif  // SYZYGY_AGENT_ASAN_REPORTERS_EXPORTED_FUNCTION_H_

Coverage information generated Fri Jul 29 11:00:21 2016.