Skip to content

Commit

Permalink
done with 09 and everything
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Apr 6, 2019
1 parent a85d850 commit d37f0a4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 93 deletions.
136 changes: 43 additions & 93 deletions 09-jailbreaking-python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not allowed to change it: 'bytes' object does not support item assignment\n",
"a = [ 72 101 108 108 111 44 32 119 111 114 108 100 33]\n",
"a.view('S1') = [b'H' b'e' b'l' b'l' b'o' b',' b' ' b'w' b'o' b'r' b'l' b'd' b'!']\n",
"hello = b'HellEVILorld!'\n"
]
}
],
"outputs": [],
"source": [
"# Let's break Python!\n",
"\n",
Expand All @@ -67,18 +56,9 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"case 1: False\n",
"case 2: True\n"
]
}
],
"outputs": [],
"source": [
"# See how evil this is:\n",
"\n",
Expand Down Expand Up @@ -112,18 +92,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = [ 2 0 0 0 0 0 0 0 32 22 161 221 72 86 0 0 1 0\n",
" 0 0 0 0 0 0 57 48 0 0]\n"
]
}
],
"outputs": [],
"source": [
"x = 12345\n",
"\n",
Expand All @@ -140,24 +111,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = [ 2 0 0 0 0 0 0 0 32 226 160 221 72 86 0 0 10 0\n",
" 0 0 0 0 0 0 79 79 192 9 38 9 212 73 228 171 183 242\n",
" 166 127 0 0 0 0 0 0 0 0 0 0 72 101 121 32 116 104\n",
" 101 114 101 46 0]\n",
"\n",
"a[-11:] = [ 72 101 121 32 116 104 101 114 101 46 0]\n",
"\n",
"a[-11:].tostring() = b'Hey there.\\x00'\n"
]
}
],
"outputs": [],
"source": [
"# Now for a string.\n",
"\n",
Expand All @@ -173,23 +129,9 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ptr = <__main__.PyObject object at 0x7fa6f2b67d08>\n",
"\n",
"Before:\n",
"ptr.ob_refcnt = 2 sys.getrefcount(hello) = 3\n",
"\n",
"After:\n",
"ptr.ob_refcnt = 1002 sys.getrefcount(hello) = 1003\n"
]
}
],
"outputs": [],
"source": [
"# The snake eats its own tail: reading PyObject refcount without changing it.\n",
"class PyObject(ctypes.Structure): pass\n",
Expand Down Expand Up @@ -221,17 +163,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = [-511.5 -510.5 -509.5 ... 509.5 510.5 511.5]\n"
]
}
],
"outputs": [],
"source": [
"# Suppose you're on a supercomputer with Non-Uniform Memory Access (NUMA) and you want Numpy arrays.\n",
"\n",
Expand All @@ -250,7 +184,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -287,17 +221,9 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"103.04\n"
]
}
],
"outputs": [],
"source": [
"# It's easy to get started with pybind11, and it uses a modern subset of C++.\n",
"import os\n",
Expand All @@ -321,11 +247,35 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": []
"source": [
"#### You can do the reverse: compile Python, pass as function pointer to C\n",
"\n",
"<center><img src=\"img/writing-c-functions-in-python.png\" width=\"90%\"></center>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Numpy ufuncs are a C function protocol, overridden by compiled code\n",
"\n",
"<center><img src=\"img/creating-your-own-ufunc.png\" width=\"90%\"></center>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br><br><br><br>\n",
"\n",
"# Day 3 Homework\n",
"\n",
"Did you bring a project that you'd like to accelerate, rework in Numpy/awkward, or bind to C++?\n",
"\n",
"<br><br><br><br>"
]
}
],
"metadata": {
Expand Down
Binary file added img/creating-your-own-ufunc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/writing-c-functions-in-python.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d37f0a4

Please sign in to comment.