From 08977f5d5e344fa0ac0b80af0b72fc3f1468d6a5 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 8 Sep 2022 12:55:42 +0200 Subject: executor: add setup_ext_test extension point The extension point allows to setup the test process in a custom way without overwriting any of the existing files. --- executor/common.h | 3 +++ executor/common_ext.h | 3 +++ executor/common_ext_example.h | 7 +++++++ executor/common_ext_test.go | 29 +++++++++++++++++++++++++++++ pkg/csource/csource_test.go | 1 + pkg/csource/generated.go | 3 +++ 6 files changed, 46 insertions(+) diff --git a/executor/common.h b/executor/common.h index 2ee0d5bd2..fb7b04f4a 100644 --- a/executor/common.h +++ b/executor/common.h @@ -668,6 +668,9 @@ static void loop(void) #if SYZ_HAVE_SETUP_TEST setup_test(); #endif +#if SYZ_HAVE_SETUP_EXT_TEST + setup_ext_test(); +#endif #if GOOS_akaros #if SYZ_EXECUTOR dup2(child_pipe[0], kInPipeFd); diff --git a/executor/common_ext.h b/executor/common_ext.h index 22361649f..edbfcac0b 100644 --- a/executor/common_ext.h +++ b/executor/common_ext.h @@ -9,3 +9,6 @@ // This file can also define SYZ_HAVE_SETUP_EXT to 1 and provide // void setup_ext() function that will be called during VM setup. + +// This file can also define SYZ_HAVE_SETUP_EXT_TEST to 1 and provide +// void setup_ext_test() function that will be called during setup of each test process. diff --git a/executor/common_ext_example.h b/executor/common_ext_example.h index 0299b6d4f..0cabcb75c 100644 --- a/executor/common_ext_example.h +++ b/executor/common_ext_example.h @@ -8,3 +8,10 @@ static void setup_ext() { debug("example setup_ext called\n"); } + +#define SYZ_HAVE_SETUP_EXT_TEST 1 +static void setup_ext_test() +{ + // See TestCommonExt. + *(uint64*)(SYZ_DATA_OFFSET + 0x1234) = 0xbadc0ffee; +} diff --git a/executor/common_ext_test.go b/executor/common_ext_test.go index 45ba3a3cf..2815b095a 100644 --- a/executor/common_ext_test.go +++ b/executor/common_ext_test.go @@ -10,6 +10,8 @@ import ( "time" "github.com/google/syzkaller/pkg/csource" + "github.com/google/syzkaller/pkg/ipc" + "github.com/google/syzkaller/pkg/ipc/ipcconfig" "github.com/google/syzkaller/pkg/osutil" "github.com/google/syzkaller/prog" ) @@ -31,4 +33,31 @@ func TestCommonExt(t *testing.T) { if !bytes.Contains(out, []byte("example setup_ext called")) { t.Fatalf("setup_ext wasn't called:\n%s", out) } + + // The example setup_ext_test does: + // *(uint64*)(SYZ_DATA_OFFSET + 0x1234) = 0xbadc0ffee; + // The following program tests that that value is present at 0x1234. + test := `syz_compare(&(0x7f0000001234)="", 0x8, &(0x7f0000000000)=@blob="eeffc0ad0b000000", AUTO)` + p, err := target.Deserialize([]byte(test), prog.Strict) + if err != nil { + t.Fatal(err) + } + cfg, opts, err := ipcconfig.Default(target) + if err != nil { + t.Fatal(err) + } + cfg.Executor = bin + cfg.Flags |= ipc.FlagDebug + env, err := ipc.MakeEnv(cfg, 0) + if err != nil { + t.Fatalf("failed to create env: %v", err) + } + defer env.Close() + _, info, _, err := env.Exec(opts, p) + if err != nil { + t.Fatal(err) + } + if call := info.Calls[0]; (call.Flags&ipc.CallFinished) == 0 || call.Errno != 0 { + t.Fatalf("bad call result: flags=%x errno=%v", call.Flags, call.Errno) + } } diff --git a/pkg/csource/csource_test.go b/pkg/csource/csource_test.go index 491b84b03..bfeb8f6a7 100644 --- a/pkg/csource/csource_test.go +++ b/pkg/csource/csource_test.go @@ -151,6 +151,7 @@ func TestExecutorMacros(t *testing.T) { expected["SYZ_HAVE_SETUP_LOOP"] = true expected["SYZ_HAVE_RESET_LOOP"] = true expected["SYZ_HAVE_SETUP_TEST"] = true + expected["SYZ_TEST_COMMON_EXT_EXAMPLE"] = true macros := regexp.MustCompile("SYZ_[A-Za-z0-9_]+").FindAllString(commonHeader, -1) for _, macro := range macros { if strings.HasPrefix(macro, "SYZ_HAVE_") { diff --git a/pkg/csource/generated.go b/pkg/csource/generated.go index 711e74801..6713c04c9 100644 --- a/pkg/csource/generated.go +++ b/pkg/csource/generated.go @@ -11370,6 +11370,9 @@ static void loop(void) #if SYZ_HAVE_SETUP_TEST setup_test(); #endif +#if SYZ_HAVE_SETUP_EXT_TEST + setup_ext_test(); +#endif #if GOOS_akaros #if SYZ_EXECUTOR dup2(child_pipe[0], kInPipeFd); -- cgit mrf-deployment