1 /*
2  * Copyright (C) 2017 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  * Tests accessibility of platform native libraries
19  */
20 
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <jni.h>
24 #include <libgen.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/user.h>
31 #include <unistd.h>
32 
33 #include <string>
34 
35 #include "nativehelper/scoped_local_ref.h"
36 #include "nativehelper/scoped_utf_chars.h"
37 
38 static constexpr const char* kTestLibName = "libjni_test_dlclose.so";
39 
run_test(std::string * error_msg)40 static bool run_test(std::string* error_msg) {
41   void* handle = dlopen(kTestLibName, RTLD_NOW);
42   if (handle == nullptr) {
43     *error_msg = dlerror();
44     return false;
45   }
46 
47   void* taxicab_number = dlsym(handle, "dlopen_testlib_taxicab_number");
48   if (taxicab_number == nullptr) {
49     *error_msg = dlerror();
50     dlclose(handle);
51     return false;
52   }
53 
54   dlclose(handle);
55 
56   static const size_t kPageSize = getpagesize();
57   uintptr_t page_start = reinterpret_cast<uintptr_t>(taxicab_number) & ~(kPageSize - 1);
58   if (mprotect(reinterpret_cast<void*>(page_start), kPageSize, PROT_NONE) == 0) {
59     *error_msg = std::string("The library \"") +
60                  kTestLibName +
61                  "\" has not been unloaded on dlclose()";
62     return false;
63   }
64 
65   if (errno != ENOMEM) {
66     *error_msg = std::string("Unexpected error on mprotect: ") + strerror(errno);
67     return false;
68   }
69 
70   return true;
71 }
72 
Java_android_jni_cts_BasicLoaderTestHelper_nativeRunTests(JNIEnv * env)73 extern "C" JNIEXPORT jstring JNICALL Java_android_jni_cts_BasicLoaderTestHelper_nativeRunTests(
74         JNIEnv* env) {
75   std::string error_str;
76 
77   if (!run_test(&error_str)) {
78     return env->NewStringUTF(error_str.c_str());
79   }
80 
81   return nullptr;
82 }
83 
84