diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2023-04-24 12:43:48 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-24 12:43:48 -0400 |
| commit | fbe37ea6d90f7bfe18506b042657c6e533eaf9b2 (patch) | |
| tree | 3ba92ecef38e5e4518bfb31e982573fbfe5fa661 | |
| parent | cef7a478de583cdd4825d642f055a90948b833bc (diff) | |
Fix issue with Obfuscated hash (#2834)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Remove legacy container writing.
Test using module without source map.
* Change hashing for obfuscated source map such that takes into account different line endings.
5 files changed, 27 insertions, 85 deletions
diff --git a/source/compiler-core/slang-artifact-container-util.cpp b/source/compiler-core/slang-artifact-container-util.cpp index dfc385f9a..94ea792c2 100644 --- a/source/compiler-core/slang-artifact-container-util.cpp +++ b/source/compiler-core/slang-artifact-container-util.cpp @@ -462,75 +462,6 @@ SlangResult FileSystemContents::find(ISlangFileSystemExt* fileSystem, const Unow return SLANG_OK; } -/* static */SlangResult ArtifactContainerUtil::writeLegacy(IArtifact* artifact, const String& fileName) -{ - ComPtr<ISlangBlob> containerBlob; - SLANG_RETURN_ON_FAIL(artifact->loadBlob(ArtifactKeep::Yes, containerBlob.writeRef())); - - { - FileStream stream; - SLANG_RETURN_ON_FAIL(stream.init(fileName, FileMode::Create, FileAccess::Write, FileShare::ReadWrite)); - SLANG_RETURN_ON_FAIL(stream.write(containerBlob->getBufferPointer(), containerBlob->getBufferSize())); - } - - auto parentPath = Path::getParentDirectory(fileName); - - // Lets look to see if we have any maps - { - Index nameCount = 0; - - for (auto associatedArtifact : artifact->getAssociated()) - { - auto desc = associatedArtifact->getDesc(); - - if (isDerivedFrom(desc.payload, ArtifactPayload::SourceMap)) - { - StringBuilder artifactFilename; - - // Dump out - const char* artifactName = associatedArtifact->getName(); - if (artifactName && artifactName[0] != 0) - { - SLANG_RETURN_ON_FAIL(ArtifactUtil::calcName(associatedArtifact, UnownedStringSlice(artifactName), artifactFilename)); - } - else - { - // Perhaps we can generate the name from the output basename - StringBuilder baseName; - - baseName << Path::getFileNameWithoutExt(fileName); - - if (nameCount != 0) - { - baseName.appendChar('-'); - baseName.append(nameCount); - } - - SLANG_RETURN_ON_FAIL(ArtifactUtil::calcName(associatedArtifact, baseName.getUnownedSlice(), artifactFilename)); - - nameCount ++; - } - - ComPtr<ISlangBlob> blob; - SLANG_RETURN_ON_FAIL(associatedArtifact->loadBlob(ArtifactKeep::No, blob.writeRef())); - - // Try to write it out - { - // Work out the path to the artifact - auto artifactPath = Path::combine(parentPath, artifactFilename); - - // Write out the map - FileStream stream; - SLANG_RETURN_ON_FAIL(stream.init(artifactPath, FileMode::Create, FileAccess::Write, FileShare::ReadWrite)); - SLANG_RETURN_ON_FAIL(stream.write(blob->getBufferPointer(), blob->getBufferSize())); - } - } - } - } - - return SLANG_OK; -} - static SlangResult _remove(ISlangMutableFileSystem* fileSystem, const String& path) { SlangPathType pathType; @@ -547,11 +478,11 @@ static SlangResult _remove(ISlangMutableFileSystem* fileSystem, const String& pa const auto ext = Path::getPathExt(fileName); - if (ext == "zip") + if (ext == toSlice("zip")) { SLANG_RETURN_ON_FAIL(_remove(osFileSystem, fileName)); - // Create the zip + // Create the zip ComPtr<ISlangMutableFileSystem> fileSystem; SLANG_RETURN_ON_FAIL(ZipFileSystem::create(fileSystem)); @@ -567,13 +498,12 @@ static SlangResult _remove(ISlangMutableFileSystem* fileSystem, const String& pa // Okay we can now write out the zip SLANG_RETURN_ON_FAIL(osFileSystem->saveFileBlob(fileName.getBuffer(), blob)); - return SLANG_OK; } else if (ext == toSlice("dir")) { // We use the special extension "dir" to write out to a directory. - // This is a little hokey, but at list is consistent + // This is a little hokey arguably... auto path = Path::getPathWithoutExt(fileName); SLANG_RETURN_ON_FAIL(_remove(osFileSystem, path)); @@ -583,11 +513,18 @@ static SlangResult _remove(ISlangMutableFileSystem* fileSystem, const String& pa ComPtr<ISlangMutableFileSystem> fileSystem(new RelativeFileSystem(osFileSystem, path)); SLANG_RETURN_ON_FAIL(writeContainer(artifact, fileName, fileSystem)); + return SLANG_OK; } - else + + // In order to write out as a artifact hierarchy we need a file system. If we don't have that + // we only write out the "main" (or root) artifact. All associated/children are typically ignored. { - // This is the legacy way to write out the container artifact - SLANG_RETURN_ON_FAIL(ArtifactContainerUtil::writeLegacy(artifact, fileName)); + // Get the artifact as a blob + ComPtr<ISlangBlob> containerBlob; + SLANG_RETURN_ON_FAIL(artifact->loadBlob(ArtifactKeep::Yes, containerBlob.writeRef())); + + // Write out the blob + SLANG_RETURN_ON_FAIL(osFileSystem->saveFileBlob(fileName.getBuffer(), containerBlob)); } return SLANG_OK; diff --git a/source/compiler-core/slang-artifact-container-util.h b/source/compiler-core/slang-artifact-container-util.h index 0ee99de26..68c1f11f5 100644 --- a/source/compiler-core/slang-artifact-container-util.h +++ b/source/compiler-core/slang-artifact-container-util.h @@ -21,9 +21,6 @@ or other file like representations */ struct ArtifactContainerUtil { - /// Writes using the legacy mechanism - static SlangResult writeLegacy(IArtifact* artifact, const String& filename); - /// Write the container using the specified path. /// Uses the extension of the path to determine how to write static SlangResult writeContainer(IArtifact* artifact, const String& path); diff --git a/source/slang/slang-ir-obfuscate-loc.cpp b/source/slang/slang-ir-obfuscate-loc.cpp index 7bb4bef7d..ecc16d2b2 100644 --- a/source/slang/slang-ir-obfuscate-loc.cpp +++ b/source/slang/slang-ir-obfuscate-loc.cpp @@ -130,9 +130,18 @@ SlangResult obfuscateModuleLocs(IRModule* module, SourceManager* sourceManager) hash = combineHash(hash, nameHash); } - // We combine the *offset* which is stable + // We *can't* just use the offset to produce the hash, because the source might have + // different line endings on different platforms (in particular linux/unix-like and windows). + // So we hash the line number/line offset to work around + const auto offset = sourceView->getRange().getOffset(curLoc); - hash = combineHash(hash, getHashCode(offset)); + + const auto sourceFile = sourceView->getSourceFile(); + const auto lineIndex = sourceFile->calcLineIndexFromOffset(offset); + const auto lineOffset = sourceFile->calcColumnOffset(lineIndex, offset); + + hash = combineHash(hash, getHashCode(lineIndex)); + hash = combineHash(hash, getHashCode(lineOffset)); } } } diff --git a/tests/serialization/obfuscated-module-check-loc.slang b/tests/serialization/obfuscated-module-check-loc.slang index 2570c8843..853449995 100644 --- a/tests/serialization/obfuscated-module-check-loc.slang +++ b/tests/serialization/obfuscated-module-check-loc.slang @@ -1,8 +1,7 @@ //TEST:COMPILE: tests/serialization/obfuscated-loc-module.slang -o tests/serialization/obfuscated-loc-module.zip -g -obfuscate //TEST:SIMPLE:-target hlsl -stage compute -entry computeMain -obfuscate -r tests/serialization/obfuscated-loc-module.zip -//TEST:COMPILE: tests/serialization/obfuscated-loc-module.slang -o tests/serialization/obfuscated-loc-module.zip -g -obfuscate -// Disable for now as it breaks on gcc/release as different hash seems to be produced -//DISABLE_TEST:SIMPLE:-target hlsl -stage compute -entry computeMain -obfuscate -r tests/serialization/obfuscated-loc-module.zip +//TEST:COMPILE: tests/serialization/obfuscated-loc-module.slang -o tests/serialization/obfuscated-loc-module.slang-module -g -obfuscate +//TEST:SIMPLE:-target hlsl -stage compute -entry computeMain -obfuscate -r tests/serialization/obfuscated-loc-module.slang-module //TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer RWStructuredBuffer<float> outputBuffer; diff --git a/tests/serialization/obfuscated-module-check-loc.slang.3.expected b/tests/serialization/obfuscated-module-check-loc.slang.3.expected index 94f740eb5..3219cce77 100644 --- a/tests/serialization/obfuscated-module-check-loc.slang.3.expected +++ b/tests/serialization/obfuscated-module-check-loc.slang.3.expected @@ -1,6 +1,6 @@ result code = -1 standard error = { -bc65f637-obfuscated(6): error 40020: loop does not terminate within the limited number of iterations, unrolling is aborted. +cae4a81b-obfuscated(2): error 40020: loop does not terminate within the limited number of iterations, unrolling is aborted. } standard output = { } |
