Skip to content

Commit

Permalink
Fix some errors (#776)
Browse files Browse the repository at this point in the history
* Fix some wrapping error

* Fix multithreading error
  • Loading branch information
wannacu authored Mar 8, 2023
1 parent a4752af commit 99059ff
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 177 deletions.
36 changes: 15 additions & 21 deletions src/tools/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,21 @@ uintptr_t AddBridge(bridge_t* bridge, wrapper_t w, void* fnc, int N, const char*
if(!bridge) return 0;
brick_t *b = NULL;
int sz = -1;
#ifdef DYNAREC
int prot = 0;
do {
#endif
mutex_lock(&my_context->mutex_bridge);
b = bridge->last;
if(b->sz == NBRICK) {
b->next = NewBrick(b->b);
b = b->next;
bridge->last = b;
}
sz = b->sz;
#ifdef DYNAREC
mutex_unlock(&my_context->mutex_bridge);
if(box86_dynarec) {
prot=(getProtection((uintptr_t)&b->b[sz])&(PROT_DYNAREC|PROT_DYNAREC_R))?1:0;
if(prot)
unprotectDB((uintptr_t)b->b, NBRICK*sizeof(onebridge_t), 0); // don't mark blocks, it's only new one
}
} while(sz!=b->sz); // this while loop if someone took the slot when the bridge mutex was unlocked doing memory protection managment
mutex_lock(&my_context->mutex_bridge);
b = bridge->last;
if(b->sz == NBRICK) {
b->next = NewBrick(b->b);
b = b->next;
bridge->last = b;
}
sz = b->sz;
#ifdef DYNAREC
if(box86_dynarec) {
int prot = 0;
prot=(getProtection((uintptr_t)&b->b[sz])&(PROT_DYNAREC|PROT_DYNAREC_R))?1:0;
if(prot)
unprotectDB((uintptr_t)b->b, NBRICK*sizeof(onebridge_t), 0); // don't mark blocks, it's only new one
}
#endif
b->sz++;
b->b[sz].CC = 0xCC;
Expand All @@ -127,11 +121,11 @@ uintptr_t AddBridge(bridge_t* bridge, wrapper_t w, void* fnc, int N, const char*
int ret;
khint_t k = kh_put(bridgemap, bridge->bridgemap, (uintptr_t)fnc, &ret);
kh_value(bridge->bridgemap, k) = (uintptr_t)&b->b[sz].CC;
mutex_unlock(&my_context->mutex_bridge);
#ifdef DYNAREC
if(box86_dynarec)
protectDB((uintptr_t)b->b, NBRICK*sizeof(onebridge_t));
#endif
mutex_unlock(&my_context->mutex_bridge);
#ifdef HAVE_TRACE
if(name)
addBridgeName(fnc, name);
Expand Down
49 changes: 32 additions & 17 deletions src/wrapped/wrappedlibjpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,22 @@ typedef struct jpeg_common_struct_s {
int global_state;
} jpeg_common_struct_t;

static struct __jmp_buf_tag jmpbuf;
static int is_jmpbuf;
typedef struct jmpbuf_helper {
struct __jmp_buf_tag jmpbuf;
void* client_data;
}jmpbuf_helper;

#define RunFunctionWithEmu_helper(...) \
jmpbuf_helper* helper = (jmpbuf_helper*)((jpeg_common_struct_t*)cinfo)->client_data; \
((jpeg_common_struct_t*)cinfo)->client_data = helper->client_data; \
uint32_t ret = RunFunctionWithEmu(__VA_ARGS__); \
((jpeg_common_struct_t*)cinfo)->client_data = helper;

#define RunFunction_helper(...) \
jmpbuf_helper* helper = ((jpeg_common_struct_t*)cinfo)->client_data; \
((jpeg_common_struct_t*)cinfo)->client_data = helper->client_data; \
uint32_t ret = RunFunction(__VA_ARGS__); \
((jpeg_common_struct_t*)cinfo)->client_data = helper;

static jpeg_error_mgr_t native_err_mgr;

Expand Down Expand Up @@ -115,19 +129,18 @@ GO(1) \
GO(2) \
GO(3)

static x86emu_t* my_jpegcb_emu = NULL;\
// error_exit
#define GO(A) \
static uintptr_t my_error_exit_fct_##A = 0; \
static void my_error_exit_##A(jpeg_common_struct_t* cinfo) \
{ \
uintptr_t oldip = my_jpegcb_emu->ip.dword[0]; \
uintptr_t oldip = thread_get_emu()->ip.dword[0]; \
wrapErrorMgr(my_bridge, cinfo->err); \
RunFunctionWithEmu(my_jpegcb_emu, 1, my_error_exit_fct_##A, 1, cinfo); \
if(oldip==my_jpegcb_emu->ip.dword[0]) \
RunFunctionWithEmu_helper(thread_get_emu(), 1, my_error_exit_fct_##A, 1, cinfo); \
if(oldip==thread_get_emu()->ip.dword[0]) \
unwrapErrorMgr(my_bridge, cinfo->err); \
else \
if(is_jmpbuf) longjmp(&jmpbuf, 1); \
longjmp(cinfo->client_data, 1); \
}
SUPER()
#undef GO
Expand Down Expand Up @@ -158,7 +171,7 @@ static void* is_error_exitFct(void* fct)
static uintptr_t my_emit_message_fct_##A = 0; \
static void my_emit_message_##A(void* cinfo, int msg_level) \
{ \
RunFunctionWithEmu(my_jpegcb_emu, 1, my_emit_message_fct_##A, 2, cinfo, msg_level);\
RunFunctionWithEmu_helper(thread_get_emu(), 1, my_emit_message_fct_##A, 2, cinfo, msg_level);\
}
SUPER()
#undef GO
Expand Down Expand Up @@ -189,7 +202,7 @@ static void* is_emit_messageFct(void* fct)
static uintptr_t my_output_message_fct_##A = 0; \
static void my_output_message_##A(void* cinfo) \
{ \
RunFunctionWithEmu(my_jpegcb_emu, 1, my_output_message_fct_##A, 1, cinfo);\
RunFunctionWithEmu_helper(thread_get_emu(), 1, my_output_message_fct_##A, 1, cinfo);\
}
SUPER()
#undef GO
Expand Down Expand Up @@ -220,7 +233,7 @@ static void* is_output_messageFct(void* fct)
static uintptr_t my_format_message_fct_##A = 0; \
static void my_format_message_##A(void* cinfo, char* buffer) \
{ \
RunFunctionWithEmu(my_jpegcb_emu, 1, my_format_message_fct_##A, 2, cinfo, buffer);\
RunFunctionWithEmu_helper(thread_get_emu(), 1, my_format_message_fct_##A, 2, cinfo, buffer);\
}
SUPER()
#undef GO
Expand Down Expand Up @@ -251,7 +264,7 @@ static void* is_format_messageFct(void* fct)
static uintptr_t my_reset_error_mgr_fct_##A = 0; \
static void my_reset_error_mgr_##A(void* cinfo) \
{ \
RunFunctionWithEmu(my_jpegcb_emu, 1, my_reset_error_mgr_fct_##A, 1, cinfo);\
RunFunctionWithEmu_helper(thread_get_emu(), 1, my_reset_error_mgr_fct_##A, 1, cinfo);\
}
SUPER()
#undef GO
Expand Down Expand Up @@ -282,7 +295,8 @@ static void* is_reset_error_mgrFct(void* fct)
static uintptr_t my_jpeg_marker_parser_method_fct_##A = 0; \
static int my_jpeg_marker_parser_method_##A(void* cinfo) \
{ \
return (int)RunFunction(my_context, my_jpeg_marker_parser_method_fct_##A, 1, cinfo);\
RunFunction_helper(my_context, my_jpeg_marker_parser_method_fct_##A, 1, cinfo);\
return (int)ret; \
}
SUPER()
#undef GO
Expand Down Expand Up @@ -363,16 +377,17 @@ EXPORT void* my_jpeg_std_error(x86emu_t* emu, void* errmgr)
}

#define WRAP(T, A) \
is_jmpbuf = 1; \
my_jpegcb_emu = emu; \
jmpbuf_helper helper; \
unwrapErrorMgr(my_lib->w.bridge, cinfo->err); \
if(setjmp(&jmpbuf)) { \
if(setjmp(&helper.jmpbuf)) { \
cinfo->client_data = helper.client_data; \
wrapErrorMgr(my_lib->w.bridge, cinfo->err);\
is_jmpbuf = 0; \
return (T)R_EAX; \
} \
helper.client_data = cinfo->client_data; \
cinfo->client_data = &helper; \
A; \
is_jmpbuf = 0; \
cinfo->client_data = helper.client_data; \
wrapErrorMgr(my_lib->w.bridge, cinfo->err)


Expand Down
Loading

0 comments on commit 99059ff

Please sign in to comment.