xref: /aosp_15_r20/external/toolchain-utils/compiler_wrapper/stackprotector_flags_test.go (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1// Copyright 2019 The ChromiumOS Authors
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 TestAddStrongStackProtectorFlag(t *testing.T) {
12	withTestContext(t, func(ctx *testContext) {
13		initStackProtectorStrongConfig(ctx.cfg)
14		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
15			ctx.newCommand(gccX86_64, mainCc)))
16		if err := verifyArgOrder(cmd, "-fstack-protector-strong", mainCc); err != nil {
17			t.Error(err)
18		}
19	})
20}
21
22func TestAddNoStackProtectorFlagWhenKernelDefined(t *testing.T) {
23	withTestContext(t, func(ctx *testContext) {
24		initStackProtectorStrongConfig(ctx.cfg)
25		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
26			ctx.newCommand(gccX86_64, "-D__KERNEL__", mainCc)))
27		if err := verifyArgOrder(cmd, "-fno-stack-protector", mainCc); err != nil {
28			t.Error(err)
29		}
30	})
31}
32
33func TestOmitNoStackProtectorFlagWhenAlreadyInCommonFlags(t *testing.T) {
34	withTestContext(t, func(ctx *testContext) {
35		ctx.cfg.commonFlags = []string{"-fno-stack-protector"}
36		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
37			ctx.newCommand(gccX86_64, mainCc)))
38		if err := verifyArgCount(cmd, 1, "-fno-stack-protector"); err != nil {
39			t.Error(err)
40		}
41	})
42}
43
44func TestAddStrongStackProtectorFlagForEabiEvenIfNoStackProtectorGiven(t *testing.T) {
45	withTestContext(t, func(ctx *testContext) {
46		initStackProtectorStrongConfig(ctx.cfg)
47		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
48			ctx.newCommand(gccX86_64Eabi, "-fno-stack-protector", mainCc)))
49		if err := verifyArgCount(cmd, 1, "-fstack-protector-strong"); err != nil {
50			t.Error(err)
51		}
52	})
53}
54
55func initStackProtectorStrongConfig(cfg *config) {
56	cfg.commonFlags = []string{"-fstack-protector-strong"}
57}
58