WebAssembly: Duplicating the top of stack - bytecode

There is not dup instruction, one that lets me duplicate the top of the stack. Which instruction sequence can I use to replicate this behavior?

Wasm doesn't have stack juggling primitives because it has locals. To duplicate the top of the operand stack you need to define a local variable of the right type. Then you can e.g. use the following instruction sequence:
(tee_local $x) (get_local $x)
where $x is your variable.

Related

Pass offset from stack/frame pointer to variable to GCC inline assembly

In all flavors of GCC, local variables that don't fit into registers are stored on the stack. For accessing them, one uses constructs like [ESP+n] or [EBP-n], where n might involve an offset within the variable.
When passing such variables to GCC inline assembly as operands, a spare register is used to store the calculated address. Is there a way to designate operands as "the base register of this variable" and/or "the offset of this variable relative to the base register"?
If you do something like
int stackvar;
...
asm ("...":"r"(stackvar))
you force GCC to load stackvar into register. If you add m constraint, you don't:
int stackvar;
...
asm ("...":"rm"(stackvar))

Match the left side variable of an assignment to the return value of the right side function call

For the following statement inside function func(), I'm trying to figure out the variable name (which is 'dictionary' in the example) that points to the malloc'ed memory region.
Void func() {
uint64_t * dictionary = (uint64_t *) malloc ( sizeof(uint64_t) * 128 );
}
The instrumented malloc() can record the start address and size of the allocation. However, no knowledge of variable 'dictionary' that will be assigned to, any features from the compilers side can help to solve this problem, without modifying the compiler to instrument such assignment statements?
One way I've been thinking is to use the feature that variable 'dictionary' and function 'malloc' is on one source code line or next to each other, the dwarf provides line information.
One thing you can do with Clang and LLVM is emit the code with debug information and then look for malloc calls. These will be assigned to LLVM values, which can be traced (when not compiled with optimizations, that is) to the original C/C++ source code via the debug information metadata.

Deallocate memory previously allocated to a variable (using create)

