Skip to content
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

Fix memory access out of bounds in numpy + chrome 89 #1474

Merged
merged 23 commits into from
Apr 17, 2021
Merged
Prev Previous commit
Next Next commit
Another try at patching PyArray_Broadcast
  • Loading branch information
Hood committed Apr 15, 2021
commit 3b50bd9e4447a186b4e8cc8e6b711114a0bdd617
54 changes: 36 additions & 18 deletions packages/numpy/patches/0001-temp.patch
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
From d2f7f5dc409a93deaf4ae7b8f701e1c2689b1ee6 Mon Sep 17 00:00:00 2001
From faa38a034ce859b83b8a7f3c80822acca0cf332f Mon Sep 17 00:00:00 2001
From: Hood <hood@mit.edu>
Date: Fri, 2 Apr 2021 14:24:24 -0700
Subject: [PATCH] temp

---
numpy/core/src/multiarray/conversion_utils.c | 5 +++--
numpy/core/src/multiarray/iterators.c | 12 ++++++++----
2 files changed, 11 insertions(+), 6 deletions(-)
numpy/core/src/multiarray/conversion_utils.c | 5 ++--
numpy/core/src/multiarray/iterators.c | 26 +++++++++++++++-----
2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c
index 52cb58726..5467f4d78 100644
Expand All @@ -33,37 +33,55 @@ index 52cb58726..5467f4d78 100644
}
return nd;
diff --git a/numpy/core/src/multiarray/iterators.c b/numpy/core/src/multiarray/iterators.c
index 9da811f69..9dcc32442 100644
index 9da811f69..3bfd64505 100644
--- a/numpy/core/src/multiarray/iterators.c
+++ b/numpy/core/src/multiarray/iterators.c
@@ -1163,6 +1163,7 @@ PyArray_Broadcast(PyArrayMultiIterObject *mit)
@@ -1153,6 +1153,17 @@ NPY_NO_EXPORT PyTypeObject PyArrayIter_Type = {

/** END of Array Iterator **/

+__attribute__((noinline))
+NPY_NO_EXPORT int
+PyArray_Broadcast_GetNd(PyArrayMultiIterObject *mit){
+ int i, nd;
+ /* Discover the broadcast number of dimensions */
+ for (i = 0, nd = 0; i < mit->numiter; i++) {
+ nd = PyArray_MAX(nd, PyArray_NDIM(mit->iters[i]->ao));
+ }
+ return nd
+}
+
/* Adjust dimensionality and strides for index object iterators
--- i.e. broadcast
*/
@@ -1163,18 +1174,21 @@ PyArray_Broadcast(PyArrayMultiIterObject *mit)
int i, nd, k, j;
npy_intp tmp;
PyArrayIterObject *it;
+ PyArrayIterObject *it_ptr;
+ PyArrayObject *ao;

/* Discover the broadcast number of dimensions */
for (i = 0, nd = 0; i < mit->numiter; i++) {
@@ -1173,12 +1174,15 @@ PyArray_Broadcast(PyArrayMultiIterObject *mit)
- /* Discover the broadcast number of dimensions */
- for (i = 0, nd = 0; i < mit->numiter; i++) {
- nd = PyArray_MAX(nd, PyArray_NDIM(mit->iters[i]->ao));
- }
+ nd = PyArray_Broadcast_GetNd(mit);
mit->nd = nd;

/* Discover the broadcast shape in each dimension */
for (i = 0; i < nd; i++) {
mit->dimensions[i] = 1;
- for (j = 0; j < mit->numiter; j++) {
- it = mit->iters[j];
+ }
+
+ for (j = 0; j < mit->numiter; j++) {
+ ao = mit->iters[j]->ao;
+ it_ptr = &mit->iters[0];
+ for (j = 0; j < mit->numiter; j++, it_ptr++) {
+ it = *it_ptr;
+ for (i = 0; i < nd; i++) {
/* This prepends 1 to shapes not already equal to nd */
- k = i + PyArray_NDIM(it->ao) - nd;
+ k = i + PyArray_NDIM(ao) - nd;
k = i + PyArray_NDIM(it->ao) - nd;
if (k >= 0) {
- tmp = PyArray_DIMS(it->ao)[k];
+ tmp = PyArray_DIMS(ao)[k];
if (tmp == 1) {
continue;
}
--
2.17.1