Compare commits

..

No commits in common. 'e47caa80358a32f45cf1eac3294f2a3e4cb09f94' and 'f1c89d50e140d92214eab3dfeb58570784d62fa8' have entirely different histories.

2
.gitignore vendored

@ -5,7 +5,5 @@ perf/
*.o *.o
*-debug *-debug
*-release *-release
*-test
*.so *.so
*.so.*
*.gch *.gch

@ -3,13 +3,12 @@
PROJECT=memfd_secret-shim PROJECT=memfd_secret-shim
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me> AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
DESCRIPTION= DESCRIPTION=
VERSION=0.0.0r1 VERSION=0.0.0
SRC = src SRC = src
# Exclude `src/test/*` from search. SRC_C = $(shell find $(SRC)/ -type f -name \*.c)
SRC_C = $(shell find $(SRC)/ -type f -name \*.c -a -not -path src/test/\*) SRC_CXX = $(shell find -O2 $(SRC)/ -type f -name \*.cpp -or -name \*.cxx)
SRC_CXX = $(shell find -O2 $(SRC)/ -type f -name \*.cpp -or -name \*.cxx -a -not -path src/test/\*)
INCLUDE=include INCLUDE=include
# If PCH should be auto-included for all TUs, set to 1. # If PCH should be auto-included for all TUs, set to 1.
@ -18,54 +17,27 @@ INCLUDE_PCH_GLOBAL?=0
INCLUDE_GLOBAL= INCLUDE_GLOBAL=
# Link to these libraries dynamicalls # Link to these libraries dynamicalls
SHARED_LIBS= SHARED_LIBS=fmt
# Link to these libraries statically # Link to these libraries statically
STATIC_LIBS= STATIC_LIBS=
# Prefix for un/install targets
ifeq ($(PREFIX),)
PREFIX := /usr/local
endif
INCLUDE_PREFIX := $(PROJECT)/
# Default archivers
AR?=ar
RANLIB?=ranlib
# Use gcc-{ar,ranlib} when using gcc
ifeq ($(CXX),g++)
AR:=gcc-ar
RANLIB:=gcc-ranlib
endif
# Pre-compile these headers # Pre-compile these headers
PCH_HEADERS+= PCH_HEADERS+=
# PCH_HEADERS depend on these header files # PCH_HEADERS depend on these header files
PCH_INCLUDES+= PCH_INCLUDES+=
# Link executable statically (in release builds only.)
STATIC?=no
# Compile-time default program features (see `Features application` below.) # Compile-time default program features (see `Features application` below.)
FEATURES?= FEATURES?=
# Build constants # Build constants
CONSTANTS+=_GNU_SOURCE CONSTANTS+=_GNU_SOURCE
# Testing
# Can be {release,debug}.{a,so}
TARGET?=debug.a
TEST_LDFLAGS+= -lfmt -lstdc++ -Wl,-z,now -Wl,-z -Wl,relro
TEST_CFLAGS+= -Og -g -fwhole-program
override __COMMA=, override __COMMA=,
override __VERSION_SPLIT:= $(subst ., ,$(VERSION)) override __VERSION_SPLIT:= $(subst ., ,$(VERSION))
override __VERSION_REVISION:=$(word 3,$(__VERSION_SPLIT)) 0 override __VERSION_REVISION:=$(word 3,$(__VERSION_SPLIT)) 0
VERSION_MAJOR:= $(word 1,$(__VERSION_SPLIT))
VERSION_MINOR:= $(word 2,$(__VERSION_SPLIT))
VERSION_BUGFIX:= $(word 3,$(__VERSION_SPLIT))
VERSION_REVISION:= $(word 2,$(subst r, ,$(__VERSION_REVISION)))
override __VERSION_SPLIT:= MAJOR:$(word 1,$(__VERSION_SPLIT)) MINOR:$(word 2,$(__VERSION_SPLIT)) BUGFIX:$(word 1,$(subst r, ,$(__VERSION_REVISION))) REVISION:$(word 2,$(subst r, ,$(__VERSION_REVISION))) REVISION_STRING:$(word 3,$(__VERSION_SPLIT)) override __VERSION_SPLIT:= MAJOR:$(word 1,$(__VERSION_SPLIT)) MINOR:$(word 2,$(__VERSION_SPLIT)) BUGFIX:$(word 1,$(subst r, ,$(__VERSION_REVISION))) REVISION:$(word 2,$(subst r, ,$(__VERSION_REVISION))) REVISION_STRING:$(word 3,$(__VERSION_SPLIT))
COMMON_FLAGS+= -W -Wall COMMON_FLAGS+= -W -Wall
@ -79,27 +51,14 @@ COMMON_FLAGS+= $(addprefix -D_VERSION_,$(subst :,=,$(__VERSION_SPLIT))) '-D_VERS
ARCH?=native ARCH?=native
CPU?=native CPU?=native
# Enable OpenMP and loop parallelisation? (dyn-links to openmp) # Enable OpenMP and loop parallelisation? (dyn-links to openmp)
PARALLEL?=no PARALLEL?=yes
# Enable CPU-specific features # Enable CPU-specific features
CPU_FLAGS?= CPU_FLAGS?=
BINFLAGS+=
DEBUG_BINFLAGS+=
RELEASE_BINFLAGS+= -fuse-linker-plugin
OPT_FLAGS?= -fgraphite \ OPT_FLAGS?= -fgraphite \
-floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \
-fno-stack-check -fno-stack-check
# Static and shared common flags
SHARED_FLAGS+=-fPIC
SHARED_RELEASE_FLAGS+=
SHARED_DEBUG_FLAGS+=
STATIC_FLAGS+=
STATIC_RELEASE_FLAGS+=-ffat-lto-objects
STATIC_DEBUG_FLAGS+=
# Features application # Features application
## Tell program which features are enabled via `FEATURE_<feature name in UPPER_SNAKE_CASE>`. ## Tell program which features are enabled via `FEATURE_<feature name in UPPER_SNAKE_CASE>`.
@ -124,8 +83,7 @@ ifneq ($(CPU),)
endif endif
ifeq ($(PARALLEL),yes) ifeq ($(PARALLEL),yes)
SHARED_FLAGS+= -fopenmp OPT_FLAGS+= -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4
SHARED_RELEASE_FLAGS+= -floop-parallelize-all -ftree-parallelize-loops=4
endif endif
COMMON_FLAGS+=$(addprefix -m,$(CPU_FLAGS)) COMMON_FLAGS+=$(addprefix -m,$(CPU_FLAGS))
@ -145,7 +103,7 @@ DEBUG_COMMON_FLAGS+= -ggdb -gz -ftrapv -fbounds-check
ifneq ($(TARGET_SPEC_FLAGS),no) ifneq ($(TARGET_SPEC_FLAGS),no)
RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS)
RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS)
RELEASE_LDFLAGS?= -Wl,-O3 -Wl,-flto RELEASE_LDFLAGS?= -Wl,-O3 -Wl,-flto -fuse-linker-plugin
DEBUG_CFLAGS?= -Og DEBUG_CFLAGS?= -Og
DEBUG_CXXFLAGS?= -Og DEBUG_CXXFLAGS?= -Og
@ -153,6 +111,10 @@ ifneq ($(TARGET_SPEC_FLAGS),no)
DEBUG_LDFLAGS?= DEBUG_LDFLAGS?=
endif endif
ifeq ($(STATIC),yes)
RELEASE_LDFLAGS+=-static
endif
DEBUG_CFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) DEBUG_CFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS)
DEBUG_CXXFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) -fasynchronous-unwind-tables DEBUG_CXXFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) -fasynchronous-unwind-tables
@ -207,7 +169,7 @@ CFLAGS += $(COMMON_FLAGS) --std=$(CSTD)
CXXFLAGS += $(COMMON_FLAGS) --std=$(CXXSTD) CXXFLAGS += $(COMMON_FLAGS) --std=$(CXXSTD)
LDFLAGS += $(addsuffix .a,$(addprefix -l:lib,$(STATIC_LIBS))) $(addprefix -l,$(SHARED_LIBS)) LDFLAGS += $(addsuffix .a,$(addprefix -l:lib,$(STATIC_LIBS))) $(addprefix -l,$(SHARED_LIBS))
# PGO (unused for lib targets) # PGO
PROF_FLAGS= -D_PGO_GEN -fprofile-generate PROF_FLAGS= -D_PGO_GEN -fprofile-generate
PGO_OBJ_C= $(addprefix prof/c/,$(SRC_C:.c=.o)) PGO_OBJ_C= $(addprefix prof/c/,$(SRC_C:.c=.o))
@ -222,33 +184,14 @@ PROF_SMALL_BOUND= 1024
# Phonies # Phonies
# XXX: This doesn't force them to run in series for some reason? # XXX: This doesn't force them to run in series for some reason?
.PHONY: release .PHONY: release
release: | dirs release: | dirs $(PROJECT)-release
$(MAKE) lib$(PROJECT).a
@$(MAKE) clean-rebuild >> /dev/null
@$(MAKE) dirs >> /dev/null
$(MAKE) lib$(PROJECT).so
.PHONY: debug .PHONY: debug
debug: | dirs debug: | dirs $(PROJECT)-debug
$(MAKE) lib$(PROJECT)-debug.a
@$(MAKE) clean-rebuild >> /dev/null .PHONY: pgo
@$(MAKE) dirs >> /dev/null pgo: | dirs $(PROJECT)-pgo
$(MAKE) lib$(PROJECT)-debug.so
# Rebuild both release and debug targets from scratch
.PHONY: all
all: | clean
@$(MAKE) release
@$(MAKE) clean-rebuild
@$(MAKE) debug
.PHONY: test
test: $(PROJECT)-test
-strace ./$<
-valgrind ./$<
./$(PROJECT)-test
# Targets # Targets
@ -296,43 +239,75 @@ prof/cxx/%.o: %.cpp $(PCH_OUTPUT)
$(CXX) -c $< $(CXXFLAGS) -o $@ $(PROF_FLAGS) $(CXX) -c $< $(CXXFLAGS) -o $@ $(PROF_FLAGS)
#$(LDFLAGS) #$(LDFLAGS)
lib$(PROJECT)-release.a: CFLAGS+= $(RELEASE_CFLAGS) $(STATIC_FLAGS) $(STATIC_RELEASE_FLAGS) $(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) $(PCH_USE_CFLAGS)
lib$(PROJECT)-release.a: CXXFLAGS += $(RELEASE_CXXFLAGS) $(STATIC_FLAGS) $(STATIC_RELEASE_FLAGS) $(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS) $(PCH_USE_CXXFLAGS)
lib$(PROJECT)-release.a: LDFLAGS += $(RELEASE_LDFLAGS) $(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS)
lib$(PROJECT)-release.a: $(OBJ) $(PROJECT)-release: $(OBJ)
$(AR) rcs $@ $^ $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS)
$(RANLIB) $@
lib$(PROJECT)-debug.a: CFLAGS+= $(DEBUG_CFLAGS) $(STATIC_FLAGS) $(STATIC_DEBUG_FLAGS)
lib$(PROJECT)-debug.a: CXXFLAGS += $(DEBUG_CXXFLAGS) $(STATIC_FLAGS) $(STATIC_DEBUG_FLAGS)
lib$(PROJECT)-debug.a: LDFLAGS += $(DEBUG_LDFLAGS)
lib$(PROJECT)-debug.a: $(OBJ)
$(AR) rcs $@ $^
$(RANLIB) $@
lib$(PROJECT)-release.so: CFLAGS+= $(RELEASE_CFLAGS) $(SHARED_FLAGS) $(SHARED_RELEASE_FLAGS)
lib$(PROJECT)-release.so: CXXFLAGS += $(RELEASE_CXXFLAGS) $(SHARED_FLAGS) $(SHARED_RELEASE_FLAGS)
lib$(PROJECT)-release.so: LDFLAGS += $(RELEASE_LDFLAGS)
lib$(PROJECT)-release.so: BINFLAGS += $(RELEASE_BINFLAGS)
lib$(PROJECT)-release.so: $(OBJ)
$(CXX) -shared $^ $(BINFLAGS) $(CXXFLAGS) -o $@ $(LDFLAGS)
$(STRIP) $@ $(STRIP) $@
lib$(PROJECT)-debug.so: CFLAGS+= $(DEBUG_CFLAGS) $(SHARED_FLAGS) $(SHARED_DEBUG_FLAGS) $(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) $(PCH_USE_CFLAGS)
lib$(PROJECT)-debug.so: CXXFLAGS += $(DEBUG_CXXFLAGS) $(SHARED_FLAGS) $(SHARED_DEBUG_FLAGS) $(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS) $(PCH_USE_CXXFLAGS)
lib$(PROJECT)-debug.so: LDFLAGS += $(DEBUG_LDFLAGS) $(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS)
lib$(PROJECT)-debug.so: BINFLAGS += $(DEBUG_BINFLAGS) $(PROJECT)-debug: $(OBJ)
lib$(PROJECT)-debug.so: $(OBJ) $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS)
$(CXX) -shared $^ $(BINFLAGS) $(CXXFLAGS) -o $@ $(LDFLAGS)
pgo-reset:
lib$(PROJECT).a: lib$(PROJECT)-release.a find -O3 prof -type f -name \*.gcda -exec rm {} +
ln -f $< $@
pgo-generate: CFLAGS+= $(RELEASE_CFLAGS) $(PCH_USE_CFLAGS)
lib$(PROJECT).so: LDFLAGS+= -Wl,-soname,lib$(PROJECT).so.$(VERSION_MAJOR) pgo-generate: CXXFLAGS+= $(RELEASE_CXXFLAGS) $(PCH_USE_CXXFLAGS)
lib$(PROJECT).so: lib$(PROJECT)-release.so pgo-generate: LDFLAGS+= $(RELEASE_LDFLAGS)
ln -f $< $@.$(VERSION) pgo-generate: $(PGO_OBJ)
ln -sf $@.$(VERSION) $@.$(VERSION_MAJOR) $(CXX) $^ $(CXXFLAGS) $(PROF_FLAGS) -o $@ $(LDFLAGS) $(PROF_FLAGS)
ln -sf $@.$(VERSION_MAJOR) $@
pgo-profile: | pgo-generate pgo-reset
set -e errexit && shopt -s inherit_errexit && set -eo pipefail; \
rm -rf $(PROF_LOCATION); \
for i in {0..$(PROF_ITERATIONS)}; do \
>&2 printf ">>> Iteration $$i \b\b"; \
mkdir -p $(PROF_LOCATION)/{direct,indirect}; \
for j in {0..$(PROF_LARGE_BOUND)}; do \
./pgo-generate; \
done > $(PROF_LOCATION)/full; \
for j in {0..$(PROF_SMALL_BOUND)}; do \
./pgo-generate > $(PROF_LOCATION)/direct/$$j; \
done; \
for j in {0..$(PROF_SMALL_BOUND)}; do \
./pgo-generate >> $(PROF_LOCATION)/indirect/$$i; \
done; \
for j in {0..$(PROF_SMALL_BOUND)}; do \
./pgo-generate > $(PROF_LOCATION)/direct/$$i-$$j & : ; \
done; \
for j in {0..$(PROF_SMALL_BOUND)}; do \
./pgo-generate >> $(PROF_LOCATION)/indirect/$$i-$$j & : ; \
done; \
for j in {0..$(PROF_SMALL_BOUND)}; do \
./pgo-generate >/dev/null & : ; \
done; \
wait; \
rm -rf $(PROF_LOCATION)/{direct,indirect,full}; \
>&2 printf "OK\r"; \
done
@echo ""
rm -rf $(PROF_LOCATION)
rm pgo-generate
pgo-use: CFLAGS+= $(RELEASE_CFLAGS) $(PCH_USE_CFLAGS)
pgo-use: CXXFLAGS+= $(RELEASE_CXXFLAGS) $(PCH_USE_CXXFLAGS)
pgo-use: LDFLAGS+= $(RELEASE_LDFLAGS)
pgo-use: PROF_FLAGS = -fprofile-use -fprofile-correction
pgo-use: $(PGO_OBJ)
$(CXX) $^ $(CXXFLAGS) $(PROF_FLAGS) -o $@ $(LDFLAGS) $(PROF_FLAGS)
$(PROJECT)-pgo: CFLAGS+= $(RELEASE_CFLAGS) $(PCH_USE_CFLAGS)
$(PROJECT)-pgo: CXXFLAGS+= $(RELEASE_CXXFLAGS) $(PCH_USE_CXXFLAGS)
$(PROJECT)-pgo: LDFLAGS+= $(RELEASE_LDFLAGS)
$(PROJECT)-pgo: pgo-profile
find -O3 ./prof -type f -name \*.o -exec rm {} +
$(MAKE) pgo-use
mv pgo-use $@
strip $@
clean-source: clean-source:
find -O2 {obj,prof}/ -type f -exec rm {} + find -O2 {obj,prof}/ -type f -exec rm {} +
@ -342,26 +317,7 @@ clean-rebuild: clean-source
clean: clean-rebuild clean: clean-rebuild
rm -f lib$(PROJECT){,-{release,debug,pgo}}.{a,so{,.*}} rm -f $(PROJECT)-{release,debug,pgo}
rm -f $(PROJECT)-test
clean-full: clean clean-full: clean
rm -rf {obj,prof} rm -rf {obj,prof}
install:
install -d $(DESTDIR)$(PREFIX)/lib/
install -m 644 lib$(PROJECT).a $(DESTDIR)$(PREFIX)/lib/
install -s -m 755 lib$(PROJECT).so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/
ln -sf lib$(PROJECT).so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).so.$(VERSION_MAJOR)
ln -sf lib$(PROJECT).so.$(VERSION_MAJOR) $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).so
install -d $(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX)
install -m 644 $(wildcard $(INCLUDE)/*.*) $(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX)
uninstall:
-rm $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).{a,so{,.*}}
cd $(INCLUDE) && find . -type f | xargs -I {} rm "$(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX){}"
[[ -d "$(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX)" ]] && \
rmdir $(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX) || :
#TODO: We can make the target between `{debug,release}{.a,.so}` configurable by call. e.g. `TARGET=release.a make test`
$(PROJECT)-test: lib$(PROJECT)-$(TARGET)
$(CC) $(CFLAGS) $(TEST_CFLAGS) src/test/*.c -o $@ -l:$< $(LDFLAGS) $(TEST_LDFLAGS)

@ -2,14 +2,10 @@
#define _IT_IFUNC_H #define _IT_IFUNC_H
//! ifunc helpers //! ifunc helpers
#if !defined(IFUNC_PREFIX) #if !IFUNC_PREFIX
#define IFUNC_PREFIX _ifun__ #define IFUNC_PREFIX _ifun__
#endif #endif
#if !defined(IFUNC_IMPL_PREFIX)
#define IFUNC_IMPL_PREFIX _impl__
#endif
#define _IFUNC_STR_(X) #X #define _IFUNC_STR_(X) #X
#define _IFUNC_STR(X) _IFUNC_STR_(X) #define _IFUNC_STR(X) _IFUNC_STR_(X)
@ -20,11 +16,10 @@
#define _IFUNC_PREFIX(X) _IFUNC_PASTE(IFUNC_PREFIX, X) #define _IFUNC_PREFIX(X) _IFUNC_PASTE(IFUNC_PREFIX, X)
#define IFUNC_NAME(name, ver) _IFUNC_PASTE(_IFUNC_PASTE(_IFUNC_PASTE(IFUNC_IMPL_PREFIX, name), __), ver) #define IFUNC_NAME(name, ver) _impl__ ## name ## __ ## ver
#define IFUNC_IMPL(name, ver) __attribute__((copy(name))) IFUNC_NAME(name, ver) #define IFUNC_IMPL(name, ver) __attribute__((copy(name))) IFUNC_NAME(name, ver)
#define IFUNC_RESOLVER_A(attr, name) __attribute__((returns_nonnull)) (* __attribute__(attr) _IFUNC_PREFIX(name) /*_ifun__ ## name*/ (void)) // When the ifunc resolver wants to return a function pointer that has attributes on it, the attribute inner list (e.g. `(returns_nonnull, const, nonnull)') can be provided as the first argument #define IFUNC_RESOLVER_A(attr, name) __attribute__((returns_nonnull)) (* __attribute__(attr) _IFUNC_PREFIX(name) /*_ifun__ ## name*/ (void)) // When the ifunc resolver wants to return a function pointer that has attributes on it, the attribute inner list (e.g. `(returns_nonnull, const, nonnull)') can be provided as the first argument
#define IFUNC_RESOLVER(name) IFUNC_RESOLVER_A((copy(name)), name) #define IFUNC_RESOLVER(name) IFUNC_RESOLVER_A((copy(name)), name)
//TODO: Find out if changing visibility of IFUNC resolver is sound. If it is, set to hidden or internal. #define IFUNC_DEF(name, params) name params __attribute__((__ifunc__(_IFUNC_PREFIX_S #name)))
#define IFUNC_DEF(name, params) name params __attribute__((/*__visibility__("internal"),*/ __ifunc__(_IFUNC_PREFIX_S #name)))
#endif /* _IT_IFUNC_H */ #endif /* _IT_IFUNC_H */

