From deeb8647012fc6362f1bb33842cf0842e16f13b7 Mon Sep 17 00:00:00 2001 From: Robert Stepinski Date: Fri, 11 Oct 2019 11:30:32 -0400 Subject: Add ability to obfuscate name when generating GLSL/HLSL source (#1075) --- slang.h | 3 +++ source/slang/slang-compiler.h | 3 +++ source/slang/slang-emit-c-like.cpp | 27 ++++++++++++++++++++------- source/slang/slang-options.cpp | 4 ++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/slang.h b/slang.h index e31689200..00957227a 100644 --- a/slang.h +++ b/slang.h @@ -560,6 +560,9 @@ extern "C" /* Skip code generation step, just check the code and generate layout */ SLANG_COMPILE_FLAG_NO_CODEGEN = 1 << 4, + /* Obfuscate shader names on release products */ + SLANG_COMPILE_FLAG_OBFUSCATE = 1 << 5, + /* Deprecated flags: kept around to allow existing applications to compile. Note that the relevant features will still be left in their default state. */ diff --git a/source/slang/slang-compiler.h b/source/slang/slang-compiler.h index ad90bb5ee..ebde8b6d3 100644 --- a/source/slang/slang-compiler.h +++ b/source/slang/slang-compiler.h @@ -1636,6 +1636,9 @@ namespace Slang // bool useUnknownImageFormatAsDefault = false; + // Remove name hints to help obfuscate code + // + bool obfuscateCode = false; private: RefPtr m_program; }; diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp index 6b828085e..991540782 100644 --- a/source/slang/slang-emit-c-like.cpp +++ b/source/slang/slang-emit-c-like.cpp @@ -637,7 +637,7 @@ String CLikeSourceEmitter::generateName(IRInst* inst) // if(auto nameHintDecoration = inst->findDecoration()) { - // The name we output will basically be: + // The (non-obfuscated) name we output will basically be: // // _ // @@ -645,15 +645,28 @@ String CLikeSourceEmitter::generateName(IRInst* inst) // and we will omit the underscore if the (scrubbed) // name hint already ends with one. // - - String nameHint = nameHintDecoration->getName(); - nameHint = scrubName(nameHint); + // The obfuscated name we output will simply be: + // + // _ + // StringBuilder sb; - sb.append(nameHint); - // Avoid introducing a double underscore - if(!nameHint.endsWith("_")) + if (!m_compileRequest->obfuscateCode) + { + + String nameHint = nameHintDecoration->getName(); + nameHint = scrubName(nameHint); + + sb.append(nameHint); + + // Avoid introducing a double underscore + if (!nameHint.endsWith("_")) + { + sb.append("_"); + } + } + else { sb.append("_"); } diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index 94f82ef31..31a5c7c7f 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -775,6 +775,10 @@ struct OptionsParser { requestImpl->getBackEndReq()->useUnknownImageFormatAsDefault = true; } + else if (argStr == "-obfuscate") + { + requestImpl->getBackEndReq()->obfuscateCode = true; + } else if (argStr == "-file-system") { String name; -- cgit v1.2.3