aboutsummaryrefslogtreecommitdiffstats
path: root/vm/isolated
diff options
context:
space:
mode:
authorJoey Jiao <joeyjiaojg@gmail.com>2021-03-29 14:37:22 +0800
committerDmitry Vyukov <dvyukov@google.com>2021-04-14 19:04:39 +0200
commite40a490e9c5e71bb3c0aca2a1ddcf7af4845635d (patch)
tree96fb8d65e2b684cd46c579a58a7c20dd5f0e3e39 /vm/isolated
parent5c9ed2bcc8d10336c1389d63db51a4bde97b16a0 (diff)
vm/adb: add startup_script config
Diffstat (limited to 'vm/isolated')
-rwxr-xr-xvm/isolated/isolated.go42
-rw-r--r--vm/isolated/isolated_test.go8
2 files changed, 7 insertions, 43 deletions
diff --git a/vm/isolated/isolated.go b/vm/isolated/isolated.go
index 3d69683e9..5bff5f1c0 100755
--- a/vm/isolated/isolated.go
+++ b/vm/isolated/isolated.go
@@ -214,46 +214,6 @@ func (inst *instance) waitRebootAndSSH(rebootTimeout int, sshTimeout time.Durati
return nil
}
-// Escapes double quotes(and nested double quote escapes). Ignores any other escapes.
-// Reference: https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
-func escapeDoubleQuotes(inp string) string {
- var ret strings.Builder
- for pos := 0; pos < len(inp); pos++ {
- // If inp[pos] is not a double quote or a backslash, just use
- // as is.
- if inp[pos] != '"' && inp[pos] != '\\' {
- ret.WriteByte(inp[pos])
- continue
- }
- // If it is a double quote, escape.
- if inp[pos] == '"' {
- ret.WriteString("\\\"")
- continue
- }
- // If we detect a backslash, reescape only if what it's already escaping
- // is a double-quotes.
- temp := ""
- j := pos
- for ; j < len(inp); j++ {
- if inp[j] == '\\' {
- temp += string(inp[j])
- continue
- }
- // If the escape corresponds to a double quotes, re-escape.
- // Else, just use as is.
- if inp[j] == '"' {
- temp = temp + temp + "\\\""
- } else {
- temp += string(inp[j])
- }
- break
- }
- ret.WriteString(temp)
- pos = j
- }
- return ret.String()
-}
-
func (inst *instance) repair() error {
log.Logf(2, "isolated: trying to ssh")
if err := inst.waitForSSH(30 * time.Minute); err != nil {
@@ -288,7 +248,7 @@ func (inst *instance) repair() error {
return fmt.Errorf("unable to read startup_script: %v", err)
}
c := string(contents)
- if err := inst.ssh(fmt.Sprintf("bash -c \"%v\"", escapeDoubleQuotes(c))); err != nil {
+ if err := inst.ssh(fmt.Sprintf("bash -c \"%v\"", vmimpl.EscapeDoubleQuotes(c))); err != nil {
return fmt.Errorf("failed to execute startup_script: %v", err)
}
log.Logf(2, "isolated: done executing startup_script")
diff --git a/vm/isolated/isolated_test.go b/vm/isolated/isolated_test.go
index 74bbff9f1..f1b4dbda0 100644
--- a/vm/isolated/isolated_test.go
+++ b/vm/isolated/isolated_test.go
@@ -3,7 +3,11 @@
package isolated
-import "testing"
+import (
+ "testing"
+
+ "github.com/google/syzkaller/vm/vmimpl"
+)
func TestEscapeDoubleQuotes(t *testing.T) {
testcases := []struct {
@@ -65,7 +69,7 @@ bash -c \"bash -c \\\"ls -al\\\"\"`,
},
}
for i, tc := range testcases {
- output := escapeDoubleQuotes(tc.inp)
+ output := vmimpl.EscapeDoubleQuotes(tc.inp)
if tc.expected != output {
t.Fatalf("%v: For input %v Expected escaped string %v got %v", i+1, tc.inp, tc.expected, output)
}