Skip to content

Commit

Permalink
Fix bug where analysing subexpr relied on them not being analysed. Fi…
Browse files Browse the repository at this point in the history
…x issue where converting a const initializer bool to integer failed. Fix of issue where the case check assumed other cases were const values. Remove PTHREAD for windows.
  • Loading branch information
lerno committed Jul 16, 2023
1 parent 0469012 commit 5e9a950
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 40 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ jobs:
install: git binutils mingw-w64-x86_64-ninja mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain mingw-w64-x86_64-python
- shell: msys2 {0}
run: |
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-llvm-16.0.5-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lld-16.0.5-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-llvm-15.0.3-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lld-15.0.3-1-any.pkg.tar.zst
- name: CMake
run: |
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
Expand All @@ -108,8 +108,8 @@ jobs:
../build/c3c compile-run examples/fannkuch-redux.c3
../build/c3c compile-run examples/contextfree/boolerr.c3
../build/c3c compile-run examples/load_world.c3
../build/c3c compile-run examples/load_world.c3
../build/c3c compile --test -g -O0 --threads 1 --target macos-x64 examples/constants.c3
- name: Build testproject
run: |
cd resources/testproject
Expand Down Expand Up @@ -164,7 +164,7 @@ jobs:
../build/c3c compile-run examples/fannkuch-redux.c3
../build/c3c compile-run examples/contextfree/boolerr.c3
../build/c3c compile-run examples/load_world.c3
../build/c3c compile --test -g -O0 --threads 1 --target macos-x64 examples/constants.c3
- name: Build testproject
run: |
cd resources/testproject
Expand Down
9 changes: 3 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ else()
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-3 -O3 -fno-exceptions")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-3 -fno-exceptions")
else()
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O1 -fsanitize=undefined -fno-exceptions")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O1 -fsanitize=undefined -fno-exceptions")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf-3 -O3 -fsanitize=undefined -fno-exceptions")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -gdwarf-3 -O1 -fsanitize=undefined -fno-exceptions")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-3 -O3 -fsanitize=undefined -fno-exceptions")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-3 -fsanitize=undefined -fno-exceptions")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-3 -O1 -fsanitize=undefined -fno-exceptions")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined -fno-exceptions")
endif()
endif()
Expand Down Expand Up @@ -386,9 +386,6 @@ else()
target_link_options(c3c PRIVATE -pthread)
target_compile_options(c3c PRIVATE -pthread -Wall -Werror -Wno-unknown-pragmas -Wno-unused-result
-Wno-unused-function -Wno-unused-variable -Wno-unused-parameter)
if (WIN32)
target_compile_definitions(c3c PRIVATE USE_PTHREAD=1)
endif()
endif()


Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static void free_arenas(void)
if (debug_stats) print_arena_status();
}

