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.h>
37 #include "chre/util/nanoapp/assert.h"
38
39 #if defined(stderr) && !defined(_CSTD)
40 // Provides a definition for stderr when the macro has been defined, but the
41 // file has been externed. Some platforms might define their own macros for
42 // stderr (vs the glibc style of matching the macro to the filename), in which
43 // case we have to guard against those definitions as well (eg: _CSTD check
44 // above).
45 FILE *stderr = NULL;
46 #endif
47
malloc(size_t size)48 void *malloc(size_t size) {
49 // On platforms where size(size_t) might be 8 bytes, we need a cast to
50 // maintain adherence to CHRE's heap alloc API. The size check to reject
51 // requests of size > 4Gb could be used for debugging, though any requests
52 // that even remotely approach this limit is bound to fail anyway.
53 return size > UINT32_MAX ? nullptr
54 : chreHeapAlloc(static_cast<uint32_t>(size));
55 }
56
free(void * ptr)57 void free(void *ptr) {
58 chreHeapFree(ptr);
59 }
60
realloc(void *,size_t)61 void *realloc(void * /*ptr*/, size_t /*newSize*/) {
62 // realloc() is not supported, verify that there's no call to it!
63 CHRE_ASSERT(false);
64 return NULL;
65 }
66
exit(int exitCode)67 void exit(int exitCode) {
68 chreAbort(static_cast<uint32_t>(exitCode));
69 // Add an explicit forever-loop to bypass compilation warnings on platforms
70 // that might have defined exit with a noreturn tag. The loop shouldn't ever
71 // execute, since abort terminates the program.
72 while (42)
73 ;
74 }
75
fprintf(FILE *,const char *,...)76 int fprintf(FILE * /*stream*/, const char * /*fmt*/, ...) {
77 return 0;
78 }
79
fwrite(const void *,size_t,size_t,FILE *)80 size_t fwrite(const void * /*ptr*/, size_t /*size*/, size_t /*count*/,
81 FILE * /*stream*/) {
82 return 0;
83 }
84
operator delete(void *)85 void operator delete(void * /*ptr*/) {
86 CHRE_ASSERT(false);
87 }
88
operator delete(void *,std::size_t)89 void operator delete(void * /*ptr*/, std::size_t /*sz*/) {
90 CHRE_ASSERT(false);
91 }
92
operator delete(void *,std::align_val_t)93 void operator delete(void * /*ptr*/, std::align_val_t /*al*/) {
94 CHRE_ASSERT(false);
95 }
96
operator delete(void *,std::size_t,std::align_val_t)97 void operator delete(void * /*ptr*/, std::size_t /*sz*/,
98 std::align_val_t /*al*/) {
99 CHRE_ASSERT(false);
100 }