Skip to content

Commit

Permalink
[fix] Fixed inner types for unnamed structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
S1eGa authored and misonijnik committed Oct 15, 2023
1 parent f1e8720 commit ca88a99
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/Core/TypeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ KType *TypeManager::getWrappedType(llvm::Type *type) {
if (typesMap.count(type) == 0) {
types.emplace_back(new KType(type, this));
typesMap.emplace(type, types.back().get());
if (type && type->isPointerTy()) {
getWrappedType(type->getPointerElementType());
}
if (type && type->isArrayTy()) {
getWrappedType(type->getArrayElementType());
}
}
return typesMap[type];
}
Expand All @@ -61,15 +67,21 @@ void TypeManager::initTypesFromStructs() {
* and pull types to top.
*/

std::vector<llvm::StructType *> collectedStructTypes =
parent->module->getIdentifiedStructTypes();
for (auto &structType : collectedStructTypes) {
for (auto &structType : parent->module->getIdentifiedStructTypes()) {
getWrappedType(structType);
}

std::unordered_set<llvm::StructType *> collectedStructTypes;
for (const auto &it : typesMap) {
if (llvm::StructType *itStruct =
llvm::dyn_cast<llvm::StructType>(it.first)) {
collectedStructTypes.insert(itStruct);
}
}

for (auto &typesToOffsets : typesMap) {
if (llvm::isa<llvm::StructType>(typesToOffsets.first)) {
collectedStructTypes.emplace_back(
collectedStructTypes.insert(
llvm::cast<llvm::StructType>(typesToOffsets.first));
}
}
Expand Down
89 changes: 89 additions & 0 deletions test/regression/2023-10-12-inner-types-fix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// RUN: %clang -Wno-everything %s -emit-llvm %O0opt -g -c -o %t1.bc
// RUN: rm -rf %t.klee-out
// RUN: %klee --output-dir=%t.klee-out --entry-point=klee_entry --skip-not-lazy-initialized --min-number-elements-li=1 %t1.bc 2>&1
// RUN: %ktest-tool %t.klee-out/test000003.ktest | FileCheck %s

#include "klee/klee.h"

struct StructWithPointer {
int x;
int *y;
};

struct StructWithDoublePointer {
int x;
int **y;
};

struct StructWithArrayOfPointer {
int x;
int *y[2];
};

struct StructWithStructWithPointer {
struct StructWithPointer swp;
struct StructWithDoublePointer *swdp;
};

struct StructManyPointers {
int a;
int *b;
int **c;
int ***d;
};

struct StructComplex {
int x;
int *y;
int **z;
struct StructWithPointer *swp;
struct StructWithDoublePointer **swdp;
struct StructManyPointers smp;
};

int sumStructWithPointer(struct StructWithPointer par) {
return par.x + *par.y;
}

int sumStructWithPointerAsPointer(struct StructWithPointer *par) {
return par->x + *par->y;
}

int sumStructWithDoublePointer(struct StructWithDoublePointer par) {
return par.x + **par.y;
}

int sumStructWithArrayOfPointer(struct StructWithArrayOfPointer par) {
return par.x + *(par.y[0]) + *(par.y[1]);
}

int sumStructWithStructWithPointer(struct StructWithStructWithPointer par) {
int sswp = sumStructWithPointer(par.swp);
int sswdp = sumStructWithDoublePointer(*par.swdp);
return sswp + sswdp;
}

int sumStructManyPointers(struct StructManyPointers par) {
return par.a + *par.b + **par.c + ***par.d;
}

int sumStructComplex(struct StructComplex par) {
int sswp = sumStructWithPointer(*par.swp);
int sswdp = sumStructWithDoublePointer(**par.swdp);
int ssmp = sumStructManyPointers(par.smp);
return par.x + *par.y + **par.z + sswp + sswdp + ssmp;
}

// CHECK: object 2: pointers: [(8, 3, 0)]
int klee_entry(int utbot_argc, char **utbot_argv, char **utbot_envp) {
struct StructComplex par;
klee_make_symbolic(&par, sizeof(par), "par");
klee_prefer_cex(&par, par.x >= -10 & par.x <= 10);
klee_prefer_cex(&par, par.smp.a >= -10 & par.smp.a <= 10);
////////////////////////////////////////////
int utbot_result;
klee_make_symbolic(&utbot_result, sizeof(utbot_result), "utbot_result");
int utbot_tmp = sumStructComplex(par);
klee_assume(utbot_tmp == utbot_result);
return 0;
}

0 comments on commit ca88a99

Please sign in to comment.