Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions src/passes/SafeHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,20 @@ struct SafeHeap : public Pass {
auto func = Builder::makeFunction(name, funcSig, {indexType});
Builder builder(*module);
auto* block = builder.makeBlock();
// stash the sum of the pointer (0) and the size (1) in a local (2)
block->list.push_back(builder.makeLocalSet(
2,
builder.makeBinary(memory->is64() ? AddInt64 : AddInt32,
builder.makeLocalGet(0, indexType),
builder.makeLocalGet(1, indexType))));
// check for an overflow when adding the pointer and the size, using the
// rule that for any unsigned x and y,
// x + y < x <=> x + y overflows
block->list.push_back(
builder.makeIf(builder.makeBinary(memory->is64() ? LtUInt64 : LtUInt32,
builder.makeLocalGet(2, indexType),
builder.makeLocalGet(0, indexType)),
builder.makeCall(segfault, {}, Type::none)));
// check for reading past valid memory: if pointer + offset + bytes
block->list.push_back(makeBoundsCheck(style.type,
builder,
Expand Down
Loading