brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · 293d105 Raw
101 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// Make sure that the iostreams are initialized before everything else.10// This has been an issue when statically linking libc++ in some contexts.11// See https://llvm.org/PR28954 for details.12//13// This test works by checking that std::{cin,cout,cerr} is the same in a14// static object constructor and in the main function. It dumps the memory of15// each stream in the static object constructor and compares it with the memory16// in the main function.17//18// The assumption is that if there are no uses of the stream object (such as19// construction), then its memory must be the same. In the case where the test20// "fails" and we are actually accessing an uninitialized object when we perform21// the memcpy, the behavior is technically undefined (so the test could still22// pass).23 24#include <cassert>25#include <cstring>26#include <iostream>27 28#include "test_macros.h"29 30struct Checker {31    char *cerr_mem_dump;32    char *cin_mem_dump;33    char *cout_mem_dump;34    char *clog_mem_dump;35 36    Checker()37        : cerr_mem_dump(new char[sizeof(std::cerr)])38        , cin_mem_dump(new char[sizeof(std::cin)])39        , cout_mem_dump(new char[sizeof(std::cout)])40        , clog_mem_dump(new char[sizeof(std::clog)])41     {42        std::memcpy(cerr_mem_dump, (char*)&std::cerr, sizeof(std::cerr));43        std::memcpy(cin_mem_dump, (char*)&std::cin, sizeof(std::cin));44        std::memcpy(cout_mem_dump, (char*)&std::cout, sizeof(std::cout));45        std::memcpy(clog_mem_dump, (char*)&std::clog, sizeof(std::clog));46    }47 48    ~Checker() {49        delete[] cerr_mem_dump;50        delete[] cin_mem_dump;51        delete[] cout_mem_dump;52        delete[] clog_mem_dump;53    }54};55static Checker check;56 57#ifndef TEST_HAS_NO_WIDE_CHARACTERS58struct WideChecker {59    char *wcerr_mem_dump;60    char *wcin_mem_dump;61    char *wcout_mem_dump;62    char *wclog_mem_dump;63 64    WideChecker()65        : wcerr_mem_dump(new char[sizeof(std::wcerr)])66        , wcin_mem_dump(new char[sizeof(std::wcin)])67        , wcout_mem_dump(new char[sizeof(std::wcout)])68        , wclog_mem_dump(new char[sizeof(std::wclog)])69     {70        std::memcpy(wcerr_mem_dump, (char*)&std::wcerr, sizeof(std::wcerr));71        std::memcpy(wcin_mem_dump, (char*)&std::wcin, sizeof(std::wcin));72        std::memcpy(wcout_mem_dump, (char*)&std::wcout, sizeof(std::wcout));73        std::memcpy(wclog_mem_dump, (char*)&std::wclog, sizeof(std::wclog));74    }75 76    ~WideChecker() {77        delete[] wcerr_mem_dump;78        delete[] wcin_mem_dump;79        delete[] wcout_mem_dump;80        delete[] wclog_mem_dump;81    }82};83 84static WideChecker wide_check;85#endif86 87int main(int, char**) {88    assert(std::memcmp(check.cerr_mem_dump, (char const*)&std::cerr, sizeof(std::cerr)) == 0);89    assert(std::memcmp(check.cin_mem_dump, (char const*)&std::cin, sizeof(std::cin)) == 0);90    assert(std::memcmp(check.cout_mem_dump, (char const*)&std::cout, sizeof(std::cout)) == 0);91    assert(std::memcmp(check.clog_mem_dump, (char const*)&std::clog, sizeof(std::clog)) == 0);92 93#ifndef TEST_HAS_NO_WIDE_CHARACTERS94    assert(std::memcmp(wide_check.wcerr_mem_dump, (char const*)&std::wcerr, sizeof(std::wcerr)) == 0);95    assert(std::memcmp(wide_check.wcin_mem_dump, (char const*)&std::wcin, sizeof(std::wcin)) == 0);96    assert(std::memcmp(wide_check.wcout_mem_dump, (char const*)&std::wcout, sizeof(std::wcout)) == 0);97    assert(std::memcmp(wide_check.wclog_mem_dump, (char const*)&std::wclog, sizeof(std::wclog)) == 0);98#endif99    return 0;100}101