Skip to content

Commit

Permalink
[OpenMP] Create a temp file in /tmp if /dev/shm is not accessible
Browse files Browse the repository at this point in the history
When `libomp` is initialized, it creates a temp file in `/dev/shm` to store
registration flag. Some systems, like Android, don't have `/dev/shm`, then this
feature is disabled by the macro `KMP_USE_SHM`, though most Linux distributions
have that. However, some customized distribution, such as the one reported in
#53955, doesn't support it either.
It causes a core dump. In this patch, if it is the case, we will try to create a
temporary file in `/tmp`, and if it still doesn't make it, then we error out.
Note that we don't consider in this patch if the temporary directory has been
set to `TMPDIR` in this patch. If `/tmp` is not accessible, we error out.

Fix #53955.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D142175
  • Loading branch information
shiltian committed Jan 25, 2023
1 parent 56313f6 commit dafebd5
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions openmp/runtime/src/kmp_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "kmp_wait_release.h"
#include "kmp_wrapper_getpid.h"
#include "kmp_dispatch.h"
#include <cstdio>
#if KMP_USE_HIER_SCHED
#include "kmp_dispatch_hier.h"
#endif
Expand Down Expand Up @@ -6739,6 +6740,11 @@ static inline char *__kmp_reg_status_name() {
#endif
} // __kmp_reg_status_get

#if defined(KMP_USE_SHM)
// If /dev/shm is not accessible, we will create a temporary file under /tmp.
char *temp_reg_status_file_name = nullptr;
#endif

void __kmp_register_library_startup(void) {

char *name = __kmp_reg_status_name(); // Name of the environment variable.
Expand Down Expand Up @@ -6780,11 +6786,19 @@ void __kmp_register_library_startup(void) {
// able to open existing file
shm_preexist = 1;
}
} else if (fd1 == -1) { // SHM didn't open; it was due to error other than
// already exists.
// error out here.
__kmp_fatal(KMP_MSG(FunctionError, "Can't open SHM2"), KMP_ERR(errno),
__kmp_msg_null);
} else if (fd1 == -1) {
// SHM didn't open; it was due to error other than already exists. Try to
// create a temp file under /tmp.
// TODO: /tmp might not always be the temporary directory. For now we will
// not consider TMPDIR. If /tmp is not accessible, we simply error out.
char *temp_file_name = __kmp_str_format("/tmp/%sXXXXXX", name);
fd1 = mkstemp(temp_file_name);
if (fd1 == -1) {
// error out here.
__kmp_fatal(KMP_MSG(FunctionError, "Can't open TEMP"), KMP_ERR(errno),
__kmp_msg_null);
}
temp_reg_status_file_name = temp_file_name;
}
if (shm_preexist == 0) {
// we created SHM now set size
Expand Down Expand Up @@ -6896,11 +6910,19 @@ void __kmp_unregister_library(void) {
char *value = NULL;

#if defined(KMP_USE_SHM)
bool use_shm = true;
char *shm_name = __kmp_str_format("/%s", name);
int fd1 = shm_open(shm_name, O_RDONLY, 0666);
if (fd1 == -1) {
// file did not open. return.
return;
// File did not open. Try the temporary file.
use_shm = false;
KMP_DEBUG_ASSERT(temp_reg_status_file_name);
FILE *tf = fopen(temp_reg_status_file_name, O_RDONLY);
if (!tf) {
// give it up now.
return;
}
fd1 = fileno(tf);
}
char *data1 = (char *)mmap(0, SHM_SIZE, PROT_READ, MAP_SHARED, fd1, 0);
if (data1 != MAP_FAILED) {
Expand All @@ -6917,14 +6939,23 @@ void __kmp_unregister_library(void) {
if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
// Ok, this is our variable. Delete it.
#if defined(KMP_USE_SHM)
shm_unlink(shm_name); // this removes file in /dev/shm
if (use_shm) {
shm_unlink(shm_name); // this removes file in /dev/shm
} else {
KMP_DEBUG_ASSERT(temp_reg_status_file_name);
unlink(temp_reg_status_file_name); // this removes the temp file
}
#else
__kmp_env_unset(name);
#endif
}

#if defined(KMP_USE_SHM)
KMP_INTERNAL_FREE(shm_name);
if (!use_shm) {
KMP_DEBUG_ASSERT(temp_reg_status_file_name);
KMP_INTERNAL_FREE(temp_reg_status_file_name);
}
#endif

KMP_INTERNAL_FREE(__kmp_registration_str);
Expand Down

0 comments on commit dafebd5

Please sign in to comment.