@ -1,20 +1,15 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <errno.h> #include <errno.h>
#define IFUNC_PREFIX _$ifun__
#define IFUNC_IMPL_PREFIX _$impl__
#include "ifunc.h" #include "ifunc.h"
#include <memfd_secret.h> #include <memfd_secret.h>
#define READ_ONCE(slot) ((__typeof__(slot))(*(const volatile __typeof__(slot)*)&(slot))) #define READ_ONCE(slot) ((__typeof__(slot))(*(const volatile __typeof__(slot)*)(slot)))
#define WRITE_ONCE(slot, value) (*((volatile __typeof__(slot)*)&(slot)) = (value)) #define WRITE_ONCE(slot, value) (*((volatile __typeof__(slot)*)(slot)) = (value))
__attribute__((gnu_inline/*, always_inline*/)) __attribute__((gnu_inline))
static inline static inline
int _memfd_secret_raw(unsigned int flags) int _memfd_secret_raw(unsigned int flags)
{ {
@ -31,7 +26,7 @@ static inline
int _has_memfd_secret_raw() int _has_memfd_secret_raw()
{ {
// Attempt syscall // Attempt syscall
int fd = _memfd_secret(0); //XXX: NOTE: man page says `FD_CLOEXEC` is a valid flag, but using it returns `EINVAL`? int fd = _memfd_secret_raw(FD_CLOEXEC);
// If failure to create new fd was caused by `ENOSYS`, it is not available. // If failure to create new fd was caused by `ENOSYS`, it is not available.
if(fd < 0 && errno == ENOSYS) if(fd < 0 && errno == ENOSYS)
@ -74,15 +69,8 @@ int IFUNC_IMPL(memfd_secret, $enabled) (unsigned int flags)
__attribute__((visibility("hidden"))) __attribute__((visibility("hidden")))
int IFUNC_IMPL(memfd_secret, $disabled) (unsigned int flags) int IFUNC_IMPL(memfd_secret, $disabled) (unsigned int flags)
{ {
// Translate mask `flags`, from `FD_CLOEXEC` (if it is set) -> `MEMFD_CLOEXEC`. if( FD_CLOEXEC != MEMFD_CLOEXEC ) { // NOTE: This is a constant expression, and this code will be removed if they are equal.
if( FD_CLOEXEC != MFD_CLOEXEC ) { // NOTE: This is a constant expression, and this code will be removed if they are equal. //TODO: Translate mask `flags`, from `FD_CLOEXEC` (if it is set) -> `MEMFD_CLOEXEC`.
// Check if all bit(s) of `FD_CLOEXEC` is in `flags`.
if((flags & FD_CLOEXEC) == FD_CLOEXEC) {
// Mask out the `FD_CLOEXEC` bit(s)
flags &= ~FD_CLOEXEC;
// Mask in the `MFD_CLOEXEC` bit(s)
flags |= MFD_CLOEXEC;
} // NOTE: We do not need to check cases where `flags & FD_CLOEXEC` is non-zero but the above branch is not hit, that would be an invalid call anyway. Plus I highly doubt any system will set `FD_CLOEXEC` to be more than 1 set bit anyway.
} }
return memfd_create("memfd_secret@?", flags); return memfd_create("memfd_secret@?", flags);
} }

@ -1,21 +0,0 @@
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <memfd_secret.h>
int main()
{
int fd = memfd_secret(0);
if(fd < 0) {
perror("memfd_secret() failed");
return 1;
}
printf("Created memfd_secret (sec: %d): fd = %d\n", (int)_has_memfd_secret(), fd);
close(fd);
return 0;
}
Loading…
Cancel
Save