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/compiler-core/slang-artifact-desc-util.cpp | 7 +++--- source/compiler-core/slang-artifact.h | 6 ++++- source/compiler-core/slang-source-loc.cpp | 29 ++++++++++++++++------- source/compiler-core/slang-source-loc.h | 20 ++++++++++++---- 4 files changed, 45 insertions(+), 17 deletions(-) (limited to 'source/compiler-core') diff --git a/source/compiler-core/slang-artifact-desc-util.cpp b/source/compiler-core/slang-artifact-desc-util.cpp index ee014daf0..08b7c8412 100644 --- a/source/compiler-core/slang-artifact-desc-util.cpp +++ b/source/compiler-core/slang-artifact-desc-util.cpp @@ -175,6 +175,7 @@ bool isDerivedFrom(ENUM_TYPE kind, ENUM_TYPE base) { return g_table##ENUM_TYPE.i x(HumanText, Text) \ x(Source, Text) \ x(Assembly, Text) \ + x(Json, Text) \ x(Instance, Base) #define SLANG_ARTIFACT_KIND_ENTRY(TYPE, PARENT) { Index(ArtifactKind::TYPE), Index(ArtifactKind::PARENT), #TYPE }, @@ -239,13 +240,13 @@ SLANG_HIERARCHICAL_ENUM(ArtifactPayload, SLANG_ARTIFACT_PAYLOAD, SLANG_ARTIFACT_ x(Unknown, Base) \ x(CodeLike, Base) \ x(Kernel, CodeLike) \ - x(Host, CodeLike) + x(Host, CodeLike) \ + x(Obfuscated, Base) #define SLANG_ARTIFACT_STYLE_ENTRY(TYPE, PARENT) { Index(ArtifactStyle::TYPE), Index(ArtifactStyle::PARENT), #TYPE }, SLANG_HIERARCHICAL_ENUM(ArtifactStyle, SLANG_ARTIFACT_STYLE, SLANG_ARTIFACT_STYLE_ENTRY) - /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactDescUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ /* static */ArtifactDesc ArtifactDescUtil::makeDescForCompileTarget(SlangCompileTarget target) @@ -555,7 +556,7 @@ static const KindExtension g_cpuKindExts[] = // Visual Studio. It's used here for source map. if (slice == toSlice("map")) { - return ArtifactDesc::make(ArtifactKind::Text, ArtifactPayload::SourceMap); + return ArtifactDesc::make(ArtifactKind::Json, ArtifactPayload::SourceMap); } if (slice == toSlice("pdb")) diff --git a/source/compiler-core/slang-artifact.h b/source/compiler-core/slang-artifact.h index c51ee729a..cc4d3e9fd 100644 --- a/source/compiler-core/slang-artifact.h +++ b/source/compiler-core/slang-artifact.h @@ -107,6 +107,8 @@ enum class ArtifactKind : uint8_t Instance, ///< Primary representation is an interface/class instance + Json, ///< It's JSON + CountOf, }; @@ -180,7 +182,7 @@ enum class ArtifactPayload : uint8_t PdbDebugInfo, ///< PDB debug info - SourceMap, ///< SourceMap + SourceMap, ///< A source map CountOf, }; @@ -207,6 +209,8 @@ enum class ArtifactStyle : uint8_t Kernel, ///< Compiled as `GPU kernel` style. Host, ///< Compiled in `host` style + Obfuscated, ///< Holds something specific to obfuscation, such as an obfuscated source map + CountOf, }; diff --git a/source/compiler-core/slang-source-loc.cpp b/source/compiler-core/slang-source-loc.cpp index 95c6f5db3..1314d8066 100644 --- a/source/compiler-core/slang-source-loc.cpp +++ b/source/compiler-core/slang-source-loc.cpp @@ -189,7 +189,7 @@ void SourceView::addDefaultLineDirective(SourceLoc directiveLoc) m_entries.add(entry); } -HumaneSourceLoc SourceView::getHumaneLoc(SourceLoc loc, SourceLocType type) +HandleSourceLoc SourceView::getHandleLoc(SourceLoc loc, SourceLocType type) { const int offset = m_range.getOffset(loc); @@ -206,9 +206,9 @@ HumaneSourceLoc SourceView::getHumaneLoc(SourceLoc loc, SourceLocType type) // that an IDE expects us to use when reporting locations?) const int columnIndex = m_sourceFile->calcColumnIndex(lineIndex, offset); - HumaneSourceLoc humaneLoc; - humaneLoc.column = columnIndex + 1; - humaneLoc.line = lineIndex + 1; + HandleSourceLoc handleLoc; + handleLoc.column = columnIndex + 1; + handleLoc.line = lineIndex + 1; // Make up a default entry StringSlicePool::Handle pathHandle = StringSlicePool::Handle(0); @@ -219,16 +219,27 @@ HumaneSourceLoc SourceView::getHumaneLoc(SourceLoc loc, SourceLocType type) { const Entry& entry = m_entries[entryIndex]; // Adjust the line - humaneLoc.line += entry.m_lineAdjust; + handleLoc.line += entry.m_lineAdjust; // Get the pathHandle.. pathHandle = entry.m_pathHandle; } - humaneLoc.pathInfo = _getPathInfoFromHandle(pathHandle); + handleLoc.pathHandle = pathHandle; + return handleLoc; +} + +HumaneSourceLoc SourceView::getHumaneLoc(SourceLoc loc, SourceLocType type) +{ + HandleSourceLoc handleLoc = getHandleLoc(loc, type); + + HumaneSourceLoc humaneLoc; + humaneLoc.column = handleLoc.column; + humaneLoc.line = handleLoc.line; + humaneLoc.pathInfo = _getPathInfoFromHandle(handleLoc.pathHandle); return humaneLoc; } -PathInfo SourceView::_getPathInfo() const +PathInfo SourceView::getViewPathInfo() const { if (m_viewPath.getLength()) { @@ -247,7 +258,7 @@ PathInfo SourceView::_getPathInfoFromHandle(StringSlicePool::Handle pathHandle) // If there is no override path, then just the source files path if (pathHandle == StringSlicePool::Handle(0)) { - return _getPathInfo(); + return getViewPathInfo(); } else { @@ -259,7 +270,7 @@ PathInfo SourceView::getPathInfo(SourceLoc loc, SourceLocType type) { if (type == SourceLocType::Actual) { - return _getPathInfo(); + return getViewPathInfo(); } const int entryIndex = findEntryIndex(loc); diff --git a/source/compiler-core/slang-source-loc.h b/source/compiler-core/slang-source-loc.h index 66e4c0475..15c7c0a53 100644 --- a/source/compiler-core/slang-source-loc.h +++ b/source/compiler-core/slang-source-loc.h @@ -279,6 +279,13 @@ struct HumaneSourceLoc Int column = 0; }; +// Same as HumaneSourceLoc but stores the path only as a handle. +struct HandleSourceLoc +{ + StringSlicePool::Handle pathHandle = StringSlicePool::Handle(0); + Int line = 0; + Int column = 0; +}; /* A SourceView maps to a single span of SourceLoc range and is equivalent to a single include or more precisely use of a source file. It is distinct from a SourceFile - because a SourceFile may be included multiple times, with different interpretations (depending @@ -336,6 +343,10 @@ class SourceView /// Type determines if the location wanted is the original, or the 'normal' (which modifys behavior based on #line directives) HumaneSourceLoc getHumaneLoc(SourceLoc loc, SourceLocType type = SourceLocType::Nominal); + /// Get the humane location, but store the path as a handle + HandleSourceLoc getHandleLoc(SourceLoc loc, SourceLocType type = SourceLocType::Nominal); + + /// Get the path associated with a location PathInfo getPathInfo(SourceLoc loc, SourceLocType type = SourceLocType::Nominal); @@ -346,6 +357,10 @@ class SourceView /// For the original source file (ie not an include) - the view will have an initiating source loc of SourceLoc(0) SourceLoc getInitiatingSourceLoc() const { return m_initiatingSourceLoc; } + /// Gets the pathInfo for this view. It may be different from the m_sourceFile's if the path has been + /// overridden by m_viewPath + PathInfo getViewPathInfo() const; + /// Ctor SourceView(SourceFile* sourceFile, SourceRange range, const String* viewPath, SourceLoc initiatingSourceLoc): m_range(range), @@ -361,10 +376,7 @@ class SourceView protected: /// Get the pathInfo from a string handle. If it's 0, it will return the _getPathInfo PathInfo _getPathInfoFromHandle(StringSlicePool::Handle pathHandle) const; - /// Gets the pathInfo for this view. It may be different from the m_sourceFile's if the path has been - /// overridden by m_viewPath - PathInfo _getPathInfo() const; - + String m_viewPath; ///< Path to this view. If empty the path is the path to the SourceView SourceLoc m_initiatingSourceLoc; ///< An optional source loc that defines where this view was initiated from. SourceLoc(0) if not defined. -- cgit v1.2.3