aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/stackprotector_flags_test.go
blob: a87575796c79f428e58cc05586d978cf6d727099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

import (
	"testing"
)

func TestAddStrongStackProtectorFlag(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		initStackProtectorStrongConfig(ctx.cfg)
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyArgOrder(cmd, "-fstack-protector-strong", mainCc); err != nil {
			t.Error(err)
		}
	})
}

func TestAddNoStackProtectorFlagWhenKernelDefined(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		initStackProtectorStrongConfig(ctx.cfg)
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, "-D__KERNEL__", mainCc)))
		if err := verifyArgOrder(cmd, "-fno-stack-protector", mainCc); err != nil {
			t.Error(err)
		}
	})
}

func TestOmitNoStackProtectorFlagWhenAlreadyInCommonFlags(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.cfg.commonFlags = []string{"-fno-stack-protector"}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyArgCount(cmd, 1, "-fno-stack-protector"); err != nil {
			t.Error(err)
		}
	})
}

func TestAddStrongStackProtectorFlagForEabiEvenIfNoStackProtectorGiven(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		initStackProtectorStrongConfig(ctx.cfg)
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64Eabi, "-fno-stack-protector", mainCc)))
		if err := verifyArgCount(cmd, 1, "-fstack-protector-strong"); err != nil {
			t.Error(err)
		}
	})
}

func initStackProtectorStrongConfig(cfg *config) {
	cfg.commonFlags = []string{"-fstack-protector-strong"}
}