Add ability to import existing JavaScript code
This feature would require implementing a simple "compiler" to compile JavaScript to FuzzIL. Maybe this should be implemented as a separate tool as I doubt there is a good JavaScript parsing library available for swift... :) The compiled FuzzIL programs could then just be exported as one big JSON (or a different serialization format if supported by then) file similar to how Storage.swift currently exports the corpus.
Hey, I am currently writing this tool. I use esprima , which is a javascript library to convert javascript source code to the abstract syntax tree. Since FuzzIL is a SSA-like IR, I try to build a Control Flow Graph of it and turn it into SSA form, but I have basic blocks with segmented AST tree in it now. FuzzIL use various kinds of statement to represent control flow, not basic blocks. I have to convert basic blocks back to the statement, this is a bit annoying. Overall, I don't know if this is a good idea, and I wish to hear your advice about it.
Hey! That's very cool, looking forward. The approach with esprima sounds good. So probably you'll end up with a separate tool that converts a corpus of JavaScript files into one or multiple JSON files containing serialized FuzzIL programs (which can then be imported into Fuzzilli)? Anyway, I think you don't need full SSA conversion (at least for a simple converter): you can probably convert every expression into the corresponding operation and assign to an SSA variable, then covert actual JS variables into Phis which can be reassigned whenever the variable is written to. That way you don't have to go down to the basic block level I think. Afterwards, it might be a good idea to try and implement another reducer that removes unnecessary Phis during minimization. What do you think?
This might also be worth checking out: https://github.com/github/semantic
Thank you for your suggestion, I totally agree with you, doing partial SSA conversion is enough. Subject to the currently defined Operation of FuzzIL, many JS grammars cannot currently be converted to FuzzIL. For example, Class definitions, Array patterns, Object patterns. Besides, for statements, while statements, and do-while statements can only accept certain forms of conditional expression expressions. I tried to convert JavaScriptCore's test folder and got 17000~ samples, the conversion success rate is only about 50%. When I import the converted corpus into Fuzzilli, it only found four hundred interesting examples?! It seems not reasonable. I suppose that some magics exists in the program lift module.
I was using the old version code of Fuzzilli, it doesn't tell me anything about the success rate of imported corpus. When I switch to the latest version of Fuzzilli, I found that the success rate of imported corpus is only 2%. I need to review what is wrong in my conversion algorithm.
Ok interesting! So I think you should be able to convert e.g. class definitions into something like this (which is ultimately what the engine does as well):
class Foo {
constructor(a, b) {
CODE1;
}
method1() {
CODE2;
}
}
becomes
function Foo(a, b) {
CODE1;
}
let p = Foo.prototype;
function method1() {
CODE2;
}
p.method1 = method1;
The same should work for array and object literals. The loop/if header problem is tracked in https://github.com/googleprojectzero/fuzzilli/issues/8
Also I think some of the JS test files require special commandline flags and/or call custom builtins that are not normally available. That might be the main source of the low success rate. You should be able to quickly hack Fuzzilli to write the JS code for every entry from the imported corpus to disk so you can then check which ones result in exceptions and why.
Hi, I am also working on similar things, I do have one question though, with the newly introduced AbstractInterpreter and type system, the BeginFunctionDefinition operation changed to take function signature now with guessed types, now with this feature, which coverts js to FuzzIL, I don't see a way that this new type system can be plugged in. What is your thought on this? I currently can think of two approaches.
First approach is just not defining the types (i.e. set all parameters for function to .anything), and let the AbstractInterpreter interpret the types on the fly. But I have a concern that this might not work, as the allowed type subsumes interpreted type, which makes the typer narrows the type assumption
Second approach I can think of would be porting the typer from Fuzzilli to javascript from scratch, but that seems like a lot of work too.
I think the first approach should be fine, that's basically what Fuzzilli does in the CodeGenerators anyway: https://github.com/googleprojectzero/fuzzilli/blob/51151099432f00ba4a7360f8f33464b8fcb1b51e/Sources/Fuzzilli/Core/CodeGenerators.swift#L95 So either set all parameter types to .anything or even use the unknown function signature: https://github.com/googleprojectzero/fuzzilli/blob/51151099432f00ba4a7360f8f33464b8fcb1b51e/Sources/Fuzzilli/FuzzIL/TypeSystem.swift#L835
Eventually the goal is to be able to extract type information at runtime (see https://github.com/googleprojectzero/fuzzilli/issues/34) which would also require a way to represent these types in JavaScript. Maybe when that is available this problem can be solved in a better way (although I'm not even that sure if that's necessary).
Thanks for responding, although when I tested the forUnknownFunction signature, it does not seem to work. I don't know if I understand this correctly, but I think this is because BeginFunctionDefinition computes parameter count based on the number of signatures in inputTypes, however, with forUnknownFunction signature, the number of signatures is strictly set to 1, thus would trigger assertion inside the instruction constructor
Ah yeah correct, sorry. So yeah, with the unknown function signature it would basically become function foo(...args) { ... } instead. So if you have something like function bar(a, b, c) { ... } you'll have to use the signature [.anything, .anything, .anything] => .unknown when defining it via BeginFunctionDefinition.
How can I help with this feature?