xref: /aosp_15_r20/cts/tests/tests/bionic_app/bionic_app_jni.cpp (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 #include <android/set_abort_message.h>
18 #include <dlfcn.h>
19 #include <jni.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 extern "C" const char* __progname;
24 
Java_com_android_bionic_1app_BionicAppTest_progname(JNIEnv * env)25 extern "C" jstring Java_com_android_bionic_1app_BionicAppTest_progname(JNIEnv* env) {
26   return env->NewStringUTF(__progname);
27 }
28 
29 extern "C" int main(int argc, char* argv[]);
30 
Java_com_android_bionic_1app_BionicAppTest_callMainDirect(JNIEnv * env __unused)31 extern "C" jint Java_com_android_bionic_1app_BionicAppTest_callMainDirect(JNIEnv* env __unused) {
32   return main(0, nullptr);
33 }
34 
35 // Having a variant of the test that uses dlsym is helpful to ensure that main isn't accidentally
36 // hidden in the app's DSO.
Java_com_android_bionic_1app_BionicAppTest_callMainDlsym(JNIEnv * env __unused)37 extern "C" jint Java_com_android_bionic_1app_BionicAppTest_callMainDlsym(JNIEnv* env __unused) {
38   auto pmain = reinterpret_cast<int(*)(int, char**)>(dlsym(RTLD_DEFAULT, "main"));
39   if (pmain == nullptr) {
40     char msg[256];
41     snprintf(msg, sizeof(msg), "dlsym(RTLD_DEFAULT, \"main\") was nullptr: %s", dlerror());
42     android_set_abort_message(msg);
43     abort();
44   }
45   return pmain(0, nullptr);
46 }
47