Resolve escaping error in genrules sources with $
'$' cannot be included in the genrule source file name as it is an
invalid character.
One workaround to include a file with '$' in the filename is to use
glob(*) and match pattern.
However, shell currently evaluates the '$' sign and leads to unexpected behavior.
This change fixes the issue by shell-escaping the filepath in generating
build actions.
Test: m
Bug: b/194980152
Change-Id: I6fd919c568b5b6526e4de5155104a08ecadab307
diff --git a/genrule/genrule.go b/genrule/genrule.go
index 6686c87..7a0dac3 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -26,6 +26,7 @@
"strings"
"android/soong/bazel/cquery"
+
"github.com/google/blueprint"
"github.com/google/blueprint/bootstrap"
"github.com/google/blueprint/proptools"
@@ -468,6 +469,7 @@
return "SOONG_ERROR", nil
}
+ // Apply shell escape to each cases to prevent source file paths containing $ from being evaluated in shell
switch name {
case "location":
if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
@@ -481,15 +483,15 @@
return reportError("default label %q has multiple files, use $(locations %s) to reference it",
firstLabel, firstLabel)
}
- return paths[0], nil
+ return proptools.ShellEscape(paths[0]), nil
case "in":
- return strings.Join(cmd.PathsForInputs(srcFiles), " "), nil
+ return strings.Join(proptools.ShellEscapeList(cmd.PathsForInputs(srcFiles)), " "), nil
case "out":
var sandboxOuts []string
for _, out := range task.out {
sandboxOuts = append(sandboxOuts, cmd.PathForOutput(out))
}
- return strings.Join(sandboxOuts, " "), nil
+ return strings.Join(proptools.ShellEscapeList(sandboxOuts), " "), nil
case "depfile":
referencedDepfile = true
if !Bool(g.properties.Depfile) {
@@ -497,7 +499,7 @@
}
return "__SBOX_DEPFILE__", nil
case "genDir":
- return cmd.PathForOutput(task.genDir), nil
+ return proptools.ShellEscape(cmd.PathForOutput(task.genDir)), nil
default:
if strings.HasPrefix(name, "location ") {
label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
@@ -509,7 +511,7 @@
return reportError("label %q has multiple files, use $(locations %s) to reference it",
label, label)
}
- return paths[0], nil
+ return proptools.ShellEscape(paths[0]), nil
} else {
return reportError("unknown location label %q is not in srcs, out, tools or tool_files.", label)
}
@@ -520,7 +522,7 @@
if len(paths) == 0 {
return reportError("label %q has no files", label)
}
- return strings.Join(paths, " "), nil
+ return proptools.ShellEscape(strings.Join(paths, " ")), nil
} else {
return reportError("unknown locations label %q is not in srcs, out, tools or tool_files.", label)
}
diff --git a/genrule/genrule_test.go b/genrule/genrule_test.go
index b9be1f7..cd941cc 100644
--- a/genrule/genrule_test.go
+++ b/genrule/genrule_test.go
@@ -422,7 +422,7 @@
allowMissingDependencies: true,
- expect: "cat ***missing srcs :missing*** > __SBOX_SANDBOX_DIR__/out/out",
+ expect: "cat '***missing srcs :missing***' > __SBOX_SANDBOX_DIR__/out/out",
},
{
name: "tool allow missing dependencies",
@@ -434,7 +434,7 @@
allowMissingDependencies: true,
- expect: "***missing tool :missing*** > __SBOX_SANDBOX_DIR__/out/out",
+ expect: "'***missing tool :missing***' > __SBOX_SANDBOX_DIR__/out/out",
},
}
@@ -878,6 +878,92 @@
android.AssertDeepEquals(t, "output deps", expectedOutputFiles, gen.outputDeps.Strings())
}
+func TestGenruleWithGlobPaths(t *testing.T) {
+ testcases := []struct {
+ name string
+ bp string
+ additionalFiles android.MockFS
+ expectedCmd string
+ }{
+ {
+ name: "single file in directory with $ sign",
+ bp: `
+ genrule {
+ name: "gen",
+ srcs: ["inn*.txt"],
+ out: ["out.txt"],
+ cmd: "cp $(in) $(out)",
+ }
+ `,
+ additionalFiles: android.MockFS{"inn$1.txt": nil},
+ expectedCmd: "cp 'inn$1.txt' __SBOX_SANDBOX_DIR__/out/out.txt",
+ },
+ {
+ name: "multiple file in directory with $ sign",
+ bp: `
+ genrule {
+ name: "gen",
+ srcs: ["inn*.txt"],
+ out: ["."],
+ cmd: "cp $(in) $(out)",
+ }
+ `,
+ additionalFiles: android.MockFS{"inn$1.txt": nil, "inn$2.txt": nil},
+ expectedCmd: "cp 'inn$1.txt' 'inn$2.txt' __SBOX_SANDBOX_DIR__/out",
+ },
+ {
+ name: "file in directory with other shell unsafe character",
+ bp: `
+ genrule {
+ name: "gen",
+ srcs: ["inn*.txt"],
+ out: ["out.txt"],
+ cmd: "cp $(in) $(out)",
+ }
+ `,
+ additionalFiles: android.MockFS{"inn@1.txt": nil},
+ expectedCmd: "cp 'inn@1.txt' __SBOX_SANDBOX_DIR__/out/out.txt",
+ },
+ {
+ name: "glob location param with filepath containing $",
+ bp: `
+ genrule {
+ name: "gen",
+ srcs: ["**/inn*"],
+ out: ["."],
+ cmd: "cp $(in) $(location **/inn*)",
+ }
+ `,
+ additionalFiles: android.MockFS{"a/inn$1.txt": nil},
+ expectedCmd: "cp 'a/inn$1.txt' 'a/inn$1.txt'",
+ },
+ {
+ name: "glob locations param with filepath containing $",
+ bp: `
+ genrule {
+ name: "gen",
+ tool_files: ["**/inn*"],
+ out: ["out.txt"],
+ cmd: "cp $(locations **/inn*) $(out)",
+ }
+ `,
+ additionalFiles: android.MockFS{"a/inn$1.txt": nil},
+ expectedCmd: "cp '__SBOX_SANDBOX_DIR__/tools/src/a/inn$1.txt' __SBOX_SANDBOX_DIR__/out/out.txt",
+ },
+ }
+
+ for _, test := range testcases {
+ t.Run(test.name, func(t *testing.T) {
+ result := android.GroupFixturePreparers(
+ prepareForGenRuleTest,
+ android.FixtureMergeMockFs(test.additionalFiles),
+ ).RunTestWithBp(t, test.bp)
+ gen := result.Module("gen", "").(*Module)
+ android.AssertStringEquals(t, "command", test.expectedCmd, gen.rawCommands[0])
+ })
+ }
+}
+
type testTool struct {
android.ModuleBase
outputFile android.Path