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