Malloc Geiger

栏目: IT技术 · 发布时间: 4年前

内容简介:Malloc geiger is a hook for malloc that plays geiger counter blips in proportion to the amount of calls to malloc as a way of knowing what an application does. It's largely meant as a joke so don't expect it to work properly in every situation. It only loo

Malloc Geiger

Malloc geiger is a hook for malloc that plays geiger counter blips in proportion to the amount of calls to malloc as a way of knowing what an application does. It's largely meant as a joke so don't expect it to work properly in every situation. It only looks at malloc at this point so it won't react to any other way an application may allocate memory.

A video of malloc_geiger in action can be found here

API

The API is minimal:

// Installs the geiger clicking malloc handler
// saturation_rate, the amount of mallocs required in a cycle to max out the clicking
//
// interval the time in microseconds between each check for whether a click should be played or not.
// lower values allows more extreme rates of clicking. A good start value tends to be 10000 meaning
// a maximum of 100 clicks per second when saturating the amount of allocations
//
// The probability of a click happening in each interval is 
// min(number_of_mallocs_in_interval/saturation_rate, 1.0)
MALLOC_GEIGER_API MG_Status install_malloc_geiger(size_t saturation_rate, size_t interval);

// Uninstalls the geiger clicking malloc handler
MALLOC_GEIGER_API MG_Status uninstall_malloc_geiger();

A typical initialization looks something like this:

if(install_malloc_geiger(1000, 10000) != MG_STATUS_SUCCESS) {
    // error handling
}

This call should ideally before the application has started any other threads to make sure the patching doesn't happen while another thread is doing a call to malloc or free.

Compatiblity

malloc_geiger only works on Windows at this point. It has been tested on Win64 using visual studio 2017

Installing and Building

When you have cloned the repository you need to sync the submodules. Enter the directory you synced and run

git submodule update --init

Create and go to a directory for the build

md build
cd build

Run the cmake configuration, there is a script for doing that provided for Ninja and Release Builds installing in build/installed

../scripts/createproj.bat

Now you should be ready to build

ninja --j4 install

If everything worked you can run the test application

installed/bin/test_app.exe

Python injection

Since the library is built as a dll and does dynamic patching of the malloc functions it can be installed in a running application. If the application has a python interpreter it's an excellent vector to do the installation.

Note that this only works if the runtime libraries matches between geiger_malloc and the host application. Here is a sample script for installing it in an application

import ctypes
mg = ctypes.windll.LoadLibrary("<path_to_install_dir>/malloc_geiger.dll") 
res = mg.install_malloc_geiger(1000, 10000)
if res != 0:
    raise BaseException('Failed to install malloc geiger')

Caveats

Too many to mention all. Here are some:

  • With the current setup where malloc_geiger is built as a dll it requires the application using it to use the dynamic runtime library. If using it in an application with static runtime library it needs to be linked statically.
  • It only overrides malloc, any allocation not passing through malloc is going to be missed.
  • There is a potential deadlock in the malloc functions since there is a lock in the sound code too. Have not invested time in figuring out whether it can happen and properly avoided.
  • It overrides the malloc the dll uses. If the host application uses a different runtime library you need to configure the build settings to match for it to work.
  • The replacement malloc has an additional lock and does some extra work so it affects performance negatively.
  • Probably a million other things

Credits

The application works thanks to two external libraries

gperftools

A small part of gperftools is used to override the malloc/free functions at runtime

https://github.com/gperftools/gperftools

cute_headers

The cute_sound library is used to play sounds.

https://github.com/RandyGaul/cute_headers/

Geiger sound

Cut out from a sound found at wikipedia, here are the credits for it https://upload.wikimedia.org/wikipedia/commons/5/58/Geiger_calm.ogg Snaily [CC BY-SA 3.0 ( http://creativecommons.org/licenses/by-sa/3.0/ )]


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

代码2.0

代码2.0

(美)劳伦斯·莱斯格 / 李旭、沈伟伟 / 清华大学出版社 / 2009-7-1 / 48.00

《代码2.0:网络空间中的法律》在西方发达国家已成为法律学、公共管理学、商学、传播学、政治学和信息科学技术专业的必读书目。对于政府管理者、法律执业者、ICT企业管理者、创意产业从业者和广大信息工程技术人员来说,这的确是一本能够启迪思维的难得之作。在众多以网络为主题的书籍中,这是一本问世近10年但居然没有过时的书!于是,它成为了经典之作,荣膺学术名著和畅销读物两项桂冠。一起来看看 《代码2.0》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器