void compiler_print_bench(void)
static void compiler_print_bench(void)
{
if (debug_stats)
{
Expand Down
164 changes: 136 additions & 28 deletions src/utils/stacktrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifdef _WIN32
#include <windows.h>
#include <imagehlp.h>
#include <dbghelp.h>
#else
#include <err.h>
#include <execinfo.h>
Expand All @@ -37,39 +38,146 @@ int addr2line(char const * const program_name, void const * const addr)
return system(addr2line_cmd);
}
#ifdef _WIN32
void windows_print_stacktrace(CONTEXT* context)
#include <libloaderapi.h>

bool windows_print_stacktrace(CONTEXT* context)
{
SymInitialize(GetCurrentProcess(), 0, true);

STACKFRAME frame = { 0 };

/* setup initial stack frame */
frame.AddrPC.Offset = context->Rip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Rsp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Rbp;
frame.AddrFrame.Mode = AddrModeFlat;

while (StackWalk(
IMAGE_FILE_MACHINE_I386,
GetCurrentProcess(),
GetCurrentThread(),
&frame,
context,
0,
SymFunctionTableAccess,
SymGetModuleBase,
0))
bool result = false;

// Could use SymSetOptions here to add the SYMOPT_DEFERRED_LOADS flag

STACKFRAME64 sf;
bool first = true;
char buf[300];
size_t buflen = sizeof(buf);
// Indexes into MSJExceptionHandler data when m_Levels > 0
int i_line = 0; // Into char * m_Lines[]
int i_buf = 0; // into char m_Buffer[]
HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();
memset( &sf, 0, sizeof(sf) );

// Initialize the STACKFRAME structure for the first call. This is only
// necessary for Intel CPUs, and isn't mentioned in the documentation.

DWORD64 pFrame = 0;
DWORD64 pPrevFrame = 0;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrStack.Mode = AddrModeFlat;
sf.AddrFrame.Mode = AddrModeFlat;
sf.AddrPC.Offset = context->Rip;
sf.AddrStack.Offset = context->Rsp;
sf.AddrFrame.Offset = context->Rbp;

DWORD MachineType = IMAGE_FILE_MACHINE_AMD64;

while ( 1 )
{
addr2line(icky_global_program_name, (void *)frame.AddrPC.Offset);
}
SymCleanup( GetCurrentProcess() );
}
SetLastError( 0 );
if ( ! StackWalk64( MachineType,
process,
thread,
&sf,
context,
0,
SymFunctionTableAccess64,
SymGetModuleBase64,
0 ) )
break;

pFrame = sf.AddrFrame.Offset;
if ( 0 == pFrame ) // Basic sanity check to make sure
break; // the frame is OK. Bail if not.

// dbbTrace::OutputWithoutTimestamp( "%08X %08X ", sf.AddrPC.Offset, pFrame );

if( ! first ) {
if( pFrame <= pPrevFrame ) {
// Sanity check
break;
}
if( (pFrame - pPrevFrame) > 10000000 ) {
// Sanity check
break;
}
}
if( (pFrame % sizeof(void *)) != 0 ) {
// Sanity check
break;
}

pPrevFrame = pFrame;
first = false;

// IMAGEHLP is wacky, and requires you to pass in a pointer to an
// IMAGEHLP_SYMBOL structure. The problem is that this structure is
// variable length. That is, you determine how big the structure is
// at runtime. This means that you can't use sizeof(struct).
// So...make a buffer that's big enough, and make a pointer
// to the buffer. We also need to initialize not one, but TWO
// members of the structure before it can be used.

enum { emMaxNameLength = 512 };
// Use union to ensure proper alignment
union {
SYMBOL_INFO symb;
BYTE symbolBuffer[ sizeof(SYMBOL_INFO) + emMaxNameLength ];
} u;
PSYMBOL_INFO pSymbol = & u.symb;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = emMaxNameLength;

PDWORD64 symDisplacement = 0; // Displacement of the input address,
// relative to the start of the symbol

DWORD lineDisplacement = 0;
IMAGEHLP_LINE64 line;
line.SizeOfStruct = sizeof(line);
line.LineNumber = 0;
BOOL bLine = FALSE;

bLine = SymGetLineFromAddr64( process, sf.AddrPC.Offset,
& lineDisplacement, & line );
if ( SymFromAddr(process, sf.AddrPC.Offset,
symDisplacement, pSymbol) )
{
if( bLine ) {
printf(
"%ull %ull %s() line %d\n",
sf.AddrPC.Offset, pFrame,
pSymbol->Name, (int)line.LineNumber );
} else {
printf(
"%ull %ull %s() + %p\n",
sf.AddrPC.Offset, pFrame,
pSymbol->Name, symDisplacement );
}

}
else // No symbol found. Print out the logical address instead.
{
DWORD err = GetLastError();
TCHAR szModule[MAX_PATH];
szModule[0] = '\0';
DWORD section = 0, offset = 0;
printf("--- symbol not found \n");
/*
GetLogicalAddress( (PVOID)sf.AddrPC.Offset,
szModule, sizeof(szModule), section, offset );
printf(
"%ull %ull %04X:%08X %s (err = %d)\n",
sf.AddrPC.Offset, pFrame,
(unsigned)section, (unsigned)offset, szModule, (int)err );*/
result = true;
}

} // while

return result;
}
LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS *ExceptionInfo)
{
puts("fjefijejfe");
switch(ExceptionInfo->ExceptionRecord->ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
Expand Down
2 changes: 1 addition & 1 deletion src/utils/taskqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef struct TaskQueue_
Task **queue;
} TaskQueue;

static DWORD WINAPI taskqueue_thread(LPVOID lpParam)
static unsigned WINAPI taskqueue_thread(LPVOID lpParam)
{
TaskQueue *task_queue = (TaskQueue *)lpParam;
bool is_active = false;
Expand Down

0 comments on commit 5e9a950

Please sign in to comment.