<commit_before>/*BEGIN_LEGAL 
Intel Open Source License 

Copyright (c) 2002-2013 Intel Corporation. All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.  Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.  Neither the name of
the Intel Corporation nor the names of its contributors may be used to
endorse or promote products derived from this software without
specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
END_LEGAL */

#include "pin.H"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include "memlist.h"
#include "memoryalloc.h"
#include "stats.h"
#include <stack>
using namespace std;

// Prototypes ; TODO: Move to Seperate Header file?
void HookFree(IMG img);
void HookMalloc(IMG img);
void HookCalloc(IMG img);
void HookRealloc(IMG img);

typedef VOID* (*FP_MALLOC)(size_t);
typedef void (*FP_FREE)(void*);
typedef void* (*FP_CALLOC)(size_t, size_t);
typedef void* (*FP_REALLOC)(void*, size_t);

bool hasEnding (string const &fullString, string const &ending);
void RecordAddrSource(ADDRINT address, string message);

bool checkStack = true;
FILE * trace;
MemList ml;
Stats stats;

stack<ADDRINT> addrStack;

// Print a memory read record
VOID RecordHeapMemRead(ADDRINT ip, VOID * addr) {
    int rtn = ml.containsAddress(addr);
    if(rtn == ERR_IN_FENCE) {
        fprintf(trace,"##########BAD READ: %p \n", addr);
		RecordAddrSource(ip, "BAD READ");
        stats.incInvalidReadCount();
    }
}

// Print a memory write record
VOID RecordHeapMemWrite(ADDRINT ip, VOID * addr) {
    int rtn = ml.containsAddress(addr);
    if(rtn == ERR_IN_FENCE) {
        fprintf(trace,"##########BAD WRITE: %p \n", addr);
		RecordAddrSource(ip, "BAD WRITE");
        stats.incInvalidWriteCount();
    }    
}

// see http://software.intel.com/sites/landingpage/pintool/docs/58423/Pin/html/group__DEBUG__API.html
void RecordAddrSource(ADDRINT address, string message) {
	INT32 column = 0;   // column number within the file.
	INT32 line = 0;     // line number within the file.
	string filename;    // source file name.
	PIN_LockClient();
	PIN_GetSourceLocation(address, &column, &line, &filename);
	PIN_UnlockClient();

    string rtnname = RTN_FindNameByAddress(address);
    // print only if source was found.
    if (!filename.empty()) {
		cout << filename << ":" << dec << line << ":" << column << endl;
        cout << "Address: 0x" << hex << address << ": In function: ";
		cout << "'" << rtnname << "': ";
	}
	// Print Error message
	cout << "Error: " << message << endl << endl;
}

bool hasEnding (string const &fullString, string const &ending) {
    if (fullString.length() >= ending.length()) {
        return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
    } else {
        return false;
    }
}

VOID RecordStackMemRead(VOID * ip, VOID * addr) {    
    //printf("stack read: %p\n", addr);    
}

// Print a memory write record
VOID RecordStackMemWrite(VOID * ip, VOID * addr) {
    //printf("stack write: %p\n", addr); 
}


VOID RecordCallIns(ADDRINT nextip) {
	addrStack.push(nextip);
	//cout << "Calling  : 0x" << hex << nextip << endl;
}

