xref: /aosp_15_r20/external/executorch/runtime/platform/test/stub_platform.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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 #include <executorch/runtime/platform/test/stub_platform.h>
10 
11 #include <chrono>
12 #include <cinttypes>
13 #include <cstdio>
14 #include <cstdlib>
15 
16 #include <executorch/runtime/platform/compiler.h>
17 #include <executorch/runtime/platform/platform.h>
18 
19 /**
20  * @file
21  * An implementation of the PAL to help with testing.
22  */
23 
24 static PlatformIntercept* platform_intercept;
25 
install(PlatformIntercept * pi)26 void InterceptWith::install(PlatformIntercept* pi) {
27   platform_intercept = pi;
28 }
29 
30 /// Prints a message and aborts if an intercept is not installed.
31 #define ASSERT_INTERCEPT_INSTALLED()                                 \
32   ({                                                                 \
33     if (platform_intercept == nullptr) {                             \
34       fprintf(stderr, "%s call was not intercepted\n", ET_FUNCTION); \
35       fflush(stderr);                                                \
36       __builtin_trap();                                              \
37     }                                                                \
38   })
39 
40 extern "C" {
41 
et_pal_init(void)42 void et_pal_init(void) {
43   ASSERT_INTERCEPT_INSTALLED();
44   platform_intercept->init();
45 }
46 
et_pal_abort(void)47 ET_NORETURN void et_pal_abort(void) {
48   ASSERT_INTERCEPT_INSTALLED();
49   // We can't properly intercept this since it's marked NORETURN.
50   fprintf(stderr, "StubPlatform et_pal_abort called\n");
51   fflush(stderr);
52   __builtin_trap();
53 }
54 
et_pal_current_ticks(void)55 et_timestamp_t et_pal_current_ticks(void) {
56   ASSERT_INTERCEPT_INSTALLED();
57   return platform_intercept->current_ticks();
58 }
59 
et_pal_ticks_to_ns_multiplier(void)60 et_tick_ratio_t et_pal_ticks_to_ns_multiplier(void) {
61   ASSERT_INTERCEPT_INSTALLED();
62   return platform_intercept->ticks_to_ns_multiplier();
63 }
64 
et_pal_emit_log_message(et_timestamp_t timestamp,et_pal_log_level_t level,const char * filename,ET_UNUSED const char * function,size_t line,const char * message,ET_UNUSED size_t length)65 void et_pal_emit_log_message(
66     et_timestamp_t timestamp,
67     et_pal_log_level_t level,
68     const char* filename,
69     ET_UNUSED const char* function,
70     size_t line,
71     const char* message,
72     ET_UNUSED size_t length) {
73   ASSERT_INTERCEPT_INSTALLED();
74   platform_intercept->emit_log_message(
75       timestamp, level, filename, function, line, message, length);
76 }
77 
et_pal_allocate(size_t size)78 void* et_pal_allocate(size_t size) {
79   ASSERT_INTERCEPT_INSTALLED();
80   return platform_intercept->allocate(size);
81 }
82 
et_pal_free(void * ptr)83 void et_pal_free(void* ptr) {
84   ASSERT_INTERCEPT_INSTALLED();
85   platform_intercept->free(ptr);
86 }
87 
88 } // extern "C"
89 
90 #include <gtest/gtest.h>
91 
92 // Use a version of main() that does not call runtime_init().
93 //
94 // By default, executorch tests are built with a main() that calls
95 // runtime_init(), and ultimately et_pal_init(). The StubPlatform override of
96 // et_pal_init() will fail if it isn't intercepted, so we can't call it at start
97 // time.
main(int argc,char ** argv)98 int main(int argc, char** argv) {
99   ::testing::InitGoogleTest(&argc, argv);
100   // Purposefully do not initialize the PAL.
101   return RUN_ALL_TESTS();
102 }
103