github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/vcs/linux_configs_test.go (about)

     1  // Copyright 2023 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package vcs
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/google/syzkaller/pkg/debugtracer"
    10  	"github.com/google/syzkaller/pkg/kconfig"
    11  	"github.com/google/syzkaller/pkg/report/crash"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestDropLinuxSanitizerConfigs(t *testing.T) {
    16  	tests := []struct {
    17  		name  string
    18  		types []crash.Type
    19  		test  func(*testing.T, *kconfig.ConfigFile)
    20  	}{
    21  		{
    22  			name:  "warning",
    23  			types: []crash.Type{crash.Warning},
    24  			test: func(t *testing.T, cf *kconfig.ConfigFile) {
    25  				assertConfigs(t, cf, "BUG")
    26  				assert.Equal(t,
    27  					`"param1=a param2=b rcupdate.rcu_cpu_stall_suppress=1"`,
    28  					cf.Value("CMDLINE"),
    29  				)
    30  			},
    31  		},
    32  		{
    33  			name:  "kasan bug",
    34  			types: []crash.Type{crash.KASAN},
    35  			test: func(t *testing.T, cf *kconfig.ConfigFile) {
    36  				assertConfigs(t, cf, "KASAN")
    37  			},
    38  		},
    39  		{
    40  			name:  "warning & kasan bug",
    41  			types: []crash.Type{crash.Warning, crash.KASAN},
    42  			test: func(t *testing.T, cf *kconfig.ConfigFile) {
    43  				assertConfigs(t, cf, "KASAN", "BUG")
    44  			},
    45  		},
    46  		{
    47  			name:  "lockdep",
    48  			types: []crash.Type{crash.LockdepBug},
    49  			test: func(t *testing.T, cf *kconfig.ConfigFile) {
    50  				assertConfigs(t, cf, "LOCKDEP", "PROVE_LOCKING")
    51  			},
    52  		},
    53  		{
    54  			name:  "rcu stall",
    55  			types: []crash.Type{crash.Hang},
    56  			test: func(t *testing.T, cf *kconfig.ConfigFile) {
    57  				assertConfigs(t, cf, "RCU_STALL_COMMON")
    58  				assert.Equal(t, `"param1=a param2=b"`, cf.Value("CMDLINE"))
    59  			},
    60  		},
    61  	}
    62  
    63  	const base = `
    64  CONFIG_CMDLINE="param1=a param2=b"
    65  CONFIG_BUG=y
    66  CONFIG_KASAN=y
    67  CONFIG_LOCKDEP=y
    68  CONFIG_RCU_STALL_COMMON=y
    69  CONFIG_UBSAN=y
    70  CONFIG_DEBUG_ATOMIC_SLEEP=y
    71  CONFIG_PROVE_LOCKING=y
    72  `
    73  	for _, test := range tests {
    74  		test := test
    75  		t.Run(test.name, func(t *testing.T) {
    76  			conf, err := kconfig.ParseConfigData([]byte(base), "base")
    77  			if err != nil {
    78  				t.Fatal(err)
    79  			}
    80  			setLinuxSanitizerConfigs(conf, test.types, &debugtracer.NullTracer{})
    81  			test.test(t, conf)
    82  		})
    83  	}
    84  }
    85  
    86  // Ensure we don't add "rcupdate.rcu_cpu_stall_suppress=1" twice.
    87  func TestNoDoubleRcuSuppress(t *testing.T) {
    88  	const base = `
    89  CONFIG_CMDLINE="param1=a rcupdate.rcu_cpu_stall_suppress=1 param2=b"
    90  CONFIG_BUG=y
    91  CONFIG_KASAN=y
    92  `
    93  	conf, err := kconfig.ParseConfigData([]byte(base), "base")
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	setLinuxSanitizerConfigs(conf, []crash.Type{crash.Warning}, &debugtracer.NullTracer{})
    98  	assert.Equal(t,
    99  		`"param1=a rcupdate.rcu_cpu_stall_suppress=1 param2=b"`,
   100  		conf.Value("CMDLINE"),
   101  	)
   102  }
   103  
   104  func assertConfigs(t *testing.T, cf *kconfig.ConfigFile, names ...string) {
   105  	var setConfigs []string
   106  	for _, name := range names {
   107  		if cf.Value(name) == kconfig.Yes {
   108  			setConfigs = append(setConfigs, name)
   109  		}
   110  	}
   111  	assert.ElementsMatch(t, setConfigs, names)
   112  }