From 78bd2c84b38fbc477a07e32eab30cd8b40134947 Mon Sep 17 00:00:00 2001
From: Jason Hiser <jdhiser@gmail.com>
Date: Mon, 27 Mar 2023 18:59:45 -0400
Subject: [PATCH] Add testing for libc, update capstone to 5.0-rc2

---
 .gitlab-ci.yml                             |  1 +
 cicd_testing/libc_test.sh                  | 11 ++++++++
 irdb-libs/ir_builders/fill_in_indtargs.cpp |  4 +--
 irdb-libs/third_party/capstone             |  2 +-
 tests/libc/hello.c                         |  5 ++++
 tests/libc/test_libc.sh                    | 31 ++++++++++++++++++++++
 zipr/src/zipr.cpp                          |  2 +-
 7 files changed, 52 insertions(+), 4 deletions(-)
 create mode 100755 cicd_testing/libc_test.sh
 create mode 100644 tests/libc/hello.c
 create mode 100755 tests/libc/test_libc.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 242546cf4..f4c7f2c32 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -94,6 +94,7 @@ test-bins-arm64:
     - ./cicd_testing/elfdep.sh
     - ./cicd_testing/go_tests.sh
     - ./cicd_testing/rust_tests.sh
+    - ./cicd_testing/libc_test.sh
 
 
 
diff --git a/cicd_testing/libc_test.sh b/cicd_testing/libc_test.sh
new file mode 100755
index 000000000..14f5fe3e0
--- /dev/null
+++ b/cicd_testing/libc_test.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+set -e
+set -x
+
+cd /tmp/peasoup_test
+export IDAROOT=$CICD_MODULE_WORK_DIR/idapro71
+export IDASDK=$CICD_MODULE_WORK_DIR/idapro71_sdk
+source set_env_vars
+
+cd $PEASOUP_HOME/tests/libc/
+./test_libc.sh
diff --git a/irdb-libs/ir_builders/fill_in_indtargs.cpp b/irdb-libs/ir_builders/fill_in_indtargs.cpp
index 5c449ef63..d653b4025 100644
--- a/irdb-libs/ir_builders/fill_in_indtargs.cpp
+++ b/irdb-libs/ir_builders/fill_in_indtargs.cpp
@@ -3915,10 +3915,10 @@ V2:
 				const auto scoop_contents = scoop->getContents().c_str();
 				const auto symsize =
 					ptrsize == 8 ? sizeof(Elf64_Sym) : ptrsize == 4 ? sizeof(Elf32_Sym)
-																	: throw domain_error("Cannot detect ptr size -> ELF symbol mapping");
+					: throw domain_error("Cannot detect ptr size -> ELF symbol mapping");
 
 				auto table_entry_no = 0U;
-				for (auto i = 0U; i + symsize < scoop->getSize(); i += symsize, table_entry_no++)
+				for (auto i = 0U; i + symsize <= scoop->getSize(); i += symsize, table_entry_no++)
 				{
 					int addr_offset = 0;
 					VirtualOffset_t vo = 0;
diff --git a/irdb-libs/third_party/capstone b/irdb-libs/third_party/capstone
index 1d2305328..d5141c047 160000
--- a/irdb-libs/third_party/capstone
+++ b/irdb-libs/third_party/capstone
@@ -1 +1 @@
-Subproject commit 1d230532840a37ac032c6ab80128238fc930c6c1
+Subproject commit d5141c04785678535c7792eddc21f146186e639f
diff --git a/tests/libc/hello.c b/tests/libc/hello.c
new file mode 100644
index 000000000..469c20bec
--- /dev/null
+++ b/tests/libc/hello.c
@@ -0,0 +1,5 @@
+#include <stdio.h>
+int main() {
+	printf("Hello world\n");
+	return 0;
+}
diff --git a/tests/libc/test_libc.sh b/tests/libc/test_libc.sh
new file mode 100755
index 000000000..cefd2939b
--- /dev/null
+++ b/tests/libc/test_libc.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+
+main()
+{
+	set -e
+	set -x
+
+	# calc some names
+	local libc=$(ldd $(which ls)|grep libc|cut -d'>' -f2|cut -d'(' -f1)
+	local libc_short=$(basename $libc)
+	local libc_zipr=$(basename $libc).zipr
+
+	# run zipr
+	$PSZ $libc $libc_zipr
+
+	# move the zir'd libc to the right name.  All subsequent commands
+	# now run with the zipr'd libc
+	mv $libc_zipr $libc_short
+
+	# invoke gcc
+	gcc hello.c || exit 1
+	./a.out || exit 1
+
+	# invoke zipr on ls -- using zipr'd libc.
+	$PSZ $(which ls) ls.zipr || exit 1
+	ls.zipr -lhrSR || exit 1
+}
+
+main "$@"
+
diff --git a/zipr/src/zipr.cpp b/zipr/src/zipr.cpp
index 0363678f3..c4862f24b 100644
--- a/zipr/src/zipr.cpp
+++ b/zipr/src/zipr.cpp
@@ -360,7 +360,7 @@ void ZiprImpl_t::CreateExecutableScoops(const std::map<RangeAddress_t, int> &ord
 
 		// setup a scoop for this section.
 		// zero init is OK, after zipring we'll update with the right bytes.
-		const auto text_name     = count == 1 ? string(".text") : string(".zipr_text_")+to_string(count++);
+		const auto text_name     = count++ == 0u ? string(".text") : string(".zipr_text_")+to_string(count);
 		const auto text_contents = string(text_end->getVirtualOffset() - text_start->getVirtualOffset()+1, '\x00');
 		const auto text_scoop    = m_firp->addNewDataScoop(text_name,  text_start, text_end, nullptr, 5 /*R-X*/, false, text_contents);
 	
-- 
GitLab