From 47f979e95cd970e5400e7f36c36fcf8e7e3db5a2 Mon Sep 17 00:00:00 2001
From: whh8b <whh8b@git.zephyr-software.com>
Date: Mon, 17 Aug 2015 01:20:38 +0000
Subject: [PATCH] Update memory space to include RemoveFreeRange()

---
 .gitattributes           |  2 +-
 SConscript               |  3 +--
 include/zipr_mem_space.h |  1 +
 src/memory_space.cpp     |  5 +++++
 src/zipr.cpp             |  2 +-
 test/Makefile.in         | 39 ---------------------------------------
 test/MemorySpace.cpp     | 23 +++++++++++++++++++++++
 test/SConstruct          |  6 ++++++
 8 files changed, 38 insertions(+), 43 deletions(-)
 delete mode 100644 test/Makefile.in
 create mode 100644 test/SConstruct

diff --git a/.gitattributes b/.gitattributes
index 270c410..ab735a6 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -25,8 +25,8 @@ src/plugin_man.cpp -text
 src/zipr.cpp -text
 src/zipr_options.cpp -text
 src/zipr_stats.cpp -text
-test/Makefile.in -text
 test/MemorySpace.cpp -text
+test/SConstruct -text
 test/dylib/Makefile -text
 test/dylib/dylib.c -text
 test/dylib/dylib.h -text
diff --git a/SConscript b/SConscript
index 27c259b..ebd2350 100644
--- a/SConscript
+++ b/SConscript
@@ -21,5 +21,4 @@ SConscript(sectrans_sconscript, variant_dir='scons_build/irdb_libs')
 #print 'env='
 #print env.Dump()
 SConscript("src/SConscript", variant_dir='scons_build/zipr')
-
-
+SConscript("test/SConscript")
diff --git a/include/zipr_mem_space.h b/include/zipr_mem_space.h
index 7fae66c..8cedb89 100644
--- a/include/zipr_mem_space.h
+++ b/include/zipr_mem_space.h
@@ -48,6 +48,7 @@ class ZiprMemorySpace_t : public MemorySpace_t
 		std::set<Range_t>::iterator FindFreeRange(RangeAddress_t addr);
 		Range_t GetFreeRange(int size);
 		void AddFreeRange(Range_t newRange);
+		void RemoveFreeRange(Range_t newRange);
 
 		// queries about free areas.
 		bool AreBytesFree(RangeAddress_t addr, int num_bytes);
diff --git a/src/memory_space.cpp b/src/memory_space.cpp
index aa4a89d..a803c77 100644
--- a/src/memory_space.cpp
+++ b/src/memory_space.cpp
@@ -198,6 +198,11 @@ void ZiprMemorySpace_t::AddFreeRange(Range_t newRange)
 {
 	free_ranges.insert(Range_t(newRange.GetStart(), newRange.GetEnd()));
 }