VOID RecordReturnIns(ADDRINT ip, ADDRINT retip) {
	ADDRINT originval;
	if (addrStack.empty()) {
		RecordAddrSource(ip, "TOO MANY RETURNS");
		stats.incInvalidReturnCount();
		fprintf(trace, "ERROR: TOO MANY RETURNS\n");<commit_msg>Added color to our errors.<commit_after>/*BEGIN_LEGAL 
Intel Open Source License 

Copyright (c) 2002-2013 Intel Corporation. All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.  Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.  Neither the name of
the Intel Corporation nor the names of its contributors may be used to
endorse or promote products derived from this software without
specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
END_LEGAL */

#include "pin.H"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include "memlist.h"
#include "memoryalloc.h"
#include "stats.h"
#include <stack>
using namespace std;

// Prototypes ; TODO: Move to Seperate Header file?
void HookFree(IMG img);
void HookMalloc(IMG img);
void HookCalloc(IMG img);
void HookRealloc(IMG img);

typedef VOID* (*FP_MALLOC)(size_t);
typedef void (*FP_FREE)(void*);
typedef void* (*FP_CALLOC)(size_t, size_t);
typedef void* (*FP_REALLOC)(void*, size_t);

bool hasEnding (string const &fullString, string const &ending);
void RecordAddrSource(ADDRINT address, string message);

bool checkStack = true;
FILE * trace;
MemList ml;
Stats stats;

stack<ADDRINT> addrStack;

// Print a memory read record
VOID RecordHeapMemRead(ADDRINT ip, VOID * addr) {
    int rtn = ml.containsAddress(addr);
    if(rtn == ERR_IN_FENCE) {
        fprintf(trace,"##########BAD READ: %p \n", addr);
		RecordAddrSource(ip, "BAD READ");
        stats.incInvalidReadCount();
    }
}

// Print a memory write record
VOID RecordHeapMemWrite(ADDRINT ip, VOID * addr) {
    int rtn = ml.containsAddress(addr);
    if(rtn == ERR_IN_FENCE) {
        fprintf(trace,"##########BAD WRITE: %p \n", addr);
		RecordAddrSource(ip, "BAD WRITE");
        stats.incInvalidWriteCount();
    }    
}

#define BOLD_RED "\033[1m\033[31m"
#define BOLD_WHITE "\033[1m\033[37m"
#define RESET "\033[0m"

// see http://software.intel.com/sites/landingpage/pintool/docs/58423/Pin/html/group__DEBUG__API.html
void RecordAddrSource(ADDRINT address, string message) {
	INT32 column = 0;   // column number within the file.
	INT32 line = 0;     // line number within the file.
	string filename;    // source file name.
	PIN_LockClient();
	PIN_GetSourceLocation(address, &column, &line, &filename);
	PIN_UnlockClient();

    string rtnname = RTN_FindNameByAddress(address);
    // print only if source was found.
    if (!filename.empty()){
		cout << BOLD_WHITE << filename << ":" << dec << line << ":" << column << RESET << endl;
        cout << BOLD_WHITE << "Address: 0x" << hex << address << ": In function: ";
		cout << "'" << rtnname << "': " << RESET;
	}
	// Print Error message
	cout << BOLD_RED << "Error: " << BOLD_WHITE << message << RESET << endl << endl;
}

bool hasEnding (string const &fullString, string const &ending) {
    if (fullString.length() >= ending.length()) {
        return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
    } else {
        return false;
    }
}

VOID RecordStackMemRead(VOID * ip, VOID * addr) {    
    //printf("stack read: %p\n", addr);    
}

// Print a memory write record
VOID RecordStackMemWrite(VOID * ip, VOID * addr) {
    //printf("stack write: %p\n", addr); 
}


VOID RecordCallIns(ADDRINT nextip) {
	addrStack.push(nextip);
	//cout << "Calling  : 0x" << hex << nextip << endl;
}

VOID RecordReturnIns(ADDRINT ip, ADDRINT retip) {
	ADDRINT originval;
	if (addrStack.empty()) {
		RecordAddrSource(ip, "TOO MANY RETURNS");
		stats.incInvalidReturnCount();
		fprintf(trace, "ERROR: TOO MANY RETURNS\n");
{'fraction_non_alphanumeric': 0.11436054124836316, 'fraction_numerical': 0.007202095154954169, 'mean_word_length': 4.309965237543453, 'pattern_counts': {'":': 6, '<': 117, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '895884', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2748, 'n_words': 1007}