go-python / gpython

gpython is a python interpreter written in go "batteries not included"
BSD 3-Clause "New" or "Revised" License
870 stars 95 forks source link

'__import__' takes incorrect arguments crashing gpython #204

Open xiaxinmeng opened 1 year ago

xiaxinmeng commented 1 year ago

'import' takes incorrect arguments and it crashes gpython. we test the following code on Gopherjs and Go/wasm online.

test.py

__import__( 'pkg', {}, {}, [''] )

Output on GO/wasm(https://gpython.org/?wasm):


Gpython 3.4.0 running in your browser with go/wasm
>>> __import__( 'pkg', {}, {}, [''] )
panic: interface conversion: py.Object is *py.List, not py.Tuple
goroutine 5 [running]:
github.com/go-python/gpython/py.BuiltinImport(0x0, 0x0, 0xc040420, 0x4, 0x4, 0x0, 0xc062db0, 0x0, 0x0, 0x0, ...)
    /home/ncw/go/src/github.com/go-python/gpython/py/import.go:394 +0x18
github.com/go-python/gpython/vm.callInternal(0x9d360, 0xc02ab80, 0xc040420, 0x4, 0x4, 0x0, 0xc0a40b0, 0x0, 0x0, 0x0, ...)
    /home/ncw/go/src/github.com/go-python/gpython/vm/eval.go:1595 +0x25
github.com/go-python/gpython/vm.(*Vm).Call(0xc074540, 0x4, 0x0, 0x0, 0x0, 0x0, 0xc05ea60, 0xc040410)
    /home/ncw/go/src/github.com/go-python/gpython/vm/eval.go:1689 +0x4e
github.com/go-python/gpython/vm.do_CALL_FUNCTION(0xc074540, 0x4, 0x0, 0x0)
    /home/ncw/go/src/github.com/go-python/gpython/vm/eval.go:1428 +0x2
github.com/go-python/gpython/vm.RunFrame(0xc0a40b0, 0x0, 0xc07bd18, 0x0, 0x0)
    /home/ncw/go/src/github.com/go-python/gpython/vm/eval.go:1785 +0x3f
github.com/go-python/gpython/vm.EvalCodeEx(0xc04c200, 0xc062db0, 0xc062db0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /home/ncw/go/src/github.com/go-python/gpython/vm/eval.go:2162 +0xa3
github.com/go-python/gpython/vm.Run(0xc062db0, 0xc062db0, 0xc04c200, 0x0, 0x0, 0x0, 0x0, 0x1, 0x9d240, 0xc04c200)
    /home/ncw/go/src/github.com/go-python/gpython/vm/eval.go:2182 +0x2
github.com/go-python/gpython/repl.(*REPL).Run(0xc09a6c0, 0xc038330, 0x21)
    /home/ncw/go/src/github.com/go-python/gpython/repl/repl.go:99 +0x20
main.main.func1(0xc01e3f0, 0x2, 0x2)
    /home/ncw/go/src/github.com/go-python/gpython/repl/web/main.go:82 +0x4
syscall/js.callbackLoop()
    /opt/go/go1.11/src/syscall/js/callback.go:116 +0x7
created by syscall/js.NewCallback.func1
    /opt/go/go1.11/src/syscall/js/callback.go:40 +0x2
...

Output on Gopherjs (https://gpython.org/)

>>> __import__( 'pkg', {}, {}, [''] )
[USER]: https://gpython.org/gpython.js: interface conversion: interface is *py.List, not py.Tuple
$callDeferred@https://gpython.org/gpython.js:4:22511
$panic@https://gpython.org/gpython.js:4:22957
$assertType@https://gpython.org/gpython.js:4:21361
EP@https://gpython.org/gpython.js:41:219420
DN@https://gpython.org/gpython.js:44:81763
EE.ptr.prototype.Call@https://gpython.org/gpython.js:44:87109
DC@https://gpython.org/gpython.js:44:77098
DO@https://gpython.org/gpython.js:44:91592
DU@https://gpython.org/gpython.js:44:107450
DW@https://gpython.org/gpython.js:44:109015
G.ptr.prototype.Run@https://gpython.org/gpython.js:56:3575
$b@https://gpython.org/gpython.js:60:3908
$b@https://gpython.org/gpython.js:59:2625
r@https://gpython.org/gpython.js:4:23443
$runScheduled@https://gpython.org/gpython.js:4:24007
$schedule@https://gpython.org/gpython.js:4:24184
$go@https://gpython.org/gpython.js:4:23907
I/$packages["github.com/gopherjs/gopherwasm/js"]<@https://gpython.org/gpython.js:59:2240
$externalizeFunction/e.$externalizeWrapper@https://gpython.org/gpython.js:4:28925
a@https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/1.23.2/js/jquery.terminal.min.js:40:82615
k@https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/1.23.2/js/jquery.terminal.min.js:40:83463
ENTER@https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/1.23.2/js/jquery.terminal.min.js:40:14915
$e@https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/1.23.2/js/jquery.terminal.min.js:40:28362
dispatch@https://code.jquery.com/jquery-latest.js:4641:9
add/elemData.handle@https://code.jquery.com/jquery-latest.js:4309:28
...
ncw commented 1 year ago

This looks like unchecked type assertions...

https://github.com/go-python/gpython/blob/6f8e06a4660709ab44398d8b1a18738aa407b1c3/py/import.go#L356

In fact we should probably just be catching these and turning them into a TypeError as there are loads of them in the code base.

I see in 4b9bdd5ab64c6869fdbeb5577c4e2665839787b5 we removed those checks. Maybe we need to put some of them back. Perhaps just the one in CALL_FUNCTION