nim: extra module mangling to prevent runtime dependence

Fix #207795
This commit is contained in:
Emery Hemingway 2023-06-23 23:02:49 +01:00
parent bf0b22ff02
commit eafc69b131
2 changed files with 51 additions and 0 deletions

View file

@ -103,6 +103,9 @@ in {
./nixbuild.patch ./nixbuild.patch
# Load libraries at runtime by absolute path # Load libraries at runtime by absolute path
./extra-mangling.patch
# Mangle store paths of modules to prevent runtime dependence.
] ++ lib.optional (!stdenv.hostPlatform.isWindows) ./toLocation.patch; ] ++ lib.optional (!stdenv.hostPlatform.isWindows) ./toLocation.patch;
configurePhase = '' configurePhase = ''

View file

@ -0,0 +1,48 @@
diff --git a/compiler/modulepaths.nim b/compiler/modulepaths.nim
index e80ea3fa6..8ecf27a85 100644
--- a/compiler/modulepaths.nim
+++ b/compiler/modulepaths.nim
@@ -70,6 +70,13 @@ proc checkModuleName*(conf: ConfigRef; n: PNode; doLocalError=true): FileIndex =
else:
result = fileInfoIdx(conf, fullPath)
+proc rot13(result: var string) =
+ for i, c in result:
+ case c
+ of 'a'..'m', 'A'..'M': result[i] = char(c.uint8 + 13)
+ of 'n'..'z', 'N'..'Z': result[i] = char(c.uint8 - 13)
+ else: discard
+
proc mangleModuleName*(conf: ConfigRef; path: AbsoluteFile): string =
## Mangle a relative module path to avoid path and symbol collisions.
##
@@ -78,9 +85,11 @@ proc mangleModuleName*(conf: ConfigRef; path: AbsoluteFile): string =
##
## Example:
## `foo-#head/../bar` becomes `@foo-@hhead@s..@sbar`
- "@m" & relativeTo(path, conf.projectPath).string.multiReplace(
+ result = "@m" & relativeTo(path, conf.projectPath).string.multiReplace(
{$os.DirSep: "@s", $os.AltSep: "@s", "#": "@h", "@": "@@", ":": "@c"})
+ rot13(result)
proc demangleModuleName*(path: string): string =
## Demangle a relative module path.
result = path.multiReplace({"@@": "@", "@h": "#", "@s": "/", "@m": "", "@c": ":"})
+ rot13(result)
diff --git a/compiler/msgs.nim b/compiler/msgs.nim
index 3f386cc61..054f7f647 100644
--- a/compiler/msgs.nim
+++ b/compiler/msgs.nim
@@ -659,8 +659,10 @@ proc uniqueModuleName*(conf: ConfigRef; fid: FileIndex): string =
for i in 0..<trunc:
let c = rel[i]
case c
- of 'a'..'z':
- result.add c
+ of 'a'..'m':
+ result.add char(c.uint8 + 13)
+ of 'n'..'z':
+ result.add char(c.uint8 - 13)
of {os.DirSep, os.AltSep}:
result.add 'Z' # because it looks a bit like '/'
of '.':