prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  MIPS32BE:  stack collided with heap?
  Couldn't get to the bottom of this one...
  
  ```
  #!/home/<user>/angr_pypy/bin/python
  
  import angr
  import claripy
  import monkeyhex
  import logging
  import pickle
  import time
  import sys
  import os
  import socket
  import copy
  import argparse
  import ipdb
  
  from IPython import embed
  
  proj = angr.Project("httpd", auto_load_libs=False, except_missing_libs=False)
  
  cfg = proj.analyses.CFGFast(normalize=True,
                              fail_fast=True,
                              force_complete_scan=False,
                              data_references=False,
                              cross_references=False,
                              show_progressbar=True)
  
  # some functions we're interested in
  funcs = proj.kb.functions
  parse_http_req = funcs[0x408f90]
  
  s = proj.factory.blank_state(addr=parse_http_req.addr)
  
  # running it in a simulation manager will allow us to examine the state after it errors out
  sm  = proj.factory.simulation_manager(s)
  sm.run()
  
  embed()
  ```
  [httpd.zip](https://github.com/angr/angr/files/7671480/httpd.zip)
  
  
  
  </issue>
  <code>
  [start of angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py]
  1 import logging
  2 
  3 from .paged_memory_mixin import PagedMemoryMixin
  4 from ....errors import SimSegfaultException, SimMemoryError
  5 
  6 l = logging.getLogger(__name__)
  7 
  8 class StackAllocationMixin(PagedMemoryMixin):
  9     """
  10     This mixin adds automatic allocation for a stack region based on the stack_end and stack_size parameters.
  11     """
  12     # TODO: multiple stacks. this scheme should scale p well
  13     # TODO tbh this should be handled by an actual fault handler in simos or something
  14     def __init__(self, stack_end=None, stack_size=None, stack_perms=None, **kwargs):
  15         super().__init__(**kwargs)
  16         self._red_pageno = (stack_end - 1) // self.page_size if stack_end is not None else None
  17         self._remaining_stack = stack_size
  18         self._stack_perms = stack_perms
  19 
  20     def copy(self, memo):
  21         o = super().copy(memo)
  22         o._red_pageno = self._red_pageno
  23         o._remaining_stack = self._remaining_stack
  24         o._stack_perms = self._stack_perms
  25         return o
  26 
  27     def allocate_stack_pages(self, addr: int, size: int, **kwargs):
  28         """
  29         Pre-allocates pages for the stack without triggering any logic related to reading from them.
  30 
  31         :param addr: The highest address that should be mapped
  32         :param size: The number of bytes to be allocated. byte 1 is the one at addr, byte 2 is the one before that, and so on.
  33         :return: A list of the new page objects
  34         """
  35         # weird off-by-ones here. we want to calculate the last byte requested, find its pageno, and then use that to determine what the last page allocated will be and then how many pages are touched
  36         pageno = addr // self.page_size
  37         if pageno != self._red_pageno:
  38             raise SimMemoryError("Trying to allocate stack space in a place that isn't the top of the stack")
  39         num = pageno - ((addr - size + 1) // self.page_size) + 1
  40 
  41         result = []
  42         for _ in range(num):
  43             new_red_pageno = (self._red_pageno - 1) % ((1 << self.state.arch.bits) // self.page_size)
  44             if new_red_pageno in self._pages:
  45                 raise SimSegfaultException(self._red_pageno * self.page_size, "stack collided with heap")
  46 
  47             if self._remaining_stack is not None and self._remaining_stack < self.page_size:
  48                 raise SimSegfaultException(self._red_pageno * self.page_size, "exhausted stack quota")
  49 
  50             l.debug("Allocating new stack page at %#x", self._red_pageno * self.page_size)
  51             result.append(PagedMemoryMixin._initialize_default_page(self, self._red_pageno, permissions=self._stack_perms, **kwargs))
  52             self._pages[self._red_pageno] = result[-1]
  53 
  54             self._red_pageno = new_red_pageno
  55             if self._remaining_stack is not None:
  56                 self._remaining_stack -= self.page_size
  57 
  58         return result
  59 
  60     def _initialize_page(self, pageno: int, **kwargs):
  61         if pageno != self._red_pageno:
  62             return super()._initialize_page(pageno, **kwargs)
  63 
  64         return self.allocate_stack_pages((pageno + 1) * self.page_size - 1, self.page_size)[0]
  65
  [end of angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py b/angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py
  --- a/angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py
  +++ b/angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py
  @@ -2,6 +2,7 @@
   
   from .paged_memory_mixin import PagedMemoryMixin
   from ....errors import SimSegfaultException, SimMemoryError
  +from ....sim_options import STRICT_PAGE_ACCESS
   
   l = logging.getLogger(__name__)
   
  @@ -58,7 +59,7 @@
           return result
   
       def _initialize_page(self, pageno: int, **kwargs):
  -        if pageno != self._red_pageno:
  +        if pageno != self._red_pageno or STRICT_PAGE_ACCESS not in self.state.options:
               return super()._initialize_page(pageno, **kwargs)
   
           return self.allocate_stack_pages((pageno + 1) * self.page_size - 1, self.page_size)[0]
  
verification_info: {"golden_diff": "diff --git a/angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py b/angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py\n--- a/angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py\n+++ b/angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py\n@@ -2,6 +2,7 @@\n \n from .paged_memory_mixin import PagedMemoryMixin\n from ....errors import SimSegfaultException, SimMemoryError\n+from ....sim_options import STRICT_PAGE_ACCESS\n \n l = logging.getLogger(__name__)\n \n@@ -58,7 +59,7 @@\n         return result\n \n     def _initialize_page(self, pageno: int, **kwargs):\n-        if pageno != self._red_pageno:\n+        if pageno != self._red_pageno or STRICT_PAGE_ACCESS not in self.state.options:\n             return super()._initialize_page(pageno, **kwargs)\n \n         return self.allocate_stack_pages((pageno + 1) * self.page_size - 1, self.page_size)[0]\n", "issue": "MIPS32BE:  stack collided with heap?\nCouldn't get to the bottom of this one...\r\n\r\n```\r\n#!/home/<user>/angr_pypy/bin/python\r\n\r\nimport angr\r\nimport claripy\r\nimport monkeyhex\r\nimport logging\r\nimport pickle\r\nimport time\r\nimport sys\r\nimport os\r\nimport socket\r\nimport copy\r\nimport argparse\r\nimport ipdb\r\n\r\nfrom IPython import embed\r\n\r\nproj = angr.Project(\"httpd\", auto_load_libs=False, except_missing_libs=False)\r\n\r\ncfg = proj.analyses.CFGFast(normalize=True,\r\n                            fail_fast=True,\r\n                            force_complete_scan=False,\r\n                            data_references=False,\r\n                            cross_references=False,\r\n                            show_progressbar=True)\r\n\r\n# some functions we're interested in\r\nfuncs = proj.kb.functions\r\nparse_http_req = funcs[0x408f90]\r\n\r\ns = proj.factory.blank_state(addr=parse_http_req.addr)\r\n\r\n# running it in a simulation manager will allow us to examine the state after it errors out\r\nsm  = proj.factory.simulation_manager(s)\r\nsm.run()\r\n\r\nembed()\r\n```\r\n[httpd.zip](https://github.com/angr/angr/files/7671480/httpd.zip)\r\n\r\n\n", "before_files": [{"content": "import logging\n\nfrom .paged_memory_mixin import PagedMemoryMixin\nfrom ....errors import SimSegfaultException, SimMemoryError\n\nl = logging.getLogger(__name__)\n\nclass StackAllocationMixin(PagedMemoryMixin):\n    \"\"\"\n    This mixin adds automatic allocation for a stack region based on the stack_end and stack_size parameters.\n    \"\"\"\n    # TODO: multiple stacks. this scheme should scale p well\n    # TODO tbh this should be handled by an actual fault handler in simos or something\n    def __init__(self, stack_end=None, stack_size=None, stack_perms=None, **kwargs):\n        super().__init__(**kwargs)\n        self._red_pageno = (stack_end - 1) // self.page_size if stack_end is not None else None\n        self._remaining_stack = stack_size\n        self._stack_perms = stack_perms\n\n    def copy(self, memo):\n        o = super().copy(memo)\n        o._red_pageno = self._red_pageno\n        o._remaining_stack = self._remaining_stack\n        o._stack_perms = self._stack_perms\n        return o\n\n    def allocate_stack_pages(self, addr: int, size: int, **kwargs):\n        \"\"\"\n        Pre-allocates pages for the stack without triggering any logic related to reading from them.\n\n        :param addr: The highest address that should be mapped\n        :param size: The number of bytes to be allocated. byte 1 is the one at addr, byte 2 is the one before that, and so on.\n        :return: A list of the new page objects\n        \"\"\"\n        # weird off-by-ones here. we want to calculate the last byte requested, find its pageno, and then use that to determine what the last page allocated will be and then how many pages are touched\n        pageno = addr // self.page_size\n        if pageno != self._red_pageno:\n            raise SimMemoryError(\"Trying to allocate stack space in a place that isn't the top of the stack\")\n        num = pageno - ((addr - size + 1) // self.page_size) + 1\n\n        result = []\n        for _ in range(num):\n            new_red_pageno = (self._red_pageno - 1) % ((1 << self.state.arch.bits) // self.page_size)\n            if new_red_pageno in self._pages:\n                raise SimSegfaultException(self._red_pageno * self.page_size, \"stack collided with heap\")\n\n            if self._remaining_stack is not None and self._remaining_stack < self.page_size:\n                raise SimSegfaultException(self._red_pageno * self.page_size, \"exhausted stack quota\")\n\n            l.debug(\"Allocating new stack page at %#x\", self._red_pageno * self.page_size)\n            result.append(PagedMemoryMixin._initialize_default_page(self, self._red_pageno, permissions=self._stack_perms, **kwargs))\n            self._pages[self._red_pageno] = result[-1]\n\n            self._red_pageno = new_red_pageno\n            if self._remaining_stack is not None:\n                self._remaining_stack -= self.page_size\n\n        return result\n\n    def _initialize_page(self, pageno: int, **kwargs):\n        if pageno != self._red_pageno:\n            return super()._initialize_page(pageno, **kwargs)\n\n        return self.allocate_stack_pages((pageno + 1) * self.page_size - 1, self.page_size)[0]\n", "path": "angr/storage/memory_mixins/paged_memory/stack_allocation_mixin.py"}]}