Skip to content

Prevent potential overflow #4372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Septa2112
Copy link
Contributor

PR #4300 introduced the rationale for validating heap_type.

This patch moves the validation before the computation of type1 to prevent potential overflow.

PR bytecodealliance#4300 introduced the rationale for validating heap_type.
This patch moves the validation before the computation of
type1 to prevent potential overflow.
@Septa2112
Copy link
Contributor Author

Further improved the heap type check logic:

  1. Moved the heap type validation ahead of the if (!is_byte_a_type...) check. Depending on whether heap_type >= 0, different methods are used to get type1.

  2. The type_index check is now performed only when heap_type >= 0, to ensure that type1 is correctly retrieved in this case.

  3. Why not checking type_index when heap_type < 0? Because in the current WAMR implementation, if wasm_is_valid_heap_type(heap_type) is true, then the condition if (!is_byte_a_type(type1) || wasm_is_type_multi_byte_type(type1)) will always be false.

goto fail;
}
type1 = (uint8)((int32)0x80 + heap_type);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

better have a comment @l867 to hint that if heap_type < 0, if (!is_byte_a_type(type1) || wasm_is_type_multi_byte_type(type1)) will be false.

goto fail;
}
type1 = (uint8)((int32)0x80 + heap_type);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another suggestion is to merge those if...else.. blocks, like:

if (heap_type >= 0) {
    if (!check_type_index(module, module->type_count, heap_type,
                          error_buf, error_buf_size)) {
        goto fail;
    }
    wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
                                 true, heap_type);
    if (!push_const_expr_stack(&const_expr_ctx, flag,
                               cur_ref_type.ref_type,
                               &cur_ref_type, 0, &cur_value,
                               error_buf, error_buf_size))
        goto fail;

}
else {
    if (!wasm_is_valid_heap_type(heap_type)) {
        set_error_buf_v(error_buf, error_buf_size,
                        "unknown type %d", heap_type);
        goto fail;
    }
    type1 = (uint8)((int32)0x80 + heap_type);
    if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
                               NULL, 0, &cur_value, error_buf,
                               error_buf_size))
        goto fail;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants