microsoft / SizeBench

SizeBench is a binary size investigation tool for Windows
MIT License
103 stars 14 forks source link

Enum / class in different unnamed namespaces cause Error opening binary #16

Open floriankirsch opened 1 year ago

floriankirsch commented 1 year ago

I have found a bug in Sizebench (the version downloaded from the Microsoft AppStore, I did not try to compile a version from source). The bug happens when compiling an executable or dll with VC2017, when in one translation unit an unnamed namespace exists with a virtual class, and in another translation unit an unnamed namespace with an enum with the same name. The following is a minimal example. If you rename NameClash in either file to something different, the problem no longer occurs.

-------- virtualstruct.cpp --------

namespace
{
    struct NameClash
    {
        virtual ~NameClash() = default;
    };
    NameClash instance;
}

-------- main.cpp --------

namespace
{
    enum NameClash { FOO };
}

int main(int argc, char ** argv)
{
    NameClash t = NameClash(argc);
    if (t == FOO)
        return 1;
    else
        return 0;
}

When you compile this with Visual Studio 2017, and open the resulting executable with Sizebench, you get a dialog with an error message "Error opening binary! - There was an error opening this binary or PDB", following by a link to some log, and a call stack.

I have found out that this problem does not occur when compiling the same files with Visual Studio 2022.

I'll try to attach a screenshot, the content of the log file, and a zip file with the project file, which can be used with Visual Studio 2017 to compile and reproduce the bug.

sizebencherror

tmpAD61.tmp.sizebenchlog.txt

sizebenchbug.zip