11#include " klee/Core/MockBuilder.h"
22
3+ #include " klee/Config/Version.h"
34#include " klee/Module/Annotation.h"
45#include " klee/Support/ErrorHandling.h"
6+
57#include " llvm/IR/IRBuilder.h"
68#include " llvm/IR/Module.h"
7- #include < llvm/Support/raw_ostream.h>
9+ #include " llvm/Support/raw_ostream.h"
810
911#include < memory>
1012#include < utility>
@@ -98,8 +100,19 @@ void MockBuilder::buildExternalGlobalsDefinitions() {
98100 }
99101}
100102
101- // TODO: discuss should we annotate not external function
102103void MockBuilder::buildExternalFunctionsDefinitions () {
104+ bool mockAllAnnotation = true ;
105+
106+ if (mockAllAnnotation) {
107+ for (const auto &i : annotations) {
108+ llvm::Function *func = userModule->getFunction (i.first );
109+ auto ext = externals.find (i.first );
110+ if (ext == externals.end ()) {
111+ externals[i.first ] = func->getType ();
112+ }
113+ }
114+ }
115+
103116 for (const auto &it : externals) {
104117 if (!it.second ->isFunctionTy ()) {
105118 continue ;
@@ -133,7 +146,9 @@ void MockBuilder::buildExternalFunctionsDefinitions() {
133146 annotation.returnStatements );
134147 buildAnnotationForExternalFunctionProperties (func, annotation.properties );
135148 } else {
136- buildAnnotationForExternalFunctionReturn (func, {});
149+ // Default annotation for externals return
150+ buildAnnotationForExternalFunctionReturn (
151+ func, {std::make_shared<Statement::InitNull>()});
137152 }
138153 }
139154}
@@ -172,7 +187,8 @@ MockBuilder::goByOffset(llvm::Value *value,
172187 if (!current->getType ()->isPointerTy ()) {
173188 klee_error (" Incorrect annotation offset." );
174189 }
175- current = builder->CreateLoad (current);
190+ current = builder->CreateLoad (current->getType ()->getPointerElementType (),
191+ current);
176192 } else if (inst == " &" ) {
177193 auto addr = builder->CreateAlloca (current->getType ());
178194 current = builder->CreateStore (current, addr);
@@ -236,7 +252,15 @@ bool tryAlign(llvm::Function *func,
236252 }
237253
238254 for (size_t i = 0 , j = 0 ; j < func->arg_size () && i < statements.size ();) {
239- while (isCorrectStatements (statements[i], func->getArg (j))) {
255+ while (true ) {
256+ #if LLVM_VERSION_CODE >= LLVM_VERSION(10, 0)
257+ auto arg = func->getArg (j);
258+ #else
259+ auto arg = &func->arg_begin ()[j];
260+ #endif
261+ if (isCorrectStatements (statements[i], arg)) {
262+ break ;
263+ }
240264 res.emplace_back ();
241265 j++;
242266 if (j >= func->arg_size ()) {
@@ -264,7 +288,11 @@ void MockBuilder::buildAnnotationForExternalFunctionArgs(
264288 return ;
265289 }
266290 for (size_t i = 0 ; i < statements.size (); i++) {
291+ #if LLVM_VERSION_CODE >= LLVM_VERSION(10, 0)
267292 const auto arg = func->getArg (i);
293+ #else
294+ const auto arg = &func->arg_begin ()[i];
295+ #endif
268296 for (const auto &statement : statements[i]) {
269297 llvm::Value *elem = goByOffset (arg, statement->offset );
270298 switch (statement->getKind ()) {
@@ -288,7 +316,7 @@ void MockBuilder::buildAnnotationForExternalFunctionArgs(
288316 mockModule->getContext (), derefCondName, newFunc);
289317 llvm::BasicBlock *contBB = llvm::BasicBlock::Create (
290318 mockModule->getContext (), " continue_" + derefCondName);
291- auto brValue = builder->CreateLoad (derefCond);
319+ auto brValue = builder->CreateLoad (intType, derefCond);
292320 builder->CreateCondBr (brValue, derefBB, contBB);
293321
294322 builder->SetInsertPoint (derefBB);
@@ -320,7 +348,8 @@ void MockBuilder::buildAnnotationForExternalFunctionReturn(
320348 auto *loadInst =
321349 builder->CreateLoad (func->getReturnType (), mockReturnValue, retName);
322350
323- bool initNull = false ;
351+ // For non pointer type do nothing, same as InitNull
352+ bool initNull = !returnType->isPointerTy ();
324353
325354 for (const auto &statement : statements) {
326355 switch (statement->getKind ()) {
@@ -329,9 +358,6 @@ void MockBuilder::buildAnnotationForExternalFunctionReturn(
329358 func->getName ().str ().c_str ());
330359 break ;
331360 case Statement::Kind::InitNull: {
332- if (!returnType->isPointerTy ()) {
333- klee_error (" incorrect type for InitNull" );
334- }
335361 initNull = true ;
336362 break ;
337363 }
0 commit comments