+void ZiprMemorySpace_t::RemoveFreeRange(Range_t oldRange)
+{
+	free_ranges.erase(Range_t(oldRange.GetStart(), oldRange.GetEnd()));
+}
+
 int ZiprMemorySpace_t::GetRangeCount()
 {
 	return free_ranges.size();
diff --git a/src/zipr.cpp b/src/zipr.cpp
index 789c8a0..b44b366 100644
--- a/src/zipr.cpp
+++ b/src/zipr.cpp
@@ -1527,7 +1527,7 @@ RangeAddress_t Zipr_t::PlopWithTarget(Instruction_t* insn, RangeAddress_t at)
 
 void Zipr_t::RewritePCRelOffset(RangeAddress_t from_addr,RangeAddress_t to_addr, int insn_length, int offset_pos)
 {
-	int new_offset=to_addr-from_addr-insn_length;
+	int new_offset=((unsigned int)to_addr)-((unsigned int)from_addr)-((unsigned int)insn_length);
 
 	memory_space[from_addr+offset_pos+0]=(new_offset>>0)&0xff;
 	memory_space[from_addr+offset_pos+1]=(new_offset>>8)&0xff;
diff --git a/test/Makefile.in b/test/Makefile.in
deleted file mode 100644
index 668d5c9..0000000
--- a/test/Makefile.in
+++ /dev/null
@@ -1,39 +0,0 @@
-
-INC=-I../include -I../third_party/ELFIO/elfio-2.2 -I$(SECURITY_TRANSFORMS_HOME)/include -I$(SECURITY_TRANSFORMS_HOME)/libIRDB/include  -I$(SECURITY_TRANSFORMS_HOME)/beaengine/include -I${SECURITY_TRANSFORMS_HOME}/tools/transforms/
-
-
-
-SRCS=MemorySpace.cpp ../src/zipr_options.cpp ../src/memory_space.cpp
-OBJS=$(subst .cpp,.o, $(SRCS))
-EXE=MemoryTest.exe
-
-CXX=@CXX@
-CXXFLAGS=@CXXFLAGS@ @OPTIMIZE@
-LDFLAGS=-L$(SECURITY_TRANSFORMS_HOME)/lib -Wl,-E
-LIBS=-lIRDB-core -lBeaEngine_s_d -lpqxx -lpq 
-
-
-
-
-all: $(EXE)
-
--include $(OBJS:.o=.d)
-
-$(EXE): $(OBJS)
-	g++ $(LDFLAGS) $(OBJS) $(LIBS) -o $@
-
-%.o: %.cpp
-	$(CXX) -c  $(CXXFLAGS) $*.cpp -o $@
-	@#
-	@# build dependencies --  http://scottmcpeak.com/autodepend/autodepend.html
-	@#
-	$(CXX) -MM $(CXXFLAGS) $*.cpp > $*.d
-	@cp -f $*.d $*.d.tmp
-	@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
-	@rm -f $*.d.tmp
-
-
-
-
-clean:
-	rm -f $(OBJS) $(EXE) *.d *.o
diff --git a/test/MemorySpace.cpp b/test/MemorySpace.cpp
index 302e97d..fce502c 100644
--- a/test/MemorySpace.cpp
+++ b/test/MemorySpace.cpp
@@ -164,6 +164,28 @@ bool TestSort()
 
 	return true;
 }
+
+bool TestInsertRemoveFreeRange()
+{
+	ZiprOptions_t opts;
+	opts.SetVerbose(true);
+	ZiprMemorySpace_t m(&opts);
+	m.AddFreeRange(Range_t(256, 512));
+	m.AddFreeRange(Range_t(513, 1024));
+	m.AddFreeRange(Range_t(1025, 4096));
+	m.PrintMemorySpace(cout);
+	if (m.GetRangeCount() != 3)
+		return false;
+	m.RemoveFreeRange(Range_t(513, 1024));
+	if (m.GetRangeCount() != 2)
+		return false;
+	m.PrintMemorySpace(cout);
+	m.RemoveFreeRange(Range_t(256, 512));
+	m.RemoveFreeRange(Range_t(1025, 4096));
+	m.PrintMemorySpace(cout);
+	return (m.GetRangeCount() == 0);
+}
+
 bool TestMergeFreeRange()
 {
 	ZiprOptions_t opts;
@@ -220,4 +242,5 @@ int main(int argc, char *argv[])
 	INVOKE(TestSort);
 	INVOKE(TestBinarySearch);
 	INVOKE(TestBinarySearchMaxRange);
+	INVOKE(TestInsertRemoveFreeRange);
 }
diff --git a/test/SConstruct b/test/SConstruct
new file mode 100644
index 0000000..c0dd68a
--- /dev/null
+++ b/test/SConstruct
@@ -0,0 +1,6 @@
+
+
+
+env=Environment()
+Export('env')
+SConscript("SConscript")
-- 
GitLab