xref: /aosp_15_r20/system/core/init/main.cpp (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1*00c7fec1SAndroid Build Coastguard Worker /*
2*00c7fec1SAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*00c7fec1SAndroid Build Coastguard Worker  *
4*00c7fec1SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*00c7fec1SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*00c7fec1SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*00c7fec1SAndroid Build Coastguard Worker  *
8*00c7fec1SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*00c7fec1SAndroid Build Coastguard Worker  *
10*00c7fec1SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*00c7fec1SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*00c7fec1SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*00c7fec1SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*00c7fec1SAndroid Build Coastguard Worker  * limitations under the License.
15*00c7fec1SAndroid Build Coastguard Worker  */
16*00c7fec1SAndroid Build Coastguard Worker 
17*00c7fec1SAndroid Build Coastguard Worker #include "builtins.h"
18*00c7fec1SAndroid Build Coastguard Worker #include "first_stage_init.h"
19*00c7fec1SAndroid Build Coastguard Worker #include "init.h"
20*00c7fec1SAndroid Build Coastguard Worker #include "selinux.h"
21*00c7fec1SAndroid Build Coastguard Worker #include "subcontext.h"
22*00c7fec1SAndroid Build Coastguard Worker #include "ueventd.h"
23*00c7fec1SAndroid Build Coastguard Worker 
24*00c7fec1SAndroid Build Coastguard Worker #include <android-base/logging.h>
25*00c7fec1SAndroid Build Coastguard Worker 
26*00c7fec1SAndroid Build Coastguard Worker #if __has_feature(address_sanitizer)
27*00c7fec1SAndroid Build Coastguard Worker #include <sanitizer/asan_interface.h>
28*00c7fec1SAndroid Build Coastguard Worker #elif __has_feature(hwaddress_sanitizer)
29*00c7fec1SAndroid Build Coastguard Worker #include <sanitizer/hwasan_interface.h>
30*00c7fec1SAndroid Build Coastguard Worker #endif
31*00c7fec1SAndroid Build Coastguard Worker 
32*00c7fec1SAndroid Build Coastguard Worker #if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer)
33*00c7fec1SAndroid Build Coastguard Worker // Load asan.options if it exists since these are not yet in the environment.
34*00c7fec1SAndroid Build Coastguard Worker // Always ensure detect_container_overflow=0 as there are false positives with this check.
35*00c7fec1SAndroid Build Coastguard Worker // Always ensure abort_on_error=1 to ensure we reboot to bootloader for development builds.
__asan_default_options()36*00c7fec1SAndroid Build Coastguard Worker extern "C" const char* __asan_default_options() {
37*00c7fec1SAndroid Build Coastguard Worker     return "include_if_exists=/system/asan.options:detect_container_overflow=0:abort_on_error=1";
38*00c7fec1SAndroid Build Coastguard Worker }
39*00c7fec1SAndroid Build Coastguard Worker 
40*00c7fec1SAndroid Build Coastguard Worker __attribute__((no_sanitize("address", "memory", "thread", "undefined"))) extern "C" void
__sanitizer_report_error_summary(const char * summary)41*00c7fec1SAndroid Build Coastguard Worker __sanitizer_report_error_summary(const char* summary) {
42*00c7fec1SAndroid Build Coastguard Worker     LOG(ERROR) << "Init (error summary): " << summary;
43*00c7fec1SAndroid Build Coastguard Worker }
44*00c7fec1SAndroid Build Coastguard Worker 
45*00c7fec1SAndroid Build Coastguard Worker __attribute__((no_sanitize("address", "memory", "thread", "undefined"))) static void
AsanReportCallback(const char * str)46*00c7fec1SAndroid Build Coastguard Worker AsanReportCallback(const char* str) {
47*00c7fec1SAndroid Build Coastguard Worker     LOG(ERROR) << "Init: " << str;
48*00c7fec1SAndroid Build Coastguard Worker }
49*00c7fec1SAndroid Build Coastguard Worker #endif
50*00c7fec1SAndroid Build Coastguard Worker 
51*00c7fec1SAndroid Build Coastguard Worker using namespace android::init;
52*00c7fec1SAndroid Build Coastguard Worker 
main(int argc,char ** argv)53*00c7fec1SAndroid Build Coastguard Worker int main(int argc, char** argv) {
54*00c7fec1SAndroid Build Coastguard Worker #if __has_feature(address_sanitizer)
55*00c7fec1SAndroid Build Coastguard Worker     __asan_set_error_report_callback(AsanReportCallback);
56*00c7fec1SAndroid Build Coastguard Worker #elif __has_feature(hwaddress_sanitizer)
57*00c7fec1SAndroid Build Coastguard Worker     __hwasan_set_error_report_callback(AsanReportCallback);
58*00c7fec1SAndroid Build Coastguard Worker #endif
59*00c7fec1SAndroid Build Coastguard Worker     // Boost prio which will be restored later
60*00c7fec1SAndroid Build Coastguard Worker     setpriority(PRIO_PROCESS, 0, -20);
61*00c7fec1SAndroid Build Coastguard Worker     if (!strcmp(basename(argv[0]), "ueventd")) {
62*00c7fec1SAndroid Build Coastguard Worker         return ueventd_main(argc, argv);
63*00c7fec1SAndroid Build Coastguard Worker     }
64*00c7fec1SAndroid Build Coastguard Worker 
65*00c7fec1SAndroid Build Coastguard Worker     if (argc > 1) {
66*00c7fec1SAndroid Build Coastguard Worker         if (!strcmp(argv[1], "subcontext")) {
67*00c7fec1SAndroid Build Coastguard Worker             android::base::InitLogging(argv, &android::base::KernelLogger);
68*00c7fec1SAndroid Build Coastguard Worker             const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
69*00c7fec1SAndroid Build Coastguard Worker 
70*00c7fec1SAndroid Build Coastguard Worker             return SubcontextMain(argc, argv, &function_map);
71*00c7fec1SAndroid Build Coastguard Worker         }
72*00c7fec1SAndroid Build Coastguard Worker 
73*00c7fec1SAndroid Build Coastguard Worker         if (!strcmp(argv[1], "selinux_setup")) {
74*00c7fec1SAndroid Build Coastguard Worker             return SetupSelinux(argv);
75*00c7fec1SAndroid Build Coastguard Worker         }
76*00c7fec1SAndroid Build Coastguard Worker 
77*00c7fec1SAndroid Build Coastguard Worker         if (!strcmp(argv[1], "second_stage")) {
78*00c7fec1SAndroid Build Coastguard Worker             return SecondStageMain(argc, argv);
79*00c7fec1SAndroid Build Coastguard Worker         }
80*00c7fec1SAndroid Build Coastguard Worker     }
81*00c7fec1SAndroid Build Coastguard Worker 
82*00c7fec1SAndroid Build Coastguard Worker     return FirstStageMain(argc, argv);
83*00c7fec1SAndroid Build Coastguard Worker }
84