From 5bce63c0471a6b99ddf73dbeef813110380ec3f8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Koren=C4=8Dik?= <xkorenc1@fi.muni.cz>
Date: Thu, 30 Jan 2020 15:55:37 +0000
Subject: [PATCH] lart: Deallocate memory frames used to return values in
 lower_ret_agg.

---
 lart/mcsema/lowerreturn.hpp | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/lart/mcsema/lowerreturn.hpp b/lart/mcsema/lowerreturn.hpp
index dc96c9527..1cff42370 100644
--- a/lart/mcsema/lowerreturn.hpp
+++ b/lart/mcsema/lowerreturn.hpp
@@ -111,11 +111,27 @@ namespace lart::mcsema
             return std::string( wrapper_prefix ) + std::to_string( ++counter );
         }
 
-        // FIXME: Currently we leak memory
-        template< typename irb_t >
-        void free( llvm::Value * val, irb_t &irb )
+        void deallocate( llvm::Value *ptr, llvm::Instruction *ret )
+        {
+            auto free_f = _m->getFunction( "__vm_obj_free" );
+            if ( !free_f )
+                UNREACHABLE( "Could not find free function" );
+
+            llvm::IRBuilder<> irb( ret );
+            auto i8ptr = irb.CreateBitCast( ptr, i8PTy() );
+            irb.CreateCall( i8ptr );
+
+        }
+
+        void deallocate( llvm::Value * val )
         {
-            UNREACHABLE( "Not implemented" );
+            auto all_rets = query::query( *util::get_function( val ) )
+                            .flatten()
+                            .filter( query::llvmdyncast< llvm::ReturnInst > )
+                            .map( query::refToPtr )
+                            .freeze();
+            for ( auto ret : all_rets )
+                deallocate( val, ret );
         }
 
         template< typename irb_t >
-- 
GitLab