<commit_before>import org.strangeway.disttest.Distro
import org.strangeway.disttest.Initramfs
import org.strangeway.disttest.KernelBinary
import org.strangeway.disttest.KernelRepo
import org.strangeway.disttest.KernelSourcePool
import org.strangeway.disttest.Utils

import java.util.concurrent.ConcurrentHashMap

class Starter {
    public static void threadedDemo(){
        KernelSourcePool kernelSourcePool = new KernelSourcePool()
        Initramfs initramfs = new Initramfs("hello.sh")
        initramfs.getArtifact() //Hack to prevent races in other threads
        Map<String, Distro> distros = new HashMap<>()
        List<Thread> threads = new ArrayList<>()
        ConcurrentHashMap<String, String> results = new ConcurrentHashMap<>()
        for (version in ["linux-2.6.32.71", "linux-3.18.28"]) {
            KernelBinary kernelBinary = new KernelBinary(version, version.replace(/linux-/, "v"), "acpi", kernelSourcePool)
            Distro distro = new Distro(kernelBinary, initramfs)
            distros[version] = distro
            String ver = version
            threads.add(Thread.start {
                results[ver] = distro.run()
                distro.close()
            })
        }
        while (true) {
            boolean needBreak = true
            threads.each { t ->
                if (t.alive) {
                    needBreak = false
                }
            }
            Thread.sleep(100)
            distros.each { k, v ->
                if (results.containsKey(k)) {
                    println String.format("%-20s %-100s", k, results[k])
                    //ToDo: use more reliable method to clear terminal
                } else {
                    println String.format("%-20s %-100s", k, Utils.renderProgress(v))
                }
            }
            print String.format("%c[%dA", 0x1B, distros.size());

            if (needBreak) {
                break
            }
        }

        results.each { k, v ->
            println String.format("%-20s %-100s", k, v)
        }
    }

    public static void bisectDemo(){
        KernelSourcePool kernelSourcePool = new KernelSourcePool()
        Initramfs initramfs = new Initramfs("hello.sh")
        initramfs.getArtifact()

        String[] commits = KernelRepo.getVersionBetweenTags("v3.18", "v3.18.28")
        int broken = 0
        int working = commits.length-1
        while(broken+1 != working){
            int testIndex = (broken+working)/2
            KernelBinary kernelBinary = new KernelBinary("linux-3.18", commits[testIndex], "acpi", kernelSourcePool)
            println "Testing commit #$testIndex($broken..$working)"+commits[testIndex]
            Distro distro = new Distro(kernelBinary, initramfs)
            String res = distro.run()
            distro.close()
            println "Res: $res\n"
            if(res == "<KILLED>"){
                broken = testIndex
            }else{
                working = testIndex
            }
        }
    }

