flat assembler
Message board for the users of flat assembler.
Index
> Windows > 64 bit not understood sub rsp,8 ! Goto page Previous 1, 2, 3, 4, 5, 6, 7, 8, 9 Next |
Author |
|
revolution 18 May 2022, 14:01
The only reason for the OS to push a stack frame to your stack is if there is an exception and you asked to receive them.
Consider the case where you don't ask to receive exceptions, the normal case for a program like fasm. Then it simply crashes with the "you app has been terminated" message. Meaning you neither gain or lose anything, either way your app dies. Remember all exceptions have to be dealt with, you can't ignore them. Fix the problem your app caused, or die: are the only two options when an exception is forwarded to your app. And simply die: if the exception isn't forwarded and you let the OS decide for you. |
|||
18 May 2022, 14:01 |
|
DimonSoft 18 May 2022, 14:31
Furs wrote: What do you understand from this? Didn’t bother to check but if my memory serves me about the context, this part is talking of the case when someone stores data at Address < ESP (for x86 case). Then it definitely can be rewritten, since any part of the stack where Address < ESP is defined as not used at the moment. Doing sub rsp, 1 just somewhat increases the amount of stack memory that is guaranteed to not be overwritten. The problem could be with add rsp, 1 if there is some data in that byte that gets from under the RSP “protection”, but it would be about overwriting the data, not exceptions. Whether any version of Windows pushes something onto the application thread’s stack on exception occurence is another question. If it does (or did) in any version, one would better avoid arbitrary RSP values for compatibility. Values that fall into the stack address range should still cause no new exception (unless some alignment checking happens while processing the exception), only data before the old [RSP] would get rewritten. |
|||
18 May 2022, 14:31 |
|
Ville 18 May 2022, 14:37
In general, interrupts and exceptions should be handled fully at the OS side (OS stack mem.), unless otherwise requested by the application.
|
|||
18 May 2022, 14:37 |
|
revolution 18 May 2022, 14:40
DimonSoft wrote: Whether any version of Windows pushes something onto the application thread’s stack on exception occurence is another question. If it does (or did) in any version, one would better avoid arbitrary RSP values for compatibility. Clearly the explanation I gave is insufficient. There is no need to guess for this. It is 100% characterisable. You only ever get exceptions delivered if you ask for them, they are relevant to an instruction you just executed, and you can do something to fix it. Windows doesn't deliver informative exceptions that you can only look at but not fix. It won't deliver paged-out exceptions to you. What could you do about it? You can't rewrite the page tables to fix it. Only a ring-0 process can do that. |
|||
18 May 2022, 14:40 |
|
DimonSoft 18 May 2022, 15:10
revolution wrote:
Oh dear. You haven’t given a link to a place where this behaviour is clearly documented. I didn’t bother checking. That’s why I used the word IF. That’s the way people do when they learn to be cautious about information they see in the Internet, no matter how trustworthy the source is considered to be. It’s about informational hygiene and avoiding to spread claims that one is not completely sure about (whether they’re true or not). |
|||
18 May 2022, 15:10 |
|
macomics 18 May 2022, 18:08
IntelSDM-vol-2b: SUB wrote: SUB—Subtract IntelSDM-vol-2b: SUB wrote:
Indirect exceptions that occur when switching tasks or loading pages from disk are processed in ring-0 and are not delivered to ring-3. |
|||
18 May 2022, 18:08 |
|
sinsi 18 May 2022, 21:19
Raymond wrote about exceptions and exception handling, he basically said to forget about exception handling.
If you handle an exception, you are hiding why the exception occurred. Better to have your app crash and fix the bad code, since properly written code should never cause an exception. |
|||
18 May 2022, 21:19 |
|
macomics 18 May 2022, 21:28
This is great, but exceptions do not occur in random places. They are generated by certain conditions. And we are talking about a specific instruction that does not generate a exceptions. But some argue that the stack pointer cannot be adjusted using times 8:sub esp, 1.
|
|||
18 May 2022, 21:28 |
|
Overclick 18 May 2022, 23:28
What about OS context switch? What if it interupted between times 8:sub esp, 1 ? Just asking
|
|||
18 May 2022, 23:28 |
|
macomics 19 May 2022, 00:11
Read it
Hardware interrupts and exceptions are handled first by the system task. That is, when an interrupt signal is received, the CPU switches to the system task, preserving the context of the user task. Each TSS has not only 4 stack pointers for each protection ring, but also ShadowStack. To save the context of the task, there are TSS that do not require a block in the stack for any data. The entire execution context is saved in the TSS segment and a new system task context is loaded. Even the value in CR3 is updated. Then the interrupt procedure is called with the return address stored in the stack. Upon returning from an interruption, the system must manually restore the context of the interrupted user task itself or switch to another one. But not all interrupts require switching tasks. Some can switch only the protection ring from ring3 to ring0 by reloading the rsp0 value from TSS into rsp, get a symbol from the keyboard, put it in the system data structures and return back. For all this charm, the ring0 stack will be filled. I tried to simplify the explanation to focus on stack operations. Last edited by macomics on 19 May 2022, 01:00; edited 1 time in total |
|||
19 May 2022, 00:11 |
|
Overclick 19 May 2022, 00:56
Does Windows use all of that hardware to conrol usermode? Doesn't it stores context to 'reserved stack space'?
|
|||
19 May 2022, 00:56 |
|
macomics 19 May 2022, 01:06
All the mechanisms described above are related to the peculiarity of the functioning of the processor itself. Windows has no choice how to handle interrupts. Windows can only build its own model of working with this hardware.
It is enough for you to execute one call task_gate command to generate a huge amount of CPU work on reloading task contexts. After switching the context, the CPU in priority order may cause several more interrupts. For example, to update the FPU/XMM context. Overclick wrote: Doesn't it stores context to 'reserved stack space'? SEH/VEH calls reaching ring3 are already made by the system code as an alternative to the processing procedure. By this point, the processing of the hardware interrupt has already been completed. Last edited by macomics on 19 May 2022, 01:11; edited 1 time in total |
|||
19 May 2022, 01:06 |
|
revolution 19 May 2022, 01:10
Overclick wrote: Does Windows use all of that hardware to conrol usermode? Overclick wrote: Doesn't it stores context to 'reserved stack space'? |
|||
19 May 2022, 01:10 |
|
revolution 19 May 2022, 01:15
sinsi wrote: Raymond wrote about exceptions and exception handling, he basically said to forget about exception handling. I showed this for console fasm a few years ago, to reduce the memory footprint and not be greedy and hog all the RAM. Also DIV/0 is another place where user controlled scripting can do unexpected things, and catching exceptions is very handy and tidy. So I don't believe there is a one-exception-style that suits all apps equally. |
|||
19 May 2022, 01:15 |
|
macomics 19 May 2022, 01:21
revolution wrote: Not for unrelated events like mouse move. Code: TSS | ring0 ring3 ss:sp0| ss:sp3+$38 = ip ss:sp1| ss:sp3+$30 = bp ss:sp2| ss:sp3+$20 = xword ss:sp3| ss:sp3+$00 = $20 bytes for calls ------+ interrupt here (vector/trap handler) ss:sp = TSS.ss:sp0 (TSS.ss:sp3 doesn't change) ss:sp0+$20 = ss3 ss:sp0+$18 = sp3 ss:sp0+$10 = fl ss:sp0+$08 = cs3 ss:sp0+$00 = ip3 iret ss:sp3+$00 = $20 bytes for calls revolution wrote: I disagree there. An app can use reserved un-committed memory to allocate on demand. Then use exceptions to allocate each new piece. Code: stack <commit>,<reserv> Last edited by macomics on 19 May 2022, 01:29; edited 1 time in total |
|||
19 May 2022, 01:21 |
|
Overclick 19 May 2022, 01:26
Quote:
What winapi SetTimer does then? I was thinking that all usermode under programmable interupts exept kernel or something. |
|||
19 May 2022, 01:26 |
|
revolution 19 May 2022, 01:30
So let's discuss asynchronous exception like CTRL-C. How does Windows provide those if is is not allowed to interrupt your thread with random exceptions you didn't cause?
The answer is really very nice. But let's clear this up first. If you do nothing and don't ask to receive them, then CTRL-C will do as all unhandled exceptions do in Windows, kill your app. But you can tell Windows to deliver CTRL-C into the keystroke buffer it maintains for your app. So when you read the keystrokes then CTRL-C is just another entry in the buffer. If you don't want CTRL-C in the key buffer then you can elect to receive it as an exception. But it won't interrupt any existing thread. Windows isn't permitted to interrupt your code randomly. What it does is it creates a new thread in your process, with its associated new register set and new stack, and sends the exception to that thread executing your designated exception handler in the new thread. |
|||
19 May 2022, 01:30 |
|
revolution 19 May 2022, 01:32
Overclick wrote: What winapi SetTimer does then? I was thinking that all usermode under programmable interupts exept kernel or something. |
|||
19 May 2022, 01:32 |
|
macomics 19 May 2022, 01:33
To determine whether the timer is triggered, it is enough to keep an array of trigger tick counters sorted in ascending order and check whether the nearest one has fired on a cyclic interrupt.
|
|||
19 May 2022, 01:33 |
|
Goto page Previous 1, 2, 3, 4, 5, 6, 7, 8, 9 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.