<commit_before>//-----------------------------------------------------------------------------
// Copyright (c) 2014 <NAME>
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//-----------------------------------------------------------------------------

#ifndef __ELFARGUMENTS_H_
#define __ELFARGUMENTS_H_
#pragma once

#include "auxvec.h"						// Include auxiliary vector decls
#include "elf.h"						// Include ELF format declarations
#include "Exception.h"					// Include Exception declarations
#include "MemoryRegion.h"				// Include MemoryRegion declarations
#include "Win32Exception.h"				// Include Win32Exception declarations

#pragma warning(push, 4)				// Enable maximum compiler warnings

//-----------------------------------------------------------------------------
// ElfArgumentsT
//
// ELF arguments on the x86/x64 platform are provided by pushing a vector of
// values/pointers onto the stack prior to jumping to the entry point.  The
// typical memory format is as follows:
//
//  STACK POINTER -->   argc          number of arguments
//                      argv[0-n]     pointers to command line arguments
//                      NULL          separator
//                      env[0-n]      pointers to environment variables
//                      NULL          separator
//                      auxv[0-n]     auxiliary vectors
//                      AT_NULL       separator
//                      NULL          terminator
//                      zero[0-15]    16-byte alignment
//  INFO BLOCK ----->   [auxv]        packed auxiliary vector data
//                      [env]         packed environment strings
//                      [argv]        packed command line argument strings
//  STACK BOTTOM ---->  NULL          terminator
//
// ElfArgumentsT does things differently since we want to use the existing stack
// space of the process/thread.  A single chunk of virtual memory (64KiB) is
// allocated, and the arguments/variables/aux vectors are appended it to it.
// When complete, the ELF arguments are allocated in the same chunk of memory.
// The arguments can then be copied into the process/thread stack for execution:
//
//  BASE ADDRESS --->   [info]         packed information block  <--+
//                      zero[0-15]     16-byte alignment            |
//  ARGUMENTS ------>   argc           number of arguments          |
//                      argv           argument pointers -----------+ 
//                      NULL           separator                    |
//                      env[0-n]       environment pointers --------+
//                      NULL           separators                   |
//                      auxv[0-n]      auxliliary vectors ----------+
//                      AT_NULL        separator                          
//                      NULL           terminator
//                      zero[0-15]     16-byte alignment

template <class addr_t, class auxv_t>
class ElfArgumentsT
{
public:

	// Instance constructors
	//
	ElfArgumentsT();

	//-------------------------------------------------------------------------
	// Member Functions

	// AppendArgument
	//
	// Appends a command line argument
	void AppendArgument(const char* value);
	void AppendArgument(const wchar_t* value);

	// AppendAuxiliaryVector
	//
	// Appends an auxiliary vector
	void AppendAuxiliaryVector(addr_t type, addr_t value);
	void AppendAuxiliaryVector(addr_t type, const char* value);
	void AppendAuxiliaryVector(addr_t type, const wchar_t* value);
	void AppendAuxiliaryVector(addr_t type, const void* buffer, size_t length);

	// AppendEnvironmentVariable
	//
	// Appends an environment variable
	void AppendEnvironmentVariable(const char* key, const char* value);
	void AppendEnvironmentVariable(const wchar_t* key, const wchar_t* value);

	// CreateArgumentVector
	//
	// Creates the argument vector
	const void* CreateArgumentVector(size_t* length);

private:

	ElfArgumentsT(const ElfArgumentsT&);
	ElfArgumentsT& operator=(const ElfArgumentsT&);

	//-------------------------------------------------------------------------
	// Private Member Functions

	// AlignUp
	//
	// Address alignment helper function
	static size_t AlignUp(size_t offset, size_t alignment);

