Skip to content

Instantly share code, notes, and snippets.

@supermomonga
Created April 13, 2013 15:26
Show Gist options
  • Save supermomonga/5378829 to your computer and use it in GitHub Desktop.
Save supermomonga/5378829 to your computer and use it in GitHub Desktop.
cyan.py by 23
# coding=mbcs
import time
import os
import fnmatch
from ctypes import *
from ctypes.wintypes import *
class MODULEENTRY32(Structure):
_fields_ = [
('dwSize', DWORD),
('th32ModuleID', DWORD),
('th32ProcessID',DWORD),
('GlblcntUsage', DWORD),
('ProccntUsage', DWORD),
('modBaseAddr', LPVOID),
('modBaseSize', DWORD),
('hModule', HMODULE),
('szModule', c_char * 0x100),
('szExePath', c_char * MAX_PATH),
]
__str__ = lambda self:self.szExePath
@classmethod
def byProcessID(cls,th32ProcessID):
hSnapshot = windll.kernel32.CreateToolhelp32Snapshot(0x00000008,th32ProcessID)
try:
me = cls(sizeof(cls))
while windll.kernel32.Module32Next(hSnapshot,byref(me)):
yield cls.from_buffer_copy(me)
finally:
windll.kernel32.CloseHandle(hSnapshot)
def GetProcAddress(self,lpProcName):
return windll.kernel32.GetProcAddress(self.hModule,lpProcName)
class PROCESSENTRY32(Structure):
_fields_ = [
('dwSize', DWORD),
('cntUsage', DWORD),
('th32ProcessID', DWORD),
('th32DefaultHeapID', WPARAM),
('th32ModuleID', DWORD),
('cntThreads', DWORD),
('th32ParentProcessID',DWORD),
('pcPriClassBase', LONG),
('dwFlags', DWORD),
('szExeFile', c_char * MAX_PATH),
]
__str__ = lambda self:self.szExeFile
@classmethod
def byImageName(cls,name):
hSnapshot = windll.kernel32.CreateToolhelp32Snapshot(0x00000002,0)
try:
pe = cls(sizeof(cls))
while windll.kernel32.Process32Next(hSnapshot,byref(pe)):
if fnmatch.fnmatch(pe.szExeFile,name):yield cls.from_buffer_copy(pe)
finally:
windll.kernel32.CloseHandle(hSnapshot)
def __enter__(self):
self.hProcess = windll.kernel32.OpenProcess(
0x0030,
False,
self.th32ProcessID,
)
self.modules = {m.szModule:m for m in MODULEENTRY32.byProcessID(self.th32ProcessID)}
self.lpBaseAddress = LPVOID(self.modules[self.szExeFile].modBaseAddr)
self.dwNumberOfBytes = DWORD()
return self
def __exit__(self,exc_type,exc_val,exc_tb):
return not windll.kernel32.CloseHandle(self.hProcess)
def tell(self):
return self.lpBaseAddress.value
def seek(self,offset,whence=os.SEEK_SET):
if whence == os.SEEK_SET:self.lpBaseAddress.value = offset
elif whence == os.SEEK_CUR:self.lpBaseAddress.value += offset
else:raise ValueError(whence)
def read(self,c):
if not sizeof(c):return c
if not windll.kernel32.ReadProcessMemory(
self.hProcess,
self.lpBaseAddress,
byref(c),
sizeof(c),
byref(self.dwNumberOfBytes),
):raise WinError()
self.lpBaseAddress.value += self.dwNumberOfBytes.value
return c
def write(self,c):
if not sizeof(c):return c
if not windll.kernel32.WriteProcessMemory(
self.hProcess,
self.lpBaseAddress,
byref(c),
sizeof(c),
byref(self.dwNumberOfBytes),
):raise WinError()
return self.dwNumberOfBytes.value
class OpenCL(object):
PLATFORM_VERSION = 0x0901
DEVICE_TYPE_DEFAULT = 1 << 0
DEVICE_TYPE_CPU = 1 << 1
DEVICE_TYPE_GPU = 1 << 2
DEVICE_TYPE_ACCELERATOR = 1 << 3
DEVICE_TYPE_CUSTOM = 1 << 4
DEVICE_TYPE_ALL = -1
DEVICE_NAME = 0x102B
QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE = 1 << 0
QUEUE_PROFILING_ENABLE = 1 << 1
PROGRAM_BUILD_LOG = 0x1183
MEM_READ_WRITE = 1 << 0
MEM_WRITE_ONLY = 1 << 1
MEM_READ_ONLY = 1 << 2
MEM_USE_HOST_PTR = 1 << 3
MEM_ALLOC_HOST_PTR = 1 << 4
MEM_COPY_HOST_PTR = 1 << 5
MEM_USE_PERSISTENT_MEM_AMD = 1 << 6
MEM_HOST_WRITE_ONLY = 1 << 7
MEM_HOST_READ_ONLY = 1 << 8
MEM_HOST_NO_ACCESS = 1 << 9
MEM_SIZE = 0x1102
try:
_dll = oledll.opencl
_dll.clGetPlatformIDs.argtypes = [
c_uint,
POINTER(c_void_p),
POINTER(c_uint),
]
_dll.clGetPlatformInfo.argtypes = [
c_void_p,
c_uint,
c_size_t,
c_void_p,
POINTER(c_size_t),
]
_dll.clGetDeviceIDs.argtypes = [
c_void_p,
c_ulonglong,
c_uint,
POINTER(c_void_p),
POINTER(c_uint),
]
_dll.clGetDeviceInfo.argtypes = [
c_void_p,
c_uint,
c_size_t,
c_void_p,
POINTER(c_size_t),
]
_dll.clCreateContext.argtypes = [
POINTER(c_void_p),
c_uint,
POINTER(c_void_p),
c_void_p,
c_void_p,
POINTER(c_int),
]
_dll.clReleaseContext.argtypes = [c_void_p]
_dll.clCreateProgramWithSource.argtypes = [
c_void_p,
c_uint,
POINTER(c_char_p),
POINTER(c_size_t),
POINTER(c_int),
]
_dll.clBuildProgram.argtypes = [
c_void_p,
c_uint,
POINTER(c_void_p),
c_char_p,
c_void_p,
c_void_p,
]
_dll.clGetProgramBuildInfo.argtypes = [
c_void_p,
c_void_p,
c_int,
c_size_t,
c_void_p,
POINTER(c_size_t),
]
_dll.clReleaseProgram.argtypes = [c_void_p]
_dll.clCreateKernel.argtypes = [
c_void_p,
c_char_p,
POINTER(c_int),
]
_dll.clSetKernelArg.argtypes = [
c_void_p,
c_uint,
c_size_t,
c_void_p,
]
_dll.clReleaseKernel.argtypes = [c_void_p]
_dll.clCreateBuffer.argtypes = [
c_void_p,
c_ulonglong,
c_size_t,
c_void_p,
POINTER(c_int),
]
_dll.clGetMemObjectInfo.argtypes = [
c_void_p,
c_uint,
c_size_t,
c_void_p,
POINTER(c_size_t),
]
_dll.clReleaseMemObject.argtypes = [c_void_p]
_dll.clCreateCommandQueue.argtypes = [
c_void_p,
c_void_p,
c_ulonglong,
POINTER(c_int),
]
_dll.clFlush.argtypes = [c_void_p]
_dll.clFinish.argtypes = [c_void_p]
_dll.clEnqueueNDRangeKernel.argtypes = [
c_void_p,
c_void_p,
c_uint,
POINTER(c_size_t),
POINTER(c_size_t),
POINTER(c_size_t),
c_uint,
POINTER(c_void_p),
POINTER(c_void_p),
]
_dll.clEnqueueReadBuffer.argtypes = [
c_void_p,
c_void_p,
c_bool,
c_size_t,
c_size_t,
c_void_p,
c_uint,
POINTER(c_void_p),
POINTER(c_void_p),
]
_dll.clReleaseCommandQueue.argtypes = [c_void_p]
except WindowsError:
pass
def __init__(self,platform=0):
self.num_platforms = c_uint()
self._dll.clGetPlatformIDs(
0,
None,
self.num_platforms,
)
self.platforms = (c_void_p * self.num_platforms.value)()
self._dll.clGetPlatformIDs(
self.num_platforms,
self.platforms,
None,
)
self.platform = self.platforms[platform]
def __str__(self):
param_value_size = c_size_t()
self._dll.clGetPlatformInfo(
self.platform,
self.PLATFORM_VERSION,
0,
None,
param_value_size,
)
version = create_string_buffer(param_value_size.value)
self._dll.clGetPlatformInfo(
self.platform,
self.PLATFORM_VERSION,
param_value_size,
version,
None,
)
return version.value
@property
class Context(c_void_p):
@PYFUNCTYPE(None,c_char_p,c_void_p,c_size_t,c_void_p)
def notify(errinfo,private_info,cb,user_data):
raise RuntimeError(errinfo)
def __init__(self,cl):
self.cl = cl
def __call__(self,device_type=1,device=0):
self.num_devices = c_uint()
self.cl._dll.clGetDeviceIDs(
self.cl.platform,
device_type,
0,
None,
self.num_devices,
)
self.devices = (c_void_p * self.num_devices.value)()
self.cl._dll.clGetDeviceIDs(
self.cl.platform,
device_type,
self.num_devices,
self.devices,
None,
)
self.device = self.devices[device]
return self
def __str__(self):
param_value_size = c_size_t()
self.cl._dll.clGetDeviceInfo(
self.device,
self.cl.DEVICE_NAME,
0,
None,
param_value_size,
)
name = create_string_buffer(param_value_size.value)
self.cl._dll.clGetDeviceInfo(
self.device,
self.cl.DEVICE_NAME,
param_value_size,
name,
None,
)
return name.value
def __enter__(self):
errcode_ret = c_int()
self.value = self.cl._dll.clCreateContext(
None,
self.num_devices,
self.devices,
self.notify,
None,
errcode_ret,
)
HRESULT._check_retval_(errcode_ret.value)
return self
def __exit__(self, exc_type, exc_value, traceback):
return self.cl._dll.clReleaseContext(self)
@property
class Program(c_void_p):
def __init__(self, ctx):
self.ctx = ctx
def __call__(self,strings):
self.strings = (c_char_p * len(strings))(*strings)
return self
def __enter__(self):
errcode_ret = c_int()
self.value = self.ctx.cl._dll.clCreateProgramWithSource(
self.ctx,
len(self.strings),
self.strings,
None,
errcode_ret,
)
HRESULT._check_retval_(errcode_ret.value)
return self
def errcheck(self,*args):
raise RuntimeError
def Build(self,options=None):
try:
self.ctx.cl._dll.clBuildProgram(
self,
self.ctx.num_devices,
self.ctx.devices,
options,
None,
None,
)
except WindowsError:
param_value_size = c_size_t()
self.ctx.cl._dll.clGetProgramBuildInfo(
self,
self.ctx.device,
OpenCL.PROGRAM_BUILD_LOG,
0,
None,
param_value_size,
)
log = create_string_buffer(param_value_size.value)
self.ctx.cl._dll.clGetProgramBuildInfo(
self,
self.ctx.device,
OpenCL.PROGRAM_BUILD_LOG,
param_value_size,
log,
None,
)
raise RuntimeError(log.value)
return self
def __exit__(self, exc_type, exc_value, traceback):
return self.ctx.cl._dll.clReleaseProgram(self)
@property
class Kernel(c_void_p):
def __init__(self,pg):
self.pg = pg
def __call__(self,kernel_name):
self.kernel_name = kernel_name
return self
def __enter__(self):
errcode_ret = c_int()
self.value = self.pg.ctx.cl._dll.clCreateKernel(
self.pg,
self.kernel_name,
errcode_ret,
)
HRESULT._check_retval_(errcode_ret.value)
return self
def SetArg(self,arg_index,arg):
self.pg.ctx.cl._dll.clSetKernelArg(
self,
arg_index,
sizeof(arg),
byref(arg),
)
return self
def __exit__(self, exc_type, exc_value, traceback):
return self.pg.ctx.cl._dll.clReleaseMemObject(self)
@property
class Memory(c_void_p):
def __init__(self, ctx):
self.ctx = ctx
def __call__(self,flags,size,host_ptr):
self.flags = flags
self.size = size
self.host_ptr = host_ptr
return self
def __enter__(self):
errcode_ret = c_int()
self.value = self.ctx.cl._dll.clCreateBuffer(
self.ctx,
self.flags,
self.size,
self.host_ptr,
errcode_ret,
)
HRESULT._check_retval_(errcode_ret.value)
return self
def __len__(self):
param = c_size_t()
self.ctx.cl._dll.clGetMemObjectInfo(
self,
self.ctx.cl.MEM_SIZE,
sizeof(param),
byref(param),
None,
)
return param.value
def __exit__(self, exc_type, exc_value, traceback):
return self.ctx.cl._dll.clReleaseMemObject(self)
@property
class Queue(c_void_p):
def __init__(self, ctx):
self.ctx = ctx
def __call__(self,properties=0):
self.properties = properties
return self
def __enter__(self):
errcode_ret = c_int()
self.value = self.ctx.cl._dll.clCreateCommandQueue(
self.ctx,
self.ctx.device,
self.properties,
errcode_ret,
)
HRESULT._check_retval_(errcode_ret.value)
return self
def Finish(self):
return self.ctx.cl._dll.clFinish(self)
def Flush(self):
return self.ctx.cl._dll.clFlush(self)
def NDRangeKernel(self,kernel,global_work_size,*args):
for i,arg in enumerate(args):kernel.SetArg(i,arg)
self.ctx.cl._dll.clEnqueueNDRangeKernel(
self,
kernel,
len(global_work_size),
None,
(c_size_t * len(global_work_size))(*global_work_size),
None,
0,
None,
None,
)
return self
def ReadBuffer(self,buffer,offset,cb,ptr,blocking_read=True):
self.ctx.cl._dll.clEnqueueReadBuffer(
self,
buffer,
blocking_read,
offset,
cb,
ptr,
0,
None,
None,
)
return self
def __exit__(self, exc_type, exc_value, traceback):
return self.ctx.cl._dll.clReleaseCommandQueue(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment