From fb1e177130648d2e4b1e327f28f66e38176f9f53 Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Thu, 16 Feb 2023 15:22:26 +0100 Subject: [PATCH] Testing different stack pointers --- src/GC/Makefile | 12 ++--------- src/GC/lib/heap.cpp | 5 ++++- src/GC/tests/stack.cpp | 21 +++++++++++++++++++- src/GC/tests/stack2.cpp | 44 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 src/GC/tests/stack2.cpp diff --git a/src/GC/Makefile b/src/GC/Makefile index 64d90d6..7c0c38f 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -1,6 +1,6 @@ CC = clang++ PWD = /home/virre/dev/systemF/org/language/src/GC/include -CFLAGS = -Wall -Wextra -v -std=gnu++20 -stdlib=libc++ -I +CFLAGS = -Wall -Wextra -v -g -std=gnu++20 -stdlib=libc++ -I heap: $(CC) $(CFLAGS)$(PWD) lib/heap.cpp @@ -9,14 +9,6 @@ h_test: rm -f tests/h_test.out $(CC) $(CFLAGS)$(PWD) tests/h_test.cpp lib/heap.cpp -o tests/h_test.out -h_test_vg: - rm -f tests/h_test.out - $(CC) $(CFLAGS)$(PWD) -g tests/h_test.cpp lib/heap.cpp -o tests/h_test.out - linker: rm -f tests/linker.out - $(CC) $(CFLAGS)$(PWD) tests/linker.cpp lib/heap.cpp -o tests/linker.out - -linker_vg: - rm -f tests/linker.out - $(CC) $(CFLAGS)$(PWD) -g tests/linker.cpp lib/heap.cpp -o tests/linker.out \ No newline at end of file + $(CC) $(CFLAGS)$(PWD) tests/linker.cpp lib/heap.cpp -o tests/linker.out \ No newline at end of file diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index 8c296b5..59f4e7d 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -65,7 +65,10 @@ namespace GC { // get the frame adress, whwere local variables and saved registers are located auto stack_start = reinterpret_cast(__builtin_frame_address(0)); // looking at 10 stack frames back - const uintptr_t *stack_end = (uintptr_t *)0; //reinterpret_cast(__builtin_frame_address(10)); + const uintptr_t *stack_end = (uintptr_t *)0; + + // denna segfaultar om arg för __b_f_a är > 2 + // reinterpret_cast(__builtin_frame_address(10)); auto work_list = m_allocated_chunks; mark(stack_start, stack_end, work_list); diff --git a/src/GC/tests/stack.cpp b/src/GC/tests/stack.cpp index 6cdf2d0..8f8382e 100644 --- a/src/GC/tests/stack.cpp +++ b/src/GC/tests/stack.cpp @@ -3,13 +3,32 @@ #include #include +/* + * Stack.cpp + * - Tests stack scanning and stack pointers + * + * Goal: Find the values of the following variables + * and their position on the stack + * - unsigned long a + * - unsigned long b + * - unsigned long global_1 + * - unsigned long global_2 + * + * Result: Passed +*/ + + + + std::vector iv; void collect() { std::cout << "in collect" << std::endl; uintptr_t *stack_start = reinterpret_cast(__builtin_frame_address(0)); - // uintptr_t *stack_end = reinterpret_cast(__builtin_frame_address(100)); + + // denna orsakar segfault om man ger __b_f_a ett värde större än 2 + // uintptr_t *stack_end = reinterpret_cast(__builtin_frame_address(100)); std::cout << "SP1:\t" << stack_start << "\nSP2:\t" << (stack_start - 1*sizeof(int)) << std::endl; std::cout << "SP-:\t" << --stack_start << std::endl; diff --git a/src/GC/tests/stack2.cpp b/src/GC/tests/stack2.cpp new file mode 100644 index 0000000..0444e10 --- /dev/null +++ b/src/GC/tests/stack2.cpp @@ -0,0 +1,44 @@ +#include +#include + +void dummy1(); +void dummy2(); + +int main() { + + uintptr_t *prev1 = reinterpret_cast(__builtin_frame_address(0)); + uintptr_t *prev2 = static_cast(__builtin_frame_address(0)); + + std::cout << "reinterpret:\t" << prev1 << "\nstatic:\t\t" << prev2 << std::endl; + + std::cout << "Start:\t\t" << prev1 << std::endl; +#pragma clang diagnostic ignored "-Wframe-address" + uintptr_t *tmp = reinterpret_cast(__builtin_frame_address(1)); + std::cout << "Frame 1:\t" << tmp << "\t\tDiff:\t" << std::hex << "0x"<< prev1 - tmp << std::endl; + prev1 = tmp; + +#pragma clang diagnostic ignored "-Wframe-address" + tmp = reinterpret_cast(__builtin_frame_address(2)); + std::cout << "Frame 2:\t" << tmp << "\tDiff:\t" << std::hex << "0x" << prev1 - tmp << std::endl; + + dummy1(); + + return 0; +} + +void dummy1() { + std::cout << "Frame:\t\t" << __builtin_frame_address(0); +#pragma clang diagnostic ignored "-Wframe-address" + std::cout << "\t\tPrev:\t" << __builtin_frame_address(1) << std::endl; + dummy2(); + std::cout << "D1 RA:\t\t" << std::hex << __builtin_return_address(0) << std::endl; +} + +void dummy2() { + std::cout << "Frame:\t\t" << __builtin_frame_address(0); +#pragma clang diagnostic ignored "-Wframe-address" + std::cout << "\t\tPrev:\t" << __builtin_frame_address(1) << std::endl; + void *ra = __builtin_return_address(0); + std::cout << "D2 RA:\t\t" << std::hex << ra << std::endl; + std::cout << "D2 ERA:\t\t" << std::hex << __builtin_extract_return_addr(ra) << std::endl; +} \ No newline at end of file