• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package main
6
7import (
8	"testing"
9)
10
11func TestFilterUnsupportedSanitizerFlagsIfSanitizeGiven(t *testing.T) {
12	withTestContext(t, func(ctx *testContext) {
13		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
14			ctx.newCommand(gccX86_64, "-fsanitize=kernel-address", "-Wl,--no-undefined", mainCc)))
15		if err := verifyArgCount(cmd, 0, "-Wl,--no-undefined"); err != nil {
16			t.Error(err)
17		}
18
19		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
20			ctx.newCommand(gccX86_64, "-fsanitize=kernel-address", "-Wl,-z,defs", mainCc)))
21		if err := verifyArgCount(cmd, 0, "-Wl,-z,defs"); err != nil {
22			t.Error(err)
23		}
24
25		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
26			ctx.newCommand(gccX86_64, "-fsanitize=kernel-address", "-D_FORTIFY_SOURCE=1", mainCc)))
27		if err := verifyArgCount(cmd, 0, "-D_FORTIFY_SOURCE=1"); err != nil {
28			t.Error(err)
29		}
30
31		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
32			ctx.newCommand(gccX86_64, "-fsanitize=kernel-address", "-D_FORTIFY_SOURCE=2", mainCc)))
33		if err := verifyArgCount(cmd, 0, "-D_FORTIFY_SOURCE=2"); err != nil {
34			t.Error(err)
35		}
36	})
37}
38
39func TestFilterUnsupportedDefaultSanitizerFlagsIfSanitizeGivenForClang(t *testing.T) {
40	withTestContext(t, func(ctx *testContext) {
41		ctx.cfg.commonFlags = []string{"-D_FORTIFY_SOURCE=1"}
42		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
43			ctx.newCommand(clangX86_64, "-fsanitize=kernel-address", mainCc)))
44		if err := verifyArgCount(cmd, 0, "-D_FORTIFY_SOURCE=1"); err != nil {
45			t.Error(err)
46		}
47	})
48}
49
50func TestKeepUnsupportedDefaultSanitizerFlagsIfSanitizeGivenForGcc(t *testing.T) {
51	withTestContext(t, func(ctx *testContext) {
52		ctx.cfg.commonFlags = []string{"-D_FORTIFY_SOURCE=1"}
53		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
54			ctx.newCommand(gccX86_64, "-fsanitize=kernel-address", mainCc)))
55		if err := verifyArgCount(cmd, 1, "-D_FORTIFY_SOURCE=1"); err != nil {
56			t.Error(err)
57		}
58	})
59}
60
61// TODO: This is a bug in the old wrapper to not filter
62// non user args for gcc. Fix this once we don't compare to the old wrapper anymore.
63func TestKeepSanitizerFlagsIfNoSanitizeGiven(t *testing.T) {
64	withTestContext(t, func(ctx *testContext) {
65		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
66			ctx.newCommand(gccX86_64, "-Wl,--no-undefined", mainCc)))
67		if err := verifyArgCount(cmd, 1, "-Wl,--no-undefined"); err != nil {
68			t.Error(err)
69		}
70
71		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
72			ctx.newCommand(gccX86_64, "-Wl,-z,defs", mainCc)))
73		if err := verifyArgCount(cmd, 1, "-Wl,-z,defs"); err != nil {
74			t.Error(err)
75		}
76
77		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
78			ctx.newCommand(gccX86_64, "-D_FORTIFY_SOURCE=1", mainCc)))
79		if err := verifyArgCount(cmd, 1, "-D_FORTIFY_SOURCE=1"); err != nil {
80			t.Error(err)
81		}
82
83		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
84			ctx.newCommand(gccX86_64, "-D_FORTIFY_SOURCE=2", mainCc)))
85		if err := verifyArgCount(cmd, 1, "-D_FORTIFY_SOURCE=2"); err != nil {
86			t.Error(err)
87		}
88	})
89}
90
91func TestKeepSanitizerFlagsIfSanitizeGivenInCommonFlags(t *testing.T) {
92	withTestContext(t, func(ctx *testContext) {
93		ctx.cfg.commonFlags = []string{"-fsanitize=kernel-address"}
94		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
95			ctx.newCommand(gccX86_64, "-Wl,--no-undefined", mainCc)))
96		if err := verifyArgCount(cmd, 1, "-Wl,--no-undefined"); err != nil {
97			t.Error(err)
98		}
99	})
100}
101