	// AppendInfo
	//<commit_msg>Add a couple new convenience overloads for appending auxiliary vectors<commit_after>//-----------------------------------------------------------------------------
// Copyright (c) 2014 <NAME>
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//-----------------------------------------------------------------------------

#ifndef __ELFARGUMENTS_H_
#define __ELFARGUMENTS_H_
#pragma once

#include "auxvec.h"						// Include auxiliary vector decls
#include "elf.h"						// Include ELF format declarations
#include "Exception.h"					// Include Exception declarations
#include "MemoryRegion.h"				// Include MemoryRegion declarations
#include "Win32Exception.h"				// Include Win32Exception declarations

#pragma warning(push, 4)				// Enable maximum compiler warnings

//-----------------------------------------------------------------------------
// ElfArgumentsT
//
// ELF arguments on the x86/x64 platform are provided by pushing a vector of
// values/pointers onto the stack prior to jumping to the entry point.  The
// typical memory format is as follows:
//
//  STACK POINTER -->   argc          number of arguments
//                      argv[0-n]     pointers to command line arguments
//                      NULL          separator
//                      env[0-n]      pointers to environment variables
//                      NULL          separator
//                      auxv[0-n]     auxiliary vectors
//                      AT_NULL       separator
//                      NULL          terminator
//                      zero[0-15]    16-byte alignment
//  INFO BLOCK ----->   [auxv]        packed auxiliary vector data
//                      [env]         packed environment strings
//                      [argv]        packed command line argument strings
//  STACK BOTTOM ---->  NULL          terminator
//
// ElfArgumentsT does things differently since we want to use the existing stack
// space of the process/thread.  A single chunk of virtual memory (64KiB) is
// allocated, and the arguments/variables/aux vectors are appended it to it.
// When complete, the ELF arguments are allocated in the same chunk of memory.
// The arguments can then be copied into the process/thread stack for execution:
//
//  BASE ADDRESS --->   [info]         packed information block  <--+
//                      zero[0-15]     16-byte alignment            |
//  ARGUMENTS ------>   argc           number of arguments          |
//                      argv           argument pointers -----------+ 
//                      NULL           separator                    |
//                      env[0-n]       environment pointers --------+
//                      NULL           separators                   |
//                      auxv[0-n]      auxliliary vectors ----------+
//                      AT_NULL        separator                          
//                      NULL           terminator
//                      zero[0-15]     16-byte alignment

template <class addr_t, class auxv_t>
class ElfArgumentsT
{
public:

	// Instance constructors
	//
	ElfArgumentsT();

	//-------------------------------------------------------------------------
	// Member Functions

	// AppendArgument
	//
	// Appends a command line argument
	void AppendArgument(const char* value);
	void AppendArgument(const wchar_t* value);

	// AppendAuxiliaryVector
	//
	// Appends an auxiliary vector
	void AppendAuxiliaryVector(addr_t type, addr_t value);
	void AppendAuxiliaryVector(addr_t type, const char* value);
	void AppendAuxiliaryVector(addr_t type, const wchar_t* value);
	void AppendAuxiliaryVector(addr_t type, const void* buffer, size_t length);
	void AppendAuxiliaryVector(addr_t type, int value) { AppendAuxiliaryVector(type, static_cast<addr_t>(value)); }
	void AppendAuxiliaryVector(addr_t type, const void* value) { AppendAuxiliaryVector(type, reinterpret_cast<addr_t>(value)); }


	// AppendEnvironmentVariable
	//
	// Appends an environment variable
	void AppendEnvironmentVariable(const char* key, const char* value);
	void AppendEnvironmentVariable(const wchar_t* key, const wchar_t* value);

	// CreateArgumentVector
	//
	// Creates the argument vector
	const void* CreateArgumentVector(size_t* length);

private:

	ElfArgumentsT(const ElfArgumentsT&);
	ElfArgumentsT& operator=(const ElfArgumentsT&);

	//-------------------------------------------------------------------------
	// Private Member Functions

	// AlignUp
	//
	// Address alignment helper function
	static size_t AlignUp(size_t offset, size_t alignment);

	// AppendInfo
	//
{'fraction_non_alphanumeric': 0.15028954867175293, 'fraction_numerical': 0.006434414927842633, 'mean_word_length': 1.732295328980412, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '4000507', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2556, 'n_words': 1004}