1 /*
2  * Copyright (C) 2019 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 #include <string>
19 #include <iostream>
20 #include <sstream>
21 
22 #include "jvmti.h"
23 
24 #include "base/logging.h"
25 #include "base/globals.h"
26 #include "base/memfd.h"
27 
28 #ifdef __linux__
29 #include <sys/utsname.h>
30 #endif
31 
32 namespace art {
33 namespace Test1963AddToDexClassLoaderInMemory {
34 
Java_Main_hasWorkingMemfdCreate(JNIEnv *,jclass)35 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasWorkingMemfdCreate(JNIEnv*, jclass) {
36   // We should always have a working version if we're on normal buildbots.
37   if (!art::kIsTargetBuild) {
38     return true;
39   }
40 #ifdef __linux__
41   struct utsname name;
42   if (uname(&name) >= 0) {
43     std::istringstream version(name.release);
44     std::string major_str;
45     std::string minor_str;
46     std::getline(version, major_str, '.');
47     std::getline(version, minor_str, '.');
48     int major = std::stoi(major_str);
49     int minor = std::stoi(minor_str);
50     if (major >= 4 || (major == 3 && minor >= 17)) {
51       // memfd_create syscall was added in 3.17
52       return true;
53     }
54   }
55 #endif
56   int res = memfd_create_compat("TEST THAT MEMFD CREATE WORKS", 0);
57   if (res < 0) {
58     PLOG(ERROR) << "Unable to call memfd_create_compat successfully!";
59     return false;
60   } else {
61     close(res);
62     return true;
63   }
64 }
65 
66 }  // namespace Test1963AddToDexClassLoaderInMemory
67 }  // namespace art
68