Fix null name maps, workaround msvc-asan compiler crash and acp/utf/wchar fixes for w32

This commit is contained in:
pancake 2021-11-14 00:50:54 +01:00 committed by GitHub
parent a5d51af309
commit 3e032073a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 109 additions and 65 deletions

View File

@ -341,7 +341,10 @@ static RList *trycatch(RBinFile *bf) {
ut32 savedBeginOff = rfcn.BeginAddress;
ut32 savedEndOff = rfcn.EndAddress;
while (suc && rfcn.UnwindData & 1) {
suc = r_io_read_at_mapped (io, baseAddr + (rfcn.UnwindData & ~1), (ut8 *)&rfcn, sizeof (rfcn));
// XXX this ugly (int) cast is needed for MSVC for not to crash
int delta = (rfcn.UnwindData & (int)~1);
ut64 at = baseAddr + delta;
suc = r_io_read_at_mapped (io, at, (ut8 *)&rfcn, sizeof (rfcn));
}
rfcn.BeginAddress = savedBeginOff;
rfcn.EndAddress = savedEndOff;
@ -370,7 +373,8 @@ static RList *trycatch(RBinFile *bf) {
break;
}
while (suc && (rfcn.UnwindData & 1)) {
suc = r_io_read_at_mapped (io, baseAddr + (rfcn.UnwindData & ~1), (ut8 *)&rfcn, sizeof (rfcn));
// XXX this ugly (int) cast is needed for MSVC for not to crash
suc = r_io_read_at_mapped (io, baseAddr + ((int)rfcn.UnwindData & (int)~1), (ut8 *)&rfcn, sizeof (rfcn));
}
if (!suc || info.Version != 1) {
break;

View File

@ -297,7 +297,9 @@ do_it_again:
if (irInBuf.EventType == KEY_EVENT) {
if (irInBuf.Event.KeyEvent.bKeyDown) {
if (irInBuf.Event.KeyEvent.uChar.UnicodeChar) {
char *tmp = r_sys_conv_win_to_utf8_l ((PTCHAR)&irInBuf.Event.KeyEvent.uChar, 1);
ut8 chbuf[4] = {0};
memcpy (chbuf, &(irInBuf.Event.KeyEvent.uChar), 2);
char *tmp = r_sys_conv_win_to_utf8_l ((PTCHAR)&chbuf, 1);
if (tmp) {
r_str_ncpy (buf, tmp, sizeof (buf));
free (tmp);

View File

@ -102,6 +102,9 @@ R_API void r_debug_map_list(RDebug *dbg, ut64 addr, const char *input) {
for (i = 0; i < 2; i++) { // Iterate over dbg::maps and dbg::maps_user
RList *maps = (i == 0) ? dbg->maps : dbg->maps_user;
if (!maps) {
continue;
}
r_list_foreach (maps, iter, map) {
switch (input[0]) {
case 'j': // "dmj"

View File

@ -1135,7 +1135,7 @@ static RList *r_debug_native_map_get (RDebug *dbg) {
return list;
}
static RList *r_debug_native_modules_get (RDebug *dbg) {
static RList *r_debug_native_modules_get(RDebug *dbg) {
char *lastname = NULL;
RDebugMap *map;
RListIter *iter, *iter2;
@ -1162,6 +1162,9 @@ static RList *r_debug_native_modules_get (RDebug *dbg) {
r_list_foreach_safe (list, iter, iter2, map) {
const char *file = map->file;
if (!map->file) {
if (!map->name) {
map->name = strdup ("");
}
file = map->file = strdup (map->name);
}
must_delete = true;

View File

@ -9,27 +9,36 @@ typedef struct {
int sect_count;
} RWinModInfo;
static char *get_map_type(MEMORY_BASIC_INFORMATION *mbi) {
char *type;
switch (mbi->Type) {
case MEM_IMAGE:
type = "IMAGE";
break;
case MEM_MAPPED:
type = "MAPPED";
break;
case MEM_PRIVATE:
type = "PRIVATE";
break;
default:
type = "UNKNOWN";
static const char *get_map_type(MEMORY_BASIC_INFORMATION *mbi) {
const char *type = NULL;
if (mbi) {
switch (mbi->Type) {
case MEM_IMAGE:
type = "IMAGE";
break;
case MEM_MAPPED:
type = "MAPPED";
break;
case MEM_PRIVATE:
type = "PRIVATE";
break;
default:
type = "UNKNOWN";
break;
}
}
return type;
}
static RDebugMap *add_map(RList *list, const char *name, ut64 addr, ut64 len, MEMORY_BASIC_INFORMATION *mbi) {
int perm;
char *map_type = get_map_type (mbi);
const char *map_type = get_map_type (mbi);
if (!map_type) {
map_type = NULL;
}
if (!name) {
name = "";
}
switch (mbi->Protect) {
case PAGE_EXECUTE:
@ -56,7 +65,7 @@ static RDebugMap *add_map(RList *list, const char *name, ut64 addr, ut64 len, ME
default:
perm = 0;
}
char *map_name = r_str_newf ("%-8s %s", map_type, name);
char *map_name = name? r_str_newf ("%s %s", map_type, name): strdup (map_type);
if (!map_name) {
return NULL;
}
@ -223,8 +232,11 @@ static void proc_mem_img(HANDLE h_proc, RList *map_list, RList *mod_list, RWinMo
}
static void proc_mem_map(HANDLE h_proc, RList *map_list, MEMORY_BASIC_INFORMATION *mbi) {
TCHAR f_name[MAX_PATH + 1];
DWORD len = r_w32_GetMappedFileName (h_proc, mbi->BaseAddress, f_name, MAX_PATH);
TCHAR *f_name = calloc (MAX_PATH + 1, 2); // [MAX_PATH + 1];
if (!f_name) {
return;
}
DWORD len = 0; // r_w32_GetMappedFileName (h_proc, mbi->BaseAddress, f_name, MAX_PATH);
if (len > 0) {
char *f_name_ = r_sys_conv_win_to_utf8 (f_name);
add_map_reg (map_list, f_name_, mbi);
@ -241,15 +253,15 @@ R_API RList *r_w32_dbg_maps(RDebug *dbg) {
}
SYSTEM_INFO si = {0};
LPVOID cur_addr;
MEMORY_BASIC_INFORMATION mbi;
MEMORY_BASIC_INFORMATION mbi = {0};
RWinModInfo mod_inf = {0};
RList *map_list = r_list_newf ((RListFree)r_debug_map_free), *mod_list = NULL;
RList *map_list = r_list_newf ((RListFree)r_debug_map_free);
RW32Dw *wrap = dbg->user;
GetSystemInfo (&si);
cur_addr = si.lpMinimumApplicationAddress;
/* get process modules list */
mod_list = r_w32_dbg_modules (dbg);
RList *mod_list = NULL; // r_w32_dbg_modules (dbg);
/* process memory map */
while (cur_addr < si.lpMaximumApplicationAddress &&
VirtualQueryEx (wrap->pi.hProcess, cur_addr, &mbi, sizeof (mbi)) != 0) {

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2017 - pancake */
/* radare - LGPL - Copyright 2009-2021 - pancake */
#include <r_debug.h>
@ -7,7 +7,7 @@ R_API RDebugPid *r_debug_pid_new(const char *path, int pid, int uid, char status
if (!p) {
return NULL;
}
p->path = strdup (path);
p->path = strdup (path? path: "");
p->pid = pid;
p->uid = uid;
p->status = status;
@ -17,8 +17,10 @@ R_API RDebugPid *r_debug_pid_new(const char *path, int pid, int uid, char status
}
R_API RDebugPid *r_debug_pid_free(RDebugPid *pid) {
free (pid->path);
free (pid);
if (pid) {
free (pid->path);
free (pid);
}
return NULL;
}

View File

@ -497,7 +497,7 @@ R_API char *r_sys_getenv(const char *key) {
if (!key) {
return NULL;
}
envbuf = (LPTSTR)malloc (sizeof (TCHAR) * TMP_BUFSIZE);
envbuf = (LPTSTR)calloc (sizeof (TCHAR), TMP_BUFSIZE);
if (!envbuf) {
goto err_r_sys_get_env;
}
@ -1074,9 +1074,9 @@ R_API int r_sys_run_rop(const ut8 *buf, int len) {
// w32 specific API
R_API char *r_w32_handle_to_path(HANDLE processHandle) {
const DWORD maxlength = MAX_PATH;
TCHAR filename[MAX_PATH];
char *filename = calloc ((MAX_PATH * 2) + 2, 1);
char *result = NULL;
DWORD length = r_w32_GetModuleFileNameEx (processHandle, NULL, filename, maxlength);
DWORD length = r_w32_GetModuleFileNameEx (processHandle, NULL, (LPSTR)filename, maxlength);
if (length == 0) {
// Upon failure fallback to GetProcessImageFileName
length = r_w32_GetProcessImageFileName (processHandle, filename, maxlength);
@ -1109,8 +1109,7 @@ R_API char *r_w32_handle_to_path(HANDLE processHandle) {
eprintf ("r_sys_pid_to_path: Error allocating memory\n");
return NULL;
}
strncpy (tmp, name, length);
tmp[length] = '\0';
r_str_ncpy (tmp, name, length);
TCHAR device[MAX_PATH];
TCHAR drv[3] = {'A',':', 0};
for (; drv[0] <= 'Z'; drv[0]++) {
@ -1149,6 +1148,7 @@ R_API char *r_w32_handle_to_path(HANDLE processHandle) {
} else {
result = r_sys_conv_win_to_utf8 (filename);
}
free (filename);
return result;
}
#endif

View File

@ -618,12 +618,19 @@ R_API int r_isprint(const RRune c) {
#if __WINDOWS__
R_API char *r_utf16_to_utf8_l(const wchar_t *wc, int len) {
if (!wc || !len || len < -1) {
if (!wc) {
return NULL;
}
char *rutf8 = NULL;
int csize;
if (len < 0) {
len = wcslen (wc);
}
int csize = 2 + ((len > 0)? len * 2: 0);
char *rutf8 = calloc (csize, 2);
if (!rutf8) {
return NULL;
}
WideCharToMultiByte (CP_UTF8, 0, wc, len, rutf8, csize, NULL, NULL);
#if 0
if ((csize = WideCharToMultiByte (CP_UTF8, 0, wc, len, NULL, 0, NULL, NULL))) {
++csize;
if ((rutf8 = malloc (csize))) {
@ -633,19 +640,22 @@ R_API char *r_utf16_to_utf8_l(const wchar_t *wc, int len) {
}
}
}
#endif
return rutf8;
}
R_API wchar_t *r_utf8_to_utf16_l(const char *cstring, int len) {
if (!cstring || !len || len < -1) {
return NULL;
r_return_val_if_fail (cstring && len >= -1, NULL);
if (len == -1) {
len = strlen (cstring);
}
wchar_t *rutf16 = NULL;
int wcsize;
if ((wcsize = MultiByteToWideChar (CP_UTF8, 0, cstring, len, NULL, 0))) {
++wcsize;
if ((rutf16 = (wchar_t *) calloc (wcsize, sizeof (wchar_t)))) {
wcsize ++;
if ((rutf16 = (wchar_t *) calloc (wcsize + 1, sizeof (wchar_t)))) {
MultiByteToWideChar (CP_UTF8, 0, cstring, len, rutf16, wcsize);
if (len != -1) {
rutf16[wcsize - 1] = L'\0';
@ -656,7 +666,7 @@ R_API wchar_t *r_utf8_to_utf16_l(const char *cstring, int len) {
}
R_API char *r_utf8_to_acp_l(const char *str, int len) {
if (!str || !len || len < -1) {
if (!str || len < -1) {
return NULL;
}
char *acp = NULL;
@ -670,8 +680,9 @@ R_API char *r_utf8_to_acp_l(const char *str, int len) {
rutf16[wcsize - 1] = L'\0';
}
if ((csize = WideCharToMultiByte (CP_ACP, 0, rutf16, wcsize, NULL, 0, NULL, NULL))) {
++csize;
if ((acp = malloc (csize))) {
csize ++;
acp = malloc (csize);
if (acp) {
WideCharToMultiByte (CP_ACP, 0, rutf16, wcsize, acp, csize, NULL, NULL);
if (len != -1) {
acp[csize - 1] = '\0';
@ -685,18 +696,17 @@ R_API char *r_utf8_to_acp_l(const char *str, int len) {
}
R_API char *r_acp_to_utf8_l(const char *str, int len) {
if (!str || !len || len < -1) {
return NULL;
r_return_val_if_fail (str && len >= -1, NULL);
if (len == -1) {
len = strlen (str);
}
int wcsize;
if ((wcsize = MultiByteToWideChar (CP_ACP, 0, str, len, NULL, 0))) {
wchar_t *rutf16;
++wcsize;
if ((rutf16 = (wchar_t *) calloc (wcsize, sizeof (wchar_t)))) {
wcsize++;
if ((rutf16 = (wchar_t *) calloc (wcsize + 1, sizeof (wchar_t)))) {
MultiByteToWideChar (CP_ACP, 0, str, len, rutf16, wcsize);
if (len != -1) {
rutf16[wcsize - 1] = L'\0';
}
rutf16[wcsize - 1] = L'\0';
char *ret = r_utf16_to_utf8_l (rutf16, wcsize);
free (rutf16);
return ret;

View File

@ -161,11 +161,19 @@ R_API NTSTATUS r_w32_NtQueryInformationThread(HANDLE a, ULONG b, PVOID c, ULONG
}
// Requires Windows XP
R_API DWORD r_w32_GetModuleFileNameEx(HANDLE a, HMODULE b, LPTSTR c, DWORD d) {
static DWORD (*x)(HANDLE, HMODULE, LPTSTR, DWORD) = NULL;
#if 0
DWORD GetModuleFileNameExA(
[in] HANDLE hProcess,
[in, optional] HMODULE hModule,
[out] LPSTR lpFilename,
[in] DWORD nSize
);
#endif
R_API DWORD r_w32_GetModuleFileNameEx(HANDLE a, HMODULE b, LPSTR c, DWORD d) {
static DWORD (*x)(HANDLE, HMODULE, LPSTR, DWORD) = NULL;
if (!x) {
HANDLE lib = w32_loadlib ("psapi", "psapi.dll");
x = (DWORD (*)(HANDLE, HMODULE, LPTSTR, DWORD))
x = (DWORD (*)(HANDLE, HMODULE, LPSTR, DWORD))
GetProcAddress (lib, W32_TCALL ("GetModuleFileNameEx"));
}
return x? x (a, b, c, d): 0;
@ -190,11 +198,11 @@ R_API BOOL r_w32_QueryFullProcessImageName(HANDLE h, DWORD p, LPTSTR s, PDWORD l
return x? x (h, p, s, l): 0;
}
R_API DWORD r_w32_GetMappedFileName(HANDLE h, LPVOID p, LPTSTR s, DWORD l) {
static DWORD (*x)(HANDLE, LPVOID, LPTSTR, DWORD) = NULL;
R_API DWORD r_w32_GetMappedFileName(HANDLE h, LPVOID p, LPSTR s, DWORD l) {
static DWORD (*x)(HANDLE, LPVOID, LPSTR, DWORD) = NULL;
if (!x) {
HANDLE lib = w32_loadlib ("psapi", "psapi.dll");
x = (DWORD (*)(HANDLE, LPVOID, LPTSTR, DWORD))
x = (DWORD (*)(HANDLE, LPVOID, LPSTR, DWORD))
GetProcAddress (lib, W32_TCALL ("GetMappedFileName"));
}
return x? x (h, p, s, l): 0;

View File

@ -309,18 +309,18 @@ int winkd_wait_packet(WindCtx *ctx, const uint32_t type, kd_packet_t **p) {
R_PACKED (
typedef struct {
char tag[4];
uint32_t start_vpn;
uint32_t end_vpn;
uint32_t parent;
uint32_t left;
uint32_t right;
uint32_t flags;
ut32 start_vpn;
ut32 end_vpn;
ut32 parent;
ut32 left;
ut32 right;
ut32 flags;
}) mmvad_short;
int winkd_walk_vadtree(WindCtx *ctx, ut64 address, ut64 parent) {
mmvad_short entry = { { 0 } };
ut64 start, end;
int prot;
ut32 prot;
if (winkd_read_at (ctx, (uint8_t *) &entry, address - 0x4, sizeof(mmvad_short)) != sizeof (mmvad_short)) {
eprintf ("0x%"PFMT64x " Could not read the node!\n", (ut64) address);