1 // Copyright 2017 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <Python.h>
16 #include <android-base/file.h>
17 #include <osdefs.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string>
22
23 static_assert(sizeof(double) == SIZEOF_DOUBLE);
24 static_assert(sizeof(float) == SIZEOF_FLOAT);
25 static_assert(sizeof(fpos_t) == SIZEOF_FPOS_T);
26 static_assert(sizeof(int) == SIZEOF_INT);
27 static_assert(sizeof(long) == SIZEOF_LONG);
28 static_assert(sizeof(long double) == SIZEOF_LONG_DOUBLE);
29 static_assert(sizeof(long long) == SIZEOF_LONG_LONG);
30 static_assert(sizeof(off_t) == SIZEOF_OFF_T);
31 static_assert(sizeof(pid_t) == SIZEOF_PID_T);
32 static_assert(sizeof(pthread_key_t) == SIZEOF_PTHREAD_KEY_T);
33 static_assert(sizeof(pthread_t) == SIZEOF_PTHREAD_T);
34 static_assert(sizeof(short) == SIZEOF_SHORT);
35 static_assert(sizeof(size_t) == SIZEOF_SIZE_T);
36 static_assert(sizeof(time_t) == SIZEOF_TIME_T);
37 static_assert(sizeof(uintptr_t) == SIZEOF_UINTPTR_T);
38 static_assert(sizeof(void *) == SIZEOF_VOID_P);
39 static_assert(sizeof(wchar_t) == SIZEOF_WCHAR_T);
40 // This should be _Bool, but _Bool doesn't exist in C++. Assume C++ bool will
41 // be the same size as C _Bool.
42 static_assert(sizeof(bool) == SIZEOF__BOOL);
43
44 // TODO(b/141583221): stop leaks
__asan_default_options()45 const char *__asan_default_options() {
46 return "detect_leaks=0";
47 }
48
main(int argc,char * argv[])49 int main(int argc, char *argv[]) {
50 // PYTHONEXECUTABLE is only used on MacOs X, when the Python interpreter
51 // embedded in an application bundle. It is not sure that we have this use case
52 // for Android hermetic Python. So remove this environment variable to make
53 // our self-contained environment more strict.
54 // For user (.py) program, it can access hermetic .par file path through
55 // sys.argv[0].
56 unsetenv(const_cast<char *>("PYTHONEXECUTABLE"));
57
58 // Resolving absolute path based on argv[0] is not reliable since it may
59 // include something unusable, too bad.
60 // android::base::GetExecutablePath() also handles for Darwin/Windows.
61 std::string executable_path = android::base::GetExecutablePath();
62 std::string internal_path = executable_path + "/internal";
63 std::string stdlib_path = internal_path + "/stdlib";
64
65 PyStatus status;
66
67 PyConfig config;
68 PyConfig_InitPythonConfig(&config);
69
70 wchar_t *path_entry;
71
72 // Ignore PYTHONPATH and PYTHONHOME from the environment. Unless we're not
73 // running from inside the zip file, in which case the user may have
74 // specified a PYTHONPATH.
75 #ifdef ANDROID_AUTORUN
76 config.use_environment = 0;
77 config.module_search_paths_set = 1;
78 config.parse_argv = 0;
79 #endif
80
81 // Set the equivalent of PYTHONHOME internally.
82 config.home = Py_DecodeLocale(executable_path.c_str(), NULL);
83 if (config.home == NULL) {
84 fprintf(stderr, "Unable to parse executable name\n");
85 return 1;
86 }
87
88 #ifdef ANDROID_AUTORUN
89 // Execute the __main__.py script inside the zip file attached to our executable.
90 config.run_filename = wcsdup(config.home);
91 #endif
92
93 status = PyConfig_SetBytesArgv(&config, argc, argv);
94 if (PyStatus_Exception(status)) {
95 goto fail;
96 }
97
98 status = PyConfig_Read(&config);
99 if (PyStatus_Exception(status)) {
100 goto fail;
101 }
102
103 path_entry = Py_DecodeLocale(internal_path.c_str(), NULL);
104 if (path_entry == NULL) {
105 fprintf(stderr, "Unable to parse path\n");
106 return 1;
107 }
108
109 status = PyWideStringList_Append(&config.module_search_paths, path_entry);
110 if (PyStatus_Exception(status)) {
111 goto fail;
112 }
113
114 path_entry = Py_DecodeLocale(stdlib_path.c_str(), NULL);
115 if (path_entry == NULL) {
116 fprintf(stderr, "Unable to parse path\n");
117 return 1;
118 }
119
120 status = PyWideStringList_Append(&config.module_search_paths, path_entry);
121 if (PyStatus_Exception(status)) {
122 goto fail;
123 }
124
125 // Always enable Python "-S" option. We don't need site directories,
126 // everything's supposed to be hermetic.
127 config.site_import = 0;
128
129 // Always enable Python "-s" option. We don't need user-site directories,
130 // everything's supposed to be hermetic.
131 config.user_site_directory = 0;
132
133 config.write_bytecode = 0;
134
135 // We've already parsed argv in PyConfig_Read
136 config.parse_argv = 0;
137
138 status = Py_InitializeFromConfig(&config);
139 if (PyStatus_Exception(status)) {
140 goto fail;
141 }
142 PyConfig_Clear(&config);
143
144 return Py_RunMain();
145
146 fail:
147 PyConfig_Clear(&config);
148 if (PyStatus_IsExit(status)) {
149 return status.exitcode;
150 }
151 Py_ExitStatusException(status);
152 }
153