Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename,
CrashDiagDir = "/";
path::append(CrashDiagDir, "Library/Logs/DiagnosticReports");
int PID =
#if LLVM_ON_UNIX
#if LLVM_ON_UNIX && !defined(__wasi__)
getpid();
#else
0;
Expand Down
1 change: 1 addition & 0 deletions llvm/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ check_symbol_exists(getrlimit "sys/types.h;sys/time.h;sys/resource.h" HAVE_GETRL
check_symbol_exists(posix_spawn spawn.h HAVE_POSIX_SPAWN)
check_symbol_exists(pread unistd.h HAVE_PREAD)
check_symbol_exists(sbrk unistd.h HAVE_SBRK)
check_symbol_exists(setjmp setjmp.h HAVE_SETJMP)
check_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
check_symbol_exists(strerror_s string.h HAVE_DECL_STRERROR_S)
check_symbol_exists(setenv stdlib.h HAVE_SETENV)
Expand Down
4 changes: 4 additions & 0 deletions llvm/cmake/modules/HandleLLVMOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ elseif(FUCHSIA OR UNIX)
else()
set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
endif()
elseif(WASI)
set(LLVM_ON_WIN32 0)
set(LLVM_ON_UNIX 1)
set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Generic")
set(LLVM_ON_WIN32 0)
set(LLVM_ON_UNIX 0)
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/ADT/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \
defined(__Fuchsia__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) || \
defined(__OpenBSD__) || defined(__DragonFly__)
defined(__OpenBSD__) || defined(__DragonFly__) || defined(__wasm__)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should probably be __wasi__ rather than __wasm__ since the later is the architecture, which by itself doesn't mean we have endian.h available.

#include <endian.h>
#elif defined(_AIX)
#include <sys/machine.h>
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/Config/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@
/* Define to 1 if you have the `sbrk' function. */
#cmakedefine HAVE_SBRK ${HAVE_SBRK}

/* Define to 1 if you have the `setjmp' function. */
/* This function is expected to be present everywhere except for a subset of WebAssembly builds. */
#cmakedefine HAVE_SETJMP ${HAVE_SETJMP}

/* Define to 1 if you have the `setenv' function. */
#cmakedefine HAVE_SETENV ${HAVE_SETENV}

Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cmath>
#if !defined(__wasi__)
#include <csignal>
#endif
#include <cstdint>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -340,7 +342,11 @@ static GenericValue lle_X_exit(FunctionType *FT, ArrayRef<GenericValue> Args) {
static GenericValue lle_X_abort(FunctionType *FT, ArrayRef<GenericValue> Args) {
//FIXME: should we report or raise here?
//report_fatal_error("Interpreted program raised SIGABRT");
#if defined(__wasi__)
abort();
#else
raise (SIGABRT);
#endif
return GenericValue();
}

Expand Down
30 changes: 26 additions & 4 deletions llvm/lib/Support/CrashRecoveryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
#include "llvm/Support/Signals.h"
#include "llvm/Support/thread.h"
#include <cassert>
#if !defined(__wasi__)
#include <csignal>
#endif
#include <mutex>
#if HAVE_SETJMP
// We can rely on setjmp to exist everywhere except for a subset of WebAssembly
// builds.
#include <setjmp.h>
#endif

using namespace llvm;

Expand All @@ -31,7 +38,9 @@ struct CrashRecoveryContextImpl {
const CrashRecoveryContextImpl *Next;

CrashRecoveryContext *CRC;
#ifdef HAVE_SETJMP
::jmp_buf JumpBuffer;
#endif
volatile unsigned Failed : 1;
unsigned SwitchedThread : 1;
unsigned ValidJumpBuffer : 1;
Expand All @@ -50,9 +59,7 @@ struct CrashRecoveryContextImpl {
/// Called when the separate crash-recovery thread was finished, to
/// indicate that we don't need to clear the thread-local CurrentContext.
void setSwitchedThread() {
#if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0
SwitchedThread = true;
#endif
}

// If the function ran by the CrashRecoveryContext crashes or fails, then
Expand All @@ -72,9 +79,11 @@ struct CrashRecoveryContextImpl {

CRC->RetCode = RetCode;

#if HAVE_SETJMP
// Jump back to the RunSafely we were called under.
if (ValidJumpBuffer)
longjmp(JumpBuffer, 1);
#endif

// Otherwise let the caller decide of the outcome of the crash. Currently
// this occurs when using SEH on Windows with MSVC or clang-cl.
Expand Down Expand Up @@ -329,7 +338,16 @@ static void uninstallExceptionOrSignalHandlers() {
}
}

#else // !_WIN32
#elif defined(__wasi__)

// WASI implementation.
//
// WASI traps are always fatal, and recovery is not possible. Do nothing.

static void installExceptionOrSignalHandlers() {}
static void uninstallExceptionOrSignalHandlers() {}

#else // !_WIN32 && !__wasi__

// Generic POSIX implementation.
//
Expand Down Expand Up @@ -417,10 +435,12 @@ bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
Impl = CRCI;

#if HAVE_SETJMP
CRCI->ValidJumpBuffer = true;
if (setjmp(CRCI->JumpBuffer) != 0) {
return false;
}
#endif
}

Fn();
Expand Down Expand Up @@ -467,7 +487,9 @@ bool CrashRecoveryContext::isCrash(int RetCode) {
bool CrashRecoveryContext::throwIfCrash(int RetCode) {
if (!isCrash(RetCode))
return false;
#if defined(_WIN32)
#if defined(__wasi__)
abort();
#elif defined(_WIN32)
::RaiseException(RetCode, 0, 0, NULL);
#else
llvm::sys::unregisterHandlers();
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Support/InitLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv,
// Bring stdin/stdout/stderr into a known state.
sys::AddSignalHandler(CleanupStdHandles, nullptr);
#endif
#if !defined(__wasi__)
if (InstallPipeSignalExitHandler)
// The pipe signal handler must be installed before any other handlers are
// registered. This is because the Unix \ref RegisterHandlers function does
Expand All @@ -59,6 +60,7 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv,
StackPrinter.emplace(Argc, Argv);
sys::PrintStackTraceOnErrorSignal(Argv[0]);
install_out_of_memory_new_handler();
#endif

#ifdef __MVS__

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Support/LockFileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
StringRef UUIDRef(UUIDStr);
HostID.append(UUIDRef.begin(), UUIDRef.end());

#elif LLVM_ON_UNIX
#elif !defined(__wasi__)
Copy link
Member

Choose a reason for hiding this comment

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

You're changing behavior for !LLVM_ON_UNIX (like Windows) to use this branch now. That seems probably unintended. Meant #elif LLVM_ON_UNIX && !defined(__wasi__)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, this is the cause of buildbot failures that I missed during my own review.

char HostName[256];
HostName[255] = 0;
HostName[0] = 0;
Expand All @@ -111,7 +111,7 @@ static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
}

bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
#if LLVM_ON_UNIX && !defined(__ANDROID__)
#if LLVM_ON_UNIX && !defined(__ANDROID__) && !defined(__wasi__)
SmallString<256> StoredHostID;
if (getHostID(StoredHostID))
return true; // Conservatively assume it's executing on error.
Expand Down
16 changes: 13 additions & 3 deletions llvm/lib/Support/Signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,19 @@ static bool printMarkupStackTrace(StringRef Argv0, void **StackTrace, int Depth,
}

// Include the platform-specific parts of this class.
#ifdef LLVM_ON_UNIX
#if defined(__wasi__)
// WASI does not have signals.
Copy link
Member

Choose a reason for hiding this comment

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

Move these stubs to WASI/Signals.inc for consistency with the other platform support files.

void llvm::sys::AddSignalHandler(sys::SignalHandlerCallback FnPtr,
void *Cookie) {}
void llvm::sys::RunInterruptHandlers() {}
void sys::CleanupOnSignal(uintptr_t Context) {}
bool llvm::sys::RemoveFileOnSignal(StringRef Filename, std::string *ErrMsg) {
return false;
}
void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) {}
void llvm::sys::DisableSystemDialogsOnCrash() {}
#elif defined(LLVM_ON_UNIX)
#include "Unix/Signals.inc"
#endif
#ifdef _WIN32
#elif defined(_WIN32)
#include "Windows/Signals.inc"
#endif
5 changes: 5 additions & 0 deletions llvm/lib/Support/Unix/Memory.inc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ std::error_code Memory::releaseMappedMemory(MemoryBlock &M) {

std::error_code Memory::protectMappedMemory(const MemoryBlock &M,
unsigned Flags) {
#if defined(__wasi__)
// Wasm does not allow making memory read-only or executable.
return std::error_code(ENOSYS, std::generic_category());
#endif

static const Align PageSize = Align(Process::getPageSizeEstimate());
if (M.Address == nullptr || M.AllocatedSize == 0)
return std::error_code();
Expand Down
47 changes: 41 additions & 6 deletions llvm/lib/Support/Unix/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
#endif

#include <dirent.h>
#if !defined(__wasi__)
#include <pwd.h>
#endif
#include <sys/file.h>

#ifdef __APPLE__
Expand Down Expand Up @@ -126,10 +128,12 @@ namespace fs {

const file_t kInvalidFile = -1;

#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__FreeBSD_kernel__) || defined(__linux__) || defined(__CYGWIN__) || \
defined(__DragonFly__) || defined(_AIX) || defined(__GNU__) || \
(defined(__sun__) && defined(__svr4__) || defined(__HAIKU__))
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__FreeBSD_kernel__) || defined(__linux__) || \
defined(__CYGWIN__) || defined(__DragonFly__) || defined(_AIX) || \
defined(__GNU__) || \
(defined(__sun__) && defined(__svr4__) || defined(__HAIKU__)) || \
defined(__wasi__)
static int test_dir(char ret[PATH_MAX], const char *dir, const char *bin) {
struct stat sb;
char fullpath[PATH_MAX];
Expand Down Expand Up @@ -283,7 +287,7 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) {
// Fall back to the classical detection.
if (getprogpath(exe_path, argv0))
return exe_path;
#elif defined(__OpenBSD__) || defined(__HAIKU__)
#elif defined(__OpenBSD__) || defined(__HAIKU__) || defined(__wasi__)
char exe_path[PATH_MAX];
// argv[0] only
if (getprogpath(exe_path, argv0) != NULL)
Expand Down Expand Up @@ -508,6 +512,9 @@ static bool is_local_impl(struct STATVFS &Vfs) {
#elif defined(__EMSCRIPTEN__)
// Emscripten doesn't currently support remote filesystem mounts.
return true;
#elif defined(__wasi__)
Copy link
Member

Choose a reason for hiding this comment

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

__EMSCRIPTEN__ || __wasi__ is effectively equal __wasm__; merge clauses?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Does emscripten no longer have asm.js mode? Last time I checked it did, but I'm not sure if it still does today...

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't; C++ compilation only targets wasm.

The emscripten driver still supports generating javascript, but only via invoking the binaryen "wasm2js" tool post-link -- which translates the wasm binary into javascript.

// WASI doesn't currently support remote filesystem mounts.
return true;
#elif defined(__HAIKU__)
// Haiku doesn't expose this information.
return false;
Expand Down Expand Up @@ -673,6 +680,11 @@ static void expandTildeExpr(SmallVectorImpl<char> &Path) {
return;
}

#if defined(__wasi__)
// No access to password database, return back the original path.
(void)Remainder;
return;
#else
// This is a string of the form ~username/, look up this user's entry in the
// password database.
std::unique_ptr<char[]> Buf;
Expand All @@ -694,6 +706,7 @@ static void expandTildeExpr(SmallVectorImpl<char> &Path) {
Path.clear();
Path.append(Entry->pw_dir, Entry->pw_dir + strlen(Entry->pw_dir));
llvm::sys::path::append(Path, Storage);
#endif
}

void expand_tilde(const Twine &path, SmallVectorImpl<char> &dest) {
Expand Down Expand Up @@ -770,11 +783,15 @@ std::error_code status(int FD, file_status &Result) {
}

unsigned getUmask() {
#if defined(__wasi__)
return 0022;
#else
// Chose arbitary new mask and reset the umask to the old mask.
// umask(2) never fails so ignore the return of the second call.
unsigned Mask = ::umask(0);
(void)::umask(Mask);
return Mask;
#endif
}

std::error_code setPermissions(const Twine &Path, perms Permissions) {
Expand Down Expand Up @@ -880,7 +897,7 @@ void mapped_file_region::dontNeedImpl() {
assert(Mode == mapped_file_region::readonly);
if (!Mapping)
return;
#if defined(__MVS__) || defined(_AIX)
#if defined(__MVS__) || defined(_AIX) || defined(__wasi__)
// If we don't have madvise, or it isn't beneficial, treat this as a no-op.
#elif defined(POSIX_MADV_DONTNEED)
::posix_madvise(Mapping, Size, POSIX_MADV_DONTNEED);
Expand Down Expand Up @@ -1224,6 +1241,9 @@ Expected<size_t> readNativeFileSlice(file_t FD, MutableArrayRef<char> Buf,
}

std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout) {
#if defined(__wasi__)
return std::error_code(ENOSYS, std::generic_category());
#else
auto Start = std::chrono::steady_clock::now();
auto End = Start + Timeout;
do {
Expand All @@ -1241,9 +1261,13 @@ std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout) {
usleep(1000);
} while (std::chrono::steady_clock::now() < End);
return make_error_code(errc::no_lock_available);
#endif
}

std::error_code lockFile(int FD) {
#if defined(__wasi__)
return std::error_code(ENOSYS, std::generic_category());
#else
struct flock Lock;
memset(&Lock, 0, sizeof(Lock));
Lock.l_type = F_WRLCK;
Expand All @@ -1253,9 +1277,13 @@ std::error_code lockFile(int FD) {
if (::fcntl(FD, F_SETLKW, &Lock) != -1)
return std::error_code();
return errnoAsErrorCode();
#endif
}

std::error_code unlockFile(int FD) {
#if defined(__wasi__)
return std::error_code(ENOSYS, std::generic_category());
#else
struct flock Lock;
Lock.l_type = F_UNLCK;
Lock.l_whence = SEEK_SET;
Expand All @@ -1264,6 +1292,7 @@ std::error_code unlockFile(int FD) {
if (::fcntl(FD, F_SETLK, &Lock) != -1)
return std::error_code();
return errnoAsErrorCode();
#endif
}

std::error_code closeFile(file_t &F) {
Expand Down Expand Up @@ -1335,11 +1364,15 @@ std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
}

std::error_code changeFileOwnership(int FD, uint32_t Owner, uint32_t Group) {
#if defined(__wasi__)
return std::error_code(ENOTSUP, std::generic_category());
#else
auto FChown = [&]() { return ::fchown(FD, Owner, Group); };
// Retry if fchown call fails due to interruption.
if ((sys::RetryAfterSignal(-1, FChown)) < 0)
return errnoAsErrorCode();
return std::error_code();
#endif
}

} // end namespace fs
Expand All @@ -1349,6 +1382,7 @@ namespace path {
bool home_directory(SmallVectorImpl<char> &result) {
std::unique_ptr<char[]> Buf;
char *RequestedDir = getenv("HOME");
#if !defined(__wasi__)
if (!RequestedDir) {
long BufSize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (BufSize <= 0)
Expand All @@ -1360,6 +1394,7 @@ bool home_directory(SmallVectorImpl<char> &result) {
if (pw && pw->pw_dir)
RequestedDir = pw->pw_dir;
}
#endif
if (!RequestedDir)
return false;

Expand Down
Loading