From 19995840a0e7ddd2ab54bd4830b2ae5e3c527daa Mon Sep 17 00:00:00 2001
From: Jeffrey Lee <jlee@gitlab.riscosopen.org>
Date: Sat, 10 Dec 2011 20:50:06 +0000
Subject: [PATCH] Fix OS_Heap 4. Fix stack imbalance in system heap code.

Detail:
  s/HeapMan - The code to check if it's safe to shrink a block by 4 bytes assumed 'addr' was the address of the block, when in reality it's just an offset. This was causing the "OK to shrink?" check to always ignore the shrink request on heaps located below the 2G limit, but fail with "heap corrupt" errors in heaps located above the 2G limit.
  s/ArthurSWIs - Fix stack imbalance causing a crash if OS_Heap returns an unexpected error in DoSysHeapOpWithExtension/ClaimSysHeapNode
Admin:
  Tested in Iyonix ROM softload
  Testbed didn't pick up heap shrink bug due to the sanity checks it performs not being vigorous enough.


Version 5.35, 4.79.2.130. Tagged as 'Kernel-5_35-4_79_2_130'
---
 VersionASM   | 6 +++---
 VersionNum   | 8 ++++----
 s/ArthurSWIs | 2 +-
 s/HeapMan    | 5 ++---
 4 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/VersionASM b/VersionASM
index c70775b..23dfddc 100644
--- a/VersionASM
+++ b/VersionASM
@@ -13,11 +13,11 @@
                         GBLS    Module_ComponentPath
 Module_MajorVersion     SETS    "5.35"
 Module_Version          SETA    535
-Module_MinorVersion     SETS    "4.79.2.129"
+Module_MinorVersion     SETS    "4.79.2.130"
 Module_Date             SETS    "10 Dec 2011"
 Module_ApplicationDate  SETS    "10-Dec-11"
 Module_ComponentName    SETS    "Kernel"
 Module_ComponentPath    SETS    "castle/RiscOS/Sources/Kernel"
-Module_FullVersion      SETS    "5.35 (4.79.2.129)"
-Module_HelpVersion      SETS    "5.35 (10 Dec 2011) 4.79.2.129"
+Module_FullVersion      SETS    "5.35 (4.79.2.130)"
+Module_HelpVersion      SETS    "5.35 (10 Dec 2011) 4.79.2.130"
                         END
diff --git a/VersionNum b/VersionNum
index 3c9a097..c01f60f 100644
--- a/VersionNum
+++ b/VersionNum
@@ -5,12 +5,12 @@
  *
  */
 #define Module_MajorVersion_CMHG        5.35
-#define Module_MinorVersion_CMHG        4.79.2.129
+#define Module_MinorVersion_CMHG        4.79.2.130
 #define Module_Date_CMHG                10 Dec 2011
 
 #define Module_MajorVersion             "5.35"
 #define Module_Version                  535
-#define Module_MinorVersion             "4.79.2.129"
+#define Module_MinorVersion             "4.79.2.130"
 #define Module_Date                     "10 Dec 2011"
 
 #define Module_ApplicationDate          "10-Dec-11"
@@ -18,6 +18,6 @@
 #define Module_ComponentName            "Kernel"
 #define Module_ComponentPath            "castle/RiscOS/Sources/Kernel"
 
-#define Module_FullVersion              "5.35 (4.79.2.129)"
-#define Module_HelpVersion              "5.35 (10 Dec 2011) 4.79.2.129"
+#define Module_FullVersion              "5.35 (4.79.2.130)"
+#define Module_HelpVersion              "5.35 (10 Dec 2011) 4.79.2.130"
 #define Module_LibraryVersionInfo       "5:35"
diff --git a/s/ArthurSWIs b/s/ArthurSWIs
index 5242bba..98c78f8 100644
--- a/s/ArthurSWIs
+++ b/s/ArthurSWIs
@@ -1212,7 +1212,7 @@ IntoSysHeapOp
        LDR     r14, [r0]                   ; look at error number
        TEQ     r14, #ErrorNumber_HeapFail_Alloc
        STRNE   r0, [stack]
-       Pull   "r0, r3, PC", NE            ; can only retry if ran out of room
+       Pull   "r0, PC", NE                 ; can only retry if ran out of room
 
        Push    r3                          ; in case extension
        LDR     r1, [stack, #4]
diff --git a/s/HeapMan b/s/HeapMan
index 794e7a4..306fac1 100644
--- a/s/HeapMan
+++ b/s/HeapMan
@@ -1176,15 +1176,14 @@ ExtendBlock
         BGT      DoShrink
         LDR      HpTemp, [hpd, tp]
         CMP      HpTemp, #Nil
-        ADDNE    HpTemp, HpTemp, tp
+        ADDNE    HpTemp, HpTemp, tp        ; Offset of next free block
         LDREQ    HpTemp, hpdbase
-        ADD      HpTemp, HpTemp, hpd       ; Next free block ptr
         SUB      HpTemp, HpTemp, addr      ; Offset from start of this block
         SUB      HpTemp, HpTemp, size      ; Apply shrink amount to match bp
         CMP      HpTemp, bp
         MOVGT    size, #0                  ; Used block after us. Deny shrink.
         BGT      GoodExtension
-        BLT      CorruptExtension          ; Heap corrupt!
+        BLT      CorruptExtension          ; Heap corrupt! Next free block is before us
         ; Else there's a free block (or hpdbase) directly after us
 DoShrink
         STR      bp, [addr, hpd]           ; update size of block left
-- 
GitLab