ClickHouse coverage report · #100895
Current view: top level - programs - main.cpp Coverage Total Hit
Current: branch=fix-intdiv-array-nullable-tuple, current_commit=93ad667d0942541878252f9b724c785837247222 Lines: 89.1 % 101 90
Current Date: 2026-03-27 16:26:04 Branches: 87.2 % 78 68
Baseline: base_branch=master, baseline_commit=e8e5cf435d50996dd9d938f86af2aa1f23b489aa
Baseline Date:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed

             Branch data     Line data    Source code
       1                 :             : #include <base/phdr_cache.h>
       2                 :             : #include <base/scope_guard.h>
       3                 :             : #include <base/defines.h>
       4                 :             : 
       5                 :             : #include <Common/EnvironmentChecks.h>
       6                 :             : #include <Common/Exception.h>
       7                 :             : #include <Common/StringUtils.h>
       8                 :             : #include <Common/getHashOfLoadedBinary.h>
       9                 :             : #include <Common/Crypto/OpenSSLInitializer.h>
      10                 :             : 
      11                 :             : #if defined(SANITIZE_COVERAGE)
      12                 :             : #    include <Common/Coverage.h>
      13                 :             : #endif
      14                 :             : 
      15                 :             : #include "config.h"
      16                 :             : #include "config_tools.h"
      17                 :             : 
      18                 :             : #include <unistd.h>
      19                 :             : 
      20                 :             : #include <filesystem>
      21                 :             : #include <iostream>
      22                 :             : #include <new>
      23                 :             : #include <string>
      24                 :             : #include <string_view>
      25                 :             : #include <utility> /// pair
      26                 :             : #include <vector>
      27                 :             : 
      28                 :             : #ifdef SANITIZER
      29                 :             : #pragma clang diagnostic push
      30                 :             : #pragma clang diagnostic ignored "-Wreserved-identifier"
      31                 :             : extern "C" {
      32                 :             : #ifdef ADDRESS_SANITIZER
      33                 :             : const char * __asan_default_options()
      34                 :             : {
      35                 :             :     return "halt_on_error=1 abort_on_error=1";
      36                 :             : }
      37                 :             : const char * __lsan_default_options()
      38                 :             : {
      39                 :             :     return "max_allocation_size_mb=32768";
      40                 :             : }
      41                 :             : const char * __lsan_default_suppressions()
      42                 :             : {
      43                 :             :     /// OpenSSL intentionally does not free all global state at exit.
      44                 :             :     /// These are known false positives from OpenSSL provider and EVP initialization.
      45                 :             :     return "leak:ossl_provider_new\n"
      46                 :             :            "leak:OSSL_PROVIDER_try_load_ex\n"
      47                 :             :            "leak:ossl_rand_ctx_new\n"
      48                 :             :            "leak:OSSL_LIB_CTX_new\n"
      49                 :             :            "leak:ossl_legacy_provider_init\n"
      50                 :             :            /// OpenSSL EVP method objects are cached globally and never freed at exit.
      51                 :             :            /// Triggered when S3 client initializes HMAC (AWS SDK -> OpenSSL HMAC_Init_ex).
      52                 :             :            "leak:evp_md_new\n"
      53                 :             :            "leak:construct_evp_method\n"
      54                 :             :            "leak:CRYPTO_THREAD_lock_new\n";
      55                 :             : }
      56                 :             : #endif
      57                 :             : 
      58                 :             : #ifdef MEMORY_SANITIZER
      59                 :             : const char * __msan_default_options()
      60                 :             : {
      61                 :             :     return "abort_on_error=1 poison_in_dtor=1 max_allocation_size_mb=32768";
      62                 :             : }
      63                 :             : #endif
      64                 :             : 
      65                 :             : #ifdef THREAD_SANITIZER
      66                 :             : const char * __tsan_default_options()
      67                 :             : {
      68                 :             :     return "halt_on_error=1 abort_on_error=1 history_size=7 second_deadlock_stack=1 max_allocation_size_mb=32768";
      69                 :             : }
      70                 :             : #endif
      71                 :             : 
      72                 :             : #ifdef UNDEFINED_BEHAVIOR_SANITIZER
      73                 :             : const char * __ubsan_default_options()
      74                 :             : {
      75                 :             :     return "print_stacktrace=1 max_allocation_size_mb=32768";
      76                 :             : }
      77                 :             : #endif
      78                 :             : }
      79                 :             : #pragma clang diagnostic pop
      80                 :             : #endif
      81                 :             : 
      82                 :             : /// Universal executable for various clickhouse applications
      83                 :             : int mainEntryClickHouseBenchmark(int argc, char ** argv);
      84                 :             : int mainEntryClickHouseCheckMarks(int argc, char ** argv);
      85                 :             : int mainEntryClickHouseChecksumForCompressedBlock(int, char **);
      86                 :             : int mainEntryClickHouseClient(int argc, char ** argv);
      87                 :             : int mainEntryClickHouseCompressor(int argc, char ** argv);
      88                 :             : int mainEntryClickHouseDisks(int argc, char ** argv);
      89                 :             : int mainEntryClickHouseExtractFromConfig(int argc, char ** argv);
      90                 :             : int mainEntryClickHouseFormat(int argc, char ** argv);
      91                 :             : int mainEntryClickHouseFstDumpTree(int argc, char ** argv);
      92                 :             : int mainEntryClickHouseGitImport(int argc, char ** argv);
      93                 :             : int mainEntryClickHouseLocal(int argc, char ** argv);
      94                 :             : int mainEntryClickHouseObfuscator(int argc, char ** argv);
      95                 :             : int mainEntryClickHouseSU(int argc, char ** argv);
      96                 :             : int mainEntryClickHouseServer(int argc, char ** argv);
      97                 :             : int mainEntryClickHouseStaticFilesDiskUploader(int argc, char ** argv);
      98                 :             : int mainEntryClickHouseZooKeeperDumpTree(int argc, char ** argv);
      99                 :             : int mainEntryClickHouseZooKeeperRemoveByList(int argc, char ** argv);
     100                 :             : 
     101                 :             : int mainEntryClickHouseHashBinary(int, char **)
     102                 :           0 : {
     103                 :             :     /// Intentionally without newline. So you can run:
     104                 :             :     /// objcopy --add-section .clickhouse.hash=<(./clickhouse hash-binary) clickhouse
     105                 :           0 :     std::cout << getHashOfLoadedBinaryHex();
     106                 :           0 :     return 0;
     107                 :           0 : }
     108                 :             : 
     109                 :             : #if ENABLE_CLICKHOUSE_KEEPER
     110                 :             : int mainEntryClickHouseKeeper(int argc, char ** argv);
     111                 :             : #endif
     112                 :             : #if ENABLE_CLICKHOUSE_KEEPER_CONVERTER
     113                 :             : int mainEntryClickHouseKeeperConverter(int argc, char ** argv);
     114                 :             : #endif
     115                 :             : #if ENABLE_CLICKHOUSE_KEEPER_CLIENT
     116                 :             : int mainEntryClickHouseKeeperClient(int argc, char ** argv);
     117                 :             : #endif
     118                 :             : #if USE_RAPIDJSON && USE_NURAFT
     119                 :             : int mainEntryClickHouseKeeperBench(int argc, char ** argv);
     120                 :             : #endif
     121                 :             : #if USE_NURAFT
     122                 :             : int mainEntryClickHouseKeeperDataDumper(int argc, char ** argv);
     123                 :             : int mainEntryClickHouseKeeperUtils(int argc, char ** argv);
     124                 :             : #endif
     125                 :             : 
     126                 :             : #if USE_CHDIG
     127                 :             : extern "C" int chdig_main(int argc, char ** argv);
     128                 :             : int mainEntryClickHouseChdig(int argc, char ** argv)
     129                 :           4 : {
     130                 :           4 :     return chdig_main(argc, argv);
     131                 :           4 : }
     132                 :             : #endif
     133                 :             : 
     134                 :             : // install
     135                 :             : int mainEntryClickHouseInstall(int argc, char ** argv);
     136                 :             : int mainEntryClickHouseStart(int argc, char ** argv);
     137                 :             : int mainEntryClickHouseStop(int argc, char ** argv);
     138                 :             : int mainEntryClickHouseStatus(int argc, char ** argv);
     139                 :             : int mainEntryClickHouseRestart(int argc, char ** argv);
     140                 :             : 
     141                 :             : namespace
     142                 :             : {
     143                 :             : 
     144                 :             : using MainFunc = int (*)(int, char**);
     145                 :             : 
     146                 :             : /// Add an item here to register new application.
     147                 :             : /// This list has a "priority" - e.g. we need to disambiguate clickhouse --format being
     148                 :             : /// either clickouse-format or clickhouse-{local, client} --format.
     149                 :             : /// Currently we will prefer the latter option.
     150                 :             : std::pair<std::string_view, MainFunc> clickhouse_applications[] =
     151                 :             : {
     152                 :             :     {"local", mainEntryClickHouseLocal},
     153                 :             :     {"client", mainEntryClickHouseClient},
     154                 :             : #if USE_CHDIG
     155                 :             :     {"chdig", mainEntryClickHouseChdig},
     156                 :             :     {"dig", mainEntryClickHouseChdig},
     157                 :             : #endif
     158                 :             :     {"benchmark", mainEntryClickHouseBenchmark},
     159                 :             :     {"server", mainEntryClickHouseServer},
     160                 :             :     {"extract-from-config", mainEntryClickHouseExtractFromConfig},
     161                 :             :     {"compressor", mainEntryClickHouseCompressor},
     162                 :             :     {"format", mainEntryClickHouseFormat},
     163                 :             :     {"obfuscator", mainEntryClickHouseObfuscator},
     164                 :             :     {"git-import", mainEntryClickHouseGitImport},
     165                 :             :     {"static-files-disk-uploader", mainEntryClickHouseStaticFilesDiskUploader},
     166                 :             :     {"su", mainEntryClickHouseSU},
     167                 :             :     {"hash-binary", mainEntryClickHouseHashBinary},
     168                 :             :     {"disks", mainEntryClickHouseDisks},
     169                 :             :     {"check-marks", mainEntryClickHouseCheckMarks},
     170                 :             :     {"checksum-for-compressed-block", mainEntryClickHouseChecksumForCompressedBlock},
     171                 :             :     {"zookeeper-dump-tree", mainEntryClickHouseZooKeeperDumpTree},
     172                 :             :     {"zookeeper-remove-by-list", mainEntryClickHouseZooKeeperRemoveByList},
     173                 :             : 
     174                 :             :     // keeper
     175                 :             : #if ENABLE_CLICKHOUSE_KEEPER
     176                 :             :     {"keeper", mainEntryClickHouseKeeper},
     177                 :             : #endif
     178                 :             : #if ENABLE_CLICKHOUSE_KEEPER_CONVERTER
     179                 :             :     {"keeper-converter", mainEntryClickHouseKeeperConverter},
     180                 :             : #endif
     181                 :             : #if ENABLE_CLICKHOUSE_KEEPER_CLIENT
     182                 :             :     {"keeper-client", mainEntryClickHouseKeeperClient},
     183                 :             : #endif
     184                 :             : #if USE_RAPIDJSON && USE_NURAFT
     185                 :             :     {"keeper-bench", mainEntryClickHouseKeeperBench},
     186                 :             : #endif
     187                 :             : #if USE_NURAFT
     188                 :             :     {"keeper-data-dumper", mainEntryClickHouseKeeperDataDumper},
     189                 :             :     {"keeper-utils", mainEntryClickHouseKeeperUtils},
     190                 :             : #endif
     191                 :             :     // install
     192                 :             :     {"install", mainEntryClickHouseInstall},
     193                 :             :     {"start", mainEntryClickHouseStart},
     194                 :             :     {"stop", mainEntryClickHouseStop},
     195                 :             :     {"status", mainEntryClickHouseStatus},
     196                 :             :     {"restart", mainEntryClickHouseRestart},
     197                 :             : };
     198                 :             : 
     199                 :             : int printHelp(int, char **)
     200                 :          20 : {
     201                 :          20 :     std::cerr << "Use one of the following commands:" << std::endl;
     202         [ +  + ]:          20 :     for (auto & application : clickhouse_applications)
     203                 :         600 :         std::cerr << "clickhouse " << application.first << " [args] " << std::endl;
     204                 :          20 :     return -1;
     205                 :          20 : }
     206                 :             : 
     207                 :             : /// Add an item here to register a new short name
     208                 :             : std::pair<std::string_view, std::string_view> clickhouse_short_names[] =
     209                 :             : {
     210                 :             :     {"chl", "local"},
     211                 :             :     {"chc", "client"},
     212                 :             : #if USE_CHDIG
     213                 :             :     {"chdig", "chdig"},
     214                 :             : #endif
     215                 :             : };
     216                 :             : 
     217                 :             : }
     218                 :             : 
     219                 :             : bool isClickhouseApp(std::string_view app_suffix, std::vector<char *> & argv)
     220                 :      846372 : {
     221         [ +  + ]:      846372 :     for (const auto & [alias, name] : clickhouse_short_names)
     222         [ +  + ]:     2539116 :         if (app_suffix == name
     223 [ +  - ][ -  + ]:     2539116 :             && !argv.empty() && (alias == argv[0] || endsWith(argv[0], "/" + std::string(alias))))
                 [ -  + ]
     224                 :           0 :             return true;
     225                 :             : 
     226                 :             :     /// Use app if the first arg 'app' is passed (the arg should be quietly removed)
     227         [ +  + ]:      846372 :     if (argv.size() >= 2)
     228                 :      844030 :     {
     229                 :      844030 :         auto first_arg = argv.begin() + 1;
     230                 :             : 
     231                 :             :         /// 'clickhouse --client ...' and 'clickhouse client ...' are Ok
     232         [ +  + ]:      844030 :         if (*first_arg == app_suffix
     233 [ +  + ][ -  + ]:      844030 :             || (std::string_view(*first_arg).starts_with("--") && std::string_view(*first_arg).substr(2) == app_suffix))
     234                 :       85982 :         {
     235                 :       85982 :             argv.erase(first_arg);
     236                 :       85982 :             return true;
     237                 :       85982 :         }
     238                 :      844030 :     }
     239                 :             : 
     240                 :             :     /// Use app if clickhouse binary is run through symbolic link with name clickhouse-app
     241                 :      760390 :     std::string app_name = "clickhouse-" + std::string(app_suffix);
     242 [ +  - ][ +  + ]:      760390 :     return !argv.empty() && (app_name == argv[0] || endsWith(argv[0], "/" + app_name));
                 [ +  + ]
     243                 :      846372 : }
     244                 :             : 
     245                 :             : /// Don't allow dlopen in the main ClickHouse binary, because it is harmful and insecure.
     246                 :             : /// We don't use it. But it can be used by some libraries for implementation of "plugins".
     247                 :             : /// We absolutely discourage the ancient technique of loading
     248                 :             : /// 3rd-party uncontrolled dangerous libraries into the process address space,
     249                 :             : /// because it is insane.
     250                 :             : 
     251                 :             : #if !defined(USE_MUSL)
     252                 :             : extern "C"
     253                 :             : {
     254                 :             :     void * dlopen(const char *, int)
     255                 :        4879 :     {
     256                 :        4879 :         return nullptr;
     257                 :        4879 :     }
     258                 :             : 
     259                 :             :     void * dlmopen(long, const char *, int) // NOLINT
     260                 :           0 :     {
     261                 :           0 :         return nullptr;
     262                 :           0 :     }
     263                 :             : 
     264                 :             :     int dlclose(void *)
     265                 :           0 :     {
     266                 :           0 :         return 0;
     267                 :           0 :     }
     268                 :             : 
     269                 :             :     const char * dlerror()
     270                 :          36 :     {
     271                 :          36 :         return "ClickHouse does not allow dynamic library loading";
     272                 :          36 :     }
     273                 :             : }
     274                 :             : #endif
     275                 :             : 
     276                 :             : /// Prevent messages from JeMalloc in the release build.
     277                 :             : /// Some of these messages are non-actionable for the users, such as:
     278                 :             : /// <jemalloc>: Number of CPUs detected is not deterministic. Per-CPU arena disabled.
     279                 :             : #if USE_JEMALLOC && defined(NDEBUG) && !defined(SANITIZER)
     280                 :             : extern "C" void (*je_malloc_message)(void *, const char *s);
     281                 :      196022 : __attribute__((constructor(0))) void init_je_malloc_message() { je_malloc_message = [](void *, const char *){}; }
     282                 :             : #elif USE_JEMALLOC
     283                 :             : #include <unordered_set>
     284                 :             : /// Ignore messages which can be safely ignored, e.g. EAGAIN on pthread_create
     285                 :             : extern "C" void (*je_malloc_message)(void *, const char * s);
     286                 :             : __attribute__((constructor(0))) void init_je_malloc_message()
     287                 :             : {
     288                 :             :     je_malloc_message = [](void *, const char * str)
     289                 :             :     {
     290                 :             :         using namespace std::literals;
     291                 :             :         static const std::unordered_set<std::string_view> ignore_messages{
     292                 :             :             "<jemalloc>: background thread creation failed (11)\n"sv};
     293                 :             : 
     294                 :             :         std::string_view message_view{str};
     295                 :             :         if (ignore_messages.contains(message_view))
     296                 :             :             return;
     297                 :             : 
     298                 :             : #    if defined(SYS_write)
     299                 :             :         syscall(SYS_write, 2 /*stderr*/, message_view.data(), message_view.size());
     300                 :             : #    else
     301                 :             :         write(STDERR_FILENO, message_view.data(), message_view.size());
     302                 :             : #    endif
     303                 :             :     };
     304                 :             : }
     305                 :             : #endif
     306                 :             : 
     307                 :             : /// OpenSSL early initialization.
     308                 :             : /// See also EnvironmentChecks.cpp for other static initializers.
     309                 :             : /// Must be ran after EnvironmentChecks.cpp, as OpenSSL uses SSE4.1 and POPCNT.
     310                 :             : __attribute__((constructor(202))) void init_ssl()
     311                 :      196022 : {
     312                 :      196022 :     DB::OpenSSLInitializer::instance();
     313                 :      196022 : }
     314                 :             : 
     315                 :             : /// This allows to implement assert to forbid initialization of a class in static constructors.
     316                 :             : /// Usage:
     317                 :             : ///
     318                 :             : /// extern bool inside_main;
     319                 :             : /// class C { C() { assert(inside_main); } };
     320                 :             : bool inside_main = false;
     321                 :             : 
     322                 :             : int main(int argc_, char ** argv_)
     323                 :      196022 : {
     324                 :      196022 :     inside_main = true;
     325                 :      196022 :     SCOPE_EXIT({ inside_main = false; });
     326                 :             : 
     327                 :             :     /// PHDR cache is required for query profiler to work reliably
     328                 :             :     /// It also speed up exception handling, but exceptions from dynamically loaded libraries (dlopen)
     329                 :             :     ///  will work only after additional call of this function.
     330                 :             :     /// Note: we forbid dlopen in our code.
     331                 :      196022 :     updatePHDRCache();
     332                 :             : 
     333                 :      196022 : #if !defined(USE_MUSL)
     334                 :      196022 :     checkHarmfulEnvironmentVariables(argv_);
     335                 :      196022 : #endif
     336                 :             : 
     337                 :             :     /// This is used for testing. For example,
     338                 :             :     /// clickhouse-local should be able to run a simple query without throw/catch.
     339         [ +  + ]:      196022 :     if (getenv("CLICKHOUSE_TERMINATE_ON_ANY_EXCEPTION")) // NOLINT(concurrency-mt-unsafe)
     340                 :          36 :         DB::terminate_on_any_exception = true;
     341                 :             : 
     342                 :             :     /// Reset new handler to default (that throws std::bad_alloc)
     343                 :             :     /// It is needed because LLVM library clobbers it.
     344                 :      196022 :     std::set_new_handler(nullptr);
     345                 :             : 
     346                 :      196022 :     std::vector<char *> argv(argv_, argv_ + argc_);
     347                 :             : 
     348                 :             :     /// Print a basic help if nothing was matched
     349                 :      196022 :     MainFunc main_func = printHelp;
     350                 :             : 
     351         [ +  + ]:      196022 :     for (auto & application : clickhouse_applications)
     352                 :      846372 :     {
     353         [ +  + ]:      846372 :         if (isClickhouseApp(application.first, argv))
     354                 :      195893 :         {
     355                 :      195893 :             main_func = application.second;
     356                 :      195893 :             break;
     357                 :      195893 :         }
     358                 :      846372 :     }
     359                 :             : 
     360                 :             :     /// If host/port arguments are passed to clickhouse/ch shortcuts,
     361                 :             :     /// interpret it as clickhouse-client invocation for usability.
     362 [ +  + ][ +  + ]:      196022 :     if (main_func == printHelp && argv.size() >= 2)
     363                 :         125 :     {
     364         [ +  + ]:         306 :         for (size_t i = 1, num_args = argv.size(); i < num_args; ++i)
     365                 :         189 :         {
     366 [ +  + ][ +  + ]:         189 :             if ((i + 1 < num_args && argv[i] == std::string_view("--host")) || startsWith(argv[i], "--host=")
         [ +  + ][ -  + ]
     367 [ +  + ][ -  + ]:         189 :                 || (i + 1 < num_args && argv[i] == std::string_view("--port")) || startsWith(argv[i], "--port=")
                 [ -  + ]
     368         [ +  + ]:         189 :                 || startsWith(argv[i], "-h"))
     369                 :           8 :             {
     370                 :           8 :                 main_func = mainEntryClickHouseClient;
     371                 :           8 :                 break;
     372                 :           8 :             }
     373                 :         189 :         }
     374                 :         125 :     }
     375                 :             : 
     376                 :             :     /// Interpret binary without argument or with arguments starts with dash
     377                 :             :     /// ('-') as clickhouse-local for better usability:
     378                 :             :     ///
     379                 :             :     ///     clickhouse help # dumps help
     380                 :             :     ///     clickhouse -q 'select 1' # use local
     381                 :             :     ///     clickhouse # spawn local
     382                 :             :     ///     clickhouse local # spawn local
     383                 :             :     ///     clickhouse "select ..." # spawn local
     384                 :             :     ///     clickhouse /tmp/repro --enable-analyzer
     385                 :             :     ///
     386                 :      196022 :     std::error_code ec;
     387 [ +  + ][ +  - ]:      196022 :     if (main_func == printHelp && !argv.empty()
     388 [ +  + ][ +  + ]:      196022 :         && (argv.size() < 2 || argv[1] != std::string_view("--help"))
     389 [ +  + ][ +  + ]:      196022 :         && (argv.size() == 1 || argv[1][0] == '-' || std::string_view(argv[1]).contains(' ')
                 [ +  + ]
     390         [ +  + ]:         113 :             || std::filesystem::is_regular_file(std::filesystem::path{argv[1]}, ec)))
     391                 :          93 :     {
     392                 :          93 :         main_func = mainEntryClickHouseLocal;
     393                 :          93 :     }
     394                 :             : 
     395                 :             :     /// If the argument looks like a file path but doesn't exist, provide a helpful error
     396                 :             :     /// instead of the generic "Use one of the following commands" message.
     397                 :             :     /// The check above routes existing files to clickhouse-local, but when the file
     398                 :             :     /// doesn't exist, we fall through to `printHelp` which is confusing:
     399                 :             :     ///     $ clickhouse tests/queries/0_stateless/my_test.sql
     400                 :             :     ///     Use one of the following commands: ...
     401                 :             :     /// We detect file-like arguments by the presence of `/` (path separator)
     402                 :             :     /// or `.` (file extension), which distinguishes them from mistyped subcommand
     403                 :             :     /// names like "clickhouse sever" where the generic help is appropriate.
     404 [ +  + ][ +  - ]:      196022 :     if (main_func == printHelp && argv.size() >= 2)
     405                 :          28 :     {
     406                 :          28 :         std::string_view arg(argv[1]);
     407 [ +  + ][ +  + ]:          28 :         if (arg.contains('/') || arg.contains('.'))
     408                 :           8 :         {
     409                 :           8 :             std::cerr << "Error: no such file: " << arg << std::endl;
     410                 :           8 :             std::cerr << "If you intended to run a script, please check the path." << std::endl;
     411                 :           8 :             return 1;
     412                 :           8 :         }
     413                 :          28 :     }
     414                 :             : 
     415                 :      196014 :     int exit_code = main_func(static_cast<int>(argv.size()), argv.data());
     416                 :             : 
     417                 :             : #if defined(SANITIZE_COVERAGE)
     418                 :             :     dumpCoverage();
     419                 :             : #endif
     420                 :             : 
     421                 :      196014 :     return exit_code;
     422                 :      196022 : }
        

Generated by: LCOV version 2.4-0