| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "os" |
| "path/filepath" |
| "testing" |
| |
| "go.fuchsia.dev/fuchsia/src/testing/qemu" |
| ) |
| |
| const cmdline = "kernel.halt-on-panic=true kernel.bypass-debuglog=true zircon.autorun.boot=/boot/bin/sh+-c+k" |
| |
| func zbiPath(t *testing.T) string { |
| ex, err := os.Executable() |
| if err != nil { |
| t.Fatal(err) |
| return "" |
| } |
| exPath := filepath.Dir(ex) |
| return filepath.Join(exPath, "../fuchsia.zbi") |
| } |
| |
| // See that `k crash` crashes the kernel. |
| func TestBasicCrash(t *testing.T) { |
| distro, err := qemu.Unpack() |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer distro.Delete() |
| arch, err := distro.TargetCPU() |
| if err != nil { |
| t.Fatal(err) |
| } |
| |
| i := distro.Create(qemu.Params{ |
| Arch: arch, |
| ZBI: zbiPath(t), |
| AppendCmdline: cmdline, |
| }) |
| |
| err = i.Start() |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer i.Kill() |
| |
| // Wait for the system to finish booting. |
| i.WaitForLogMessage("usage: k <command>") |
| |
| // Crash the kernel. |
| i.RunCommand("k crash") |
| |
| // See that it panicked. |
| i.WaitForLogMessage("ZIRCON KERNEL PANIC") |
| i.WaitForLogMessage("{{{bt:0:") |
| } |
| |
| // See that an SMAP violation is fatal. |
| func TestSMAPViolation(t *testing.T) { |
| distro, err := qemu.Unpack() |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer distro.Delete() |
| arch, err := distro.TargetCPU() |
| if err != nil { |
| t.Fatal(err) |
| } |
| if arch != qemu.X64 { |
| t.Skipf("Skipping test. This test only supports x64 targets.\n") |
| return |
| } |
| |
| i := distro.Create(qemu.Params{ |
| Arch: arch, |
| ZBI: zbiPath(t), |
| AppendCmdline: cmdline, |
| }) |
| |
| err = i.Start() |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer i.Kill() |
| |
| // Wait for the system to finish booting. |
| i.WaitForLogMessage("usage: k <command>") |
| |
| // Crash the kernel by violating SMAP. |
| i.RunCommand("k crash_user_read") |
| |
| // See that an SMAP failure was identified and that the kernel panicked. |
| i.WaitForLogMessage("SMAP failure") |
| i.WaitForLogMessage("ZIRCON KERNEL PANIC") |
| i.WaitForLogMessage("{{{bt:0:") |
| } |