diff options
Diffstat (limited to 'source/slang')
| -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); } |
