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 
63   PyStatus status;
64   PyConfig config;
65   PyConfig_InitPythonConfig(&config);
66 
67   // Ignore PYTHONPATH and PYTHONHOME from the environment. Unless we're not
68   // running from inside the zip file, in which case the user may have
69   // specified a PYTHONPATH.
70 #ifdef ANDROID_AUTORUN
71   config.use_environment = 0;
72   config.parse_argv = 0;
73 #endif
74 
75   // Set the equivalent of PYTHONHOME internally.
76   config.home = Py_DecodeLocale(executable_path.c_str(), NULL);
77   if (config.home == NULL) {
78     fprintf(stderr, "Unable to parse executable name\n");
79     return 1;
80   }
81 
82   config.platlibdir = Py_DecodeLocale("internal", NULL);
83   if (config.platlibdir == NULL) {
84     fprintf(stderr, "Unable to parse platlibdir\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   // Always enable Python "-S" option. We don't need site directories,
104   // everything's supposed to be hermetic.
105   config.site_import = 0;
106 
107   // Always enable Python "-s" option. We don't need user-site directories,
108   // everything's supposed to be hermetic.
109   config.user_site_directory = 0;
110 
111   config.write_bytecode = 0;
112 
113   // We've already parsed argv in PyConfig_Read
114   config.parse_argv = 0;
115 
116   status = Py_InitializeFromConfig(&config);
117   if (PyStatus_Exception(status)) {
118     goto fail;
119   }
120   PyConfig_Clear(&config);
121 
122   return Py_RunMain();
123 
124 fail:
125   PyConfig_Clear(&config);
126   if (PyStatus_IsExit(status)) {
127       return status.exitcode;
128   }
129   Py_ExitStatusException(status);
130 }
131