Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ILASM] Add support for deterministic builds and PDB checksums #109091

Open
wants to merge 76 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
1419f57
Added '/DET' flag to ILASM for deterministic compilations
TIHan Apr 25, 2023
d3b329c
Using 'm_pInternalEmitForDeterministicMvid'
TIHan Apr 25, 2023
0357bf1
Removing use of BCRYPT APIs. Added TODOs.
TIHan Apr 25, 2023
54147cc
Add use of BCYRPT for windows-only. Added TODO for non-win32.
TIHan Apr 25, 2023
5516e77
Preliminary work for deterministic PDB guid
TIHan Apr 25, 2023
d6a16fa
Fixed ordering problem
TIHan Apr 25, 2023
f2e4d2c
Computing checksum and using result for the PDB guid
TIHan Apr 25, 2023
89df2d8
Refactoring apis
TIHan Apr 26, 2023
7374450
Added PdbChecksum and Deterministic Debug Directory entries
TIHan Apr 26, 2023
7a0dfcb
Minor whitespace cleanup
TIHan Apr 26, 2023
615c6ce
Merge remote-tracking branch 'upstream/main' into ilasm-det
TIHan Jul 14, 2023
68482a4
mering with main
TIHan Jun 4, 2024
ef59bed
Fix build
TIHan Jun 4, 2024
028a8b6
Added IILAsmPortablePdbWriter interface
TIHan Jun 4, 2024
e1a10f8
Checking deterministic output for ILASM for all roundtrip tests
TIHan Jun 4, 2024
8ba6868
Fixed getting interface. Testing determinism of pdb.
TIHan Jun 5, 2024
c023315
add comment
TIHan Jun 5, 2024
9e14b48
Remove use of goto
TIHan Jun 5, 2024
4038a1f
Added sha256 for non-win32
TIHan Jun 5, 2024
69fadf4
Using different impl for getting sha256 of a file in python. Using sh…
TIHan Jun 5, 2024
936f0a3
Trying to fix build
TIHan Jun 5, 2024
d6b4258
Fix build
TIHan Jun 6, 2024
9c1a645
disable testing determinism due to timeouts
TIHan Jun 6, 2024
e8b9b7f
Merge remote-tracking branch 'upstream/main' into ilasm-det
TIHan Jul 23, 2024
3331d9d
Temporarily disable emitting debug directories to see if the tests ti…
TIHan Jul 23, 2024
7df2c80
Merge branch 'ilasm-det' of https://github.com/TIHan/runtime into ila…
TIHan Jul 23, 2024
e78450f
Merge remote-tracking branch 'upstream/main' into ilasm-det
TIHan Jul 24, 2024
8e357b8
Renable deterministic tests and debug directory creation
TIHan Jul 24, 2024
8867295
Added sha256.cpp
TIHan Jul 24, 2024
6dd077f
Remove sha256 impl for now
TIHan Jul 24, 2024
ce016fa
memset for now
TIHan Jul 24, 2024
0086e7d
Temp disable det testing
TIHan Jul 25, 2024
246e42e
Wip: windows/macos sha256 impl
amanasifkhalid Oct 21, 2024
b65f049
Fix build
amanasifkhalid Oct 21, 2024
7cad7db
Non-zero default timestamp
amanasifkhalid Oct 22, 2024
d673645
OpenSSL shim dependency
amanasifkhalid Oct 24, 2024
f3fbd13
Revert default timestamp
amanasifkhalid Oct 24, 2024
5dc8b2a
Error checking in Apple sha256 impl
amanasifkhalid Oct 24, 2024
dd1161c
Linux hash impl
amanasifkhalid Oct 24, 2024
11e02e7
Move static dep to sha256 impl
amanasifkhalid Oct 24, 2024
984f323
whitespace
amanasifkhalid Oct 24, 2024
9665944
Enable determinism tests
amanasifkhalid Oct 24, 2024
dba93ca
Remove debug code; fix macOS build
amanasifkhalid Oct 24, 2024
52c29fd
Fix source build
amanasifkhalid Oct 24, 2024
8802b8d
Remove OpenSSL find requirement
amanasifkhalid Oct 24, 2024
80b4fa4
Fix building linuxdac on win
amanasifkhalid Oct 24, 2024
f8272ff
Ensure OpenSSL is initialized
amanasifkhalid Oct 31, 2024
6c1b4ea
Merge branch 'main' into ilasm-det
amanasifkhalid Nov 1, 2024
0e06b0a
Exclude incompatible test from ILAsm roundtrip
amanasifkhalid Nov 1, 2024
097067d
Revert "Exclude incompatible test from ILAsm roundtrip"
amanasifkhalid Nov 1, 2024
9bfcb7f
Run determinism tests only if build succeeded
amanasifkhalid Nov 1, 2024
0940674
File header timestamp of 1
amanasifkhalid Nov 4, 2024
786061d
print hashes
amanasifkhalid Nov 5, 2024
6780404
Fix int cmp
amanasifkhalid Nov 5, 2024
b2b92a7
One-shot CommonCrypto SHA-256
amanasifkhalid Nov 5, 2024
41c6e27
Remove debug logic
amanasifkhalid Nov 5, 2024
047da9b
Move SHA-256 impl to ilasm
amanasifkhalid Nov 5, 2024
1bf147c
Add hash function arg to ComputeSha256Checksum
amanasifkhalid Nov 5, 2024
5cbe577
Merge from main
amanasifkhalid Nov 7, 2024
8214e8d
Fix hash call site
amanasifkhalid Nov 7, 2024
ca3edb1
Tweak determinism test logic
amanasifkhalid Nov 7, 2024
ea562ae
Revert "Tweak determinism test logic"
amanasifkhalid Nov 8, 2024
6d6b058
Whitespace
amanasifkhalid Nov 8, 2024
91c2acf
Clean up macro
amanasifkhalid Nov 8, 2024
4d0789d
Merge branch 'main' into ilasm-det
amanasifkhalid Dec 14, 2024
db19e8a
Use valid deterministic timestamp
amanasifkhalid Jan 6, 2025
f7162b4
Ensure SHA256 prefix to PDB checksum is null-terminated
amanasifkhalid Jan 6, 2025
2614e36
Error out if OpenSSL not available
amanasifkhalid Jan 7, 2025
9247c7a
Compare files instead of hashes for determinism test
amanasifkhalid Jan 7, 2025
92ef0a9
Computed timestamp for debug dirs from PDB checksum
amanasifkhalid Jan 7, 2025
b7ad2c6
Extend PEWriter API surface to change file header timestamp
amanasifkhalid Jan 7, 2025
b15f810
Fix ILAsm roundtrip tests
amanasifkhalid Jan 7, 2025
db1cdfe
Remove comment
amanasifkhalid Jan 7, 2025
2ce7321
Don't use PDB checksum for GUID/timestamp
amanasifkhalid Jan 8, 2025
266538d
Fix file renaming in determinism tests on Windows
amanasifkhalid Jan 8, 2025
c957abf
Error message consistency
amanasifkhalid Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/coreclr/dlls/mscorpe/pewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ HRESULT PEWriter::Init(PESectionMan *pFrom, DWORD createFlags)
m_ntHeaders->FileHeader.Characteristics |= VAL16(IMAGE_FILE_RELOCS_STRIPPED);
}

