near / assemblyscript-json

JSON encoder / decoder for AssemblyScript
MIT License
173 stars 26 forks source link

Memory allocation crash whilst parsing JSON. #322

Open FrankTaylorLieder opened 2 years ago

FrankTaylorLieder commented 2 years ago

Hi. I am building an Envoy extension and looking at using AssemblyScript to produce WASM.

I can't get this library running in my extension. When trying to parse some JSON, I see the following stack:

proxy_1 | [2021-12-13 16:10:14.377][8][critical][wasm] [source/extensions/common/wasm/context.cc:1231] wasm log Extension: at: ~lib/rt/tlsf.ts(580:3) proxy_1 | [2021-12-13 16:10:14.378][8][error][wasm] [source/extensions/common/wasm/wasm_vm.cc:38] Function: proxy_on_vm_start failed: Uncaught RuntimeError: unreachable proxy_1 | Proxy-Wasm plugin in-VM backtrace: proxy_1 | 0: 0x3408 - ~lib/rt/tlsf/checkUsedBlock proxy_1 | 1: 0x3509 - ~lib/rt/tlsf/__realloc proxy_1 | 2: 0x391a - ~lib/array/ensureSize proxy_1 | 3: 0x5678 - ~lib/array/Array<~lib/assemblyscript-json/JSON/Value>#push proxy_1 | 4: 0x5a6c - ~lib/assemblyscript-json/JSON/Handler#addValue proxy_1 | 5: 0x5b29 - ~lib/assemblyscript-json/JSON/Handler#pushObject proxy_1 | 6: 0x632f - ~lib/assemblyscript-json/decoder/JSONDecoder<~lib/assemblyscript-json/JSON/Handler>#parseObject proxy_1 | 7: 0x74c8 - ~lib/assemblyscript-json/decoder/JSONDecoder<~lib/assemblyscript-json/JSON/Handler>#parseValue proxy_1 | 8: 0x754a - ~lib/assemblyscript-json/decoder/JSONDecoder<~lib/assemblyscript-json/JSON/Handler>#deserialize proxy_1 | 9: 0x75ba - ~lib/assemblyscript-json/JSON/_JSON.parse<~lib/string/String> proxy_1 | [2021-12-13 16:10:14.378][8][error][wasm] [source/extensions/common/wasm/wasm.cc:119] Wasm VM failed Failed to start base Wasm

The AS code that provokes this is:

let jsonObj: JSON.Obj = <JSON.Obj>(JSON.parse('{"hello": "world", "o": { "o1": 42 }}'));

I am using the following versions:

"assemblyscript": "^0.14.9" "assemblyscript-json": "^1.1.0"

Any ideas on where I start to debug this?

willemneal commented 2 years ago

You're using a very old version of the AS compiler. That's where I would start. Is there a reason it's so old?

FrankTaylorLieder commented 2 years ago

You're using a very old version of the AS compiler. That's where I would start. Is there a reason it's so old?

Thanks for getting back quickly.

After posting I realised this is an old version. This version came as a dependency from: https://github.com/solo-io/proxy-runtime/blob/v0.1.10/package.json

I've manually updated my local compiler to: (any newer and I start seeing errors during compile from proxy-runtime)

"assemblyscript": "0.17.x"

This crashes at the same point, but with a different error/stack:

proxy_1 | [2021-12-13 17:42:50.835][8][critical][wasm] [source/extensions/common/wasm/context.cc:1231] wasm log ZTService zt_service_vm: Element type must be nullable if array is holey at: ~lib/array.ts(108:40) proxy_1 | [2021-12-13 17:42:50.835][8][error][wasm] [source/extensions/common/wasm/wasm_vm.cc:38] Function: proxy_on_vm_start failed: Uncaught RuntimeError: unreachable proxy_1 | Proxy-Wasm plugin in-VM backtrace: proxy_1 | 0: 0x57a4 - ~lib/array/Array<~lib/assemblyscript-json/JSON/Value>#__get proxy_1 | 1: 0x57be - ~lib/assemblyscript-json/JSON/Handler#get:peek proxy_1 | 2: 0x5b2d - ~lib/assemblyscript-json/JSON/Handler#addValue proxy_1 | 3: 0x5bda - ~lib/assemblyscript-json/JSON/Handler#pushObject proxy_1 | 4: 0x63e0 - ~lib/assemblyscript-json/decoder/JSONDecoder<~lib/assemblyscript-json/JSON/Handler>#parseObject proxy_1 | 5: 0x7588 - ~lib/assemblyscript-json/decoder/JSONDecoder<~lib/assemblyscript-json/JSON/Handler>#parseValue proxy_1 | 6: 0x760a - ~lib/assemblyscript-json/decoder/JSONDecoder<~lib/assemblyscript-json/JSON/Handler>#deserialize proxy_1 | 7: 0x767b - ~lib/assemblyscript-json/JSON/_JSON.parse<~lib/string/String> proxy_1 | 8: 0x7b81 - assembly/index/ZTService#testJson proxy_1 | 9: 0x7e18 - assembly/index/ZTService#onStart proxy_1 | [2021-12-13 17:42:50.835][8][error][wasm] [source/extensions/common/wasm/wasm.cc:119] Wasm VM failed Failed to start base Wasm

willemneal commented 2 years ago

Oh so you're using a custom VM too. So not sure where the problem is.