1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 #pragma once 10 11 #include <cstddef> 12 13 #include <executorch/runtime/platform/compiler.h> 14 #include <executorch/runtime/platform/platform.h> 15 #include <executorch/runtime/platform/types.h> 16 17 /** 18 * An interface for intercepting calls to the PAL layer. 19 */ 20 class PlatformIntercept { 21 public: 22 PlatformIntercept() = default; 23 24 /// Called when et_pal_init() is called. init()25 virtual void init() {} 26 27 // We can't intercept et_pal_abort() since it's marked NORETURN. 28 29 /// Called when et_pal_current_ticks() is called. current_ticks()30 virtual et_timestamp_t current_ticks() { 31 return 0; 32 } 33 ticks_to_ns_multiplier()34 virtual et_tick_ratio_t ticks_to_ns_multiplier() { 35 return {1, 1}; 36 } 37 38 /// Called when et_pal_emit_log_message() is called. emit_log_message(ET_UNUSED et_timestamp_t timestamp,ET_UNUSED et_pal_log_level_t level,ET_UNUSED const char * filename,ET_UNUSED const char * function,ET_UNUSED size_t line,ET_UNUSED const char * message,ET_UNUSED size_t length)39 virtual void emit_log_message( 40 ET_UNUSED et_timestamp_t timestamp, 41 ET_UNUSED et_pal_log_level_t level, 42 ET_UNUSED const char* filename, 43 ET_UNUSED const char* function, 44 ET_UNUSED size_t line, 45 ET_UNUSED const char* message, 46 ET_UNUSED size_t length) {} 47 allocate(ET_UNUSED size_t size)48 virtual void* allocate(ET_UNUSED size_t size) { 49 return nullptr; 50 } 51 free(ET_UNUSED void * ptr)52 virtual void free(ET_UNUSED void* ptr) {} 53 54 virtual ~PlatformIntercept() = default; 55 }; 56 57 /** 58 * RAII type to install a PlatformIntercept for the duration of a test case. 59 */ 60 class InterceptWith { 61 public: InterceptWith(PlatformIntercept & pi)62 explicit InterceptWith(PlatformIntercept& pi) { 63 InterceptWith::install(&pi); 64 } 65 ~InterceptWith()66 ~InterceptWith() { 67 InterceptWith::install(nullptr); 68 } 69 70 private: 71 /** 72 * Installs the PlatformIntercept to forward to when et_pal_* functions are 73 * called. To uninstall, pass in `nullptr`. 74 */ 75 static void install(PlatformIntercept* pi); 76 }; 77