Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup(userspace/libscap): avoid the usage of non-portable (glibc specific) __gnu_cxx::stdio_filebuf #2037

Merged
merged 2 commits into from
Sep 20, 2024

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Aug 29, 2024

What type of PR is this?

/kind cleanup

Any specific area of the project related to this PR?

/area libscap-engine-gvisor

Does this PR require a change in the driver versions?

What this PR does / why we need it:

Using the glibc specific __gnu_cxx::stdio_filebuf breaks while trying to build Falco/libs with zig compiler (see falcosecurity/falco#3307).
In this specific case, we can make use of POSIX standard popen.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

There are a couple more usages of __gnu_cxx::stdio_filebuf; both are inside libsinsp_e2e tests framework, but we don't really need to build libs tests when we build Falco (with zig) therefore i am skipping them for now.

Does this PR introduce a user-facing change?:

NONE

@poiana
Copy link
Contributor

poiana commented Aug 29, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FedeDP

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@FedeDP
Copy link
Contributor Author

FedeDP commented Aug 29, 2024

/milestone 0.19.0

@poiana poiana added this to the 0.19.0 milestone Aug 29, 2024
Copy link

github-actions bot commented Aug 29, 2024

Perf diff from master - unit tests

     6.18%     +1.08%  [.] sinsp::next
     6.93%     -0.93%  [.] sinsp_evt::get_type
     1.41%     +0.77%  [.] std::_Hashtable<long, std::pair<long const, std::shared_ptr<sinsp_threadinfo> >, std::allocator<std::pair<long const, std::shared_ptr<sinsp_threadinfo> > >, std::__detail::_Select1st, std::equal_to<long>, std::hash<long>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::_M_find_before_node
     4.76%     +0.74%  [.] next
    10.65%     +0.66%  [.] sinsp_parser::reset
     2.71%     -0.56%  [.] scap_event_decode_params
     0.55%     +0.51%  [.] scap_next
     0.71%     -0.45%  [.] copy_and_sanitize_path
     3.84%     +0.43%  [.] sinsp_parser::process_event
     1.52%     -0.43%  [.] sinsp_evt::get_ts

Heap diff from master - unit tests

peak heap memory consumption: 0B
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B

Heap diff from master - scap file

peak heap memory consumption: 0B
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B

Benchmarks diff from master

Comparing gbench_data.json to /root/actions-runner/_work/libs/libs/build/gbench_data.json
Benchmark                                                         Time             CPU      Time Old      Time New       CPU Old       CPU New
----------------------------------------------------------------------------------------------------------------------------------------------
BM_sinsp_split_mean                                            +0.0116         +0.0116           150           152           150           152
BM_sinsp_split_median                                          +0.0128         +0.0128           150           152           150           152
BM_sinsp_split_stddev                                          +0.3996         +0.3992             1             1             1             1
BM_sinsp_split_cv                                              +0.3836         +0.3832             0             0             0             0
BM_sinsp_concatenate_paths_relative_path_mean                  -0.0263         -0.0263            48            47            48            47
BM_sinsp_concatenate_paths_relative_path_median                -0.0411         -0.0411            48            46            48            46
BM_sinsp_concatenate_paths_relative_path_stddev                -0.0635         -0.0636             1             1             1             1
BM_sinsp_concatenate_paths_relative_path_cv                    -0.0382         -0.0383             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_mean                     +0.0086         +0.0086            17            17            17            17
BM_sinsp_concatenate_paths_empty_path_median                   +0.0044         +0.0044            17            17            17            17
BM_sinsp_concatenate_paths_empty_path_stddev                   +0.3166         +0.3161             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_cv                       +0.3055         +0.3049             0             0             0             0
BM_sinsp_concatenate_paths_absolute_path_mean                  -0.0095         -0.0095            51            50            51            50
BM_sinsp_concatenate_paths_absolute_path_median                -0.0098         -0.0098            51            50            51            50
BM_sinsp_concatenate_paths_absolute_path_stddev                +0.4263         +0.4274             0             0             0             0
BM_sinsp_concatenate_paths_absolute_path_cv                    +0.4400         +0.4411             0             0             0             0
BM_sinsp_split_container_image_mean                            -0.0367         -0.0367           356           343           356           343
BM_sinsp_split_container_image_median                          -0.0386         -0.0386           356           342           356           342
BM_sinsp_split_container_image_stddev                          -0.0971         -0.0979             2             2             2             2
BM_sinsp_split_container_image_cv                              -0.0627         -0.0636             0             0             0             0

Copy link

codecov bot commented Aug 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.58%. Comparing base (215db2d) to head (24b3164).
Report is 24 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2037   +/-   ##
=======================================
  Coverage   73.57%   73.58%           
=======================================
  Files         253      253           
  Lines       31860    31867    +7     
  Branches     5640     5641    +1     
=======================================
+ Hits        23442    23450    +8     
+ Misses       8407     8392   -15     
- Partials       11       25   +14     
Flag Coverage Δ
libsinsp 73.58% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Aug 29, 2024

X64 kernel testing matrix

KERNEL CMAKE-CONFIGURE KMOD BUILD KMOD SCAP-OPEN BPF-PROBE BUILD BPF-PROBE SCAP-OPEN MODERN-BPF SCAP-OPEN
amazonlinux2-4.19 🟢 🟢 🟢 🟢 🟢 🟡
amazonlinux2-5.10 🟢 🟢 🟢 🟢 🟢 🟢
amazonlinux2-5.15 🟢 🟢 🟢 🟢 🟢 🟢
amazonlinux2-5.4 🟢 🟢 🟢 🟢 🟢 🟡
amazonlinux2022-5.15 🟢 🟢 🟢 🟢 🟢 🟢
amazonlinux2023-6.1 🟢 🟢 🟢 🟢 🟢 🟢
archlinux-6.0 🟢 🟢 🟢 🟢 🟢 🟢
archlinux-6.7 🟢 🟢 🟢 🟢 🟢 🟢
centos-3.10 🟢 🟢 🟢 🟡 🟡 🟡
centos-4.18 🟢 🟢 🟢 🟢 🟢 🟢
centos-5.14 🟢 🟢 🟢 🟢 🟢 🟢
fedora-5.17 🟢 🟢 🟢 🟢 🟢 🟢
fedora-5.8 🟢 🟢 🟢 🟢 🟢 🟢
fedora-6.2 🟢 🟢 🟢 🟢 🟢 🟢
oraclelinux-3.10 🟢 🟢 🟢 🟡 🟡 🟡
oraclelinux-4.14 🟢 🟢 🟢 🟢 🟢 🟡
oraclelinux-5.15 🟢 🟢 🟢 🟢 🟢 🟢
oraclelinux-5.4 🟢 🟢 🟢 🟢 🟢 🟡
ubuntu-4.15 🟢 🟢 🟢 🟢 🟢 🟡
ubuntu-5.8 🟢 🟢 🟢 🟢 🟢 🟡
ubuntu-6.5 🟢 🟢 🟢 🟢 🟢 🟢

ARM64 kernel testing matrix

KERNEL CMAKE-CONFIGURE KMOD BUILD KMOD SCAP-OPEN BPF-PROBE BUILD BPF-PROBE SCAP-OPEN MODERN-BPF SCAP-OPEN
amazonlinux2-5.4 🟢 🟢 🟢 🟢 🟢 🟡
amazonlinux2022-5.15 🟢 🟢 🟢 🟢 🟢 🟢
fedora-6.2 🟢 🟢 🟢 🟢 🟢 🟢
oraclelinux-4.14 🟢 🟢 🟢 🟡 🟡 🟡
oraclelinux-5.15 🟢 🟢 🟢 🟢 🟢 🟢
ubuntu-6.5 🟢 🟢 🟢 🟢 🟢 🟢

@FedeDP FedeDP force-pushed the cleanup/avoid_non_portable_stdio_filebuf branch from f49119f to cc7d73f Compare September 10, 2024 07:31
@FedeDP
Copy link
Contributor Author

FedeDP commented Sep 10, 2024

@LucaGuerra tested with this small test file:

#include <sys/wait.h>
#include <ext/stdio_filebuf.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string>
#include <vector>

struct result {
   int error = 0;
   std::vector<std::string> output;
};

// Old method
result runsc(char *argv[])
{
	result res;
	int pipefds[2];

	int ret = pipe(pipefds);
	if(ret)
	{
		return res;
	}

	pid_t pid = vfork();
	if(pid > 0)
	{
		int status;

		close(pipefds[1]);
		wait(&status);
		if(!WIFEXITED(status) || WEXITSTATUS(status) != 0)
		{
			res.error = status;
			return res;
		}

		__gnu_cxx::stdio_filebuf<char> filebuf(pipefds[0], std::ios::in);
		std::string line;
		std::istream is(&filebuf);

		while(std::getline(is, line))
		{
			res.output.emplace_back(std::string(line));
		}
	}
	else
	{
		close(pipefds[0]);
		dup2(pipefds[1], STDOUT_FILENO);
		execvp("docker", argv);
		exit(1);
	}

	return res;
}

// New method
result runsc_popen(char *argv[])
{
	result res;

	std::string full_command;
	int i = 0;
	while (true)
	{
		if (argv[i] == nullptr)
		{
			break;
		}
		full_command.append(argv[i]);
		full_command.append(" ");
		i++;
	}
	FILE *cmd_out = popen(full_command.c_str(), "r");
	if (cmd_out == nullptr)
	{
		res.error = -errno;
	}
	else
	{
		char *out = nullptr;
		size_t len = 0;
		ssize_t readbytes;
		while ((readbytes = getline(&out, &len, cmd_out)) >= 0)
		{
			if (out[readbytes - 1] == '\n')
				out[readbytes - 1] = '\0';
			res.output.emplace_back(out);
		}
		free(out);
		int status = pclose(cmd_out);
		if(!WIFEXITED(status) || WEXITSTATUS(status) != 0)
		{
		      res.error = status;
		}
	}

	return res;
}

int main()
{
	const char *argv[] = {
		"docker",
		"--version",
		nullptr
	};

	auto res = runsc((char **)argv);
	printf("run with exit code: %d\n", res.error);
	for (const auto &o : res.output) {
	 	printf("%s\n", o.c_str());
	}
	
	auto res_popen = runsc_popen((char **)argv);
	printf("run_popen with exit code: %d\n", res_popen.error);
	for (const auto &o : res_popen.output) {
	 	printf("%s\n", o.c_str());
	}
	return 0;
}

…ecific) `__gnu_cxx::stdio_filebuf`.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@FedeDP FedeDP force-pushed the cleanup/avoid_non_portable_stdio_filebuf branch from cc7d73f to 28ba006 Compare September 11, 2024 08:35
@poiana poiana added size/S and removed size/M labels Sep 20, 2024
@FedeDP
Copy link
Contributor Author