I'm reading the Gforth manual on memory allocation / deallocation, and this is something I cannot understand. Suppose I allocated a chunk of memory to hold four integers like this:
create foo 1 , 2 , 3 , 4 ,
Then, maybe I allocated more memory and perhaps deallocated some too, and now I want to deallocate foo. How do I do that? Doing foo free and foo 4 cells free results in an error.
One option is to use forget foo but that will 'deallocate' everything that you have defined since you defined foo, and worse than that Gforth doesn't implement it. In Gforth you have to use a 'marker', but this also will revert everything that happened after the marker.
For example (I'll show what you would get entering this into a Gforth interpreter, including the interpreter's responses (denoted by double asterisks)):
marker -unfoo **ok**
create foo 1 , 2 , 3 , 4 , **ok**
/ A test word to get the first thing in foo (1) back
: test foo # . ; **ok**
test **1 ok**
-unfoo **ok**
foo
**:8: Undefined word
>>>foo<<<
Backtrace:
$7FAA4EB4 throw
$7FAB1628 no.extensions
$7FAA502C interpreter-notfound1**
test
**:8: Undefined word
>>>test<<<
Backtrace:
$7FAA4EB4 throw
$7FAB1628 no.extensions
$7FAA502C interpreter-notfound1**
The example is meant to illustrate that foo and test are both gone after you execute -unfoo.
How this actually works is probably my moving the address that the interpreter is taking as the last thing added to the dictionary. -unfoo moves this back to before the address at which foo was added, which is equivalent to freeing the memory used by foo.
Here is another reference for this Starting Forth which is pretty excellent for picking up Forth in general.
In response to a comment on this answer:
This question is quite similar and this answer is pretty helpful. This is probably the most relevant part of the Gforth documentation.
The links above explain Forth versions of malloc(), free() and resize().
So in answer to your original question, you can use free but the memory that you free has to have been allocated by allocate or resize.
create adds an item to the dictionary and is as such not exactly what you want if you are going to want the memory back. My understanding of this, which may be incorrect is that you wouldn't normally remove things from the dictionary during the course of normal execution.
The best way to store a string depends on what you want to do with it. If you don't need it to exist for the lifetime of the programme you can just use s" by itself as this returns a length and an address.
In general, I would say that using create is quite a good idea but it does have limitations. If the string changes you will have to create a new dictionary entry for it. If you can set an upper bound on the string length, then once you have created a word you can go back and overwrite the memory that has been alloted for it.
This is another answer that I gave that gives an example of defining a string word.
So in summary, if you really do need to be able to deallocate the memory, use heap methods that Gforth provides (I think that they are in the Forth standard but I don't know if all Forths implement them). If you don't you can use the dictionary as per your question.
The CREATE ALLOT and VARIABLE words consume dictionary space (look it up in the ISO 93 standard.)
Traditionally you can
FORGET aap
, but that removes aap and each definition that is defined later than aap , totally different from free().
In complicated Forth's like gforth this simple mechanism no longer works. It amounted to truncating the linked list and resetting an allocation pointer (HERE/DP)
In gforth you are obliged to use MARKER. In putting
MARKER aap
you can use aap to remove aap and later defined words.
MARKER is cumbersome and it is much easier to restart your Forth.

GDB: Create local variable?

I'm using Xcode's debugger. While stopped at a breakpoint, is there a command I can type in the GDB command prompt to create a local variable? If so, how? Please provide an example.
I know I can do it in the code and then recompile the program, but I'm looking for a faster way.
If you don't need to reference the variable in your code but just want to do some ad-hoc investigation, you can use Convenience Variables by using the set command with a variable name starting with $:
(gdb) set $foo = method_that_makes_something()
(gdb) set $bar = 15
(gdb) p $bar
$4 = 15
You'll notice when you print things it's prefixed with a numeric variable - you can use these to refer to that value later as well:
(gdb) p $4
$5 = 15
To reiterate: this doesn't actually affect the program's stack, and it can't, as that would break a lot of things. But it's useful if you just need a local playground, some loop variables, etc.
While you can't modify the stack, you can interact with the program's memory space - you can call functions (including malloc) and construct objects, but these will all live in static memory, not as local variables on the stack.
Since a local variable would require stack space and the (compiled) code is tied to the stack layout, no you can't.
Comparing this with scripting languages is not quite appropriate.
Values printed by the print command are saved in the GDB "value history". This allows you to refer to them in other expressions.
For example, suppose you have just printed a pointer to a structure and want to see the contents of the structure. It suffices to type
p *$

Setting a watch point in GDB

I am operating a huge code base and want to monitor a value of a particular variable (which is buried deep down inside one of the files)especially when it gets set to zero.
1) Variable does not belong to global scope .Is there a better option than to first set breakpoint into the function where it is defined and then set the watch point?
2) After trying the option in 1 I see that watch point gets deleted after a while saying its out of frame which used this .This way it adds to the tediousness of the procedure since I have to add it again and again?Any workarounds?
3) Is there a way to check ie watch if a particular variable is equal to 0( or any specific constant)?
want to monitor a value of a particular variable
Often this is not the best approach, especially in large codebases.
What you really likely want to do is understand the invariants, and assert that they are true on entry and exit to various parts of the code.
1) Variable does not belong to global scope .Is there a better option than to first set breakpoint into the function where it is defined and then set the watch point?
No. For automatic (stack) variables you have to be in the scope where the variable is "active".
What you can do is set a breakpoint on some line, and attach commands to that breakpoint that will set the watchpoint automatically, e.g.
(gdb) break foo.c:123
(gdb) commands 1
silent
watch some_local
continue
end
3) Is there a way to check ie watch if a particular variable is equal to 0
You can't do that with a watchpoint, but you can with a conditional breakpoint:
(gdb) break foo.c:234 if some_local == 0
I will assume that you are using Linux. You can try this:
The first step is to make the variable static, like:
static int myVar;
Then, after compiling your code using -ggdb, you must discover the address of the variable inside your binary, like so (I have used a real case as example):
readelf -s pdv | grep tmp | c++filt
In my situation, the output is:
47: 081c1474 4 OBJECT LOCAL DEFAULT 25 startProc(int)::tmp
The address in this case is 081c1474. Now you can set a watch point inside GDB:
watch *0x081c1474
Mind the "*0x" before the correct address.
I know this question is old, but I hope it helps anyway.

Resources