From 382107b84741a38b93e818e7f9d84cd7654eb7da Mon Sep 17 00:00:00 2001
From: pompolic <pompolic@special-circumstanc.es>
Date: Fri, 21 Apr 2023 22:57:38 +0200
Subject: [PATCH] Add horrible hack to set fields of our mock objects in memory

---
 .../test_stack_frame_parser_context.py           | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/gdb-port/tests/integration/test_stack_frame_parser_context.py b/gdb-port/tests/integration/test_stack_frame_parser_context.py
index e41f6db..59e3abe 100644
--- a/gdb-port/tests/integration/test_stack_frame_parser_context.py
+++ b/gdb-port/tests/integration/test_stack_frame_parser_context.py
@@ -46,6 +46,13 @@ class TestParserContextWhenReturningFromStackFrames(unittest.TestCase):
 	def setUp(self):
 		self.a = Parser("a", 128)
 		self.b = Parser("b", 32)
+		self.harena_t = gdb.lookup_type("HArena")
+		self.arena = gdb.parse_and_eval("malloc(sizeof(HArena))").cast(self.harena_t.pointer()).dereference() # Quick and dirty way to allocate something in the process GDB is debugging (the inferior)
+		self.hps_t = gdb.lookup_type("HParseState")
+		self.test_parse_state = gdb.parse_and_eval("malloc(sizeof(HParseState))").cast(self.hps_t.pointer()).dereference()
+		# TODO: make this independent of endianness and pointer size
+		gdb.selected_inferior().write_memory(int(self.test_parse_state.address)+(self.hps_t['arena'].bitpos//8), int(self.arena.address).to_bytes(8, 'little')) # horrible, horrible way to set the 
+		self.hps_t = self.test_parse_state.address.dereference() # "refresh" the gdb.Value, since the effects of .write_memory() don't seem to show up
 		self.top_level_parse = TopLevelParse()
 		self.top_level_parse.parser_objs[32] = self.b
 		self.top_level_parse.parser_objs[128] = self.a
@@ -55,10 +62,10 @@ class TestParserContextWhenReturningFromStackFrames(unittest.TestCase):
 		self.top_level_parse.enter_h_packrat_parse(self.a.address)
 
 		#TODO: are we initializing from scratch for every test?
-		self.top_level_parse.enter_h_do_parse(400, 256, self.a.address)
+		self.top_level_parse.enter_h_do_parse(self.test_parse_state.address, self.arena.address, self.a.address)
 		self.top_level_parse.parse_virtual(self.a.address)
 		self.top_level_parse.enter_perform_lowlevel_parse(self.a.address)
-		self.top_level_parse.enter_h_do_parse(400, 256, self.b.address)
+		self.top_level_parse.enter_h_do_parse(self.test_parse_state.address, self.arena.address, self.b.address)
 		self.top_level_parse.parse_virtual(self.b.address)
 		self.top_level_parse.enter_perform_lowlevel_parse(self.b.address)
 
@@ -68,8 +75,9 @@ class TestParserContextWhenReturningFromStackFrames(unittest.TestCase):
 
 	def test_after_return_from_h_do_parse(self):
 		hps_t = gdb.lookup_type("HParseState")
-		test_parse_state = gdb.Value(b'\x00'*80, hps_t)
+		#test_parse_state = gdb.Value(b'\x00'*80, hps_t)
+		test_parse_state = gdb.parse_and_eval("malloc(sizeof(HParseState))").cast(hps_t.pointer()).dereference()
 
 		self.top_level_parse.return_from_perform_lowlevel_parse()
-		self.top_level_parse.return_from_h_do_parse(test_parse_state, self.b.address, 0)
+		self.top_level_parse.return_from_h_do_parse(self.test_parse_state.address, self.b.address, 0)
 		self.assertIs(self.top_level_parse.peek_parser(), self.a)
-- 
GitLab