From d4f99c8bac8b28f18c864a717d8833db6a1c872d Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 22 Mar 2023 12:04:33 -0400 Subject: Source map obfuscation (#2717) * #include an absolute path didn't work - because paths were taken to always be relative. * WIP source map. * Split out handling of RttiTypeFuncs to a map type. * Make RttiTypeFuncsMap hold default impls. * Slightly more sophisticated RttiTypeFuncsMap * Source map decoding. * Fix tabs. * Fix asserts due to negative values. * Use less obscure mechanisms in SourceMap. * Source map decoding. Simplifying SourceMap usage. * First attempt at ouputting a source map as part of emit. * Added support for -source-map option. SourceMap is added to the artifact. * Small improvements around column calculation in SourceWriter. * Source Loc obuscation WIP. * Fix some issues around SourceMap obfuscation. * Split out obfuscation into its own file. * Keep obfuscated SourceMap even through serialization bottleneck. --- source/slang/slang-lower-to-ir.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'source/slang/slang-lower-to-ir.cpp') diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index 86df89702..f84f17886 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -3,6 +3,10 @@ #include "../../slang.h" +#include "../core/slang-random-generator.h" +#include "../core/slang-hash.h" +#include "../core/slang-char-util.h" + #include "slang-check.h" #include "slang-ir.h" #include "slang-ir-constexpr.h" @@ -21,6 +25,8 @@ #include "slang-ir-string-hash.h" #include "slang-ir-clone.h" #include "slang-ir-lower-error-handling.h" +#include "slang-ir-obfuscate-loc.h" + #include "slang-mangle.h" #include "slang-type-layout.h" #include "slang-visitor.h" @@ -9299,10 +9305,17 @@ RefPtr generateIRForTranslationUnit( Linkage* linkage = compileRequest->getLinkage(); stripOptions.shouldStripNameHints = linkage->m_obfuscateCode; - stripOptions.stripSourceLocs = linkage->m_obfuscateCode; + // If we are generating an obfuscated source map, we don't want to strip locs, + // we want to generate *new* locs that can be mapped (via source map) + // back to *actual* source. + // + // We don't do the obfuscation remapping here, because DCE and other passes may + // change what locs are actually needed, we need to be sure + // that if we have obfuscation enabled we don't forget to obfuscate. + stripOptions.stripSourceLocs = linkage->m_obfuscateCode && !linkage->m_generateSourceMap; stripFrontEndOnlyInstructions(module, stripOptions); - + // Stripping out decorations could leave some dead code behind // in the module, and in some cases that extra code is also // undesirable (e.g., the string literals referenced by name-hint @@ -9314,6 +9327,12 @@ RefPtr generateIRForTranslationUnit( IRDeadCodeEliminationOptions options; options.keepExportsAlive = true; eliminateDeadCode(module, options); + + if (linkage->m_obfuscateCode && linkage->m_generateSourceMap) + { + // The obfuscated source map is stored on the module + obfuscateModuleLocs(module, compileRequest->getSourceManager()); + } } // TODO: consider doing some more aggressive optimizations -- cgit v1.2.3