1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /**
18 * @file stdlib_wrapper.cc
19 *
20 * This file provides nanoapps with wrapper functions for standard library
21 * functions in the standard library functions that are not supported alongside
22 * CHRE, which either redirect the function to an analogous CHRE function, or
23 * fail silently. The targeted usage of this is expected to be only for third-
24 * party or generated functions, where changes to either the upstream third-
25 * party code or the code generators might not be straightforward. It is
26 * expected that the nanoapp developers are aware of the 'fail silently' clause
27 * in the wrappers, and handle those cases appropriately.
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <cinttypes>
33 #include <cstdlib>
34 #include <new>
35
36 #include "chre/util/nanoapp/assert.h"
37
38 #include "chre_api/chre.h"
39
40 #if defined(stderr) && !defined(_CSTD)
41 // Provides a definition for stderr when the macro has been defined, but the
42 // file has been externed. Some platforms might define their own macros for
43 // stderr (vs the glibc style of matching the macro to the filename), in which
44 // case we have to guard against those definitions as well (eg: _CSTD check
45 // above).
46 FILE *stderr = NULL;
47 #endif
48
malloc(size_t size)49 void *malloc(size_t size) {
50 // On platforms where size(size_t) might be 8 bytes, we need a cast to
51 // maintain adherence to CHRE's heap alloc API. The size check to reject
52 // requests of size > 4Gb could be used for debugging, though any requests
53 // that even remotely approach this limit is bound to fail anyway.
54 return size > UINT32_MAX ? nullptr
55 : chreHeapAlloc(static_cast<uint32_t>(size));
56 }
57
free(void * ptr)58 void free(void *ptr) {
59 chreHeapFree(ptr);
60 }
61
realloc(void *,size_t)62 void *realloc(void * /*ptr*/, size_t /*newSize*/) {
63 // realloc() is not supported, verify that there's no call to it!
64 CHRE_ASSERT(false);
65 return NULL;
66 }
67
exit(int exitCode)68 void exit(int exitCode) {
69 chreAbort(static_cast<uint32_t>(exitCode));
70 // Add an explicit forever-loop to bypass compilation warnings on platforms
71 // that might have defined exit with a noreturn tag. The loop shouldn't ever
72 // execute, since abort terminates the program.
73 while (42)
74 ;
75 }
76
abort(void)77 void abort(void) {
78 exit(CHRE_ERROR);
79 }
80
fprintf(FILE *,const char *,...)81 int fprintf(FILE * /*stream*/, const char * /*fmt*/, ...) {
82 return 0;
83 }
84
fwrite(const void *,size_t,size_t,FILE *)85 size_t fwrite(const void * /*ptr*/, size_t /*size*/, size_t /*count*/,
86 FILE * /*stream*/) {
87 return 0;
88 }
89
operator delete(void *)90 void operator delete(void * /*ptr*/) {
91 CHRE_ASSERT(false);
92 }
93
operator delete(void *,std::size_t)94 void operator delete(void * /*ptr*/, std::size_t /*sz*/) {
95 CHRE_ASSERT(false);
96 }
97
operator delete(void *,std::align_val_t)98 void operator delete(void * /*ptr*/, std::align_val_t /*al*/) {
99 CHRE_ASSERT(false);
100 }
101
operator delete(void *,std::size_t,std::align_val_t)102 void operator delete(void * /*ptr*/, std::size_t /*sz*/,
103 std::align_val_t /*al*/) {
104 CHRE_ASSERT(false);
105 }
106
operator delete[](void *)107 void operator delete[](void * /*ptr*/) {
108 CHRE_ASSERT(false);
109 }
110
operator delete[](void *,std::size_t)111 void operator delete[](void * /*ptr*/, std::size_t /*sz*/) {
112 CHRE_ASSERT(false);
113 }
114
operator delete[](void *,std::align_val_t)115 void operator delete[](void * /*ptr*/, std::align_val_t /*al*/) {
116 CHRE_ASSERT(false);
117 }
118
operator delete[](void *,std::size_t,std::align_val_t)119 void operator delete[](void * /*ptr*/, std::size_t /*sz*/,
120 std::align_val_t /*al*/) {
121 CHRE_ASSERT(false);
122 }
123
operator new[](std::size_t)124 void *operator new[](std::size_t /* count */) noexcept(false) {
125 // We return a static pointer here since in development build, using new will
126 // lead to crash so the returned pointer is not important.
127 CHRE_ASSERT(false);
128 return reinterpret_cast<void *>(0xDEADBEEF);
129 }
130
operator new[](std::size_t,std::align_val_t)131 void *operator new[](std::size_t /* count */,
132 std::align_val_t /* al */) noexcept(false) {
133 CHRE_ASSERT(false);
134 return reinterpret_cast<void *>(0xDEADBEEF);
135 }