Skip to content

Add checks for overflow #825

Description

@dsharlet

We should add error checking for overflow in slinky pipeline evaluation.

Adding checked arithmetic on the expression evaluation and buffer operations (e.g. for_each_element) could be expensive due to adding error handling codepaths.

Instead, I think we can ensure that there is no overflow when constructing buffers, and when calling pipelines. We can:

  1. Add a validate_buffer(const raw_buffer&) function that verifies that both the min and max address accessible by the buffer do not overflow. This will probably be similar to this function:
    std::size_t alloc_size(std::size_t rank, std::size_t elem_size, const dim* dims) {
    index_t flat_min = 0;
    index_t flat_max = 0;
    for (std::size_t i = 0; i < rank; ++i) {
    if (dims[i].stride() == 0) continue;
    index_t extent = alloc_extent(dims[i]);
    assert(extent >= 0);
    if (extent == 0) return 0;
    flat_min += (extent - 1) * std::min<index_t>(0, dims[i].stride());
    flat_max += (extent - 1) * std::max<index_t>(0, dims[i].stride());
    }
    return flat_max - flat_min + elem_size;
    }
    .
  2. Call validate_buffer in evalaute for make_buffer and allocate. These evaluators can return an error code. I think that all other buffer operations cannot cause overflow to occur.
  3. Add a call expression for validate_buffer to
    enum class intrinsic {
    and then generate checks for validate_buffer when constructing pipelines (probably here:
    void check_buffer(const buffer_expr_ptr& b, std::vector<stmt>& checks) {
    )

We should test this in https://github.com/dsharlet/slinky/blob/main/slinky/builder/test/checks.cc (at least).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions