Browse Source

[LibOS] Correctly release resources in str_close()

Thomas Knauth 4 years ago
parent
commit
15be96fbae

+ 7 - 0
LibOS/shim/src/fs/str/fs.c

@@ -76,6 +76,13 @@ int str_close(struct shim_handle* hdl) {
     }
 
     str_dput(hdl->dentry);
+
+    if (hdl->info.str.data) {
+        free(hdl->info.str.data->str);
+        free(hdl->info.str.data);
+        hdl->info.str.data = NULL;
+    }
+
     return 0;
 }
 

+ 17 - 0
LibOS/shim/test/regression/str_close_leak.c

@@ -0,0 +1,17 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char** argv) {
+    for (int i = 0; i < 1000000; i++) {
+        int fd = open("/proc/meminfo", O_RDONLY);
+        if (fd == -1)
+            abort();
+        close(fd);
+    }
+
+    printf("Success\n");
+
+    return 0;
+}

+ 4 - 0
LibOS/shim/test/regression/test_libos.py

@@ -395,6 +395,10 @@ class TC_40_FileSystem(RegressionTestCase):
         stdout, stderr = self.run_binary(['fdleak'], timeout=10)
         self.assertIn("Test succeeded.", stdout)
 
+    def test_040_str_close_leak(self):
+        stdout, _ = self.run_binary(['str_close_leak'], timeout=60)
+        self.assertIn("Success", stdout)
+
 class TC_80_Socket(RegressionTestCase):
     def test_000_getsockopt(self):
         stdout, stderr = self.run_binary(['getsockopt'])