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
Attempt 3
  • Loading branch information
Hood committed Apr 15, 2021
commit 3267a8e88711488364b3399f96072b7b9d61d467
55 changes: 47 additions & 8 deletions packages/numpy/patches/0001-temp.patch
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
From 3f777056afeac1802badb1ed475e6e898b331327 Mon Sep 17 00:00:00 2001
From 9acf1ade730da70f73bbdef4cf4a0ab37be21e41 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 | 25 +++++++++++++++-----
2 files changed, 22 insertions(+), 8 deletions(-)
numpy/core/src/multiarray/conversion_utils.c | 5 +-
numpy/core/src/multiarray/iterators.c | 56 ++++++++++++++++----
2 files changed, 48 insertions(+), 13 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,10 +33,10 @@ index 52cb58726..5467f4d78 100644
}
return nd;
diff --git a/numpy/core/src/multiarray/iterators.c b/numpy/core/src/multiarray/iterators.c
index 9da811f69..3e81aee91 100644
index 9da811f69..291c4c0af 100644
--- a/numpy/core/src/multiarray/iterators.c
+++ b/numpy/core/src/multiarray/iterators.c
@@ -1153,6 +1153,17 @@ NPY_NO_EXPORT PyTypeObject PyArrayIter_Type = {
@@ -1153,6 +1153,37 @@ NPY_NO_EXPORT PyTypeObject PyArrayIter_Type = {

/** END of Array Iterator **/

Expand All @@ -50,13 +50,36 @@ index 9da811f69..3e81aee91 100644
+ }
+ return nd;
+}
+
+__attribute__((noinline))
+NPY_NO_EXPORT
+void
+write_to_ptr(void *ptr, void* val){
+ *ptr = val;
+}
+
+__attribute__((noinline))
+NPY_NO_EXPORT
+void *
+read_from_ptr(void *ptr){
+ return *ptr;
+}
+
+#define TO_VOID(x) ((void*)(x))
+#define TO_INTP(x) ((npy_intp)(x))
+#define WRITE(x, val) write_to_ptr(TO_VOID(x), TO_VOID(val))
+#define READ(x) read_from_ptr(TO_VOID(x))
+#define READ_INTP(x, offset) TO_INTP(READ(x, offset))
+
/* Adjust dimensionality and strides for index object iterators
--- i.e. broadcast
*/
@@ -1163,18 +1174,20 @@ PyArray_Broadcast(PyArrayMultiIterObject *mit)
@@ -1161,31 +1192,34 @@ NPY_NO_EXPORT int
PyArray_Broadcast(PyArrayMultiIterObject *mit)
{
int i, nd, k, j;
npy_intp tmp;
- npy_intp tmp;
+ npy_intp tmp, tmp2;
PyArrayIterObject *it;
+ PyArrayIterObject **it_ptr;

Expand All @@ -81,6 +104,22 @@ index 9da811f69..3e81aee91 100644
/* This prepends 1 to shapes not already equal to nd */
k = i + PyArray_NDIM(it->ao) - nd;
if (k >= 0) {
- tmp = PyArray_DIMS(it->ao)[k];
+ tmp = READ_INTP(&PyArray_DIMS(it->ao)[k]);
if (tmp == 1) {
continue;
}
- if (mit->dimensions[i] == 1) {
- mit->dimensions[i] = tmp;
+ tmp2 = READ_INTP(&mit->dimensions[i])
+ if (tmp2 == 1) {
+ WRITE(&mit->dimensions[i], tmp)
}
- else if (mit->dimensions[i] != tmp) {
+ else if (tmp2 != tmp) {
PyErr_SetString(PyExc_ValueError,
"shape mismatch: objects" \
" cannot be broadcast" \
--
2.17.1