Skip to content

Commit

Permalink
gcov: use no-op functions if not enabled
Browse files Browse the repository at this point in the history
Instead of wrapping every gcov function call in an ifdef.

Note: The usage of `((void)0)` is based on section 7.2 of the C99
standard (N1256)[1] [2]:

> 7.2 Diagnostics <assert.h>
>
> 1 The header <assert.h> defines the assert macro and refers to another
> macro,
>
>     NDEBUG
>
> which is not defined by <assert.h>. If NDEBUG is defined as a macro
> name at the point in the source file where <assert.h> is included, the
> assert macro is defined simply as
>
>     #define assert(ignore) ((void)0)

See also assert.h(0p) from POSIX.1-2017[3].

Note: This is a continuation of commit b408b20 ("gcov: fix build
failure with gcc 11.1.0") / PR #4373.

[1] http:https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
[2] https://port70.net/~nsz/c/c99/n1256.html#7.2
[3] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/assert.h.html
  • Loading branch information
kmk3 committed Jun 27, 2021
1 parent 98d223d commit 5106b2e
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 108 deletions.
8 changes: 2 additions & 6 deletions src/firejail/appimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@
// sudo mount -o loop krita-3.0-x86_64.appimage mnt

#include "firejail.h"
#include "../include/gcov_wrapper.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <linux/loop.h>
#include <errno.h>

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

static char *devloop = NULL; // device file
static long unsigned size = 0; // offset into appimage file
#define MAXBUF 4096
Expand Down Expand Up @@ -140,9 +137,8 @@ void appimage_set(const char *appimage) {

if (cfg.cwd)
env_store_name_val("OWD", cfg.cwd, SETENV);
#ifdef HAVE_GCOV

__gcov_flush();
#endif
#else
fprintf(stderr, "Error: /dev/loop-control interface is not supported by your kernel\n");
exit(1);
Expand Down
8 changes: 2 additions & 6 deletions src/firejail/chroot.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#ifdef HAVE_CHROOT
#include "firejail.h"
#include "../include/gcov_wrapper.h"
#include <sys/mount.h>
#include <sys/sendfile.h>
#include <errno.h>
Expand All @@ -29,10 +30,6 @@
#define O_PATH 010000000
#endif

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

// exit if error
void fs_check_chroot_dir(void) {
EUID_ASSERT();
Expand Down Expand Up @@ -263,9 +260,8 @@ void fs_chroot(const char *rootdir) {
// update chroot resolv.conf
update_file(parentfd, "etc/resolv.conf");

#ifdef HAVE_GCOV
__gcov_flush();
#endif

// create /run/firejail/mnt/oroot
char *oroot = RUN_OVERLAY_ROOT;
if (mkdir(oroot, 0755) == -1)
Expand Down
8 changes: 2 additions & 6 deletions src/firejail/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "firejail.h"
#include "../include/gcov_wrapper.h"
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
Expand All @@ -33,10 +34,6 @@
#define O_PATH 010000000
#endif

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

#define MAX_BUF 4096
#define EMPTY_STRING ("")
// check noblacklist statements not matched by a proper blacklist in disable-*.inc files
Expand Down Expand Up @@ -1206,9 +1203,8 @@ void fs_overlayfs(void) {
fs_logger("whitelist /tmp");

// chroot in the new filesystem
#ifdef HAVE_GCOV
__gcov_flush();
#endif

if (chroot(oroot) == -1)
errExit("chroot");

Expand Down
9 changes: 3 additions & 6 deletions src/firejail/fs_mkdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "firejail.h"
#include "../include/gcov_wrapper.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <grp.h>
#include <sys/wait.h>
#include <string.h>

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

static void check(const char *fname) {
// manufacture /run/user directory
char *runuser;
Expand Down Expand Up @@ -98,9 +95,9 @@ void fs_mkdir(const char *name) {

// create directory
mkdir_recursive(expanded);
#ifdef HAVE_GCOV

__gcov_flush();
#endif

_exit(0);
}
// wait for the child to finish
Expand Down
16 changes: 6 additions & 10 deletions src/firejail/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include "firejail.h"
#include "../include/gcov_wrapper.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
Expand All @@ -31,10 +32,6 @@
//#include <stdio.h>
//#include <stdlib.h>

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

// uid/gid cache
static uid_t c_uid = 0;
static char *c_uid_name = NULL;
Expand Down Expand Up @@ -353,9 +350,8 @@ void sandboxfs(int op, pid_t pid, const char *path1, const char *path2) {
ls(fname1);
else
cat(fname1);
#ifdef HAVE_GCOV

__gcov_flush();
#endif
}
// get file from host and store it in the sandbox
else if (op == SANDBOX_FS_PUT && path2) {
Expand Down Expand Up @@ -387,9 +383,9 @@ void sandboxfs(int op, pid_t pid, const char *path1, const char *path2) {
// copy the file
if (copy_file(src_fname, tmp_fname, getuid(), getgid(), 0600)) // already a regular user
_exit(1);
#ifdef HAVE_GCOV

__gcov_flush();
#endif

_exit(0);
}

Expand Down Expand Up @@ -419,9 +415,9 @@ void sandboxfs(int op, pid_t pid, const char *path1, const char *path2) {
// copy the file
if (copy_file(tmp_fname, dest_fname, getuid(), getgid(), 0600)) // already a regular user
_exit(1);
#ifdef HAVE_GCOV

__gcov_flush();
#endif

_exit(0);
}

Expand Down
9 changes: 3 additions & 6 deletions src/firejail/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "firejail.h"
#include "../include/pid.h"
#include "../include/firejail_user.h"
#include "../include/gcov_wrapper.h"
#include "../include/syscall.h"
#include "../include/seccomp.h"
#define _GNU_SOURCE
Expand All @@ -44,10 +45,6 @@
#define O_PATH 010000000
#endif

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

#ifdef __ia64__
/* clone(2) has a different interface on ia64, as it needs to know
the size of the stack */
Expand Down Expand Up @@ -3024,9 +3021,9 @@ int main(int argc, char **argv, char **envp) {
network_main(child);
if (arg_debug)
printf("Host network configured\n");
#ifdef HAVE_GCOV

__gcov_flush();
#endif

_exit(0);
}

Expand Down
8 changes: 2 additions & 6 deletions src/firejail/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "firejail.h"
#include "../include/gcov_wrapper.h"
#include "../include/seccomp.h"
#include "../include/syscall.h"
#include <dirent.h>
#include <sys/stat.h>

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

extern char *xephyr_screen;

#define MAX_READ 8192 // line buffer for profile files
Expand Down Expand Up @@ -1799,9 +1796,8 @@ void profile_read(const char *fname) {
// else {
// free(ptr);
// }
#ifdef HAVE_GCOV

__gcov_flush();
#endif
}
fclose(fp);
}
Expand Down
30 changes: 14 additions & 16 deletions src/firejail/rlimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "firejail.h"
#include "../include/gcov_wrapper.h"
#include <sys/time.h>
#include <sys/resource.h>

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

void set_rlimits(void) {
EUID_ASSERT();
// resource limits
Expand All @@ -37,9 +34,9 @@ void set_rlimits(void) {
// set the new limit
rl.rlim_cur = (rlim_t) cfg.rlimit_cpu;
rl.rlim_max = (rlim_t) cfg.rlimit_cpu;
#ifdef HAVE_GCOV

__gcov_dump();
#endif

if (setrlimit(RLIMIT_CPU, &rl) == -1)
errExit("setrlimit");
if (arg_debug)
Expand All @@ -54,9 +51,10 @@ void set_rlimits(void) {
// set the new limit
rl.rlim_cur = (rlim_t) cfg.rlimit_nofile;
rl.rlim_max = (rlim_t) cfg.rlimit_nofile;
#ifdef HAVE_GCOV // gcov-instrumented programs might crash at this point

// gcov-instrumented programs might crash at this point
__gcov_dump();
#endif

if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
errExit("setrlimit");
if (arg_debug)
Expand All @@ -71,9 +69,9 @@ void set_rlimits(void) {
// set the new limit
rl.rlim_cur = (rlim_t) cfg.rlimit_nproc;
rl.rlim_max = (rlim_t) cfg.rlimit_nproc;
#ifdef HAVE_GCOV

__gcov_dump();
#endif

if (setrlimit(RLIMIT_NPROC, &rl) == -1)
errExit("setrlimit");
if (arg_debug)
Expand All @@ -88,9 +86,9 @@ void set_rlimits(void) {
// set the new limit
rl.rlim_cur = (rlim_t) cfg.rlimit_fsize;
rl.rlim_max = (rlim_t) cfg.rlimit_fsize;
#ifdef HAVE_GCOV

__gcov_dump();
#endif

if (setrlimit(RLIMIT_FSIZE, &rl) == -1)
errExit("setrlimit");
if (arg_debug)
Expand All @@ -105,9 +103,9 @@ void set_rlimits(void) {
// set the new limit
rl.rlim_cur = (rlim_t) cfg.rlimit_sigpending;
rl.rlim_max = (rlim_t) cfg.rlimit_sigpending;
#ifdef HAVE_GCOV

__gcov_dump();
#endif

if (setrlimit(RLIMIT_SIGPENDING, &rl) == -1)
errExit("setrlimit");
if (arg_debug)
Expand All @@ -122,9 +120,9 @@ void set_rlimits(void) {
// set the new limit
rl.rlim_cur = (rlim_t) cfg.rlimit_as;
rl.rlim_max = (rlim_t) cfg.rlimit_as;
#ifdef HAVE_GCOV

__gcov_dump();
#endif

if (setrlimit(RLIMIT_AS, &rl) == -1)
errExit("setrlimit");
if (arg_debug)
Expand Down
11 changes: 3 additions & 8 deletions src/firejail/sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include "firejail.h"
#include "../include/gcov_wrapper.h"
#include "../include/seccomp.h"
#include <sys/mman.h>
#include <sys/mount.h>
Expand Down Expand Up @@ -49,10 +50,6 @@
#include <sys/apparmor.h>
#endif

#ifdef HAVE_GCOV
#include "../include/gcov_wrapper.h"
#endif

static int force_nonewprivs = 0;

static int monitored_pid = 0;
Expand Down Expand Up @@ -507,9 +504,8 @@ void start_application(int no_sandbox, int fd, char *set_sandbox_status) {
exit(1);
}

#ifdef HAVE_GCOV
__gcov_dump();
#endif

seccomp_install_filters();

if (set_sandbox_status)
Expand Down Expand Up @@ -563,9 +559,8 @@ void start_application(int no_sandbox, int fd, char *set_sandbox_status) {
if (!arg_command && !arg_quiet)
print_time();

#ifdef HAVE_GCOV
__gcov_dump();
#endif

seccomp_install_filters();

if (set_sandbox_status)
Expand Down
Loading

0 comments on commit 5106b2e

Please sign in to comment.