How do I get result of invoke direct? - reverse-engineering

I am trying to understand what is happening in the following smali code:
I am trying to log the result or stored value in key:
# creates new instance of SecretKeySpec in register v8
new-instance v8, Ljavax/crypto/spec/SecretKeySpec;
# store contant 0x0 in v0
const/4 v0, 0x0
aget-object v0, v9, v0
# store string AES in v1
const-string v1, "AES"
# calls new SecretKeySpec(v0,v1);
invoke-direct {v8, v0, v1}, Ljavax/crypto/spec/SecretKeySpec;-><init>([BLjava/lang/String;)V
.line 115
.local v8, "key":Ljavax/crypto/spec/SecretKeySpec;

The invoke-direct call there is calling the constructor. Object creation in Java (and Dalvik) bytecode takes two instructions. The first, new-instance allocates an uninitialized object, while invoke-direct calls the constructor to initialize this object. The object is stored in v8, as you can see from the new-instance instruction.

Related

What is the cause of "cannot get label for unreachable MBB" in llvm back-end?

I'm writing a llvm back-end, I'm meeting a problem for branch conditional instruction.
I want to translate the llvm IR branch to my specific target. below is what i have tried.
here is the llvm ir
%6 = icmp slt i32 %4, %5
br i1 %6, label %7, label %14
I have defined the instruction and instruction pattern, and i have write the compare instruction and pattern:
def BNE: InstToy<4, (outs), (ins GPR32:$Rd,btargetS15:$S15), "bne\t$Rd, $S15", [(brcond GPR32:$Rd, bb:$S15)]> {
bits<15> S15;
bits<5> Rd;
let Inst{19-5} = S15;
let Inst{4-0} = Rd;
}
The BNE instruction is check the Rd is zero, if no, it will jump to the target pc, otherwise do noting.
And i set "Legal" at isellowering
setOperationAction(ISD::BRCOND, MVT::i32, Legal);
but once i try the command compile the llvm ir
llc test.ll
it will raise the error:
llc: MachineBasicBlock.cpp:59: llvm::MCSymbol* llvm::MachineBasicBlock::getSymbol() const: Assertion `getNumber() >= 0 && "cannot get label for unreachable MBB"' failed.
I expect it can compiler without exception.
with -print-after-all on in llc, i found out the MBB disappears after branch folding pass, I found there is an error in analyzeBranch, after fixed that, the problem is fixed.

Racket: eval, namespace-attach-module vs. namespace-require

Let's say I have a module "foo.rkt" which exports a struct foo, e.g.,
#lang racket (provide foo) (struct foo ())
In another module, I use "foo.rkt" but I'd also like to associate the binding to "struct foo" to another namespace (I don't use prefabs for various reasons, so I can't use namespace-require).
I thought that I could use namespace-attach-module as follows:
(define ns (make-base-namespace))
(namespace-attach-module (current-namespace) "foo.rkt" ns)
(eval '(foo) ns)
But that does not work, as namespace-mapped-symbols reveals that s is not bound in ns (if this is the only place to look for bindings). It does however work in the REPL. Why?
I assume the problem is to avoid instantiating the module in "foo.rkt" twice, since this leads to two incompatible structure definitions.
The function namespace-attach-module is part of the puzzle, but it only attaches
the instantiated module to the namespace ns - that is the name "foo.rkt" is now associated with the correct instantiation of "foo.rkt". It however doesn't make the bindings available in ns - that is the job of namespace-require.
Here is an example:
File: "computer.rkt"
#lang racket
(provide (struct-out computer))
(struct computer (name price) #:transparent)
File: "use-computer.rkt"
#lang racket
(require "computer.rkt") ; instatiate "computer.rkt"
(define ns (make-base-namespace))
(namespace-attach-module (current-namespace) "computer.rkt" ns) ; ns now knows the same instance
(define a-computer
(parameterize ([current-namespace ns])
(namespace-require "computer.rkt")
(eval '(computer "Apple" 2000) ns)))
(computer-name a-computer) ; works, since ns used the same instantiation of "computer.rkt"
The result of running this is:
"Apple"
Note that removing the namespace-attach-module line leads to the error:
computer-name: contract violation;
given value instantiates a different structure type with the same name
expected: computer?
given: (computer "Apple" 2000)
since without the attachment, the namespace-require will instatiate "computer.rkt" a second time leading to two incompatible structures begin declared.

Memory allocation in Julia function

Here is a simple function in Julia 0.5.
function foo{T<:AbstractFloat}(x::T)
a = zero(T)
b = zero(T)
return x
end
I started with julia --track-allocation=user. then include("test.jl"). test.jl only has this function. Run foo(5.). Then Profile.clear_malloc_data(). foo(5.) again in the REPL. Quit julia. Look at the file test.jl.mem.
- function foo{T<:AbstractFloat}(x::T)
- a = zero(T)
194973 b = zero(T)
0 return x
- end
-
Why is there 194973 bytes of memory allocated here? This is also not the first line of the function. Although after Profile.clear_malloc_data(), this shouldn't matter.
Let's clarify some parts of the relevant documentation, which can be a little misleading:
In interpreting the results, there are a few important details. Under the user setting, the first line of any function directly called from the REPL will exhibit allocation due to events that happen in the REPL code itself.
Indeed, the line with allocation is not the first line. However, it is still the first tracked line, since Julia 0.5 has some issues with tracking allocation on the actual first statement (this has been fixed on v0.6). Note that it may also (contrary to what the documentation says) propagate into functions, even if they are annotated with #noinline. The only real solution is to ensure the first statement of what's being called is something you don't want to measure.
More significantly, JIT-compilation also adds to allocation counts, because much of Julia’s compiler is written in Julia (and compilation usually requires memory allocation). The recommended procedure is to force compilation by executing all the commands you want to analyze, then call Profile.clear_malloc_data() to reset all allocation counters. Finally, execute the desired commands and quit Julia to trigger the generation of the .mem files.
You're right that Profile.clear_malloc_data() prevents the allocation for JIT compilation being counted. However, this paragraph is separate from the first paragraph; clear_malloc_data does not do anything about allocation due to "events that happen in the REPL code itself".
Indeed, as I'm sure you suspected, there is no allocation in this function:
julia> function foo{T<:AbstractFloat}(x::T)
a = zero(T)
b = zero(T)
return x
end
foo (generic function with 1 method)
julia> #allocated foo(5.)
0
The numbers you see are due to events in the REPL itself. To avoid this issue, wrap the code to measure in a function. That is to say, we can use this as our test harness, perhaps after disabling inlining on foo with #noinline. For instance, here's a revised test.jl:
#noinline function foo{T<:AbstractFloat}(x::T)
a = zero(T)
b = zero(T)
return x
end
function do_measurements()
x = 0. # dummy statement
x += foo(5.)
x # prevent foo call being optimized out
# (it won't, but this is good practice)
end
Then a REPL session:
julia> include("test.jl")
do_measurements (generic function with 1 method)
julia> do_measurements()
5.0
julia> Profile.clear_malloc_data()
julia> do_measurements()
5.0
Which produces the expected result:
- #noinline function foo{T<:AbstractFloat}(x::T)
0 a = zero(T)
0 b = zero(T)
0 return x
- end
-
- function do_measurements()
155351 x = 0. # dummy statement
0 x += foo(5.)
0 x # prevent foo call being optimized out
- # (it won't, but this is good practice)
- end
-

Check Object Existence in TCL

I want to check that whether object exists or not in tcl.
I looked into info object options but didn't found something specific for object existence and info exists only works for variables not objects.
Any idea?
I created an object of struct::stack
::struct::stack aa
(Dcode) 52 % info object class aa
::struct::stack::stack_oo
It seems that it is in tcloo.
I think in Itcl find command works itcl::find object aa
But not aware of tcl_oo.
You probably just missed it: info object isa object is used to test whether a particular word refers to an object.
% info object isa object abcde
0
% oo::object create abcde
::abcde
% info object isa object abcde
1
% abcde destroy
% info object isa object abcde
0
Here, with some other commands…
% info object isa object oo::object
1
% info object isa object while
0
% info object isa object no.such.thing.at.all.ever
0

Iterate over maps in cython

I would like iterate over c++ maps in cython. How to do that? I have tried it.first but it says compilation error iterator has no attribute first.
Code I tried:
from cython.operator cimport dereference as deref, preincrement as inc
....
cdef map[long,long].iterator it
....
it=cllasy.iswrong_class.begin()
while it!=cllasy.iswrong_class.end():
print ("wd",it.first,it.second)
inc(it)