Skip to content

Commit

Permalink
heap_two_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-immunant committed Oct 7, 2023
1 parent ca0d4b9 commit 532e02d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 49 deletions.
1 change: 1 addition & 0 deletions rewriter/tests/heap_two_keys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ define_test(
PKEY 1
NEEDS_LD_WRAP
INCLUDE_DIR include/plugin
CRITERION_TEST
)

define_ia2_wrapper()
65 changes: 16 additions & 49 deletions rewriter/tests/heap_two_keys/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
RUN: sh -c 'if [ ! -s "heap_two_keys_call_gates_0.ld" ]; then echo "No link args as expected"; exit 0; fi; echo "Unexpected link args"; exit 1;'
TODO: %binary_dir/tests/heap_two_keys/heap_two_keys_main_wrapped 0 | diff %S/Output/plugin.out -
// TODO(src_rewriter_wip): had to change the output here, why?
RUN: %binary_dir/tests/heap_two_keys/heap_two_keys_main_wrapped 1 | diff %S/Output/main.out -
TODO: %binary_dir/tests/heap_two_keys/heap_two_keys_main_wrapped 2 | diff %S/Output/clean_exit.out -
*/
#include <stdio.h>
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <criterion/new/assert.h>
#include <unistd.h>
#include <assert.h>
#include <ia2.h>
Expand All @@ -21,81 +21,48 @@ INIT_RUNTIME(2);

// Test that the program can exit without error
// TODO(#112): it cannot.
int test_0() {
return 0;
Test(heap_two_keys, 0, .init = trigger_compartment_init) {
}

// Test that the main binary's heap can't be read
int test_1() {
Test(heap_two_keys, 1, .init = trigger_compartment_init) {
uint32_t *x = (uint32_t *)malloc(sizeof(uint32_t));
if (!x) {
LOG("Failed to allocate memory on the heap");
return -1;
cr_log_error("Failed to allocate memory on the heap");
exit(EXIT_FAILURE);
}
*x = 0x09431233;
read_uint32_t_expect_fault(x);
free(x);
// This test shouldn't return
return -1;
cr_fatal("Should have segfaulted but didn't");
}

// Test that the main binary's heap can't be written to
int test_2() {
Test(heap_two_keys, 2, .init = trigger_compartment_init) {
// This zeroes out the allocated memory
uint8_t *x = (uint8_t *)calloc(sizeof(uint8_t), 12);
if (!x) {
LOG("Failed to allocate memory on the heap");
return -1;
cr_log_error("Failed to allocate memory on the heap");
exit(EXIT_FAILURE);
}
write_uint8_t_expect_fault(x, 12);
free(x);
// This test shouldn't return
return -1;
cr_fatal("Should have segfaulted but didn't");
}

// Test that the main binary's shared data can be read
int test_3() {
Test(heap_two_keys, 3, .init = trigger_compartment_init) {
uint16_t *x = (uint16_t *)shared_malloc(sizeof(uint16_t));
if (!x) {
LOG("Failed to allocate memory on the heap");
return -1;
cr_log_error("Failed to allocate memory on the heap");
exit(EXIT_FAILURE);
}
*x = 0xffed;
assert(read_uint16_t(x) == 0xffed);
shared_free(x);
return 0;
}

// TODO: Add tests for free, realloc, the plugin's heap and reserving more than
// once from the gigacage (> 2MB in allocations)

int main(int argc, char **argv) {
if (argc < 2) {
LOG("Run with an integer (0-3) as the first argument to select a test");
return -1;
}

// Call a no-op function to switch to this compartment's PKRU
trigger_compartment_init();

int test_num = *argv[1] - '0';

switch (test_num) {
case 0: {
return test_0();
}
case 1: {
return test_1();
}
case 2: {
return test_2();
}
case 3: {
return test_3();
}
default: {
LOG("Unknown test selected");
return -1;
}
}
}
// once from the gigacage (> 2MB in allocations)

0 comments on commit 532e02d

Please sign in to comment.