# This script inserts the seeded bug into VIPS before building it
#
# Author: Nick Jalbert (jalbert@eecs.berkeley.edu)
#

import os
import sys

# This function comments out the lock and unlock in im_semaphore_upn 
# (file vips-7.20.0/libvips/iofuncs/semaphore.c)
# at lines 87 and 92 respectively

def seedBugInSemaphore():
    print "Adding bug to semaphore.c..."
    cwd = os.getcwd()
    libvips_path = os.path.join(cwd, "vips-7.20.0", "libvips")
    assert os.path.exists(libvips_path)
    sem_path = os.path.join(libvips_path, "iofuncs", "semaphore.c")
    assert os.path.exists(sem_path)
    sem_c = open(sem_path, "r").readlines()
    assert "g_mutex_lock( s->mutex )" in sem_c[86]
    assert "g_mutex_unlock( s->mutex )" in sem_c[91]

    sem_c[86] = "        //g_mutex_lock( s->mutex );\n"
    sem_c[91] = "        //g_mutex_unlock( s->mutex );\n"

    sem_out = open(sem_path, "w")
    for x in sem_c:
        sem_out.write(x)
    sem_out.close()


def placeThrilleAssertsInThreadgroup():
    print "Changing g_assert to thrilleAssertC in threadgroup.c..."
    cwd = os.getcwd()
    libvips_path = os.path.join(cwd, "vips-7.20.0", "libvips")
    assert os.path.exists(libvips_path)
    threadgroup_path = os.path.join(libvips_path, "iofuncs", "threadgroup.c")
    assert os.path.exists(threadgroup_path)
    threadgroup_c = open(threadgroup_path, "r").readlines()
    for i in range(0, len(threadgroup_c)):
        tmp = threadgroup_c[i].replace("g_assert", "thrilleAssertC")
        threadgroup_c[i] = tmp

    threadgroup_out = open(threadgroup_path, "w")
    for x in threadgroup_c:
        threadgroup_out.write(x)
    threadgroup_out.close()

def main():
    seedBugInSemaphore()
    placeThrilleAssertsInThreadgroup()


if __name__ == "__main__":
    main()

3855ce84e2ce04f788307038c9bac919b5705c03
# This script inserts the seeded bug into VIPS before building it
#
# Author: Nick Jalbert (jalbert@eecs.berkeley.edu)
#

import os
import sys

# This function comments out the lock and unlock in im_semaphore_upn 
# (file vips-7.20.0/libvips/iofuncs/semaphore.c)
# at lines 87 and 92 respectively

def seedBugInSemaphore():
    print "Adding bug to semaphore.c..."
    cwd = os.getcwd()
    libvips_path = os.path.join(cwd, "vips-7.20.0", "libvips")
    assert os.path.exists(libvips_path)
    sem_path = os.path.join(libvips_path, "iofuncs", "semaphore.c")
    assert os.path.exists(sem_path)
    sem_c = open(sem_path, "r").readlines()
    assert "g_mutex_lock( s->mutex )" in sem_c[86]
    assert "g_mutex_unlock( s->mutex )" in sem_c[91]

    sem_c[86] = "        //g_mutex_lock( s->mutex );\n"
    sem_c[91] = "        //g_mutex_unlock( s->mutex );\n"

    sem_out = open(sem_path, "w")
    for x in sem_c:
        sem_out.write(x)
    sem_out.close()


def placeThrilleAssertsInThreadgroup():
    print "Changing g_assert to thrilleAssertC in threadgroup.c..."
    cwd = os.getcwd()
    libvips_path = os.path.join(cwd, "vips-7.20.0", "libvips")
    assert os.path.exists(libvips_path)
    threadgroup_path = os.path.join(libvips_path, "iofuncs", "threadgroup.c")
    assert os.path.exists(threadgroup_path)
    threadgroup_c = open(threadgroup_path, "r").readlines()
    for i in range(0, len(threadgroup_c)):
        tmp = threadgroup_c[i].replace("g_assert", "thrilleAssertC")
        threadgroup_c[i] = tmp

    threadgroup_out = open(threadgroup_path, "w")
    for x in threadgroup_c:
        threadgroup_out.write(x)
    threadgroup_out.close()

def main():
    seedBugInSemaphore()
    placeThrilleAssertsInThreadgroup()


if __name__ == "__main__":
    main()
