Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 69c8994

Browse files
nickkralGerrit Code Review
authored andcommitted
Merge "Add stack canaries / strcpy tests."
2 parents a12c544 + dcab1b2 commit 69c8994

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

tests/Android.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ include $(BUILD_EXECUTABLE)
4747
# -----------------------------------------------------------------------------
4848

4949
test_c_flags = \
50-
-fstack-protector \
50+
-fstack-protector-all \
5151
-g \
5252
-Wall -Wextra \
5353
-Werror \

tests/stack_protector_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,24 @@ TEST(stack_protector, global_guard) {
114114
ASSERT_NE(0U, reinterpret_cast<uintptr_t>(__stack_chk_guard));
115115
}
116116

117+
/*
118+
* When this function returns, the stack canary will be inconsistent
119+
* with the previous value, which will generate a call to __stack_chk_fail(),
120+
* eventually resulting in a SIGABRT.
121+
*
122+
* This must be marked with "__attribute__ ((noinline))", to ensure the
123+
* compiler generates the proper stack guards around this function.
124+
*/
125+
__attribute__ ((noinline))
126+
static void do_modify_stack_chk_guard() {
127+
__stack_chk_guard = (void *) 0x12345678;
128+
}
129+
130+
// We have to say "DeathTest" here so gtest knows to run this test (which exits)
131+
// in its own process.
132+
TEST(stack_protector_DeathTest, modify_stack_protector) {
133+
::testing::FLAGS_gtest_death_test_style = "threadsafe";
134+
ASSERT_EXIT(do_modify_stack_chk_guard(), testing::KilledBySignal(SIGABRT), "");
135+
}
136+
117137
#endif

tests/string_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ TEST(string, strcpy) {
305305
}
306306
}
307307

308+
309+
#if __BIONIC__
310+
// We have to say "DeathTest" here so gtest knows to run this test (which exits)
311+
// in its own process.
312+
TEST(string_DeathTest, strcpy_fortified) {
313+
::testing::FLAGS_gtest_death_test_style = "threadsafe";
314+
char buf[10];
315+
char *orig = strdup("0123456789");
316+
ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), "");
317+
free(orig);
318+
}
319+
#endif
320+
308321
#if __BIONIC__
309322
TEST(string, strlcat) {
310323
StringTestState state(SMALL);

0 commit comments

Comments
 (0)