0

My intent is to make it possible to rewrite function start assembly (.text) in a thread safe manner. The problem is that I cannot be sure other threads are not in the very beginning of the function while it is being rewritten. I write about 5 first bytes of the function. This operation is performed throughout all the process lifetime.

I have several ideas how to do this:

  1. Pausing other threads. Not bad. But it is ok on windows only. On linux there is no such a thing. You gotta implement it with signals, which complicates the operation and makes it hard to debug. Also it can harm the performance a lot, since it is a frequent operation.
  2. Using threads priority. The writing thread is granted higher priority, so others should wait for it. As I understand it it can only help on single-core platforms.

My question is are there another ways of doing so.

NuPagadi
  • 1,410
  • 1
  • 11
  • 31
  • Self-modifying code is a really bad idea. It's very obfuscated, usually done only in "bad" code (exploits), a single bit wrong could lead to the system going down hard, and could lead to cache coherency problems. – Some programmer dude Sep 17 '19 at 08:18
  • @Someprogrammerdude, yeah, I do know. – NuPagadi Sep 17 '19 at 08:30
  • I'm a little bit of confused, Are you providing a service that cannot be shutdown so that you cannot just re-deploy/compile your code? – user8510613 Sep 17 '19 at 10:59
  • @user8510613, it is for unit testing. – NuPagadi Sep 17 '19 at 11:13
  • Interesting problem. Are you adding in a jump to some address? Have you looked at using detours? – ASMJunkie Sep 17 '19 at 13:24
  • What does "function start assembly (.text)" mean? Are you talking about the entry point in the run-time library (i.e., the routine that initializes the library and then calls `main(...)`? If so, can you explain how that could ever be executed by more than one thread in the same process? – Solomon Slow Sep 17 '19 at 13:47
  • Also, can you explain why you think it (whatever it is) needs to be re-written in a thread-safe manner? It's usually a mistake to think that any _function_ needs to be "thread safe." Thread-safety really is about protecting _shared data_. What are the data that "function start..." operates on that you think need to be protected? – Solomon Slow Sep 17 '19 at 13:52
  • @ASMJunkie, I do use them. But it's not thread safe. I need to make it thread safe. – NuPagadi Sep 17 '19 at 14:05
  • @SolomonSlow, sure not every function needs to be. But the very process of rewriting does. Because if it is being rewritten in one thread, others shouldn't be in the place being rewritten. The data here is the code. – NuPagadi Sep 17 '19 at 14:17

0 Answers0