Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-iteratrable input of yieldEach #455

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/Fuzzilli/Base/ProgramBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2711,4 +2711,13 @@ public class ProgramBuilder {
break
}
}

public func buildIteratorVariable(_ b: ProgramBuilder, _ it: Variable) -> Variable{
if(b.type(of: it).Is(.iterable)){
return it
}
let initialValues = (0..<Int.random(in: 1...5)).map({ _ in b.randomVariable() })
return b.createArray(with: initialValues)
}

}
10 changes: 5 additions & 5 deletions Sources/Fuzzilli/CodeGen/CodeGenerators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -769,13 +769,13 @@ public let CodeGenerators: [CodeGenerator] = [
// These are "typically" used as arguments, so we don't directly generate a call operation here.
},

RecursiveCodeGenerator("GeneratorFunctionGenerator") { b in
RecursiveCodeGenerator("GeneratorFunctionGenerator", inputs: .preferred(.iterable)) { b, it in
let f = b.buildGeneratorFunction(with: b.randomParameters(), isStrict: probability(0.1)) { _ in
b.buildRecursive()
if probability(0.5) {
b.yield(b.randomVariable())
} else {
b.yieldEach(b.randomVariable())
b.yieldEach(b.buildIteratorVariable(b, it))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in the AsyncGeneratorFunctionGenerator, I would recommend doing this differently. For one, I think we would usually prefer to yield something that we create in this function (i.e. in the buildRecursive() call), but that's not possible if the it comes from the outside. I would probably recommend just doing something like this:

} else {
    let randomVariables = b.randomVariables(Int.random(in: 1...5))
    let array = b.createArray(with: randomVariables)
    b.yieldEach(array)
}

This will then cause us to always yield a plain array, but we have the other CodeGenerator below for yielding other stuff. Also the InputMutator will quickly shuffle this around to yield from other things.

}
b.doReturn(b.randomVariable())
}
Expand All @@ -800,14 +800,14 @@ public let CodeGenerators: [CodeGenerator] = [
// These are "typically" used as arguments, so we don't directly generate a call operation here.
},

RecursiveCodeGenerator("AsyncGeneratorFunctionGenerator") { b in
RecursiveCodeGenerator("AsyncGeneratorFunctionGenerator", inputs: .preferred(.iterable)) { b, it in
let f = b.buildAsyncGeneratorFunction(with: b.randomParameters(), isStrict: probability(0.1)) { _ in
b.buildRecursive()
b.await(b.randomVariable())
if probability(0.5) {
b.yield(b.randomVariable())
} else {
b.yieldEach(b.randomVariable())
b.yieldEach(b.buildIteratorVariable(b, it))
}
b.doReturn(b.randomVariable())
}
Expand Down Expand Up @@ -1085,7 +1085,7 @@ public let CodeGenerators: [CodeGenerator] = [
}
} else {
// TODO only do this when the value is iterable?
b.yieldEach(val)
b.yieldEach(b.buildIteratorVariable(b, val))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here it would make sense to split this into two generators:

    CodeGenerator("YieldGenerator", inContext: .generatorFunction, inputs: .one) { b, val in
        assert(b.context.contains(.generatorFunction))
        if probability(0.9) {
            b.yield(val)
        } else {
            b.yield()
        }
    },

    CodeGenerator("YieldEachGenerator", inContext: .generatorFunction, inputs: .required(.iterable)) { b, val in
        assert(b.context.contains(.generatorFunction))
        b.yieldEach(val)
    },

That way it's easy to guarantee that we get a .iterable.

}
},

Expand Down