Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Array out of bounds access #8239

Description

@aseyboldt

I reported this in #8133, but I think it got lost in my confusion about scalar values in mxnet. But I think this deserves a separate issue.

Unlike numpy, mxnet doesn't support rank 0 arrays, but uses an empty tuple to represent unknown shapes. mx.nd still allows the creation of rank 0 arrays however. This leads to confusion during shape inference and allows out of bounds memory reads and writes:

a = mx.sym.var('a')
b = mx.sym.var('b')
c = a + b

a_ = mx.nd.ones(()) # Has size 1, but bind assumes it has shape (5,)
b_ = mx.nd.ones(5)
func = c.bind(mx.cpu(), {'a': a_, 'b': b_})
func.forward()
func.outputs[0].asnumpy()
array([  2.00000000e+00,   2.52435490e-29,   8.84247875e+05,
         1.08446609e-19,  -1.92860745e-26], dtype=float32)

We can write to invalid memory (and crash the process or potentially get arbitrary code execution) by providing a gradient buffer with the wrong shape

a = mx.sym.var('a')
c = mx.sym.MakeLoss(a)

grad = mx.nd.ones(())
a_ = mx.nd.ones(500)
func = c.bind(mx.cpu(), {'a': a_}, args_grad={'a': grad})
func.forward()
func.backward()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions