xref: /aosp_15_r20/external/flac/m4/stack_protect.m4 (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1*600f14f4SXin Lidnl Copyright (C) 2013-2023  Xiph.Org Foundation
2*600f14f4SXin Lidnl
3*600f14f4SXin Lidnl Redistribution and use in source and binary forms, with or without
4*600f14f4SXin Lidnl modification, are permitted provided that the following conditions
5*600f14f4SXin Lidnl are met:
6*600f14f4SXin Lidnl
7*600f14f4SXin Lidnl - Redistributions of source code must retain the above copyright
8*600f14f4SXin Lidnl notice, this list of conditions and the following disclaimer.
9*600f14f4SXin Lidnl
10*600f14f4SXin Lidnl - Redistributions in binary form must reproduce the above copyright
11*600f14f4SXin Lidnl notice, this list of conditions and the following disclaimer in the
12*600f14f4SXin Lidnl documentation and/or other materials provided with the distribution.
13*600f14f4SXin Lidnl
14*600f14f4SXin Lidnl - Neither the name of the Xiph.org Foundation nor the names of its
15*600f14f4SXin Lidnl contributors may be used to endorse or promote products derived from
16*600f14f4SXin Lidnl this software without specific prior written permission.
17*600f14f4SXin Lidnl
18*600f14f4SXin Lidnl THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*600f14f4SXin Lidnl ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*600f14f4SXin Lidnl LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*600f14f4SXin Lidnl A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
22*600f14f4SXin Lidnl CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23*600f14f4SXin Lidnl EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24*600f14f4SXin Lidnl PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25*600f14f4SXin Lidnl PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26*600f14f4SXin Lidnl LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27*600f14f4SXin Lidnl NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*600f14f4SXin Lidnl SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*600f14f4SXin Li
30*600f14f4SXin Lidnl We want to know if GCC stack protector works, for the C and for the C++
31*600f14f4SXin Lidnl compiler.
32*600f14f4SXin Lidnl
33*600f14f4SXin Lidnl Just checking if the compiler accepts the required CFLAGSs is not enough
34*600f14f4SXin Lidnl because we have seen at least one instance where this check was
35*600f14f4SXin Lidnl in-sufficient.
36*600f14f4SXin Lidnl
37*600f14f4SXin Lidnl Instead, try to compile and link a test program with the stack protector
38*600f14f4SXin Lidnl flags. If that works, we use it.
39*600f14f4SXin Li
40*600f14f4SXin LiAC_DEFUN([XIPH_GCC_STACK_PROTECTOR],
41*600f14f4SXin Li[AC_LANG_ASSERT(C)
42*600f14f4SXin Li	AC_MSG_CHECKING([if $CC supports stack smash protection])
43*600f14f4SXin Li	xiph_stack_check_old_cflags="$CFLAGS"
44*600f14f4SXin Li	SSP_FLAGS="-fstack-protector-strong"
45*600f14f4SXin Li	CFLAGS=$SSP_FLAGS
46*600f14f4SXin Li	AC_LINK_IFELSE([AC_LANG_PROGRAM([[
47*600f14f4SXin Li			#include <stdio.h>
48*600f14f4SXin Li			]], [[puts("Hello, World!"); return 0;]])],[AC_MSG_RESULT(yes)
49*600f14f4SXin Li			CFLAGS="$xiph_stack_check_old_cflags $SSP_FLAGS"],[AC_MSG_RESULT(no)
50*600f14f4SXin Li			CFLAGS="$xiph_stack_check_old_cflags"
51*600f14f4SXin Li		])
52*600f14f4SXin Li])# XIPH_GCC_STACK_PROTECTOR
53*600f14f4SXin Li
54*600f14f4SXin LiAC_DEFUN([XIPH_GXX_STACK_PROTECTOR],
55*600f14f4SXin Li[AC_LANG_PUSH([C++])
56*600f14f4SXin Li	AC_MSG_CHECKING([if $CXX supports stack smash protection])
57*600f14f4SXin Li	xiph_stack_check_old_cxxflags="$CXXFLAGS"
58*600f14f4SXin Li	SSP_FLAGS="-fstack-protector-strong"
59*600f14f4SXin Li	CXXFLAGS=$SSP_FLAGS
60*600f14f4SXin Li	AC_LINK_IFELSE([AC_LANG_PROGRAM([[
61*600f14f4SXin Li			#include <cstdio>
62*600f14f4SXin Li			]], [[puts("Hello, World!"); return 0;]])],[AC_MSG_RESULT(yes)
63*600f14f4SXin Li			CXXFLAGS="$xiph_stack_check_old_cxxflags $SSP_FLAGS"],[AC_MSG_RESULT(no)
64*600f14f4SXin Li			CXXFLAGS="$xiph_stack_check_old_cxxflags"
65*600f14f4SXin Li		])
66*600f14f4SXin Li	AC_LANG_POP([C++])
67*600f14f4SXin Li])# XIPH_GXX_STACK_PROTECTOR
68