Skip to content

Commit 46a8f75

Browse files
author
Pascal Beyer
committed
Fix new scope system for SEH filters. Check in test.
1 parent d265940 commit 46a8f75

File tree

3 files changed

+144
-3
lines changed

3 files changed

+144
-3
lines changed

src/obj_writer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ void codeview_emit_debug_information_for_function__recursive(struct ast_function
727727
block->kind = /*S_BLOCK32*/0x1103;
728728

729729
block->scope_size = scope->end_offset - scope->start_offset;
730-
block->offset_in_section = (u32)(scope->start_offset + function->size_of_prolog); // relocated by relocation.
730+
block->offset_in_section = (u32)(scope->start_offset + function->size_of_prologue); // relocated by relocation.
731731

732732
block->section = 0; // filled in by relocation.
733733

src/parse.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9701,7 +9701,10 @@ func void parse_statement(struct context *context){
97019701

97029702
{
97039703
context->in_exception_filter += 1;
9704-
struct ast_scope *filter_scope = parser_push_new_scope(context, get_current_token_for_error_report(context), SCOPE_FLAG_none);
9704+
struct ast_scope *filter_scope = push_struct(context->arena, struct ast_scope);
9705+
filter_scope->flags = SCOPE_FLAG_is_function_scope;
9706+
filter_scope->parent = context->current_scope;
9707+
filter_scope->token = get_current_token(context);
97059708

97069709
struct ast_function *filter_function = push_uninitialized_struct(context->arena, struct ast_function);
97079710
filter_function->kind = IR_function;
@@ -9724,6 +9727,7 @@ func void parse_statement(struct context *context){
97249727
push_ir(context, IR_return);
97259728

97269729
filter_function->end_in_ir_arena = arena_current(&context->ir_arena);
9730+
filter_scope->end_offset = (u32)(filter_function->end_in_ir_arena - filter_function->start_in_ir_arena);
97279731

97289732
{
97299733
//
@@ -9736,7 +9740,6 @@ func void parse_statement(struct context *context){
97369740
sll_push_back(context->current_function->referenced_declarations, new_reference);
97379741
}
97389742

9739-
parser_scope_pop(context, filter_scope);
97409743
ast_list_append(&context->local_functions, context->arena, &filter_function->kind);
97419744

97429745
seh_exception_handler->filter_function = filter_function;

tests/run/local_variable_test.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// run
2+
// check "Found variable 'three_deep', value: 0x3333333333333333"
3+
// check "Found variable 'local_scope', value: 0xdeadbeefdeadbabe"
4+
// check "Found variable 'root_scope', value: 0x1337133713371337"
5+
// check "Found variable 'three_deep_2', value: 0x333333333332222"
6+
// check "Found variable 'local_scope_2', value: 0xdeadbeefdead2222"
7+
// check "Found variable 'root_scope', value: 0x1337133713371337"
8+
// reject "Found variable 'should_not_see_this_variable'"
9+
// reject "Found variable 'should_not_see_this_variable_2'"
10+
11+
#include <windows.h>
12+
#include <dbghelp.h>
13+
#include <stdio.h>
14+
15+
#pragma comment(lib, "dbghelp.lib")
16+
17+
BOOL SymbolCheckProc(PSYMBOL_INFO symbol, ULONG size, PVOID UserContext) {
18+
19+
if((symbol->Flags & (SYMFLAG_LOCAL | SYMFLAG_REGREL)) != symbol->Flags) return TRUE;
20+
21+
CONTEXT *StackFrame = UserContext;
22+
23+
ULONG64 register_value = (symbol->Register == /*rbp*/334) ? StackFrame->Rbp : StackFrame->Rsp;
24+
25+
printf("Found variable '%s', value: 0x%llx\n", symbol->Name, *(ULONG64 *)(register_value + symbol->Address));
26+
27+
return TRUE; // Continue enumeration
28+
}
29+
30+
#ifndef __HLC__
31+
#pragma function(memset)
32+
void *memset(void *mem, int val, size_t amount){
33+
34+
if(!amount) return mem;
35+
__stosb(mem, (unsigned char)val, amount);
36+
return mem;
37+
}
38+
#endif
39+
40+
void ReadLocalVariable(void) {
41+
// Initialize DbgHelp
42+
SymInitialize(GetCurrentProcess(), NULL, TRUE);
43+
44+
// Capture the current context
45+
CONTEXT context;
46+
RtlCaptureContext(&context);
47+
48+
// Initialize the stack frame
49+
STACKFRAME64 stackFrame;
50+
memset(&stackFrame, 0, sizeof(STACKFRAME64));
51+
52+
// Set up the stack frame for x64 architecture
53+
stackFrame.AddrPC.Offset = context.Rip;
54+
stackFrame.AddrPC.Mode = AddrModeFlat;
55+
stackFrame.AddrFrame.Offset = context.Rsp;
56+
stackFrame.AddrFrame.Mode = AddrModeFlat;
57+
stackFrame.AddrStack.Offset = context.Rsp;
58+
stackFrame.AddrStack.Mode = AddrModeFlat;
59+
60+
HANDLE process = GetCurrentProcess();
61+
HANDLE thread = GetCurrentThread();
62+
63+
int first_time = 1;
64+
65+
// Unwind the stack
66+
while (StackWalk64(IMAGE_FILE_MACHINE_AMD64, process, thread, &stackFrame, &context, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL)) {
67+
if(first_time){
68+
first_time = 0;
69+
continue;
70+
}
71+
72+
// Set the context for the current stack frame
73+
IMAGEHLP_STACK_FRAME frame = {0};
74+
frame.InstructionOffset = stackFrame.AddrPC.Offset;
75+
frame.ReturnOffset = stackFrame.AddrReturn.Offset;
76+
frame.FrameOffset = stackFrame.AddrFrame.Offset;
77+
frame.StackOffset = stackFrame.AddrStack.Offset;
78+
frame.BackingStoreOffset = stackFrame.AddrBStore.Offset;
79+
frame.FuncTableEntry = (ULONG64)stackFrame.FuncTableEntry;
80+
frame.Params[0] = stackFrame.Params[0];
81+
frame.Params[1] = stackFrame.Params[1];
82+
frame.Params[2] = stackFrame.Params[2];
83+
frame.Params[3] = stackFrame.Params[3];
84+
frame.Virtual = stackFrame.Virtual;
85+
86+
SymSetContext(process, &frame, NULL); // There is something weird here about the the return value.
87+
88+
// Enumerate local symbols
89+
SymEnumSymbols(process, /*UseSymSetContext*/0, NULL, SymbolCheckProc, &context);
90+
}
91+
92+
// Cleanup
93+
SymCleanup(process);
94+
}
95+
96+
int main() {
97+
98+
__int64 root_scope = 0x1337133713371337;
99+
100+
{
101+
__int64 local_scope = 0xdeadbeefdeadbabe;
102+
103+
{
104+
__int64 three_deep = 0x3333333333333333;
105+
106+
ReadLocalVariable();
107+
108+
int k = 1;
109+
110+
}
111+
{
112+
__int64 should_not_see_this_variable = 0x12345678abcdef;
113+
}
114+
115+
}
116+
117+
118+
119+
{
120+
__int64 local_scope_2 = 0xdeadbeefdead2222;
121+
122+
{
123+
__int64 three_deep_2 = 0x333333333332222;
124+
125+
ReadLocalVariable();
126+
127+
int k = 1;
128+
129+
}
130+
{
131+
__int64 should_not_see_this_variable_2 = 0x12345678ab2222;
132+
}
133+
134+
}
135+
136+
return 0;
137+
}
138+

0 commit comments

Comments
 (0)