From 54f83b3ffcd40da608d8e86652fb48629321b11b Mon Sep 17 00:00:00 2001
From: an7s <an7s@git.zephyr-software.com>
Date: Sat, 8 Apr 2017 21:34:49 +0000
Subject: [PATCH] Add utility to mark functions safe

Former-commit-id: d72a8eab27599f6b0e49f2fbfa0f819b0221d4c8
---
 .gitattributes                       |   1 +
 libIRDB/test/SConscript              |   6 +-
 libIRDB/test/mark_functions_safe.cpp | 124 +++++++++++++++++++++++++++
 3 files changed, 126 insertions(+), 5 deletions(-)
 create mode 100644 libIRDB/test/mark_functions_safe.cpp

diff --git a/.gitattributes b/.gitattributes
index aa5b9ebac..0b2ec112f 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -287,6 +287,7 @@ libIRDB/test/fix_calls.cpp -text
 libIRDB/test/generate_spri.cpp -text
 libIRDB/test/ilr.cpp -text
 libIRDB/test/list_programs.cpp -text
+libIRDB/test/mark_functions_safe.cpp -text
 libIRDB/test/pin_address.cpp -text
 libIRDB/test/print_variant.cpp -text
 libIRDB/test/read_ehframe.cpp -text
diff --git a/libIRDB/test/SConscript b/libIRDB/test/SConscript
index 5f772300b..9575e20c3 100644
--- a/libIRDB/test/SConscript
+++ b/libIRDB/test/SConscript
@@ -36,11 +36,10 @@ if 'build_tools' not in myenv or myenv['build_tools'] is None or int(myenv['buil
 	install=myenv.Install("$SECURITY_TRANSFORMS_HOME/bin/", pgm)
 	Default(install)
 
-
 	# most programs go to $sectrans/bin
 	pgms='''print_variant list_programs create_variant create_variantir read_variantir clone 
 		drop_variant generate_spri fill_in_cfg unfix_calls 
-		find_strings build_callgraph build_preds rename_function pin_address
+		find_strings build_callgraph build_preds rename_function pin_address mark_functions_safe
 		'''
 	for i in Split(pgms):
 		print "Registering pgm: "+ i
@@ -54,6 +53,3 @@ if 'build_tools' not in myenv or myenv['build_tools'] is None or int(myenv['buil
 	install=myenv.Install("$SECURITY_TRANSFORMS_HOME/plugins_install/", pgm)
 	Default(install)
 
-
-		
-		
diff --git a/libIRDB/test/mark_functions_safe.cpp b/libIRDB/test/mark_functions_safe.cpp
new file mode 100644
index 000000000..13a74f26c
--- /dev/null
+++ b/libIRDB/test/mark_functions_safe.cpp
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2014-2015 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+
+
+#include <libIRDB-core.hpp>
+#include <iostream>
+#include <fstream>
+#include <stdlib.h>
+
+using namespace libIRDB;
+using namespace std;
+
+Function_t* findFunction(FileIR_t* firp, string funcName)
+{
+	assert(firp);
+
+	for(
+		set<Function_t*>::iterator it=firp->GetFunctions().begin();
+		it!=firp->GetFunctions().end();
+		++it
+	   )
+	{
+		Function_t* func=*it;
+		if (!func) continue;
+		if (func->GetName() == funcName)
+		{
+			return func;
+		}
+	}
+
+	return NULL;
+}
+
+void mark_function_safe(FileIR_t* firp, const std::string& function_file)
+{
+	assert(firp);
+
+	std::ifstream ffile(function_file);
+	std::string fn;
+	while (std::getline(ffile, fn))
+	{
+		cerr << "Want to mark: " << fn << " as safe" << endl;
+		Function_t *f = findFunction(firp, fn);
+		if (f)
+		{
+			cerr << "Found in IRDB: Marking " << f->GetName() << " as safe" << endl;
+			f->SetSafe(true);
+		}
+	}
+
+	firp->WriteToDB();
+}
+
+main(int argc, char* argv[])
+{
+	if(argc!=3)
+	{
+		cerr<<"Usage: mark_function_safe <id> <file_with_functions_to_mark_safe>"<<endl;
+		exit(-1);
+	}
+
+	VariantID_t *pidp=NULL;
+	FileIR_t *firp=NULL;
+	string function_files(argv[2]);
+
+	/* setup the interface to the sql server */
+	pqxxDB_t pqxx_interface;
+	BaseObj_t::SetInterface(&pqxx_interface);
+
+	cout<<"Reading variant "<<string(argv[1])<<" from database." << endl;
+	try 
+	{
+		pidp=new VariantID_t(atoi(argv[1]));
+		assert(pidp->IsRegistered()==true);
+
+		for(set<File_t*>::iterator it=pidp->GetFiles().begin(); it!=pidp->GetFiles().end(); ++it)
+		{
+			File_t* this_file=*it;
+			assert(this_file);
+
+			// only do main file for now
+			if(this_file!=pidp->GetMainFile())
+				continue;
+
+			// read the db  
+			firp=new FileIR_t(*pidp,this_file);
+			
+			// mark as safe
+			mark_function_safe(firp, function_files);
+
+			delete firp;
+		}
+		pqxx_interface.Commit();
+
+	}
+	catch (DatabaseError_t pnide)
+	{
+		cout<<"Unexpected database error: "<<pnide<<endl;
+		exit(-1);
+        }
+
+	cout<<"Done!"<<endl;
+
+	delete pidp;
+}
+
-- 
GitLab