Skip to content

Commit

Permalink
fc
Browse files Browse the repository at this point in the history
  • Loading branch information
xor2003 committed Sep 22, 2024
1 parent 150fac9 commit 5182036
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 175 deletions.
42 changes: 42 additions & 0 deletions fc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
import sys
import ast

def compare_files(file1, file2, offset1=0, offset2=0):
with open(file1, "rb") as f1, open(file2, "rb") as f2:
# Seek to the specified offsets
offset1 = ast.literal_eval(offset1)
offset2 = ast.literal_eval(offset2)
f1.seek(offset1)
f2.seek(offset2)

index = 0
while True:
byte1 = f1.read(1)
byte2 = f2.read(1)

# Stop if we reach the end of either file
if byte1 == b"" or byte2 == b"":
break

if byte1 != byte2:
# Print differences in the desired format
addr1 = f'{index + offset1:08X} {index + offset2:08X}' # 32-bit hex address for file1
byte1_hex = f'{ord(byte1):02X}'
byte2_hex = f'{ord(byte2):02X}'
print(f"{addr1}: {byte1_hex} {byte2_hex}")

index += 1

if __name__ == "__main__":
if len(sys.argv) != 5:
print("Usage: python compare_files.py <file1> <file2> <offset1> <offset2>")
sys.exit(1)

file1 = sys.argv[1]
file2 = sys.argv[2]
offset1 = sys.argv[3]
offset2 = sys.argv[4]

compare_files(file1, file2, offset1, offset2)

3 changes: 2 additions & 1 deletion include/custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef Bit32u dd;

extern bool collect_rt_info;
extern bool collect_rt_info_vars;
extern bool compare_instructions;
extern Bit32u last_ip;

#include <unordered_set>
Expand Down Expand Up @@ -62,7 +63,7 @@ struct _STATE;

void push(_STATE *_state, dd value);

void pop(_STATE *_state);
void pop(_STATE *_state, size_t word_size);

void print(_STATE *_state);
void print_frame(const Frame& f);
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ prefers_static_libs = (get_option('default_library') == 'static')
summary('Build type', get_option('buildtype'), section : 'Build Summary')
summary('Install prefix', get_option('prefix'), section : 'Build Summary')

extra_flags = ['-ggdb3','-gdwarf-4','-rdynamic','-I/usr/include/SDL2','-Wmaybe-uninitialized', '-Wno-unused-variable','-Wno-unused-function','-mno-ms-bitfields', '-Wno-multichar', '-Wno-address-of-packed-member','-DDOSBOX_CUSTOM', '-DM2CDEBUG=1']
extra_flags = ['-ggdb3','-gdwarf-4','-rdynamic','-I/usr/include/SDL2','-Wmaybe-uninitialized', '-Wno-unused-variable','-Wno-unused-function','-mno-ms-bitfields', '-Wno-multichar', '-Wno-address-of-packed-member','-DDOSBOX_CUSTOM', '-DM2CDEBUG=1', '-D_SHADOW_STACK']
if host_machine.system() == 'windows'
extra_flags += '-Wno-pedantic-ms-format'
endif
Expand Down
4 changes: 2 additions & 2 deletions src/cpu/core_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ void print_instruction_direct(Bit16u newcs, Bit32u newip)
extern int custom_runs;

