Skip to content

Commit

Permalink
tests: add mmap_loop test
Browse files Browse the repository at this point in the history
  • Loading branch information
fw-immunant committed Sep 11, 2023
1 parent 7d25aba commit 3df0c00
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions rewriter/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_subdirectory(heap_two_keys)
# add_subdirectory(libusb)
add_subdirectory(macro_attr)
add_subdirectory(minimal)
add_subdirectory(mmap_loop)
# TODO: support C++ namespaces
#add_subdirectory(namespaces)
add_subdirectory(recursion)
Expand Down
15 changes: 15 additions & 0 deletions rewriter/tests/mmap_loop/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(STDOUT_REF ${CMAKE_CURRENT_SOURCE_DIR}/Output/mmap_loop.out)
configure_file(${STDOUT_REF} ${CMAKE_CURRENT_BINARY_DIR}/mmap_loop.out)

define_shared_lib(
SRCS mmap_loop.c
PKEY 2
)

define_test(
SRCS main.c
PKEY 1
NEEDS_LD_WRAP
)

define_ia2_wrapper()
3 changes: 3 additions & 0 deletions rewriter/tests/mmap_loop/Output/mmap_loop.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
program started
mmap buf
res=0
8 changes: 8 additions & 0 deletions rewriter/tests/mmap_loop/include/mmap_loop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <stdlib.h>

// LINKARGS: --wrap=lib_mmap_buf
char *lib_mmap_buf(size_t size);

// LINKARGS: --wrap=lib_malloc_buf
char *lib_malloc_buf(size_t size);
44 changes: 44 additions & 0 deletions rewriter/tests/mmap_loop/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
RUN: sh -c 'if [ ! -s "mmap_loop_call_gates_0.ld" ]; then echo "No link args as expected"; exit 0; fi; echo "Unexpected link args"; exit 1;'
RUN: %binary_dir/tests/mmap_loop/mmap_loop_main_wrapped | diff %S/Output/mmap_loop.out -
*/
#include "mmap_loop.h"
#include <assert.h>
#include <ia2.h>
#include <math.h>
#include <stdio.h>

#define IA2_DEFINE_TEST_HANDLER
#include "test_fault_handler.h"

/*
This program tests that mmap and heap allocations are handled properly.
*/

INIT_RUNTIME(2);
#define IA2_COMPARTMENT 1
#include <ia2_compartment_init.inc>

int main() {
puts("program started");

char *buf = malloc(4096);
buf[0] = 'r';
assert(buf[0] == 'r');

char *lib_buf_malloc = lib_malloc_buf(4096);

puts("mmap buf");
char *lib_buf_mmap = lib_mmap_buf(4096);

free(buf);

/* syscall tracing should forbid us to unmap compartment 2's buffer */
int res = munmap(lib_buf_mmap, 4096);
if (res < 0) {
perror("munmap");
}
printf("res=%d\n", res);

return 0;
}
28 changes: 28 additions & 0 deletions rewriter/tests/mmap_loop/mmap_loop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
RUN: cat mmap_loop_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/

#include "mmap_loop.h"
#include "ia2.h"
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

#define IA2_COMPARTMENT 2
#include <ia2_compartment_init.inc>

// LINKARGS: --wrap=lib_malloc_buf
char *lib_malloc_buf(size_t size) {
char *buf = malloc(size);
memset(buf, 0, size);
return buf;
}

// LINKARGS: --wrap=lib_mmap_buf
char *lib_mmap_buf(size_t size) {
// mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
char *buf = mmap(NULL, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
pkey_mprotect(buf, size, PROT_READ | PROT_WRITE, 2);
memset(buf, 0, size);
return buf;
}

0 comments on commit 3df0c00

Please sign in to comment.