FedeDP commented Sep 20, 2024

New function test:

#include <sys/wait.h>
#include <ext/stdio_filebuf.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string>
#include <vector>
#include <sstream>
#include <cstring>

struct result {
   int error = 0;
   std::vector<std::string> output;
};

result runsc(char *argv[])
{
	result res;
	int pipefds[2];

	int ret = pipe(pipefds);
	if(ret)
	{
		return res;
	}

	pid_t pid = vfork();
	if(pid > 0)
	{
		int status;

		close(pipefds[1]);
		wait(&status);
		if(!WIFEXITED(status) || WEXITSTATUS(status) != 0)
		{
			res.error = status;
			return res;
		}

		__gnu_cxx::stdio_filebuf<char> filebuf(pipefds[0], std::ios::in);
		std::string line;
		std::istream is(&filebuf);

		while(std::getline(is, line))
		{
			res.output.emplace_back(std::string(line));
		}
	}
	else
	{
		close(pipefds[0]);
		dup2(pipefds[1], STDOUT_FILENO);
		execvp("docker", argv);
		exit(1);
	}

	return res;
}

result runsc_read(char *argv[])
{
	result res;
	int pipefds[2];

	int ret = pipe(pipefds);
	if(ret)
	{
		return res;
	}

	pid_t pid = vfork();
	if(pid > 0)
	{
		int status;

		close(pipefds[1]);
		wait(&status);
		if(!WIFEXITED(status) || WEXITSTATUS(status) != 0)
		{
			res.error = status;
			return res;
		}


		std::string line;

		char buf[512] = {0};
		std::stringstream ss;
		size_t len;

		// Read until EOF
		while ((len = read(pipefds[0], buf, sizeof(buf) - 1)) != 0)
		{
			ss << buf;
			// Be smart: only reset first len bytes
			memset(buf, 0, len);
		}

		while(std::getline(ss, line))
		{
			res.output.emplace_back(line);
		}
	}
	else
	{
		close(pipefds[0]);
		dup2(pipefds[1], STDOUT_FILENO);
		execvp("docker", argv);
		exit(1);
	}

	return res;
}

int main()
{
	const char *argv[] = {
		"docker",
		"--version",
		nullptr
	};

	auto res = runsc((char **)argv);
	printf("run with exit code: %d\n", res.error);
	for (const auto &o : res.output) {
	 	printf("%s\n", o.c_str());
	}
	
	auto res_read = runsc_read((char **)argv);
	printf("run_read with exit code: %d\n", res_read.error);
	for (const auto &o : res_read.output) {
	 	printf("%s\n", o.c_str());
	}
	return 0;
}

}
} else {
close(pipefds[0]);
dup2(pipefds[1], STDOUT_FILENO);
execvp("runsc", argv);
execvp("docker", argv);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh! ops ahha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

greatest catch ever 🤣

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@FedeDP FedeDP force-pushed the cleanup/avoid_non_portable_stdio_filebuf branch from 2d067ff to 24b3164 Compare September 20, 2024 13:30
@poiana poiana merged commit b8c2d0d into master Sep 20, 2024
48 checks passed
@poiana poiana deleted the cleanup/avoid_non_portable_stdio_filebuf branch September 20, 2024 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants