Skip to content

Commit

Permalink
Merge latest tests from spec repos.
Browse files Browse the repository at this point in the history
- Implement thread and wait commands, and the either result predicate from the threads proposal.
- Make cloneCompartment gracefully handle OOM.
- Make getMemoryType/getTableType return the current size as the minimum rather than the minimum size at creation.
  • Loading branch information
AndrewScheidecker committed Oct 8, 2021
1 parent 81763a0 commit c122710
Show file tree
Hide file tree
Showing 39 changed files with 1,641 additions and 926 deletions.
137 changes: 122 additions & 15 deletions Include/WAVM/WASTParse/TestScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace WAVM { namespace WAST {
assert_malformed,
assert_unlinkable,
benchmark,
thread,
wait,
};
const Type type;
const TextFileLocus locus;
Expand Down Expand Up @@ -171,21 +173,23 @@ namespace WAVM { namespace WAST {
{
enum class Type
{
i32,
i64,
i8x16,
i16x8,
i32x4,
i64x2,

f32,
f64,
f32x4,
f64x2,

specificObject,
anyFunction,
nullref,
i32_const,
i64_const,
i8x16_const,
i16x8_const,
i32x4_const,
i64x2_const,

f32_const,
f64_const,
f32x4_const,
f64x2_const,

ref_extern,
ref_func,
ref_null,

either,
};

Type type;
Expand All @@ -206,7 +210,82 @@ namespace WAVM { namespace WAST {

Runtime::Object* object;
IR::ReferenceType nullReferenceType;

std::vector<std::shared_ptr<ResultSet>> alternatives;
};

ResultSet() : type(Type::i32_const), i32(0) {}
ResultSet(const ResultSet& copyee) { copyFrom(copyee); }
ResultSet(ResultSet&& movee) { moveFrom(std::move(movee)); }

ResultSet& operator=(const ResultSet& copyee)
{
this->~ResultSet();
copyFrom(copyee);
return *this;
}
ResultSet& operator=(ResultSet&& movee)
{
this->~ResultSet();
moveFrom(std::move(movee));
return *this;
}

~ResultSet()
{
if(type == Type::either) { alternatives.~vector<std::shared_ptr<ResultSet>>(); }
}

private:
void copyFrom(const ResultSet& copyee)
{
type = copyee.type;
switch(type)
{
case Type::i32_const: i32 = copyee.i32; break;
case Type::i64_const: i64 = copyee.i64; break;
case Type::i8x16_const: memcpy(i8x16, copyee.i8x16, sizeof(i8x16)); break;
case Type::i16x8_const: memcpy(i16x8, copyee.i16x8, sizeof(i16x8)); break;
case Type::i32x4_const: memcpy(i32x4, copyee.i32x4, sizeof(i32x4)); break;
case Type::i64x2_const: memcpy(i64x2, copyee.i64x2, sizeof(i64x2)); break;
case Type::f32_const: f32 = copyee.f32; break;
case Type::f64_const: f64 = copyee.f64; break;
case Type::f32x4_const: memcpy(f32x4, copyee.f32x4, sizeof(f32x4)); break;
case Type::f64x2_const: memcpy(f64x2, copyee.f64x2, sizeof(f64x2)); break;
case Type::ref_extern: object = copyee.object; break;
case Type::ref_func: break;
case Type::ref_null: nullReferenceType = copyee.nullReferenceType; break;
case Type::either:
new(&alternatives) std::vector<std::shared_ptr<ResultSet>>(copyee.alternatives);
break;
default: WAVM_UNREACHABLE();
};
}
void moveFrom(ResultSet&& movee)
{
type = movee.type;
switch(type)
{
case Type::i32_const: i32 = movee.i32; break;
case Type::i64_const: i64 = movee.i64; break;
case Type::i8x16_const: memcpy(i8x16, movee.i8x16, sizeof(i8x16)); break;
case Type::i16x8_const: memcpy(i16x8, movee.i16x8, sizeof(i16x8)); break;
case Type::i32x4_const: memcpy(i32x4, movee.i32x4, sizeof(i32x4)); break;
case Type::i64x2_const: memcpy(i64x2, movee.i64x2, sizeof(i64x2)); break;
case Type::f32_const: f32 = movee.f32; break;
case Type::f64_const: f64 = movee.f64; break;
case Type::f32x4_const: memcpy(f32x4, movee.f32x4, sizeof(f32x4)); break;
case Type::f64x2_const: memcpy(f64x2, movee.f64x2, sizeof(f64x2)); break;
case Type::ref_extern: object = movee.object; break;
case Type::ref_func: break;
case Type::ref_null: nullReferenceType = movee.nullReferenceType; break;
case Type::either:
new(&alternatives)
std::vector<std::shared_ptr<ResultSet>>(std::move(movee.alternatives));
break;
default: WAVM_UNREACHABLE();
};
}
};

struct AssertReturnCommand : Command
Expand Down Expand Up @@ -334,4 +413,32 @@ namespace WAVM { namespace WAST {
{
}
};

struct ThreadCommand : Command
{
std::string threadName;
std::vector<std::string> sharedModuleInternalNames;
std::vector<std::unique_ptr<Command>> commands;

ThreadCommand(TextFileLocus&& inLocus,
std::string&& inThreadName,
std::vector<std::string> inSharedModuleInternalNames,
std::vector<std::unique_ptr<Command>>&& inCommands)
: Command(Command::thread, std::move(inLocus))
, threadName(inThreadName)
, sharedModuleInternalNames(inSharedModuleInternalNames)
, commands(std::move(inCommands))
{
}
};

struct WaitCommand : Command
{
std::string threadName;

WaitCommand(TextFileLocus&& inLocus, std::string&& inThreadName)
: Command(Command::wait, std::move(inLocus)), threadName(inThreadName)
{
}
};
}}
29 changes: 22 additions & 7 deletions Lib/Platform/POSIX/MemoryPOSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ U8* Platform::allocateVirtualPages(Uptr numPages)
void* result = mmap(nullptr, numBytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(result == MAP_FAILED)
{
fprintf(stderr,
"mmap(0, %" WAVM_PRIuPTR
", PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) failed! errno=%s\n",
numBytes,
strerror(errno));
dumpErrorCallStack(0);
if(errno != ENOMEM)
{
fprintf(stderr,
"mmap(0, %" WAVM_PRIuPTR
", PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) failed! errno=%s\n",
numBytes,
strerror(errno));
dumpErrorCallStack(0);
}
return nullptr;
}
return (U8*)result;
Expand All @@ -80,7 +83,19 @@ U8* Platform::allocateAlignedVirtualPages(Uptr numPages,
const Uptr alignmentBytes = 1ull << alignmentLog2;
U8* unalignedBaseAddress = (U8*)mmap(
nullptr, numBytes + alignmentBytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(unalignedBaseAddress == MAP_FAILED) { return nullptr; }
if(unalignedBaseAddress == MAP_FAILED)
{
if(errno != ENOMEM)
{
fprintf(stderr,
"mmap(0, %" WAVM_PRIuPTR
", PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) failed! errno=%s\n",
numBytes + alignmentBytes,
strerror(errno));
dumpErrorCallStack(0);
}
return nullptr;
}

const Uptr address = reinterpret_cast<Uptr>(unalignedBaseAddress);
const Uptr alignedAddress = (address + alignmentBytes - 1) & ~(alignmentBytes - 1);
Expand Down
4 changes: 2 additions & 2 deletions Lib/Runtime/Atomics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ WAVM_DEFINE_INTRINSIC_FUNCTION(wavmIntrinsicsAtomics,
Memory* memory = getMemoryFromRuntimeData(contextRuntimeData, memoryId);

// Throw a waitOnUnsharedMemory exception if the memory is not shared.
if(!memory->type.isShared) { throwException(ExceptionTypes::waitOnUnsharedMemory, {memory}); }
if(!memory->isShared) { throwException(ExceptionTypes::waitOnUnsharedMemory, {memory}); }

// Assume that the caller has validated the alignment.
WAVM_ASSERT(!(address & 3));
Expand All @@ -257,7 +257,7 @@ WAVM_DEFINE_INTRINSIC_FUNCTION(wavmIntrinsicsAtomics,
Memory* memory = getMemoryFromRuntimeData(contextRuntimeData, memoryId);

// Throw a waitOnUnsharedMemory exception if the memory is not shared.
if(!memory->type.isShared) { throwException(ExceptionTypes::waitOnUnsharedMemory, {memory}); }
if(!memory->isShared) { throwException(ExceptionTypes::waitOnUnsharedMemory, {memory}); }

// Assume that the caller has validated the alignment.
WAVM_ASSERT(!(address & 7));
Expand Down
Loading

0 comments on commit c122710

Please sign in to comment.