self-modifying

Modifying region of memory - returns 0xCC VC++

此生再无相见时 提交于 2021-02-04 14:08:21
问题 I am modifying some sections of an executable code compiled in a dll. But a single byte at a fixed address from the entire segment that I am modifying can't be changed, not even read. The code is very simple: SEGMENT_DATA segInfo = getSegmentInfo(mHandle, segmentName); if (segInfo.inFileSegmentAddr == 0) return false; DWORD mOlProtection; DWORD mOlProtection_1; if (segInfo.architecture != MY_ARCH) { printf(" Not the same architecture!\n"); return 0; } if(VirtualProtect((LPVOID)segInfo

Self-modifying code on Darwin 10.15 resulting in “malformed mach-o image”?

前提是你 提交于 2020-05-09 04:35:08
问题 I have a program that generates self-modifying code (see https://tigress.wtf/selfModify.html in case you're interested). It runs on x86 Darwin and Linux. On Darwin, I compile with gcc -g -segprot __TEXT rwx rwx self_modifying.c -o self_modifying.exe Recently, this seems not to work, I get dyld: malformed mach-o image: __TEXT segment maps start of file but is writable when I run the program. I'm running clang version 6.0.1 on MacOS 10.15.3. Any help would be appreciated. 回答1: @AlexDenisov is

Self Modifying Code [C++]

戏子无情 提交于 2020-01-01 05:39:30
问题 I was reading a codebreakers journal article on self-modifying code and there was this code snippet: void Demo(int (*_printf) (const char *,...)) { _printf("Hello, OSIX!n"); return; } int main(int argc, char* argv[]) { char buff[1000]; int (*_printf) (const char *,...); int (*_main) (int, char **); void (*_Demo) (int (*) (const char *,...)); _printf=printf; int func_len = (unsigned int) _main ­- (unsigned int) _Demo; for (int a=0; a<func_len; a++) buff[a] = ((char *) _Demo)[a]; _Demo = (void

Observing stale instruction fetching on x86 with self-modifying code

橙三吉。 提交于 2019-12-27 09:51:09
问题 I've been told and have read from Intel's manuals that it is possible to write instructions to memory, but the instruction prefetch queue has already fetched the stale instructions and will execute those old instructions. I have been unsuccessful in observing this behavior. My methodology is as follows. The Intel software development manual states from section 11.6 that A write to a memory location in a code segment that is currently cached in the processor causes the associated cache line

Observing stale instruction fetching on x86 with self-modifying code

半世苍凉 提交于 2019-12-27 09:49:54
问题 I've been told and have read from Intel's manuals that it is possible to write instructions to memory, but the instruction prefetch queue has already fetched the stale instructions and will execute those old instructions. I have been unsuccessful in observing this behavior. My methodology is as follows. The Intel software development manual states from section 11.6 that A write to a memory location in a code segment that is currently cached in the processor causes the associated cache line

Self-modifying code in Ruby

左心房为你撑大大i 提交于 2019-12-24 15:11:40
问题 I am concerned about writing self-modifying code in Ruby. And by self-modifying, I mean being able to write functions that take a code block as an input value, and output another code block based on this. (I am not asking about basics such as redefining methods at runtime.) What I might want to do is, for example, having the following block, _x_ = lambda { |a, b, c, d| b + c } one can notice that arguments a and d are not used in the body at all, so I would like a function eg. #strip to

copy and call function

♀尐吖头ヾ 提交于 2019-12-23 04:12:12
问题 I'd like to copy and call a function, but the code below segfaults when calling the buffer. What do I have to change? (Linux, x86) #include <string.h> #include <malloc.h> #include <stdio.h> int foo () { return 12; } void foo_end () {} int main () { int s = (unsigned long long) foo_end - (unsigned long long) foo; int (*f) () = (int (*)()) malloc (s); memcpy ((void*) f, (const void*) foo, s); printf ("%d %d\n", f (), foo ()); } EDIT: Working solution: #include <string.h> #include <malloc.h>

copy and call function

此生再无相见时 提交于 2019-12-23 04:12:08
问题 I'd like to copy and call a function, but the code below segfaults when calling the buffer. What do I have to change? (Linux, x86) #include <string.h> #include <malloc.h> #include <stdio.h> int foo () { return 12; } void foo_end () {} int main () { int s = (unsigned long long) foo_end - (unsigned long long) foo; int (*f) () = (int (*)()) malloc (s); memcpy ((void*) f, (const void*) foo, s); printf ("%d %d\n", f (), foo ()); } EDIT: Working solution: #include <string.h> #include <malloc.h>

Dynamic relocation of code section

孤人 提交于 2019-12-21 07:29:23
问题 Just out of curiosity I wonder if it is possible to relocate a piece of code during the execution of a program. For instance, I have a function and this function should be replaced in memory each time after it has been executed. One idea that came up our mind is to use self-modifying code to do that. According to some online resources, self-modifying code can be executed on Linux, but still I am not sure if such a dynamic relocation is possible. Has anyone experience with that? 回答1: Yes

How can I write self-modifying code that runs efficiently on modern x64 processors?

自古美人都是妖i 提交于 2019-12-20 09:38:03
问题 I'm trying to speed up a variable-bitwidth integer compression scheme and I'm interested in generating and executing assembly code on-the-fly. Currently a lot of time is spent on mispredicted indirect branches, and generating code based on the series of bitwidths as found seems to be the only way avoid this penalty. The general technique is referred to as "subroutine threading" (or "call threading", although this has other definitions as well). The goal is to take advantage of the processors