Skip to content

Commit

Permalink
Fix incorrect parameter offset calculation on Windows beyond argument 5
Browse files Browse the repository at this point in the history
Fixes #17.
  • Loading branch information
sgraham committed May 3, 2024
1 parent d86212b commit a6d256f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/codegen.in.c
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,11 @@ static void assign_lvar_offsets(Obj* prog) {

var->offset = top;
// outaf("int stack %s at stack 0x%x\n", var->name, var->offset);
top += MAX(8, var->ty->size);
if (var->is_param_passed_by_reference) {
top += 8;
} else {
top += MAX(8, var->ty->size);
}
}

// Assign offsets to local variables.
Expand Down
30 changes: 30 additions & 0 deletions test/struct_bug17.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "test.h"

struct StructA {
int x1, x2, x3;
};

void func(int dummy1,
int dummy2,
int dummy3,
int dummy4,
struct StructA trigger,
struct StructA bug_occurred) {
// Crash was occurring due to incorrect calculation of parameter offsets when
// passed by reference beyond register slots.
ASSERT(0, bug_occurred.x1);
ASSERT(1, bug_occurred.x2);
ASSERT(2, bug_occurred.x3);
}

extern void XXXXX(int dummy1,
int dummy2,
int dummy3,
int dummy4,
struct StructA trigger,
struct StructA bug_occurred);
int main(void) {
struct StructA var = {0, 1, 2};
func(1, 2, 3, 4, var, var);
return 0;
}

0 comments on commit a6d256f

Please sign in to comment.