Skip to content

Commit

Permalink
Little touches to windows platform code (#331)
Browse files Browse the repository at this point in the history
* windows: print strerror message on failure to open file

* Remove leading _ from _program_name

* A few windows functions can be static

* ci: Always upload artifact

* linux+macos: Print message on stderr when fopen fails

* Remove merge artifacts

* Remove stray '#include <stdio.h>'
  • Loading branch information
madebr authored May 25, 2024
1 parent 7e98d3b commit 9054ba9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
cd build
ctest --verbose
- name: Upload Artifact
if: startsWith(github.ref, 'refs/tags/') || github.ref_name == 'main'
# if: startsWith(github.ref, 'refs/tags/') || github.ref_name == 'main'
uses: actions/upload-artifact@v3
with:
name: ${{ steps.build.outputs.filename }}
Expand Down
2 changes: 1 addition & 1 deletion src/BRSRC13/CORE/FW/datafile.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ br_uint_32 DfStructReadBinary(br_datafile* df, br_file_struct* str, void* base)

return 1;
}
#include <stdio.h>

// IDA: int __usercall DfStructSizeBinary@<EAX>(br_datafile *df@<EAX>, br_file_struct *str@<EDX>, void *base@<EBX>)
int DfStructSizeBinary(br_datafile* df, br_file_struct* str, void* base) {
unsigned char* mp;
Expand Down
3 changes: 3 additions & 0 deletions src/harness/os/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ FILE* OS_fopen(const char* pathname, const char* mode) {
}
}
closedir(pDir);
if (f == NULL) {
fprintf(stderr, "Failed to open \"%s\" (%s)\n", pathname, strerror(errno));
}
return f;
}

Expand Down
9 changes: 8 additions & 1 deletion src/harness/os/macos.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,14 @@ void OS_InstallSignalHandler(char* program_name) {
}

FILE* OS_fopen(const char* pathname, const char* mode) {
return fopen(pathname, mode);
FILE* f;

f = fopen(pathname, mode);
if (f == NULL) {
fprintf(stderr, "Failed to open \"%s\" (%s)\n", pathname, strerror(errno));
}

return f;
}

size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen) {
Expand Down
16 changes: 8 additions & 8 deletions src/harness/os/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
void dr_dprintf(char* fmt_string, ...);

static int stack_nbr = 0;
static char _program_name[1024];
static char windows_program_name[1024];

static char dirname_buf[_MAX_DIR];
static char fname_buf[_MAX_FNAME];

int addr2line(char const* const program_name, void const* const addr) {
static int addr2line(char const* const program_name, void const* const addr) {
char addr2line_cmd[512] = { 0 };

sprintf(addr2line_cmd, "addr2line -f -p -e %.256s %p", program_name, addr);
Expand All @@ -41,7 +41,7 @@ int addr2line(char const* const program_name, void const* const addr) {
return system(addr2line_cmd);
}

void print_stacktrace(CONTEXT* context) {
static void print_stacktrace(CONTEXT* context) {

SymInitialize(GetCurrentProcess(), 0, true);

Expand All @@ -64,13 +64,13 @@ void print_stacktrace(CONTEXT* context) {
SymFunctionTableAccess,
SymGetModuleBase,
0)) {
addr2line(_program_name, (void*)frame.AddrPC.Offset);
addr2line(windows_program_name, (void*)frame.AddrPC.Offset);
}

SymCleanup(GetCurrentProcess());
}

LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS* ExceptionInfo) {
static LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS* ExceptionInfo) {
switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
case EXCEPTION_ACCESS_VIOLATION:
fputs("Error: EXCEPTION_ACCESS_VIOLATION\n", stderr);
Expand Down Expand Up @@ -142,14 +142,14 @@ LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS* ExceptionInfo) {
if (EXCEPTION_STACK_OVERFLOW != ExceptionInfo->ExceptionRecord->ExceptionCode) {
print_stacktrace(ExceptionInfo->ContextRecord);
} else {
addr2line(_program_name, (void*)ExceptionInfo->ContextRecord->Eip);
addr2line(windows_program_name, (void*)ExceptionInfo->ContextRecord->Eip);
}

return EXCEPTION_EXECUTE_HANDLER;
}

void OS_InstallSignalHandler(char* program_name) {
strcpy(_program_name, program_name);
strcpy(windows_program_name, program_name);
SetUnhandledExceptionFilter(windows_exception_handler);
}

Expand All @@ -160,7 +160,7 @@ FILE* OS_fopen(const char* pathname, const char* mode) {
f = NULL;
err = fopen_s(&f, pathname, mode);
if (err != 0) {
fprintf(stderr, "Failed to open \"%s\"", pathname);
fprintf(stderr, "Failed to open \"%s\" (%s)\n", pathname, strerror(err));
}

return f;
Expand Down

0 comments on commit 9054ba9

Please sign in to comment.