github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/cgo/security_test.go (about)

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