ext/cs__common/cs__common.c in contrast-agent-3.13.2 vs ext/cs__common/cs__common.c in contrast-agent-3.14.0

- old
+ new

@@ -1,11 +1,11 @@ /* Copyright (c) 2020 Contrast Security, Inc. See * https://www.contrastsecurity.com/enduser-terms-0317a for more details. */ #include "cs__common.h" -#include <ruby.h> #include <dlfcn.h> +#include <ruby.h> /* Globals */ /* These are defined w/ `extern` in the header */ VALUE contrast, agent, patching, policy, assess; VALUE core_extensions, core_assess; @@ -25,23 +25,28 @@ VALUE funchook_module_wrapper = rb_define_module("Funchook"); funchook_path = rb_iv_get(funchook_module_wrapper, "@path"); void *funchook_lib_handle; void *funchook_reference, *(*funchook_create)(void); - int prepareResult, (*funchook_prepare)(void*, void**, void*); - int installResult, (*funchook_install)(void*, int); + int prepareResult, (*funchook_prepare)(void *, void **, void *); + int installResult, (*funchook_install)(void *, int); - funchook_lib_handle = dlopen(StringValueCStr(funchook_path), RTLD_NOW | RTLD_GLOBAL); + funchook_lib_handle = + dlopen(StringValueCStr(funchook_path), RTLD_NOW | RTLD_GLOBAL); /* Load the funchook methods we need */ - funchook_create = (void* (*)(void))dlsym(funchook_lib_handle, "funchook_create"); - funchook_prepare = (int (*)(void*, void**, void*))dlsym(funchook_lib_handle, "funchook_prepare"); - funchook_install = (int (*)(void*, int))dlsym(funchook_lib_handle, "funchook_install"); + funchook_create = + (void *(*)(void))dlsym(funchook_lib_handle, "funchook_create"); + funchook_prepare = (int (*)(void *, void **, void *))dlsym( + funchook_lib_handle, "funchook_prepare"); + funchook_install = + (int (*)(void *, int))dlsym(funchook_lib_handle, "funchook_install"); - funchook_reference = (void*)(*funchook_create)(); + funchook_reference = (void *)(*funchook_create)(); - prepareResult = (*funchook_prepare)(funchook_reference, (void**)original_function, hook_function); + prepareResult = (*funchook_prepare)( + funchook_reference, (void **)original_function, hook_function); installResult = (*funchook_install)(funchook_reference, 0); } void contrast_alias_method(const VALUE target, const char *to, const char *from) { @@ -51,37 +56,36 @@ VALUE contrast_patcher() { return patcher; } -VALUE contrast_register_patch(const char *module_name, - const char *method_name, - VALUE(c_fn)(const int, const VALUE*, const VALUE) - ) { - return _contrast_register_patch(module_name, method_name, c_fn, IMPL_ALIAS_INSTANCE); +VALUE contrast_register_patch(const char *module_name, const char *method_name, + VALUE(c_fn)(const int, const VALUE *, + const VALUE)) { + return _contrast_register_patch(module_name, method_name, c_fn, + IMPL_ALIAS_INSTANCE); } - VALUE contrast_register_singleton_patch(const char *module_name, const char *method_name, - VALUE(c_fn)(const int, const VALUE*, const VALUE) - ) { - return _contrast_register_patch(module_name, method_name, c_fn, IMPL_ALIAS_SINGLETON); + VALUE(c_fn)(const int, const VALUE *, + const VALUE)) { + return _contrast_register_patch(module_name, method_name, c_fn, + IMPL_ALIAS_SINGLETON); } -VALUE contrast_register_singleton_prepend_patch(const char *module_name, - const char *method_name, - VALUE(c_fn)(const int, const VALUE*, const VALUE) - ) { - return _contrast_register_patch(module_name, method_name, c_fn, IMPL_PREPEND); +VALUE contrast_register_singleton_prepend_patch( + const char *module_name, const char *method_name, + VALUE(c_fn)(const int, const VALUE *, const VALUE)) { + return _contrast_register_patch(module_name, method_name, c_fn, + IMPL_PREPEND); } -static VALUE _contrast_register_patch(const char *module_name, - const char *method_name, - VALUE(c_fn)(const int, const VALUE*, const VALUE), - patch_impl patch - ) { +static VALUE +_contrast_register_patch(const char *module_name, const char *method_name, + VALUE(c_fn)(const int, const VALUE *, const VALUE), + patch_impl patch) { VALUE contrast_bind_module = rb_funcall(rb_cModule, rb_intern("new"), 0); VALUE unbound_method = Qnil; VALUE rb_str_module_name = rb_str_new_cstr(module_name); VALUE rb_str_method_name = rb_str_new_cstr(method_name); @@ -103,28 +107,31 @@ * as Ruby functions. We immediately unbind and undef them. * This module doesn't do anything. */ rb_define_method(contrast_bind_module, method_name, (VALUE(*)())c_fn, -1); VALUE rb_sym_instance_method = rb_intern("instance_method"); - unbound_method = rb_funcall(contrast_bind_module, rb_sym_instance_method, 1, rb_str_method_name); + unbound_method = rb_funcall(contrast_bind_module, rb_sym_instance_method, 1, + rb_str_method_name); rb_undef_method(contrast_bind_module, method_name); /* map impl enum -> ruby symbol */ VALUE impl = Qnil; - switch(patch) { - case IMPL_ALIAS_INSTANCE: + switch (patch) { + case IMPL_ALIAS_INSTANCE: impl = ID2SYM(rb_sym_alias_instance); break; - case IMPL_ALIAS_SINGLETON: + case IMPL_ALIAS_SINGLETON: impl = ID2SYM(rb_sym_alias_singleton); break; - case IMPL_PREPEND: + case IMPL_PREPEND: impl = ID2SYM(rb_sym_prepend); break; } - VALUE underlying_method_name = rb_funcall(contrast_patcher(), rb_sym_register_c_patch, 3, rb_str_module_name, unbound_method, impl); + VALUE underlying_method_name = + rb_funcall(contrast_patcher(), rb_sym_register_c_patch, 3, + rb_str_module_name, unbound_method, impl); return SYM2ID(underlying_method_name); } void Init_cs__common(void) { cs__send_method = rb_intern("send"); @@ -139,12 +146,12 @@ rb_sym_method = rb_intern("__method__"); rb_sym_cs_tracked = rb_intern("cs__tracked?"); /* Used for returning unbound C functions */ rb_sym_register_c_patch = rb_intern("register_c_patch"); - rb_sym_alias_instance = rb_intern("alias_instance"); - rb_sym_alias_singleton = rb_intern("alias_singleton"); - rb_sym_prepend = rb_intern("prepend"); + rb_sym_alias_instance = rb_intern("alias_instance"); + rb_sym_alias_singleton = rb_intern("alias_singleton"); + rb_sym_prepend = rb_intern("prepend"); /* Ensure definition of core Contrast instrumentation modules */ contrast = rb_define_module("Contrast"); agent = rb_define_module_under(contrast, "Agent");