    public static void bisectSemtexDemo(){
        Map<String, String> results = new HashMap<>()
        KernelSourcePool kernelSourcePool = new KernelSourcePool()
        Initramfs initramfs = new Initramfs("cve-2013-2094.sh")
        initramfs.getArtifact()

        String[] commits = KernelRepo.getVersionBetweenTags("v3.0", "v3.0.101")
        int broken = 0
        int working = commits.length-1
        while(broken+1 != working){
            int testIndex = (broken+working)/2
            KernelBinary kernelBinary = new KernelBinary("linux-3.0", commits[testIndex], "acpi_perf_sec", kernelSourcePool)
            println "Testing commit #$testIndex($broken..$working)"+commits[testIndex]
            Distro distro = new Distro(kernelBinary, initramfs)
            String res = distro.run()
            distro.close()
            println "Res: $res\n"
            String[] prevNext = KernelRepo.getPrevNextTag(commits[testIndex])
            if(res == "uid=0(root) gid=0(root) groups=1(user)"){
                broken = testIndex
                results[ prevNext[0] ] = "FireBrick"

            }else{
                working = testIndex
                results[ prevNext[1] ] = "LimeGreen"
            }
        }
        def versions = KernelRepo.getPatchLevels().collect({ [it, KernelRepo.getSubLevels(it)].flatten() })
        Utils.renderReport(versions, results, "cve-2013-2094.svg")<commit_msg>Updated SVG reporting for cve-2013-2094.<commit_after>import org.strangeway.disttest.Distro
import org.strangeway.disttest.Initramfs
import org.strangeway.disttest.KernelBinary
import org.strangeway.disttest.KernelRepo
import org.strangeway.disttest.KernelSourcePool
import org.strangeway.disttest.Utils

import java.util.concurrent.ConcurrentHashMap

class Starter {
    public static void threadedDemo(){
        KernelSourcePool kernelSourcePool = new KernelSourcePool()
        Initramfs initramfs = new Initramfs("hello.sh")
        initramfs.getArtifact() //Hack to prevent races in other threads
        Map<String, Distro> distros = new HashMap<>()
        List<Thread> threads = new ArrayList<>()
        ConcurrentHashMap<String, String> results = new ConcurrentHashMap<>()
        for (version in ["linux-2.6.32.71", "linux-3.18.28"]) {
            KernelBinary kernelBinary = new KernelBinary(version, version.replace(/linux-/, "v"), "acpi", kernelSourcePool)
            Distro distro = new Distro(kernelBinary, initramfs)
            distros[version] = distro
            String ver = version
            threads.add(Thread.start {
                results[ver] = distro.run()
                distro.close()
            })
        }
        while (true) {
            boolean needBreak = true
            threads.each { t ->
                if (t.alive) {
                    needBreak = false
                }
            }
            Thread.sleep(100)
            distros.each { k, v ->
                if (results.containsKey(k)) {
                    println String.format("%-20s %-100s", k, results[k])
                    //ToDo: use more reliable method to clear terminal
                } else {
                    println String.format("%-20s %-100s", k, Utils.renderProgress(v))
                }
            }
            print String.format("%c[%dA", 0x1B, distros.size());

            if (needBreak) {
                break
            }
        }

        results.each { k, v ->
            println String.format("%-20s %-100s", k, v)
        }
    }

    public static void bisectDemo(){
        KernelSourcePool kernelSourcePool = new KernelSourcePool()
        Initramfs initramfs = new Initramfs("hello.sh")
        initramfs.getArtifact()

        String[] commits = KernelRepo.getVersionBetweenTags("v3.18", "v3.18.28")
        int broken = 0
        int working = commits.length-1
        while(broken+1 != working){
            int testIndex = (broken+working)/2
            KernelBinary kernelBinary = new KernelBinary("linux-3.18", commits[testIndex], "acpi", kernelSourcePool)
            println "Testing commit #$testIndex($broken..$working)"+commits[testIndex]
            Distro distro = new Distro(kernelBinary, initramfs)
            String res = distro.run()
            distro.close()
            println "Res: $res\n"
            if(res == "<KILLED>"){
                broken = testIndex
            }else{
                working = testIndex
            }
        }
    }

    public static void bisectSemtexDemo(){
        Map<String, String> results = new HashMap<>()
        KernelSourcePool kernelSourcePool = new KernelSourcePool()
        Initramfs initramfs = new Initramfs("cve-2013-2094.sh")
        initramfs.getArtifact()

        def versions = KernelRepo.getPatchLevels().collect({ [it, KernelRepo.getSubLevels(it)].flatten() })
        def toTest = ["v3.0":0]

        versions.each {
            if(toTest.containsKey(it[0])) {
                String[] commits = KernelRepo.getVersionBetweenTags(it[0], it[-1])
                int broken = 0
                int working = commits.length - 1
                while (broken + 1 != working) {
                    int testIndex = (broken + working) / 2
                    KernelBinary kernelBinary = new KernelBinary("linux-3.0", commits[testIndex], "acpi_perf_sec", kernelSourcePool)
                    println "Testing commit #$testIndex($broken..$working)" + commits[testIndex]
                    Distro distro = new Distro(kernelBinary, initramfs)
                    String res = distro.run()
                    distro.close()
                    println "Res: $res\n"
                    String[] prevNext = KernelRepo.getPrevNextTag(commits[testIndex])
                    if (res == "uid=0(root) gid=0(root) groups=1(user)") {
                        broken = testIndex
                        results[prevNext[0]] = "FireBrick"

                    } else {
                        working = testIndex
                        results[prevNext[1]] = "LimeGreen"
                    }
                }
            }
        }
        Utils.renderReport(versions, results, "cve-2013-2094.svg")
{'fraction_non_alphanumeric': 0.11083068144409086, 'fraction_numerical': 0.018325469110062546, 'mean_word_length': 1.858845671267252, 'pattern_counts': {'":': 1, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4098113', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2501, 'n_words': 549}