diff --git a/include/verilatedos.h b/include/verilatedos.h index ae3ceb3bf..0d173cb2f 100644 --- a/include/verilatedos.h +++ b/include/verilatedos.h @@ -619,6 +619,8 @@ inline double VL_ROUND(double n) { # define VL_CPU_RELAX() asm volatile("lr 0,0" ::: "memory") #elif defined(__sparc__) # define VL_CPU_RELAX() asm volatile("rd %%ccr, %%g0" ::: "memory") +#elif defined(__wasm__) // Wasm has no spin-wait hint, and no inline asm +# define VL_CPU_RELAX() #elif defined(VL_IGNORE_UNKNOWN_ARCH) # define VL_CPU_RELAX() #else diff --git a/include/verilatedos_c.h b/include/verilatedos_c.h index d329da089..6a900b5a9 100644 --- a/include/verilatedos_c.h +++ b/include/verilatedos_c.h @@ -65,7 +65,7 @@ double DeltaCpuTime::gettime() VL_MT_SAFE { | static_cast(lpUserTime.dwHighDateTime) << 32ULL) * 1e-7; } -#else +#elif !defined(__wasi__) // WASI has no CLOCK_PROCESS_CPUTIME_ID // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) timespec ts; if (0 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) // MT-Safe // LCOV_EXCL_BR_LINE diff --git a/src/V3Os.cpp b/src/V3Os.cpp index 7891dd102..62e262779 100644 --- a/src/V3Os.cpp +++ b/src/V3Os.cpp @@ -82,7 +82,9 @@ VL_DEFINE_DEBUG_FUNCTIONS; # endif #else # include -# include // Needed on FreeBSD for WIFEXITED +# ifndef __wasi__ +# include // Needed on FreeBSD for WIFEXITED +# endif # include // usleep #endif // clang-format on @@ -445,6 +447,12 @@ string V3Os::trueRandom(size_t size) VL_MT_SAFE { if (VL_UNCOVERABLE(!BCRYPT_SUCCESS(hr))) { v3fatal("Could not acquire random data. Try specifying a key instead."); // LCOV_EXCL_LINE } +#elif defined(__wasi__) + // WASI has no /dev/urandom. getentropy() rejects sizes over 256 bytes, + // which is more than any caller asks for, and errors rather than truncates. + if (VL_UNCOVERABLE(getentropy(data, size))) { + v3fatal("Could not acquire random data. Try specifying a key instead."); // LCOV_EXCL_LINE + } #else std::ifstream is{"/dev/urandom", std::ios::in | std::ios::binary}; // This read uses the size of the buffer. @@ -492,6 +500,11 @@ void V3Os::u_sleep(int64_t usec) { // METHODS (sub command) int V3Os::system(const string& command) { +#ifdef __wasi__ + // WASI has no process spawning + v3fatal("Running subcommands is not supported on this platform: " << command); + return -1; +#else UINFO(1, "Running system: " << command); const int ret = ::system(command.c_str()); if (VL_UNCOVERABLE(ret == -1)) { @@ -504,6 +517,7 @@ int V3Os::system(const string& command) { UINFO(1, command << " returned exit code of " << exit_code); UASSERT(exit_code >= 0, "exit code must not be negative"); return exit_code; +#endif } void V3Os::selfTest() { diff --git a/test_regress/t/t_dist_warn_coverage.py b/test_regress/t/t_dist_warn_coverage.py index 99c84fcae..ffbd902c9 100755 --- a/test_regress/t/t_dist_warn_coverage.py +++ b/test_regress/t/t_dist_warn_coverage.py @@ -25,6 +25,7 @@ for s in [ 'Define or directive not defined: `', # Instead V3ParseImp will warn 'Expecting define formal arguments. Found:', # Instead define syntax error 'Need $SYSTEMC_INCLUDE in environment or when Verilator configured,', # Would need ./configure + 'Running subcommands is not supported on this platform:', # Only hit on e.g. wasm 'Syntax error: Range \':\', \'+:\' etc are not allowed in the instance', # Instead get syntax error 'dynamic new() not expected in this context (expected under an assign)', # Instead get syntax error 'exited with', # Is hit; driver.py filters out