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 static_assert(sizeof(_Bool) == SIZEOF__BOOL);
41
42 // TODO(b/141583221): stop leaks
__asan_default_options()43 const char *__asan_default_options() {
44 return "detect_leaks=0";
45 }
46
main(int argc,char * argv[])47 int main(int argc, char *argv[]) {
48 // PYTHONEXECUTABLE is only used on MacOs X, when the Python interpreter
49 // embedded in an application bundle. It is not sure that we have this use case
50 // for Android hermetic Python. So remove this environment variable to make
51 // our self-contained environment more strict.
52 // For user (.py) program, it can access hermetic .par file path through
53 // sys.argv[0].
54 unsetenv(const_cast<char *>("PYTHONEXECUTABLE"));
55
56 // Resolving absolute path based on argv[0] is not reliable since it may
57 // include something unusable, too bad.
58 // android::base::GetExecutablePath() also handles for Darwin/Windows.
59 std::string executable_path = android::base::GetExecutablePath();
60 std::string internal_path = executable_path + "/internal";
61 std::string stdlib_path = internal_path + "/stdlib";
62
63 PyStatus status;
64
65 PyConfig config;
66 PyConfig_InitPythonConfig(&config);
67
68 wchar_t *path_entry;
69
70 // Ignore PYTHONPATH and PYTHONHOME from the environment. Unless we're not
71 // running from inside the zip file, in which case the user may have
72 // specified a PYTHONPATH.
73 #ifdef ANDROID_AUTORUN
74 config.use_environment = 0;
75 config.module_search_paths_set = 1;
76 config.parse_argv = 0;
77 #endif
78
79 // Set the equivalent of PYTHONHOME internally.
80 config.home = Py_DecodeLocale(executable_path.c_str(), NULL);
81 if (config.home == NULL) {
82 fprintf(stderr, "Unable to parse executable name\n");
83 return 1;
84 }
85
86 #ifdef ANDROID_AUTORUN
87 // Execute the __main__.py script inside the zip file attached to our executable.
88 config.run_filename = wcsdup(config.home);
89 #endif
90
91 status = PyConfig_SetBytesArgv(&config, argc, argv);
92 if (PyStatus_Exception(status)) {
93 goto fail;
94 }
95
96 status = PyConfig_Read(&config);
97 if (PyStatus_Exception(status)) {
98 goto fail;
99 }
100
101 path_entry = Py_DecodeLocale(internal_path.c_str(), NULL);
102 if (path_entry == NULL) {
103 fprintf(stderr, "Unable to parse path\n");
104 return 1;
105 }
106
107 status = PyWideStringList_Append(&config.module_search_paths, path_entry);
108 if (PyStatus_Exception(status)) {
109 goto fail;
110 }
111
112 path_entry = Py_DecodeLocale(stdlib_path.c_str(), NULL);
113 if (path_entry == NULL) {
114 fprintf(stderr, "Unable to parse path\n");
115 return 1;
116 }
117
118 status = PyWideStringList_Append(&config.module_search_paths, path_entry);
119 if (PyStatus_Exception(status)) {
120 goto fail;
121 }
122
123 // Always enable Python "-S" option. We don't need site directories,
124 // everything's supposed to be hermetic.
125 config.site_import = 0;
126
127 // Always enable Python "-s" option. We don't need user-site directories,
128 // everything's supposed to be hermetic.
129 config.user_site_directory = 0;
130
131 config.write_bytecode = 0;
132
133 // We've already parsed argv in PyConfig_Read
134 config.parse_argv = 0;
135
136 status = Py_InitializeFromConfig(&config);
137 if (PyStatus_Exception(status)) {
138 goto fail;
139 }
140 PyConfig_Clear(&config);
141
142 return Py_RunMain();
143
144 fail:
145 PyConfig_Clear(&config);
146 if (PyStatus_IsExit(status)) {
147 return status.exitcode;
148 }
149 Py_ExitStatusException(status);
150 }
151