Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/work/security_test.go

Documentation: cmd/go/internal/work

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package work
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  var goodCompilerFlags = [][]string{
    13  	{"-DFOO"},
    14  	{"-Dfoo=bar"},
    15  	{"-Ufoo"},
    16  	{"-Ufoo1"},
    17  	{"-F/Qt"},
    18  	{"-I/"},
    19  	{"-I/etc/passwd"},
    20  	{"-I."},
    21  	{"-O"},
    22  	{"-O2"},
    23  	{"-Osmall"},
    24  	{"-W"},
    25  	{"-Wall"},
    26  	{"-Wp,-Dfoo=bar"},
    27  	{"-Wp,-Ufoo"},
    28  	{"-Wp,-Dfoo1"},
    29  	{"-Wp,-Ufoo1"},
    30  	{"-fobjc-arc"},
    31  	{"-fno-objc-arc"},
    32  	{"-fomit-frame-pointer"},
    33  	{"-fno-omit-frame-pointer"},
    34  	{"-fpic"},
    35  	{"-fno-pic"},
    36  	{"-fPIC"},
    37  	{"-fno-PIC"},
    38  	{"-fpie"},
    39  	{"-fno-pie"},
    40  	{"-fPIE"},
    41  	{"-fno-PIE"},
    42  	{"-fsplit-stack"},
    43  	{"-fno-split-stack"},
    44  	{"-fstack-xxx"},
    45  	{"-fno-stack-xxx"},
    46  	{"-fsanitize=hands"},
    47  	{"-g"},
    48  	{"-ggdb"},
    49  	{"-march=souza"},
    50  	{"-mcpu=123"},
    51  	{"-mfpu=123"},
    52  	{"-mtune=happybirthday"},
    53  	{"-mstack-overflow"},
    54  	{"-mno-stack-overflow"},
    55  	{"-mmacosx-version"},
    56  	{"-mnop-fun-dllimport"},
    57  	{"-pthread"},
    58  	{"-std=c99"},
    59  	{"-xc"},
    60  	{"-D", "FOO"},
    61  	{"-D", "foo=bar"},
    62  	{"-I", "."},
    63  	{"-I", "/etc/passwd"},
    64  	{"-I", "世界"},
    65  	{"-I", "=/usr/include/libxml2"},
    66  	{"-I", "dir"},
    67  	{"-I", "$SYSROOT/dir"},
    68  	{"-isystem", "/usr/include/mozjs-68"},
    69  	{"-include", "/usr/include/mozjs-68/RequiredDefines.h"},
    70  	{"-framework", "Chocolate"},
    71  	{"-x", "c"},
    72  	{"-v"},
    73  }
    74  
    75  var badCompilerFlags = [][]string{
    76  	{"-D@X"},
    77  	{"-D-X"},
    78  	{"-Ufoo=bar"},
    79  	{"-F@dir"},
    80  	{"-F-dir"},
    81  	{"-I@dir"},
    82  	{"-I-dir"},
    83  	{"-O@1"},
    84  	{"-Wa,-foo"},
    85  	{"-W@foo"},
    86  	{"-Wp,-DX,-D@X"},
    87  	{"-Wp,-UX,-U@X"},
    88  	{"-g@gdb"},
    89  	{"-g-gdb"},
    90  	{"-march=@dawn"},
    91  	{"-march=-dawn"},
    92  	{"-std=@c99"},
    93  	{"-std=-c99"},
    94  	{"-x@c"},
    95  	{"-x-c"},
    96  	{"-D", "@foo"},
    97  	{"-D", "-foo"},
    98  	{"-I", "@foo"},
    99  	{"-I", "-foo"},
   100  	{"-I", "=@obj"},
   101  	{"-include", "@foo"},
   102  	{"-framework", "-Caffeine"},
   103  	{"-framework", "@Home"},
   104  	{"-x", "--c"},
   105  	{"-x", "@obj"},
   106  }
   107  
   108  func TestCheckCompilerFlags(t *testing.T) {
   109  	for _, f := range goodCompilerFlags {
   110  		if err := checkCompilerFlags("test", "test", f); err != nil {
   111  			t.Errorf("unexpected error for %q: %v", f, err)
   112  		}
   113  	}
   114  	for _, f := range badCompilerFlags {
   115  		if err := checkCompilerFlags("test", "test", f); err == nil {
   116  			t.Errorf("missing error for %q", f)
   117  		}
   118  	}
   119  }
   120  
   121  var goodLinkerFlags = [][]string{
   122  	{"-Fbar"},
   123  	{"-lbar"},
   124  	{"-Lbar"},
   125  	{"-fpic"},
   126  	{"-fno-pic"},
   127  	{"-fPIC"},
   128  	{"-fno-PIC"},
   129  	{"-fpie"},
   130  	{"-fno-pie"},
   131  	{"-fPIE"},
   132  	{"-fno-PIE"},
   133  	{"-fsanitize=hands"},
   134  	{"-g"},
   135  	{"-ggdb"},
   136  	{"-march=souza"},
   137  	{"-mcpu=123"},
   138  	{"-mfpu=123"},
   139  	{"-mtune=happybirthday"},
   140  	{"-pic"},
   141  	{"-pthread"},
   142  	{"-Wl,--hash-style=both"},
   143  	{"-Wl,-rpath,foo"},
   144  	{"-Wl,-rpath,$ORIGIN/foo"},
   145  	{"-Wl,-R", "/foo"},
   146  	{"-Wl,-R", "foo"},
   147  	{"-Wl,-R,foo"},
   148  	{"-Wl,--just-symbols=foo"},
   149  	{"-Wl,--just-symbols,foo"},
   150  	{"-Wl,--warn-error"},
   151  	{"-Wl,--no-warn-error"},
   152  	{"foo.so"},
   153  	{"_世界.dll"},
   154  	{"./x.o"},
   155  	{"libcgosotest.dylib"},
   156  	{"-F", "framework"},
   157  	{"-l", "."},
   158  	{"-l", "/etc/passwd"},
   159  	{"-l", "世界"},
   160  	{"-L", "framework"},
   161  	{"-framework", "Chocolate"},
   162  	{"-v"},
   163  	{"-Wl,-sectcreate,__TEXT,__info_plist,${SRCDIR}/Info.plist"},
   164  	{"-Wl,-framework", "-Wl,Chocolate"},
   165  	{"-Wl,-framework,Chocolate"},
   166  	{"-Wl,-unresolved-symbols=ignore-all"},
   167  	{"libcgotbdtest.tbd"},
   168  	{"./libcgotbdtest.tbd"},
   169  }
   170  
   171  var badLinkerFlags = [][]string{
   172  	{"-DFOO"},
   173  	{"-Dfoo=bar"},
   174  	{"-W"},
   175  	{"-Wall"},
   176  	{"-fobjc-arc"},
   177  	{"-fno-objc-arc"},
   178  	{"-fomit-frame-pointer"},
   179  	{"-fno-omit-frame-pointer"},
   180  	{"-fsplit-stack"},
   181  	{"-fno-split-stack"},
   182  	{"-fstack-xxx"},
   183  	{"-fno-stack-xxx"},
   184  	{"-mstack-overflow"},
   185  	{"-mno-stack-overflow"},
   186  	{"-mnop-fun-dllimport"},
   187  	{"-std=c99"},
   188  	{"-xc"},
   189  	{"-D", "FOO"},
   190  	{"-D", "foo=bar"},
   191  	{"-I", "FOO"},
   192  	{"-L", "@foo"},
   193  	{"-L", "-foo"},
   194  	{"-x", "c"},
   195  	{"-D@X"},
   196  	{"-D-X"},
   197  	{"-I@dir"},
   198  	{"-I-dir"},
   199  	{"-O@1"},
   200  	{"-Wa,-foo"},
   201  	{"-W@foo"},
   202  	{"-g@gdb"},
   203  	{"-g-gdb"},
   204  	{"-march=@dawn"},
   205  	{"-march=-dawn"},
   206  	{"-std=@c99"},
   207  	{"-std=-c99"},
   208  	{"-x@c"},
   209  	{"-x-c"},
   210  	{"-D", "@foo"},
   211  	{"-D", "-foo"},
   212  	{"-I", "@foo"},
   213  	{"-I", "-foo"},
   214  	{"-l", "@foo"},
   215  	{"-l", "-foo"},
   216  	{"-framework", "-Caffeine"},
   217  	{"-framework", "@Home"},
   218  	{"-Wl,-framework,-Caffeine"},
   219  	{"-Wl,-framework", "-Wl,@Home"},
   220  	{"-Wl,-framework", "@Home"},
   221  	{"-Wl,-framework,Chocolate,@Home"},
   222  	{"-Wl,--hash-style=foo"},
   223  	{"-x", "--c"},
   224  	{"-x", "@obj"},
   225  	{"-Wl,-rpath,@foo"},
   226  	{"-Wl,-R,foo,bar"},
   227  	{"-Wl,-R,@foo"},
   228  	{"-Wl,--just-symbols,@foo"},
   229  	{"../x.o"},
   230  }
   231  
   232  func TestCheckLinkerFlags(t *testing.T) {
   233  	for _, f := range goodLinkerFlags {
   234  		if err := checkLinkerFlags("test", "test", f); err != nil {
   235  			t.Errorf("unexpected error for %q: %v", f, err)
   236  		}
   237  	}
   238  	for _, f := range badLinkerFlags {
   239  		if err := checkLinkerFlags("test", "test", f); err == nil {
   240  			t.Errorf("missing error for %q", f)
   241  		}
   242  	}
   243  }
   244  
   245  func TestCheckFlagAllowDisallow(t *testing.T) {
   246  	if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil {
   247  		t.Fatalf("missing error for -disallow")
   248  	}
   249  	os.Setenv("CGO_TEST_ALLOW", "-disallo")
   250  	if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil {
   251  		t.Fatalf("missing error for -disallow with CGO_TEST_ALLOW=-disallo")
   252  	}
   253  	os.Setenv("CGO_TEST_ALLOW", "-disallow")
   254  	if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err != nil {
   255  		t.Fatalf("unexpected error for -disallow with CGO_TEST_ALLOW=-disallow: %v", err)
   256  	}
   257  	os.Unsetenv("CGO_TEST_ALLOW")
   258  
   259  	if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err != nil {
   260  		t.Fatalf("unexpected error for -Wall: %v", err)
   261  	}
   262  	os.Setenv("CGO_TEST_DISALLOW", "-Wall")
   263  	if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err == nil {
   264  		t.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall")
   265  	}
   266  	os.Setenv("CGO_TEST_ALLOW", "-Wall") // disallow wins
   267  	if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err == nil {
   268  		t.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall and CGO_TEST_ALLOW=-Wall")
   269  	}
   270  
   271  	os.Setenv("CGO_TEST_ALLOW", "-fplugin.*")
   272  	os.Setenv("CGO_TEST_DISALLOW", "-fplugin=lint.so")
   273  	if err := checkCompilerFlags("TEST", "test", []string{"-fplugin=faster.so"}); err != nil {
   274  		t.Fatalf("unexpected error for -fplugin=faster.so: %v", err)
   275  	}
   276  	if err := checkCompilerFlags("TEST", "test", []string{"-fplugin=lint.so"}); err == nil {
   277  		t.Fatalf("missing error for -fplugin=lint.so: %v", err)
   278  	}
   279  }
   280  

View as plain text