diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-11-06 09:01:32 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-11-06 09:01:32 -0500 |
| commit | 2e1be5c5731d93d84c3c1a25c9bfe8c1669a5d29 (patch) | |
| tree | b21cf1428bc987fb8e7c96a5ad2ec3872c9f2ff4 | |
| parent | a6a153f7d7bb9ca8c763f254ee977cb7afa1825a (diff) | |
Feature/obfuscate improvements (#1107)
* Added RiffReadHelper
* Move type to fourCC in Chunk simplifies some code.
* Make MemoryArena able to track external blocks.
Allow ownership of Data to vary.
Changed IR serialization to use moved allocations to avoid copies.
As it turns out all of the array writes could use unowned data, but doing so requires the IRData to stay in scope longer than IRSerialData, which it does at the moment - but perhaps needs better naming or a control for the feature.
* Write out slang-module container.
* WIP on -r option.
Loading modules - with -r.
* Making the serialized-module run (without using imported module).
* Split compiling module from the test.
* Separate module compilation with a function working.
* Remove serialization test as not used.
* Fix warning on gcc.
* Updated test to have types across module boundary.
* Allow entry point declaration.
A test that tries to build with just an entry point declaration and a module.
* Try to make link work with multiple modules.
* Multi module linking first pass working.
* Multi module test working with -module-name option
* Added feature to repro manifest of approximation of command line that was used.
* Use isDefinition - for determining to add decorations to entry point lowering.
* Added support for repo-file-system.h
More precise control of CacheFileSystem.
Allow RelativeFileSystem to strip paths optionally.
Use canonical paths in PathInfo cache.
Fix bug in -D options for command line output of StateSerailizeUtil
* Add missing slang-options.h
* Fix bug in bit slang-state-serialize.cpp with bit removal.
* Added documentation around -repro-file-system
Added spLoadReproAsFileSystem function.
* Fix warning.
* spAddLibraryReference
* * Add support for slang-lib extension
* Container output when using -no-codegen option
* Use the m_containerFormat to determine if the module container is constructed.
Store the result in a blob. This allows for potential access via the API.
Write the blob if a filename is set.
Use m_ prefix for container variables.
* Added spGetContainerCode.
Made spGetCompileRequestCode work.
* * Put obfuscateCode on linkage
* Remove obfuscation from variable names - as can be achieved by either stripping and/or removing NameHintDecorations at lowering
* Remove name hints being added during lowering
* Add stripping of SourceLoc location in strip phase
* Hashing of linkage import/export names.
* Do final strip in emitEntryPoint, removes any remaining SourceLoc.
| -rw-r--r-- | source/slang/slang-compiler.h | 6 | ||||
| -rw-r--r-- | source/slang/slang-emit-c-like.cpp | 20 | ||||
| -rw-r--r-- | source/slang/slang-emit.cpp | 11 | ||||
| -rw-r--r-- | source/slang/slang-ir-link.cpp | 9 | ||||
| -rw-r--r-- | source/slang/slang-ir-strip.cpp | 5 | ||||
| -rw-r--r-- | source/slang/slang-ir-strip.h | 1 | ||||
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 36 | ||||
| -rw-r--r-- | source/slang/slang-mangle.cpp | 10 | ||||
| -rw-r--r-- | source/slang/slang-mangle.h | 2 | ||||
| -rw-r--r-- | source/slang/slang-options.cpp | 3 | ||||
| -rw-r--r-- | source/slang/slang-state-serialize.cpp | 6 |
11 files changed, 79 insertions, 30 deletions
diff --git a/source/slang/slang-compiler.h b/source/slang/slang-compiler.h index 860195a90..2819269b5 100644 --- a/source/slang/slang-compiler.h +++ b/source/slang/slang-compiler.h @@ -1148,6 +1148,8 @@ namespace Slang SourceManager m_defaultSourceManager; SourceManager* m_sourceManager = nullptr; + bool m_obfuscateCode = false; + // Name pool for looking up names NamePool namePool; @@ -1335,10 +1337,6 @@ namespace Slang bool shouldDumpIR = false; bool shouldValidateIR = false; - // Remove name hints to help obfuscate code - // - bool obfuscateCode = false; - protected: CompileRequestBase( Linkage* linkage, diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp index fbe3cb40a..bf7dd3129 100644 --- a/source/slang/slang-emit-c-like.cpp +++ b/source/slang/slang-emit-c-like.cpp @@ -659,25 +659,17 @@ String CLikeSourceEmitter::generateName(IRInst* inst) StringBuilder sb; - if (!m_compileRequest->obfuscateCode) - { - - String nameHint = nameHintDecoration->getName(); - nameHint = scrubName(nameHint); + String nameHint = nameHintDecoration->getName(); + nameHint = scrubName(nameHint); - sb.append(nameHint); + sb.append(nameHint); - // Avoid introducing a double underscore - if (!nameHint.endsWith("_")) - { - sb.append("_"); - } - } - else + // Avoid introducing a double underscore + if (!nameHint.endsWith("_")) { sb.append("_"); } - + String key = sb.ProduceString(); UInt count = 0; m_uniqueNameCounters.TryGetValue(key, count); diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index 280822094..670848002 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -23,6 +23,8 @@ #include "slang-type-layout.h" #include "slang-visitor.h" +#include "slang-ir-strip.h" + #include "slang-emit-source-writer.h" #include "slang-emit-c-like.h" @@ -472,6 +474,15 @@ String emitEntryPoint( break; } + // If we have obfuscation strip + if (compileRequest->getLinkage()->m_obfuscateCode) + { + IRStripOptions options; + options.shouldStripNameHints = true; + options.stripSourceLocs = true; + stripFrontEndOnlyInstructions(irModule, options); + } + // The resource-based specialization pass above // may create specialized versions of functions, but // it does not try to completely eliminate the original diff --git a/source/slang/slang-ir-link.cpp b/source/slang/slang-ir-link.cpp index 4e1a03bb2..2d497158d 100644 --- a/source/slang/slang-ir-link.cpp +++ b/source/slang/slang-ir-link.cpp @@ -803,8 +803,13 @@ IRFunc* specializeIRForEntryPoint( RefPtr<IRSpecSymbol> sym; if (!context->getSymbols().TryGetValue(mangledName, sym)) { - SLANG_UNEXPECTED("no matching IR symbol"); - return nullptr; + String hashedName = getHashedName(mangledName.getUnownedSlice()); + + if (!context->getSymbols().TryGetValue(hashedName, sym)) + { + SLANG_UNEXPECTED("no matching IR symbol"); + return nullptr; + } } // Note: it is possible that `sym` shows multiple diff --git a/source/slang/slang-ir-strip.cpp b/source/slang/slang-ir-strip.cpp index 1cf8267d1..33ae5a25e 100644 --- a/source/slang/slang-ir-strip.cpp +++ b/source/slang/slang-ir-strip.cpp @@ -35,6 +35,11 @@ static void _stripFrontEndOnlyInstructionsRec( return; } + if (options.stripSourceLocs) + { + inst->sourceLoc = SourceLoc(); + } + IRInst* nextChild = nullptr; for( IRInst* child = inst->getFirstDecorationOrChild(); child; child = nextChild ) { diff --git a/source/slang/slang-ir-strip.h b/source/slang/slang-ir-strip.h index 50f10ba7d..952b0dca3 100644 --- a/source/slang/slang-ir-strip.h +++ b/source/slang/slang-ir-strip.h @@ -8,6 +8,7 @@ namespace Slang struct IRStripOptions { bool shouldStripNameHints = false; + bool stripSourceLocs = false; }; /// Strip out instructions that should only be used by the front-end. diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index e19ab2223..e8e2ff61a 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -305,14 +305,17 @@ struct SharedIRGenContext SharedIRGenContext( Session* session, DiagnosticSink* sink, + bool obfuscateCode, ModuleDecl* mainModuleDecl = nullptr) : m_session(session) , m_sink(sink) + , m_obfuscateCode(obfuscateCode) , m_mainModuleDecl(mainModuleDecl) {} Session* m_session = nullptr; DiagnosticSink* m_sink = nullptr; + bool m_obfuscateCode = false; ModuleDecl* m_mainModuleDecl = nullptr; // The "global" environment for mapping declarations to their IR values. @@ -1062,7 +1065,14 @@ static void addLinkageDecoration( IRInst* inst, Decl* decl) { - addLinkageDecoration(context, inst, decl, getMangledName(decl).getUnownedSlice()); + String mangledName = getMangledName(decl); + + if (context->shared->m_obfuscateCode) + { + mangledName = getHashedName(mangledName.getUnownedSlice()); + } + + addLinkageDecoration(context, inst, decl, mangledName.getUnownedSlice()); } IRStructKey* getInterfaceRequirementKey( @@ -1820,6 +1830,11 @@ static void addNameHint( IRInst* inst, Decl* decl) { + if (context->shared->m_obfuscateCode) + { + return; + } + String name = getNameForNameHint(context, decl); if(name.getLength() == 0) return; @@ -1832,6 +1847,11 @@ static void addNameHint( IRInst* inst, char const* text) { + if (context->shared->m_obfuscateCode) + { + return; + } + context->irBuilder->addNameHintDecoration(inst, UnownedTerminatedStringSlice(text)); } @@ -6598,6 +6618,7 @@ IRModule* generateIRForTranslationUnit( SharedIRGenContext sharedContextStorage( translationUnit->getSession(), translationUnit->compileRequest->getSink(), + translationUnit->compileRequest->getLinkage()->m_obfuscateCode, translationUnit->getModuleDecl()); SharedIRGenContext* sharedContext = &sharedContextStorage; @@ -6706,7 +6727,11 @@ IRModule* generateIRForTranslationUnit( // by setting up the options for the stripping pass appropriately. // IRStripOptions stripOptions; - stripOptions.shouldStripNameHints = compileRequest->obfuscateCode; + + Linkage* linkage = compileRequest->getLinkage(); + + stripOptions.shouldStripNameHints = linkage->m_obfuscateCode; + stripOptions.stripSourceLocs = linkage->m_obfuscateCode; stripFrontEndOnlyInstructions(module, stripOptions); @@ -6765,7 +6790,9 @@ struct SpecializedComponentTypeIRGenContext : ComponentTypeVisitor SharedIRGenContext sharedContextStorage( session, - sink); + sink, + linkage->m_obfuscateCode + ); SharedIRGenContext* sharedContext = &sharedContextStorage; IRGenContext contextStorage(sharedContext); @@ -7174,7 +7201,8 @@ RefPtr<IRModule> TargetProgram::createIRModuleForLayout(DiagnosticSink* sink) SharedIRGenContext sharedContextStorage( session, - sink); + sink, + linkage->m_obfuscateCode); auto sharedContext = &sharedContextStorage; IRLayoutGenContext contextStorage(sharedContext); diff --git a/source/slang/slang-mangle.cpp b/source/slang/slang-mangle.cpp index 16f7b64bb..fbeb5c9bf 100644 --- a/source/slang/slang-mangle.cpp +++ b/source/slang/slang-mangle.cpp @@ -474,5 +474,15 @@ namespace Slang return context.sb.ProduceString(); } + String getHashedName(const UnownedStringSlice& mangledName) + { + uint64_t hash = GetHashCode64(mangledName.begin(), mangledName.size()); + + StringBuilder builder; + builder << "_Sh"; + builder.append(hash, 16); + + return builder; + } } diff --git a/source/slang/slang-mangle.h b/source/slang/slang-mangle.h index 5e03f8228..186f8bae4 100644 --- a/source/slang/slang-mangle.h +++ b/source/slang/slang-mangle.h @@ -14,6 +14,8 @@ namespace Slang String getMangledName(DeclRef<Decl> const & declRef); String getMangledName(DeclRefBase const & declRef); + String getHashedName(const UnownedStringSlice& mangledName); + String getMangledNameForConformanceWitness( Type* sub, Type* sup); diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index 89e5b8c14..915bea726 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -882,8 +882,7 @@ struct OptionsParser } else if (argStr == "-obfuscate") { - requestImpl->getFrontEndReq()->obfuscateCode = true; - requestImpl->getBackEndReq()->obfuscateCode = true; + requestImpl->getLinkage()->m_obfuscateCode = true; } else if (argStr == "-file-system") { diff --git a/source/slang/slang-state-serialize.cpp b/source/slang/slang-state-serialize.cpp index f8baa23bb..113a422fa 100644 --- a/source/slang/slang-state-serialize.cpp +++ b/source/slang/slang-state-serialize.cpp @@ -355,7 +355,7 @@ static bool _isStorable(const PathInfo::Type type) dst->useUnknownImageFormatAsDefault = request->getBackEndReq()->useUnknownImageFormatAsDefault; - dst->obfuscateCode = request->getBackEndReq()->obfuscateCode; + dst->obfuscateCode = linkage->m_obfuscateCode; dst->defaultMatrixLayoutMode = linkage->defaultMatrixLayoutMode; } @@ -889,9 +889,7 @@ struct LoadContext spSetPassThrough(externalRequest, SlangPassThrough(request->passThrough)); request->getBackEndReq()->useUnknownImageFormatAsDefault = requestState->useUnknownImageFormatAsDefault; - request->getBackEndReq()->obfuscateCode = requestState->obfuscateCode; - - request->getFrontEndReq()->obfuscateCode = requestState->obfuscateCode; + linkage->m_obfuscateCode = requestState->obfuscateCode; linkage->setMatrixLayoutMode(requestState->defaultMatrixLayoutMode); } |
