mireado

starting commit

This diff is collapsed. Click to expand it.
1 +# ppsspp.pri
2 +# 12/26/2014 jichi
3 +
4 +DEPENDPATH += $$PWD
5 +
6 +HEADERS += \
7 + $$PWD/funcinfo.h
8 +
9 +# EOF
1 +# ith.pro
2 +# 10/13/2011 jichi
3 +
4 +TEMPLATE = subdirs
5 +
6 +# The order is important!
7 +SUBDIRS += \
8 + sys \
9 + hook hookxp \
10 + host
11 +
12 +OTHER_FILES += dllconfig.pri
13 +
14 +include(common/common.pri) # not used
15 +include(import/mono/mono.pri) # not used
16 +include(import/ppsspp/ppsspp.pri) # not used
17 +
18 +# EOF
1 +# sys.pro
2 +# CONFIG += noqt noeh staticlib
3 +
4 +# CONFIG(noeh) {
5 +# message(CONFIG noeh)
6 +# QMAKE_CXXFLAGS += /GR-
7 +# QMAKE_CXXFLAGS_RTTI_ON -= /GR
8 +# QMAKE_CXXFLAGS_STL_ON -= /EHsc
9 +# QMAKE_CXXFLAGS_EXCEPTIONS_ON -= /EHsc
10 +# CONFIG(dll) {
11 +# QMAKE_LFLAGS += /ENTRY:"DllMain"
12 +# }
13 +# }
14 +
15 +set(vnrsys_src
16 + sys.h
17 + sys.cc
18 +)
19 +
20 +add_library(vnrsys STATIC ${vnrsys_src})
21 +
22 +target_compile_options(vnrsys PRIVATE
23 + # http://msdn.microsoft.com/library/we6hfdy0.aspx
24 + /GR- # disable RTTI
25 + # http://msdn.microsoft.com/library/1deeycx5.aspx
26 + # /EHs-c- # disable exception handling # CMake bug 15243: http://www.cmake.org/Bug/view.php?id=15243
27 + $<$<CONFIG:Release>:>
28 + $<$<CONFIG:Debug>:>
29 +)
30 +
31 +STRING(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
32 +
33 +target_link_libraries(vnrsys comctl32.lib)
34 +
35 +target_compile_definitions(vnrsys
36 + PRIVATE
37 +)
This diff is collapsed. Click to expand it.
1 +#pragma once
2 +
3 +// ith/sys.h
4 +// 8/23/2013 jichi
5 +// Branch: ITH/IHF_SYS.h, rev 111
6 +
7 +#ifdef _MSC_VER
8 +# pragma warning(disable:4800) // C4800: forcing value to bool
9 +#endif // _MSC_VER
10 +
11 +#include "ntdll/ntdll.h"
12 +
13 +// jichi 8/24/2013: Why extern "C"? Any specific reason to use C instead of C++ naming?
14 +extern "C" {
15 +//int disasm(BYTE *opcode0); // jichi 8/15/2013: move disasm to separate file
16 +extern WORD *NlsAnsiCodePage;
17 +int FillRange(LPCWSTR name,DWORD *lower, DWORD *upper);
18 +int MB_WC(char *mb, wchar_t *wc);
19 +//int MB_WC_count(char *mb, int mb_length);
20 +int WC_MB(wchar_t *wc, char *mb);
21 +
22 +// jichi 10/1/2013: Return 0 if failed. So, it is ambiguous if the search pattern starts at 0
23 +DWORD SearchPattern(DWORD base, DWORD base_length, LPCVOID search, DWORD search_length); // KMP
24 +
25 +// jichi 2/5/2014: The same as SearchPattern except it uses 0xff to match everything
26 +// According to @Andys, 0xff seldom appear in the source code: http://sakuradite.com/topic/124
27 +enum : BYTE { SP_ANY = 0xff };
28 +#define SP_ANY_2 SP_ANY,SP_ANY
29 +#define SP_ANY_3 SP_ANY,SP_ANY,SP_ANY
30 +#define SP_ANY_4 SP_ANY,SP_ANY,SP_ANY,SP_ANY
31 +DWORD SearchPatternEx(DWORD base, DWORD base_length, LPCVOID search, DWORD search_length, BYTE wildcard=SP_ANY);
32 +
33 +BOOL IthInitSystemService();
34 +void IthCloseSystemService();
35 +DWORD IthGetMemoryRange(LPCVOID mem, DWORD *base, DWORD *size);
36 +BOOL IthCheckFile(LPCWSTR file);
37 +BOOL IthFindFile(LPCWSTR file);
38 +BOOL IthGetFileInfo(LPCWSTR file, LPVOID info, DWORD size = 0x1000);
39 +BOOL IthCheckFileFullPath(LPCWSTR file);
40 +HANDLE IthCreateFile(LPCWSTR name, DWORD option, DWORD share, DWORD disposition);
41 +HANDLE IthCreateFileInDirectory(LPCWSTR name, HANDLE dir, DWORD option, DWORD share, DWORD disposition);
42 +HANDLE IthCreateDirectory(LPCWSTR name);
43 +HANDLE IthCreateFileFullPath(LPCWSTR fullpath, DWORD option, DWORD share, DWORD disposition);
44 +HANDLE IthPromptCreateFile(DWORD option, DWORD share, DWORD disposition);
45 +HANDLE IthCreateSection(LPCWSTR name, DWORD size, DWORD right);
46 +HANDLE IthCreateEvent(LPCWSTR name, DWORD auto_reset=0, DWORD init_state=0);
47 +HANDLE IthOpenEvent(LPCWSTR name);
48 +void IthSetEvent(HANDLE hEvent);
49 +void IthResetEvent(HANDLE hEvent);
50 +HANDLE IthCreateMutex(LPCWSTR name, BOOL InitialOwner, DWORD *exist=0);
51 +HANDLE IthOpenMutex(LPCWSTR name);
52 +BOOL IthReleaseMutex(HANDLE hMutex);
53 +//DWORD IthWaitForSingleObject(HANDLE hObject, DWORD dwTime);
54 +HANDLE IthCreateThread(LPCVOID start_addr, DWORD param, HANDLE hProc=(HANDLE)-1);
55 +DWORD GetExportAddress(DWORD hModule,DWORD hash);
56 +void IthSleep(int time); // jichi 9/28/2013: in ms
57 +void IthSystemTimeToLocalTime(LARGE_INTEGER *ptime);
58 +void FreeThreadStart(HANDLE hProc);
59 +void CheckThreadStart();
60 +} // extern "C"
61 +
62 +#ifdef ITH_HAS_HEAP
63 +extern HANDLE hHeap; // used in ith/common/memory.h
64 +#endif // ITH_HAS_HEAP
65 +
66 +extern DWORD current_process_id;
67 +extern DWORD debug;
68 +extern BYTE LeadByteTable[];
69 +extern LPVOID page;
70 +extern BYTE launch_time[];
71 +
72 +inline DWORD GetHash(LPSTR str)
73 +{
74 + DWORD hash = 0;
75 + //for (; *str; str++)
76 + while (*str)
77 + hash = ((hash>>7) | (hash<<25)) + *str++;
78 + return hash;
79 +}
80 +
81 +inline DWORD GetHash(LPCWSTR str)
82 +{
83 + DWORD hash = 0;
84 + //for (; *str; str++)
85 + while (*str)
86 + hash = ((hash>>7) | (hash<<25)) + *str++;
87 + return hash;
88 +}
89 +
90 +inline void IthBreak()
91 +{ if (debug) __debugbreak(); }
92 +
93 +inline LPCWSTR GetMainModulePath()
94 +{
95 + __asm
96 + {
97 + mov eax, fs:[0x30]
98 + mov eax, [eax + 0xC]
99 + mov eax, [eax + 0xC]
100 + mov eax, [eax + 0x28]
101 + }
102 +}
103 +
104 +// jichi 9/28/2013: Add this to lock NtWriteFile in wine
105 +class IthMutexLocker
106 +{
107 + HANDLE m;
108 +public:
109 + explicit IthMutexLocker(HANDLE mutex) : m(mutex)
110 + { NtWaitForSingleObject(m, 0, 0); }
111 +
112 + ~IthMutexLocker() { if (m != INVALID_HANDLE_VALUE) IthReleaseMutex(m); }
113 +
114 + bool locked() const { return m != INVALID_HANDLE_VALUE; }
115 +
116 + void unlock() { if (m != INVALID_HANDLE_VALUE) { IthReleaseMutex(m); m = INVALID_HANDLE_VALUE; } }
117 +};
118 +
119 +void IthCoolDown();
120 +
121 +BOOL IthIsWine();
122 +BOOL IthIsWindowsXp();
123 +//BOOL IthIsWindows8OrGreater(); // not public
124 +
125 +/** Get current dll path.
126 + * @param buf
127 + * @param len
128 + * @return length of the path excluding \0
129 + */
130 +size_t IthGetCurrentModulePath(wchar_t *buf, size_t len);
131 +
132 +// EOF
1 +# sys.pri
2 +# 8/21/2013 jichi
3 +
4 +DEFINES += WITH_LIB_ITH_SYS
5 +LIBS += -lvnrsys
6 +DEPENDPATH += $$PWD
7 +HEADERS += $$PWD/sys.h
8 +#SOURCES += $$PWD/sys.cc
9 +
10 +#include($$LIBDIR/winddk/winddk.pri)
11 +#LIBS += -L$$WDK/lib/wxp/i386
12 +
13 +# EOF
1 +# sys.pro
2 +# 8/21/2013 jichi
3 +# Build vnrsys.lib
4 +
5 +CONFIG += noqt noeh staticlib
6 +
7 +include(../../../../config.pri)
8 +include($$LIBDIR/ntdll/ntdll.pri)
9 +
10 +#include($$LIBDIR/winddk/winddk.pri)
11 +#LIBS += -L$$WDK/lib/wxp/i386
12 +
13 +# jichi 9/22/2013: When ITH is on wine, certain NT functions are replaced
14 +#DEFINES += ITH_WINE
15 +
16 +# jichi 9/14/2013: Windows XP's msvnrt does not have except handler
17 +DEFINES -= ITH_HAS_SEH
18 +
19 +# jichi 11/24/2013: Disable manual heap
20 +DEFINES -= ITH_HAS_HEAP
21 +
22 +## Libraries
23 +
24 +#INCLUDEPATH += $$ITH_HOME/include
25 +#INCLUDEPATH += $$WDK7_HOME/inc/ddk
26 +
27 +#LIBS += -lgdi32 -luser32 -lkernel32
28 +#LIBS += -L$$WDK7_HOME/lib/wxp/i386 -lntdll
29 +#LIBS += $$WDK7_HOME/lib/crt/i386/msvcrt.lib # Override msvcrt10
30 +
31 +#DEFINES += ITH_HAS_CXX
32 +
33 +#LIBS += -lith_sys -lntdll
34 +#LIBS += -lith_tls -lntdll
35 +#LIBS += -lntoskrnl
36 +
37 +DEFINES += _CRT_NON_CONFORMING_SWPRINTFS
38 +
39 +## Sources
40 +
41 +TEMPLATE = lib
42 +TARGET = vnrsys
43 +
44 +HEADERS += sys.h
45 +SOURCES += sys.cc
46 +
47 +OTHER_FILES += sys.pri
48 +
49 +# EOF
1 +12/16/2013
2 +
3 +Differences between xp.dll and non-xp.dll for vnrhook.
4 +
5 +non-xp:
6 + CONFIG += eh
7 +
8 +xp:
9 + CONFIG += noeh
10 + CONFIG -= embed_manifest_dll # Pure dynamic determined. The manifest would break Windows XP support
11 + include($$LIBDIR/winseh/winseh_safe.pri)
1 +#ifndef _MEMDBG_H
2 +#define _MEMDBG_H
3 +
4 +// memdbg.h
5 +// 4/20/2014 jichi
6 +
7 +#ifndef MEMDBG_BEGIN_NAMESPACE
8 +# define MEMDBG_BEGIN_NAMESPACE namespace MemDbg {
9 +#endif
10 +#ifndef MEMDBG_END_NAMESPACE
11 +# define MEMDBG_END_NAMESPACE } // MemDbg
12 +#endif
13 +
14 +MEMDBG_BEGIN_NAMESPACE
15 +
16 +typedef unsigned char byte_t;
17 +typedef unsigned long dword_t;
18 +
19 +//typedef void *address_t; // LPVOID
20 +//typedef const void *const_address_t; // LPCVOID
21 +
22 +MEMDBG_END_NAMESPACE
23 +
24 +
25 +#endif // _MEMDBG_H
1 +# ntinspect.pri
2 +# 4/20/2014 jichi
3 +win32 {
4 +
5 +DEFINES += WITH_LIB_MEMDBG
6 +
7 +DEPENDPATH += $$PWD
8 +
9 +HEADERS += \
10 + $$PWD/memdbg.h \
11 + $$PWD/memsearch.h
12 +
13 +SOURCES += \
14 + $$PWD/memsearch.cc
15 +}
16 +
17 +# EOF
This diff is collapsed. Click to expand it.
1 +#ifndef _MEMDBG_MEMSEARCH_H
2 +#define _MEMDBG_MEMSEARCH_H
3 +
4 +// memsearch.h
5 +// 4/20/2014 jichi
6 +
7 +#include "memdbg/memdbg.h"
8 +
9 +MEMDBG_BEGIN_NAMESPACE
10 +
11 +/// Estimated maximum size of the caller function, the same as ITH FindCallAndEntryAbs
12 +enum { MaximumFunctionSize = 0x800 };
13 +
14 +/**
15 + * Return the absolute address of the caller function
16 + * The same as ITH FindCallAndEntryAbs().
17 + *
18 + * @param funcAddr callee function address
19 + * @param funcInst the machine code where the caller function starts
20 + * @param lowerBound the lower memory address to search
21 + * @param upperBound the upper memory address to search
22 + * @param* callerSearchSize the maximum size of caller
23 + * @return the caller absolute address if succeed or 0 if fail
24 + *
25 + * Example funcInst:
26 + * 0x55: push ebp
27 + * 0x81,0xec: sub esp XXOO (0xec81)
28 + * 0x83,0xec: sub esp XXOO (0xec83)
29 + */
30 +dword_t findCallerAddress(dword_t funcAddr, dword_t funcInst, dword_t lowerBound, dword_t upperBound, dword_t callerSearchSize = MaximumFunctionSize);
31 +dword_t findCallerAddressAfterInt3(dword_t funcAddr, dword_t lowerBound, dword_t upperBound, dword_t callerSearchSize = MaximumFunctionSize);
32 +dword_t findLastCallerAddress(dword_t funcAddr, dword_t funcInst, dword_t lowerBound, dword_t upperBound, dword_t callerSearchSize = MaximumFunctionSize);
33 +dword_t findLastCallerAddressAfterInt3(dword_t funcAddr, dword_t lowerBound, dword_t upperBound, dword_t callerSearchSize = MaximumFunctionSize);
34 +
35 +dword_t findMultiCallerAddress(dword_t funcAddr, const dword_t funcInsts[], dword_t funcInstCount, dword_t lowerBound, dword_t upperBound, dword_t callerSearchSize = MaximumFunctionSize);
36 +
37 +/**
38 + * Return the absolute address of the long jump (not short jump) instruction address.
39 + * The same as ITH FindCallOrJmpAbs(false).
40 + *
41 + * @param funcAddr callee function address
42 + * @param lowerBound the lower memory address to search
43 + * @param upperBound the upper memory address to search
44 + * @return the call instruction address if succeed or 0 if fail
45 + */
46 +dword_t findJumpAddress(dword_t funcAddr, dword_t lowerBound, dword_t upperBound);
47 +
48 +/**
49 + * Return the absolute address of the far call (inter-module) instruction address.
50 + * The same as ITH FindCallOrJmpAbs(true).
51 + *
52 + * @param funcAddr callee function address
53 + * @param lowerBound the lower memory address to search
54 + * @param upperBound the upper memory address to search
55 + * @return the call instruction address if succeed or 0 if fail
56 + */
57 +dword_t findFarCallAddress(dword_t funcAddr, dword_t lowerBound, dword_t upperBound);
58 +
59 +/// Near call (intra-module)
60 +dword_t findNearCallAddress(dword_t funcAddr, dword_t lowerBound, dword_t upperBound);
61 +
62 +/// Default to far call
63 +inline dword_t findCallAddress(dword_t funcAddr, dword_t lowerBound, dword_t upperBound)
64 +{ return findFarCallAddress(funcAddr, lowerBound, upperBound); }
65 +
66 +/// Push value >= 0xff
67 +dword_t findPushDwordAddress(dword_t value, dword_t lowerBound, dword_t upperBound);
68 +
69 +/// Push value <= 0xff
70 +dword_t findPushByteAddress(byte_t value, dword_t lowerBound, dword_t upperBound);
71 +
72 +/// Default to push DWORD
73 +inline dword_t findPushAddress(dword_t value, dword_t lowerBound, dword_t upperBound)
74 +{ return findPushDwordAddress(value, lowerBound, upperBound); }
75 +
76 +/**
77 + * Return the enclosing function address outside the given address.
78 + * The same as ITH FindEntryAligned().
79 + * "Aligned" here means the function must be after in3 (0xcc) or nop (0x90).
80 + *
81 + * If the function does NOT exist, this function might raise without admin privilege.
82 + * It is safer to wrap this function within SEH.
83 + *
84 + * @param addr address within th function
85 + * @param searchSize max backward search size
86 + * @return beginning address of the function
87 + * @exception illegal memory access
88 + */
89 +dword_t findEnclosingAlignedFunction(dword_t addr, dword_t searchSize = MaximumFunctionSize);
90 +
91 +/**
92 + * Return the address of the first matched pattern.
93 + * Return 0 if failed. The return result is ambiguous if the pattern address is 0.
94 + * This function simpily traverse all bytes in memory range and would raise
95 + * if no access to the region.
96 + *
97 + * @param pattern array of bytes to match
98 + * @param patternSize size of the pattern array
99 + * @param lowerBound search start address
100 + * @param upperBound search stop address
101 + * @return absolute address
102 + * @exception illegal memory access
103 + */
104 +dword_t findBytes(const void *pattern, dword_t patternSize, dword_t lowerBound, dword_t upperBound);
105 +
106 +/**
107 + * jichi 2/5/2014: The same as findBytes except it uses widecard to match everything.
108 + * The widecard should use the byte seldom appears in the pattern.
109 + * See: http://sakuradite.com/topic/124
110 + *
111 + * @param pattern array of bytes to match
112 + * @param patternSize size of the pattern array
113 + * @param lowerBound search start address
114 + * @param upperBound search stop address
115 + * @param* widecard the character to match everything
116 + * @return absolute address
117 + * @exception illegal memory access
118 + */
119 +enum : byte_t { WidecardByte = 0x11 }; // jichi 7/17/2014: 0x11 seldom appear in PSP code pattern
120 +//enum : WORD { WidecardWord = 0xffff };
121 +dword_t matchBytes(const void *pattern, dword_t patternSize, dword_t lowerBound, dword_t upperBound,
122 + byte_t wildcard = WidecardByte);
123 +
124 +// User space: 0 - 2G (0 - 0x7ffeffff)
125 +// Kernel space: 2G - 4G (0x80000000 - 0xffffffff)
126 +//
127 +// http://msdn.microsoft.com/en-us/library/windows/hardware/ff560042%28v=vs.85%29.aspx
128 +// http://codesequoia.wordpress.com/2008/11/28/understand-process-address-space-usage/
129 +// http://stackoverflow.com/questions/17244912/open-process-with-debug-privileges-and-read-write-memory
130 +enum MemoryRange : dword_t {
131 + UserMemoryStartAddress = 0, UserMemoryStopAddress = 0x7ffeffff
132 + , KernelMemoryStartAddress = 0x80000000, KernelMemoryStopAddress = 0xffffffff
133 + , MappedMemoryStartAddress = 0x01000000
134 +
135 + , MemoryStartAddress = UserMemoryStartAddress, MemoryStopAddress = UserMemoryStopAddress
136 +};
137 +
138 +#if 0 // not used
139 +/**
140 + * Traverse memory continues pages and return the address of the first matched pattern.
141 + *
142 + * @param pattern array of bytes to match
143 + * @param patternSize size of the pattern array
144 + * @param lowerBound search start address
145 + * @param upperBound search stop address
146 + * @param* search search all pages (SearchAll) or stop on first illegal access (SearchFirst)
147 + * @return absolute address
148 + */
149 +enum SearchType : byte_t { SearchAll = 0 , SearchFirst };
150 +
151 +dword_t findBytesInPages(const void *pattern, dword_t patternSize,
152 + dword_t lowerBound = MemoryStartAddress, dword_t upperBound = MemoryStopAddress,
153 + SearchType search = SearchAll);
154 +dword_t matchBytesInPages(const void *pattern, dword_t patternSize,
155 + dword_t lowerBound = MemoryStartAddress, dword_t upperBound = MemoryStopAddress,
156 + byte_t wildcard = WidecardByte, SearchType search = SearchAll);
157 +
158 +#endif // 0
159 +
160 +MEMDBG_END_NAMESPACE
161 +
162 +#endif // _MEMDBG_MEMSEARCH_H