Skip to content

Commit

Permalink
Fix infinite loop with self-referential global struct (#403)
Browse files Browse the repository at this point in the history
The issue that this change solves can be repeated with the following program

```c
// gcc -O0 -g -gdwarf-4 self_refer.c -o self_refer

#include <stdio.h>

struct foo {
  int num;
  struct foo	*foo_next;
};

static struct foo foo0 = {
  .num = 3,
  .foo_next = &foo0
};

int main() {
  printf("Num: %d\n", foo0.foo_next->num);
}
```
  • Loading branch information
ekilmer authored Nov 17, 2023
1 parent 9cfd0d6 commit d569305
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/Lifters/DataLifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,22 @@ llvm::Constant *DataLifter::LiftData(const VariableDecl &decl,
}
}

if (bytes_accessable) {
value = lifter_context.value_lifter.Lift(bytes, type, lifter_context,
decl.address);
}


auto is_constant = first_byte_perms == BytePermission::kReadable ||
first_byte_perms == BytePermission::kReadableExecutable;

auto md = type_specifier.EncodeToMetadata(decl.spec_type);
auto gvar = new llvm::GlobalVariable(*options.module, type, is_constant,
llvm::GlobalValue::ExternalLinkage,
value, var_name);
nullptr, var_name);
gvar->setMetadata("anvill.type", md);
lifter_context.AddEntity(gvar, decl.address);

if (bytes_accessable) {
value = lifter_context.value_lifter.Lift(bytes, type, lifter_context,
decl.address);
}
gvar->setInitializer(value);

return gvar;
}

Expand Down

0 comments on commit d569305

Please sign in to comment.