Skip to content

Commit

Permalink
add noexec stack note and relevant tests; fixes #21 and #22
Browse files Browse the repository at this point in the history
  • Loading branch information
nlsandler committed Aug 27, 2024
1 parent 2b5ba6a commit 935f584
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 29 deletions.
7 changes: 4 additions & 3 deletions templates/stack_alignment_check.s.jinja
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{# macros #}
{% set arg_operands=["%edi", "%esi", "%edx", "%ecx", "%r8d", "%r9d", "16(%rbp)", "24(%rbp)"] %}
{% macro alignment_check(fn_name, arg_count) %}
{% macro alignment_check(fn_name, arg_count) -%}
{% set fn_label=id_prefix~fn_name %}
.globl {{fn_label}}
{{fn_label}}:
Expand Down Expand Up @@ -30,9 +30,10 @@
call {{id_prefix}}exit{{plt_suffix}}
popq %rbp
retq
{% endmacro %}
{%- endmacro %}
{# actual program starts here #}
# generated from templates/{{ self._TemplateReference__context.name }}
.text
{{alignment_check("even_arguments", 8)}}
{{alignment_check("odd_arguments", 7)}}
{{alignment_check("odd_arguments", 7)}}
{{execstack_note}}
8 changes: 8 additions & 0 deletions test_framework/test_tests/test_programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def compile_and_run_sanitized(self, source_file: Path) -> None:
"-O3",
"-fsanitize=undefined",
]
if not basic.IS_OSX:
subproc_args.extend(
[
# Linux only: executable stack should produce linker error (this catches missing execstack note in assembly test files)
"-Xlinker",
"--error-execstack",
]
)
subproc_args.extend(build_compiler_args(source_file))

# compile it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ int main(void) {
}

/* use unsigned type specifier in for loop
* we'll iterate through this loop 11 times before dropping below 0 and wrapping around
* we'll iterate through this loop 11 times before dropping below 0 and
* wrapping around
*/
int counter = 0;
for (unsigned int index = 10; index < 4294967295U; index = index - 1) {
Expand All @@ -43,4 +44,3 @@ int main(void) {

return 0;
}

36 changes: 15 additions & 21 deletions tests/chapter_15/valid/initialization/automatic_nested.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* A fully initialized array of constants */
int test_simple(void) {
int arr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

// check the value of each element
for (int i = 0; i < 3; i = i + 1) {
Expand All @@ -13,28 +13,27 @@ int test_simple(void) {
}
}

return 1; // success
return 1; // success
}

/* A partially initialized array of constants.
* Elements that aren't explicitly initialized
* (including nested arrays) should be zeroed out.
* */
int test_partial(void) {

// explicitly initialize only the first half of each array,
// at each dimension
int first_half_only[4][2][6] = {
{{1, 2, 3}},
{{4, 5, 6}}
{{1, 2, 3}}, // first_half_only[0][0][0-2]
{{4, 5, 6}} // first_half_only[1][0][0-2]
};

int expected = 1;
for (int i = 0; i < 4; i = i + 1) {
for (int j = 0; j < 2; j = j + 1) {
for (int k = 0; k < 6; k = k + 1) {
int val = first_half_only[i][j][k];
if (i > 1 || j > 0 || k > 2 ) {
if (i > 1 || j > 0 || k > 2) {
// this wasn't explicitly initialized, should be zero
if (val) {
return 0;
Expand All @@ -49,15 +48,13 @@ int test_partial(void) {
}
}

return 1; // success
return 1; // success
}


/* elements in a compound initializer may include non-constant expressions
* and expressions of other types, which are converted to the right type
* as if by assignment */
int test_non_constant_and_type_conversion(void) {

// first let's define some value (that can't be copy propagated
// or constant-folded away in Part III)
extern unsigned int three(void);
Expand All @@ -66,8 +63,8 @@ int test_non_constant_and_type_conversion(void) {
int *ptr = &negative_four;

double arr[3][2] = {
{ x, x / *ptr },
{ three() }
{x, x / *ptr},
{three()},
};

if (arr[0][0] != 2000.0 || arr[0][1] != -500.0 || arr[1][0] != 3.0) {
Expand All @@ -78,7 +75,7 @@ int test_non_constant_and_type_conversion(void) {
return 0;
}

return 1; // success
return 1; // success
}

// helper function for previous test
Expand All @@ -89,20 +86,18 @@ unsigned int three(void) {
/* Initializing an array must not corrupt other objects on the stack. */
long one = 1l;
int test_preserve_stack(void) {

int i = -1;

/* Initialize with expressions of long type - make sure they're truncated
* before being copied into the array.
* Also use an array of < 16 bytes so it's not 16-byte aligned, so there are
* eightbytes that include both array elements and other values.
* Also leave last element uninitialized; in assembly, we should set it to zero without
* overwriting what follows
* Also leave last element uninitialized; in assembly, we should set it to
* zero without overwriting what follows
*/
int arr[3][1] = { {one * 2l}, {one + three()} };
int arr[3][1] = {{one * 2l}, {one + three()}};
unsigned int u = 2684366905;


if (i != -1) {
return 0;
}
Expand All @@ -111,11 +106,11 @@ int test_preserve_stack(void) {
return 0;
}

if ( arr[0][0] != 2 || arr[1][0] != 4 || arr[2][0] != 0 ) {
if (arr[0][0] != 2 || arr[1][0] != 4 || arr[2][0] != 0) {
return 0;
}

return 1; // success
return 1; // success
}

int main(void) {
Expand All @@ -135,6 +130,5 @@ int main(void) {
return 4;
}

return 0; // success
return 0; // success
}

4 changes: 3 additions & 1 deletion tests/chapter_4/valid/multi_short_circuit.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifdef SUPPRESS_WARNINGS
#ifndef __clang__
#ifdef __clang__
#pragma clang diagnostic ignored "-Wlogical-op-parentheses"
#else
#pragma GCC diagnostic ignored "-Wparentheses"
#endif
#endif
Expand Down
1 change: 1 addition & 0 deletions tests/chapter_4/valid/precedence.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifdef SUPPRESS_WARNINGS
#ifdef __clang__
#pragma clang diagnostic ignored "-Wconstant-logical-operand"
#pragma clang diagnostic ignored "-Wlogical-op-parentheses"
#else
#pragma GCC diagnostic ignored "-Wparentheses"
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ even_arguments:
call exit@PLT
popq %rbp
retq

.globl odd_arguments
odd_arguments:
pushq %rbp
Expand Down Expand Up @@ -79,3 +78,4 @@ odd_arguments:
call exit@PLT
popq %rbp
retq
.section ".note.GNU-stack","",@progbits
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ Lfail_even_arguments:
call _exit
popq %rbp
retq

.globl _odd_arguments
_odd_arguments:
pushq %rbp
Expand Down

0 comments on commit 935f584

Please sign in to comment.