From ee67c6dec190971563710e31f9cfe3f16dc52913 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 10 Dec 2023 12:41:37 +0100 Subject: [PATCH] Add more HEAPCHECK --- autotest/autotest.bat | 74 ++++++++++++++++++++++++++------- autotest/opp_list.cpp | 57 +++++++++++++++++++++++++ include/crt.c | 97 ++++++++++++++++++++++++++++++++++++++++++- include/opp/list.h | 72 ++++++++++++++++++++++++++++++++ oscar64/Linker.cpp | 4 +- 5 files changed, 284 insertions(+), 20 deletions(-) create mode 100644 autotest/opp_list.cpp diff --git a/autotest/autotest.bat b/autotest/autotest.bat index 37f4864..d3680b3 100644 --- a/autotest/autotest.bat +++ b/autotest/autotest.bat @@ -3,52 +3,55 @@ rem @echo off @call :test bitfields.cpp @if %errorlevel% neq 0 goto :error -@call :test opp_string.cpp +@call :testh opp_string.cpp @if %errorlevel% neq 0 goto :error -@call :test opp_array.cpp +@call :testh opp_array.cpp @if %errorlevel% neq 0 goto :error -@call :test opp_vector.cpp +@call :testh opp_vector.cpp @if %errorlevel% neq 0 goto :error -@call :test opp_vector_string.cpp +@call :testh opp_vector_string.cpp @if %errorlevel% neq 0 goto :error -@call :test opp_streamtest.cpp +@call :testh opp_streamtest.cpp @if %errorlevel% neq 0 goto :error -@call :test opp_pairtest.cpp +@call :testh opp_pairtest.cpp @if %errorlevel% neq 0 goto :error -@call :test opp_parts.cpp +@call :testh opp_parts.cpp @if %errorlevel% neq 0 goto :error -@call :test operatoroverload.cpp +@call :testh opp_list.cpp @if %errorlevel% neq 0 goto :error -@call :test virtualdestruct.cpp +@call :testh operatoroverload.cpp @if %errorlevel% neq 0 goto :error -@call :test vcalltest.cpp +@call :testh virtualdestruct.cpp @if %errorlevel% neq 0 goto :error -@call :test vcalltree.cpp +@call :testh vcalltest.cpp @if %errorlevel% neq 0 goto :error -@call :test constructortest.cpp +@call :testh vcalltree.cpp @if %errorlevel% neq 0 goto :error -@call :test copyconstructor.cpp +@call :testh constructortest.cpp @if %errorlevel% neq 0 goto :error -@call :test copyassign.cpp +@call :testh copyconstructor.cpp @if %errorlevel% neq 0 goto :error -@call :test arrayconstruct.cpp +@call :testh copyassign.cpp @if %errorlevel% neq 0 goto :error -@call :test stdlibtest.c +@call :testh arrayconstruct.cpp +@if %errorlevel% neq 0 goto :error + +@call :testh stdlibtest.c @if %errorlevel% neq 0 goto :error @call :test testint16.c @@ -225,6 +228,45 @@ rem @echo off echo Failed with error #%errorlevel%. exit /b %errorlevel% +:testh +..\release\oscar64 -e -bc %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -n %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -O2 -bc %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -O2 -n %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -O2 -n -dHEAPCHECK %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -O0 -bc %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -O0 -n %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -Os -bc %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -Os -n %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -O3 -bc %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -O3 -n %~1 +@if %errorlevel% neq 0 goto :error + +..\release\oscar64 -e -O3 -n -dHEAPCHECK %~1 +@if %errorlevel% neq 0 goto :error + +@exit /b 0 + :test ..\release\oscar64 -e -bc %~1 @if %errorlevel% neq 0 goto :error diff --git a/autotest/opp_list.cpp b/autotest/opp_list.cpp new file mode 100644 index 0000000..9118b43 --- /dev/null +++ b/autotest/opp_list.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +int main(void) +{ + opp::list a; + + for(int i=0; i<10; i++) + a.push_back(i); + + int s = 0; + for(auto i : a) + s += i; + + assert(s == 45); + + auto li = a.begin(); + for(int i=0; i<5; i++) + { + li = a.erase(li); + li++; + } + + s = 0; + for(auto i : a) + s += i; + + assert(s == 1 + 3 + 5 + 7 + 9); + + opp::list b; + + b = a; + + s = 0; + for(auto i : b) + s += i; + + assert(s == 1 + 3 + 5 + 7 + 9); + + opp::list c = opp::list(b); + + s = 0; + for(auto i : c) + s += i; + + assert(s == 1 + 3 + 5 + 7 + 9); + + s = 0; + for(auto i : b) + s += i; + + assert(s == 1 + 3 + 5 + 7 + 9); + + return 0; +} diff --git a/include/crt.c b/include/crt.c index f1a96ed..67f6877 100644 --- a/include/crt.c +++ b/include/crt.c @@ -4127,10 +4127,16 @@ __asm malloc { // make room for two additional bytes // to store pointer to end of used memory + // in case of heap check we add six bytes to + // add some guards clc lda accu + 0 +#ifdef HEAPCHECK + adc #6 +#else adc #2 +#endif sta tmp lda accu + 1 adc #$00 @@ -4224,8 +4230,13 @@ avail: clc lda tmp + 2 +#ifdef HEAPCHECK + adc #7 + and #$f8 +#else adc #3 and #$fc +#endif sta tmp + 4 lda tmp + 3 adc #0 @@ -4285,11 +4296,38 @@ found: lda tmp + 3 sta (accu), y +#ifdef HEAPCHECK + + // add guard range to memory segment + + ldy #2 + lda #$bd + sta (accu), y + iny + sta (accu), y + + sec + lda tmp + 2 + sbc #2 + sta tmp + 2 + bcs hc1 + dec tmp + 3 +hc1: + lda #$be + ldy #0 + sta (tmp + 2), y + iny + sta (tmp + 2), y + + lda accu + ora #4 + sta accu +#else // advanve by two bytes to skip size lda accu ora #2 sta accu - +#endif rts } @@ -4309,9 +4347,54 @@ __asm free // two bytes back to fix remembered end of block lda accu +#ifdef HEAPCHECK + ora accu + 1 + bne hcnn + rts +hcnn: + lda accu + and #$07 + cmp #$04 + bne hfail + lda accu + and #$f8 +#else and #$fc +#endif sta accu +#ifdef HEAPCHECK + ldy #2 + lda (accu), y + cmp #$bd + bne hfail + iny + lda (accu), y + cmp #$bd + bne hfail + + ldy #0 + sec + lda (accu), y + sbc #2 + sta tmp + 0 + iny + lda (accu), y + sbc #0 + sta tmp + 1 + + ldy #0 + lda (tmp), y + cmp #$be + bne hfail + iny + lda (tmp), y + cmp #$be + bne hfail + + lda accu +#endif + // check nullptr free ora accu + 1 @@ -4336,7 +4419,12 @@ hchk1: bne hfail cpx #pred = head; + head.pred->succ = head; + l.head.succ = (listnode *)&(l.head); + l.head.pred = (listnode *)&(l.head); + } + + list & operator=(const list & l); + + list & operator=(list && l) + { + head.succ = l.head.succ; + head.pred = l.head.pred; + head.succ->pred = head; + head.pred->succ = head; + l.head.succ = (listnode *)&(l.head); + l.head.pred = (listnode *)&(l.head); + return *this; + } + ~list(void) { listnode * n = head.succ; @@ -175,11 +200,32 @@ public: void push_back(T && t); + void clear(void); + + void append(const list & l); + list_iterator insert(list_iterator it, const T & t); list_iterator insert(list_iterator it, T && t); }; +template +list::list(const list & l) +{ + append(l); +} + +template +list & list::operator=(const list & l) +{ + if (&l != this) + { + clear(); + append(l); + } + return *this; +} + template void list::pop_front(void) { @@ -293,6 +339,32 @@ list_iterator list::insert(list_iterator it, T && t) return list_iterator(n); } + +template +void list::clear(void) +{ + listnode * n = head.succ; + while (n != &head) + { + listnode * m = n->succ; + delete n; + n = m; + } + head.succ = (listnode *)&head; + head.pred = (listnode *)&head; +} + +template +void list::append(const list & l) +{ + listnode * n = l.head.succ; + while (n != &(l.head)) + { + push_back(n->data); + n = n->succ; + } +} + } #endif diff --git a/oscar64/Linker.cpp b/oscar64/Linker.cpp index 6700c54..d2c988f 100644 --- a/oscar64/Linker.cpp +++ b/oscar64/Linker.cpp @@ -672,8 +672,8 @@ void Linker::Link(void) if (lsec->mType == LST_HEAP) { - lsec->mStart = (lrgn->mStart + lrgn->mUsed + 3) & ~3; - lsec->mEnd = lrgn->mEnd & ~3; + lsec->mStart = (lrgn->mStart + lrgn->mUsed + 7) & ~7; + lsec->mEnd = lrgn->mEnd & ~7; if (lsec->mStart + lsec->mSize > lsec->mEnd) {