chakra-core / ChakraCore

ChakraCore is an open source Javascript engine with a C API.
MIT License
9.12k stars 1.2k forks source link

Exposing GarbageCollection (GC) in ch binary #5715

Closed guyio closed 6 years ago

guyio commented 6 years ago

Hey, is there an easy way to expose GC from withing a script in ch? I'd like to run a js script that contains a statement - 'gc();' and that this statement will run a GC cycle.

MSLaguana commented 6 years ago

If you are hosting ChakraCore yourself, then there is a JsCollectGarbage native method which you can call to trigger a garbage collection. You can construct a javascript wrapper function around that and expose it on the global object if you want.

guyio commented 6 years ago

I'll add a bit more info, sorry.

I'm fuzzing the js engine via the ch.exe binary. my inputs are using the gc() function from js.

I'd like to do the minimal required changes to let the ch.exe expose the GC from js.

For example, in JavaScriptCore, V8 and SpiderMonkey it's supported with runtime flags or compilation flags.

If it's not supported in Chakra's builtin example - 'ch.exe', could you please point me to an example that adds a native function? i'll take off from there.

Thanks for your help!

MSLaguana commented 6 years ago

Ah, I see. If you are using a debug or test ch.exe binary, then if you pass it the -collectgarbage command line argument, then it will expose a global CollectGarbage() function which triggers a garbage collection.

guyio commented 6 years ago

Thank you! It's exactly what I needed.