Bits CPU_Core_Normal_Run(void) {
if (last_ip != 0xffff and last_ip != cpu_regs.ip.dword[0] and custom_runs) {
if (compare_instructions && last_ip != 0xffff and last_ip != cpu_regs.ip.dword[0] and custom_runs) {
m2c::log_debug("IP changed dbx: %x now: %x\n", last_ip, cpu_regs.ip.dword[0]);
// exit(1);
exit(1);
}

while (CPU_Cycles-->0) {
Expand Down
4 changes: 2 additions & 2 deletions src/cpu/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void CPU_Push32(Bitu value) {

Bitu CPU_Pop16(void) {
#ifdef SHADOW_STACK
m2c::shadow_stack.pop(0);
m2c::shadow_stack.pop(0, 2);
#endif
Bitu val=mem_readw(SegPhys(ss) + (reg_esp & cpu.stack.mask));
reg_esp=(reg_esp&cpu.stack.notmask)|((reg_esp+2)&cpu.stack.mask);
Expand All @@ -163,7 +163,7 @@ Bitu CPU_Pop16(void) {

Bitu CPU_Pop32(void) {
#ifdef SHADOW_STACK
m2c::shadow_stack.pop(0);
m2c::shadow_stack.pop(0, 4);
#endif
Bitu val=mem_readd(SegPhys(ss) + (reg_esp & cpu.stack.mask));
reg_esp=(reg_esp&cpu.stack.notmask)|((reg_esp+4)&cpu.stack.mask);
Expand Down
46 changes: 31 additions & 15 deletions src/custom/asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ typedef int Bits;
#ifndef NOSDL
extern struct SDL_Renderer *renderer;
#endif

namespace m2c{
typedef dd _offsets;
struct _STATE;
}
extern bool __dispatch_call(m2c::_offsets __i, struct m2c::_STATE* _state);

namespace m2c {
extern db vgaPalette[256*3];

Expand Down Expand Up @@ -496,7 +503,7 @@ inline long getdata(const long& s)
#endif

#ifdef DOSBOX_CUSTOM
#define PUSH(a) {m2c::PUSH_((dw)a);} // WARNING !!! Hacking to word
#define PUSH(a) {m2c::PUSH_(a);} // WARNING !!! Hacking to word
#define POP(a) {m2c::POP_(a);}


Expand Down Expand Up @@ -545,7 +552,7 @@ inline long getdata(const long& s)
{
X86_REGREF
#ifdef SHADOW_STACK
m2c::shadow_stack.pop(_state);
m2c::shadow_stack.pop(_state, sizeof(a));
#endif

#if M2CDEBUG > 0
Expand Down Expand Up @@ -1064,7 +1071,7 @@ AFFECT_CF(((Destination<<m2c::bitsizeof(Destination)+Source) >> (32 - Count)) &
AFFECT_CF(false); \
AFFECT_OF(false); \
AFFECT_AF(false); \
} \
} else CPU_Exception(0); \
}

#define AAM AAM1(10)
Expand Down Expand Up @@ -1204,19 +1211,28 @@ AFFECT_CF(((Destination<<m2c::bitsizeof(Destination)+Source) >> (32 - Count)) &
}


#define IDIV2(op1) \
{ \
Bits val=(int16_t)(op1); \
if (val==0) CPU_Exception(0); \
Bits num=(int32_t)((dx<<16)|ax); \
Bits quo=num/val; \
int16_t rem=(int16_t)(num % val); \
int16_t quo16s=(int16_t)quo; \
if (quo!=(int32_t)quo16s) CPU_Exception(0); \
dx=rem; \
ax=quo16s; \
AFFECT_OF(false); \
#define IDIV2(op1) m2c::IDIV2_(op1, _state)


static /* MYINLINE*/ void IDIV2_(dw op1, _STATE* _state) {
X86_REGREF
const auto val = static_cast<int16_t>(op1);
if (val==0) {
log_debug("~~fail1\n");CPU_Exception(0);__dispatch_call((cs<<16)+ip, _state);
}
const auto num = (dx << 16) | ax;
const auto quo = num / val;
const auto rem = num % val;
const auto quo16s = static_cast<int16_t>(quo);
if (quo != quo16s)
{
log_debug("~~fail2\n");CPU_Exception(0);__dispatch_call((cs<<16)+ip, _state);
}
ax = quo16s;
dx = static_cast<int16_t>(rem);
AFFECT_OF(false);
}


#define IDIV4(op1) \
{ \
Expand Down
2 changes: 1 addition & 1 deletion src/custom/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool trace_instructions = true; // m2c::debug >= 1;
bool trace_instructions_to_stdout = false; // m2c::debug >= 1;
bool compare_instructions = true; // m2c::debug >= 1;// 1 || m2c::debug == 2 ||
// m2c::debug == 3;
bool complex_self_modifications = true;
bool complex_self_modifications = false;
bool collect_rt_info = false;
bool collect_rt_info_vars = false;

Expand Down
15 changes: 4 additions & 11 deletions src/custom/shadowstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace m2c{
return m_ss[esp].itwascall;
}

void ShadowStack::pop(_STATE * _state)
void ShadowStack::pop(_STATE * _state, size_t word_size)
{
if (!m_active && !m_forceactive)
{
Expand Down Expand Up @@ -82,15 +82,15 @@ namespace m2c{
}
// else log_debug ("m_current %x not initialized\n", m_current);

m_current += 2;
m_current += word_size;
// log_debug ("m_current %x\n", m_current);
}

while (m_current <= esp);
//log_debug("m_itisret %d m_current %x m_ss.at(m_current-2).itwascall %d\n",m_itisret, m_current, m_ss.at(m_current-2).itwascall);
if (m_itisret && m_ss.at(m_current-2).itwascall) {--m_needtoskipcall;log_debug("decreased m_needtoskipcall=%d\n",m_needtoskipcall);}
if (m_itisret && m_ss.at(m_current-word_size).itwascall) {--m_needtoskipcall;log_debug("decreased m_needtoskipcall=%d\n",m_needtoskipcall);}

m_currentdeep = m_ss.at(m_current-2).call_deep;
m_currentdeep = m_ss.at(m_current-word_size).call_deep;
log_debug ("m2c::counter %x m_deep %d collected m_currentdeep %d m_needtoskipcall %d m_itisret %d\n", counter, m_deep, m_currentdeep,m_needtoskipcall,m_itisret);
}

Expand All @@ -108,13 +108,6 @@ log_debug("decreasedeep m_deep=%d ",m_deep);
bool ShadowStack::needtoskipcalls(){
/*
log_debug("ret m_currentdeep=%d ",m_currentdeep);
m_needtoskipcall=m_currentdeep?m_deep-m_currentdeep:0;
if (m_needtoskipcall<0) {m_needtoskipcall=0;}
//m_deep=m_currentdeep?m_currentdeep-1:m_deep;
--m_deep;
log_debug("m_deep=%d ",m_deep);
m_currentdeep=0;
log_debug("m_currentdeep=%d\n",m_currentdeep);
*/
log_debug("m_needtoskipcall=%d\n",m_needtoskipcall);
return m_needtoskipcall;}
Expand Down
2 changes: 1 addition & 1 deletion src/custom/src/f.exe_seg35ab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ cs=0x274d;eip=0x00009a; T(MOV(dx, *(dw*)(raddr(ds,bx+0x1A0D)))); // 117114
cs=0x274d;eip=0x00009e; T(MOV(al, *(raddr(ds,bx+0x1A0F)))); // 117115 mov al, [bx+1A0Fh] ;~ 274D:009E
cs=0x274d;eip=0x0000a2; T(CBW); // 117116 cbw ;~ 274D:00A2
cs=0x274d;eip=0x0000a3; T(XCHG(ax, dx)); // 117117 xchg ax, dx ;~ 274D:00A3
cs=0x274d;eip=0x0000a4; R(IDIV2(cx)); // 117118 idiv cx ;~ 274D:00A4
cs=0x274d;eip=0x0000a4; T(IDIV2(cx)); // 117118 idiv cx ;~ 274D:00A4
cs=0x274d;eip=0x0000a6; T(CWD); // 117119 cwd ;~ 274D:00A6
cs=0x274d;eip=0x0000a7; T(OR(ax, ax)); // 117120 or ax, ax ;~ 274D:00A7
cs=0x274d;eip=0x0000a9; T(ADD(ax, word_48bbc)); // 117121 add ax, word_48BBC ;~ 274D:00A9
Expand Down
Loading

0 comments on commit 5182036

Please sign in to comment.