if (createFlags & ICEE_CREATE_FILE_DET)
{
// A timestamp of 0 triggers asserts in the VM
m_ntHeaders->FileHeader.TimeDateStamp = VAL32(1);
Copy link
Member

@markples markples Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/dotnet/runtime/blob/main/docs/design/specs/PE-COFF.md and the behavior of csc -deterministic use a hash of the file in this field. I see other hashing going on in this PR, but I think it applies to other parts of the file (?)

It's not clear to me if this matters. I'm not sure what the difference is between this field and the value can be stored in the REPRO debug directory.

I wondered if the runtime is asserting because it wants (mostly?) uniqueness of these values, but if

// for dynamic modules use 0 as the time stamp
ULONG ulTimeStamp = 0;
if (!pModule->IsReflectionEmit())
{
ulTimeStamp = pModule->GetPEAssembly()->GetPEImageTimeDateStamp();
_ASSERTE(ulTimeStamp != 0);
is the assertion that you're referring to, then it only seems to be about the value zero, which is unfortunate since https://learn.microsoft.com/en-us/windows/win32/debug/pe-format seems to indicate that 0 and FFFFFFFF are valid. (Perhaps FFFFFFFF would be a better choice if producing a hash is problematic for some reason?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the assert I was hitting. The deterministic PE/COFF documentation suggests the actual timestamp value doesn't matter, though I suppose we ought to match csc's behavior and use the file hash here too, rather than some magic value we have to explain. Either way, I agree that the assertion in the VM is probably overzealous.

}

// Linker version should be consistent with current VC level
m_ntHeaders->OptionalHeader.MajorLinkerVersion = 11;
m_ntHeaders->OptionalHeader.MinorLinkerVersion = 0;
Expand Down
26 changes: 26 additions & 0 deletions src/coreclr/ilasm/assem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

#include "assembler.h"

#if !defined(_WIN32) && !defined(__APPLE__)
#include "sha256.h"
#endif

void indexKeywords(Indx* indx); // defined in asmparse.y

unsigned int g_uCodePage = CP_ACP;
Expand All @@ -28,6 +32,7 @@ Assembler::Assembler()
{
m_pDisp = NULL;
m_pEmitter = NULL;
m_pInternalEmitForDeterministicMvid = NULL;
m_pImporter = NULL;

char* pszFQN = new char[16];
Expand Down Expand Up @@ -107,6 +112,7 @@ Assembler::Assembler()
m_fGeneratePDB = FALSE;
m_fIsMscorlib = FALSE;
m_fOptimize = FALSE;
m_fDeterministic = FALSE;
m_tkSysObject = 0;
m_tkSysString = 0;
m_tkSysValue = 0;
Expand Down Expand Up @@ -208,6 +214,11 @@ Assembler::~Assembler()
m_pEmitter->Release();
m_pEmitter = NULL;
}
if (m_pInternalEmitForDeterministicMvid != NULL)
{
m_pInternalEmitForDeterministicMvid->Release();
m_pInternalEmitForDeterministicMvid = NULL;
}
if (m_pPortablePdbWriter != NULL)
{
delete m_pPortablePdbWriter;
Expand All @@ -234,6 +245,21 @@ BOOL Assembler::Init(BOOL generatePdb)

if (FAILED(CreateICeeFileGen(&m_pCeeFileGen))) return FALSE;

if (m_fDeterministic)
{
#if !defined(_WIN32) && !defined(__APPLE__)
if (!IsOpenSslAvailable())
{
fprintf(stderr, "\nWarning: OpenSSL not available. Disabling build determinism.\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be an error.

m_fDeterministic = FALSE;
}
else
#endif
{
m_dwCeeFileFlags |= ICEE_CREATE_FILE_DET;
}
}

if (FAILED(m_pCeeFileGen->CreateCeeFileEx(&m_pCeeFile,(ULONG)m_dwCeeFileFlags))) return FALSE;

if (FAILED(m_pCeeFileGen->GetSectionCreate(m_pCeeFile, ".il", sdReadOnly, &m_pILSection))) return FALSE;
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/ilasm/assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2434,12 +2434,9 @@ void Assembler::SetPdbFileName(_In_ __nullterminated char* szName)
HRESULT Assembler::SavePdbFile()
{
HRESULT hr = S_OK;
mdMethodDef entryPoint;

if (FAILED(hr = (m_pPortablePdbWriter == NULL ? E_FAIL : S_OK))) goto exit;
if (FAILED(hr = (m_pPortablePdbWriter->GetEmitter() == NULL ? E_FAIL : S_OK))) goto exit;
if (FAILED(hr = m_pCeeFileGen->GetEntryPoint(m_pCeeFile, &entryPoint))) goto exit;
if (FAILED(hr = m_pPortablePdbWriter->BuildPdbStream(m_pEmitter, entryPoint))) goto exit;
if (FAILED(hr = m_pPortablePdbWriter->GetEmitter()->Save(m_wzPdbFileName, 0))) goto exit;

exit:
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/ilasm/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ class Assembler {
BOOL m_fIsMscorlib;
BOOL m_fTolerateDupMethods;
BOOL m_fOptimize;
BOOL m_fDeterministic;
mdToken m_tkSysObject;
mdToken m_tkSysString;
mdToken m_tkSysValue;
Expand All @@ -760,6 +761,7 @@ class Assembler {

IMetaDataDispenserEx2 *m_pDisp;
IMetaDataEmit3 *m_pEmitter;
IMDInternalEmit *m_pInternalEmitForDeterministicMvid;
ICeeFileGen *m_pCeeFileGen;
IMetaDataImport2 *m_pImporter; // Import interface.
HCEEFILE m_pCeeFile;
Expand Down Expand Up @@ -845,7 +847,7 @@ class Assembler {
BOOL EmitClass(Class *pClass);
HRESULT CreatePEFile(_In_ __nullterminated WCHAR *pwzOutputFilename);
HRESULT CreateTLSDirectory();
HRESULT CreateDebugDirectory();
HRESULT CreateDebugDirectory(BYTE(&pdbChecksum)[32]);
HRESULT InitMetaData();
Class *FindCreateClass(_In_ __nullterminated const char *pszFQN);
BOOL EmitFieldRef(_In_z_ char *pszArg, int opcode);
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/ilasm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
printf("\n/DEBUG Disable JIT optimization, create PDB file, use sequence points from PDB");
printf("\n/DEBUG=IMPL Disable JIT optimization, create PDB file, use implicit sequence points");
printf("\n/DEBUG=OPT Enable JIT optimization, create PDB file, use implicit sequence points");
printf("\n/DET Produce deterministic outputs");
printf("\n/OPTIMIZE Optimize long instructions to short");
printf("\n/FOLD Fold the identical method bodies into one");
printf("\n/CLOCK Measure and report compilation times");
Expand Down Expand Up @@ -316,6 +317,10 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
{
pAsm->m_fOptimize = TRUE;
}
else if (!_stricmp(szOpt, "DET"))
{
pAsm->m_fDeterministic = TRUE;
}
else if (!_stricmp(szOpt, "X64"))
{
pAsm->m_dwCeeFileFlags &= ~ICEE_CREATE_MACHINE_MASK;
Expand Down
26 changes: 26 additions & 0 deletions src/coreclr/ilasm/portable_pdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "portable_pdb.h"
#include <time.h>
#include "assembler.h"
#include "sha256.h"

//*****************************************************************************
// Document
Expand Down Expand Up @@ -103,6 +104,10 @@ HRESULT PortablePdbWriter::Init(IMetaDataDispenserEx2* mdDispenser)
0,
IID_IMetaDataEmit3,
(IUnknown**)&m_pdbEmitter);

if (FAILED(hr)) goto exit;

hr = m_pdbEmitter->QueryInterface(IID_IILAsmPortablePdbWriter, (void**)&m_ilasmPdbWriter);
exit:
return hr;
}
Expand All @@ -122,6 +127,16 @@ ULONG PortablePdbWriter::GetTimestamp()
return m_pdbStream.id.pdbTimeStamp;
}

void PortablePdbWriter::SetGuid(REFGUID newGuid)
{
m_pdbStream.id.pdbGuid = newGuid;
}

void PortablePdbWriter::SetTimestamp(const ULONG newTimestamp)
{
m_pdbStream.id.pdbTimeStamp = newTimestamp;
}

Document* PortablePdbWriter::GetCurrentDocument()
{
return m_currentDocument;
Expand All @@ -145,6 +160,17 @@ HRESULT PortablePdbWriter::BuildPdbStream(IMetaDataEmit3* peEmitter, mdMethodDef
return hr;
}

HRESULT PortablePdbWriter::ComputeSha256PdbStreamChecksum(BYTE(&checksum)[32])
{
return m_ilasmPdbWriter->ComputeSha256PdbStreamChecksum(Sha256Hash, checksum);
}

HRESULT PortablePdbWriter::ChangePdbStreamGuid(REFGUID newGuid)
{
m_pdbStream.id.pdbGuid = newGuid;
return m_ilasmPdbWriter->ChangePdbStreamGuid(newGuid);
}

HRESULT PortablePdbWriter::DefineDocument(char* name, GUID* language)
{
HRESULT hr = S_OK;
Expand Down
13 changes: 9 additions & 4 deletions src/coreclr/ilasm/portable_pdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ class PortablePdbWriter
IMetaDataEmit3* GetEmitter();
GUID* GetGuid();
ULONG GetTimestamp();
void SetGuid(REFGUID newGuid);
void SetTimestamp(const ULONG newTimestamp);
Document* GetCurrentDocument();
HRESULT BuildPdbStream(IMetaDataEmit3* peEmitter, mdMethodDef entryPoint);
HRESULT ComputeSha256PdbStreamChecksum(BYTE (&checksum)[32]);
HRESULT ChangePdbStreamGuid(REFGUID newGuid);
HRESULT DefineDocument(char* name, GUID* language);
HRESULT DefineSequencePoints(Method* method);
HRESULT DefineLocalScope(Method* method);
Expand All @@ -59,10 +63,11 @@ class PortablePdbWriter
BOOL _DefineLocalScope(mdMethodDef methodDefToken, Scope* currScope);

private:
IMetaDataEmit3* m_pdbEmitter;
PORT_PDB_STREAM m_pdbStream;
DocumentList m_documentList;
Document* m_currentDocument;
IMetaDataEmit3* m_pdbEmitter;
IILAsmPortablePdbWriter* m_ilasmPdbWriter;
PORT_PDB_STREAM m_pdbStream;
DocumentList m_documentList;
Document* m_currentDocument;
};

#endif
Loading