github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/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.KASANRead},
    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.KASANRead},
    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  		t.Run(test.name, func(t *testing.T) {
    75  			conf, err := kconfig.ParseConfigData([]byte(base), "base")
    76  			if err != nil {
    77  				t.Fatal(err)
    78  			}
    79  			setLinuxSanitizerConfigs(conf, test.types, &debugtracer.NullTracer{})
    80  			test.test(t, conf)
    81  		})
    82  	}
    83  }
    84  
    85  // Ensure we don't add "rcupdate.rcu_cpu_stall_suppress=1" twice.
    86  func TestNoDoubleRcuSuppress(t *testing.T) {
    87  	const base = `
    88  CONFIG_CMDLINE="param1=a rcupdate.rcu_cpu_stall_suppress=1 param2=b"
    89  CONFIG_BUG=y
    90  CONFIG_KASAN=y
    91  `
    92  	conf, err := kconfig.ParseConfigData([]byte(base), "base")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	setLinuxSanitizerConfigs(conf, []crash.Type{crash.Warning}, &debugtracer.NullTracer{})
    97  	assert.Equal(t,
    98  		`"param1=a rcupdate.rcu_cpu_stall_suppress=1 param2=b"`,
    99  		conf.Value("CMDLINE"),
   100  	)
   101  }
   102  
   103  func assertConfigs(t *testing.T, cf *kconfig.ConfigFile, names ...string) {
   104  	var setConfigs []string
   105  	for _, name := range names {
   106  		if cf.Value(name) == kconfig.Yes {
   107  			setConfigs = append(setConfigs, name)
   108  		}
   109  	}
   110  	assert.ElementsMatch(t, setConfigs, names)
   111  }