Posts by Chang Park

File permission

If you type ls -l, you can see file permission like below.

 
drwxr-xr-x 3 chang users 512  Jan 21 22:49 101 
-rw-r--r-- 1 chang users 2312 Jan 15 21:54 index.html 
drwxr-xr-x 2 chang users 512  Jan 15 16:54 javascript 
-rw-r--r-- 1 chang users 2859 Jan 12 16:54 work.html 

permission-structure

  • First letter indicates if it is a Directory.
  • Next each group of three characters indicate file permission
    • First three are permissions for owner, second three are permissions for the group, and last three are permissions for everybody. Every “-” means  no permission.
      • r: read
      • w: write
      • x: execute

 

 

[ ssh ] Public key vs. Private key

How they work

keys

If you generate key with the command sshkeygen -t rsa, you will get two types of key.

Public key is for encrypting and private key is for decrypting. Which one do you think more important? Yes, of course private key. You can provide your public key to others to communicate. So, other people will use the public key to encrypt the file to send.

However, you are the only one who have a private key. That means you are the only one person who can decrypt the files. This is how the public key and private key work. 

  • Example
    • For Git, you need to copy your public key and paste it on the git site before use.
    • So if you pull repositories from Git, the server encrypts the repositories with your public key before sending.

Dead code

dead

Definition

Dead code can never be executed (unreachable code), and it only affects dead variables (written, but never read again), that is, irrelevant to the program. It needs to be deleted for several reasons.

  • It shrinks program size
  • It allows the running program to avoid executing irrelevant operations, which reduces its running time.
  • It can also enable further optimizations by simplifying program structure. (avoid confusion)

 

Method Inlining

Definition

Inlining is an optimization performed by the Java Just-In-Time compiler.

public int addPlusOne(int a, int b){
   return a + b + 1;
}

public void testAddPlusOne(){
   int v1 = addPlusOne(2, 5);
   int v2 = addPlusOne(7,13);
}

The compiler may decide to replace your function call with the body of the function, so the result would effectively look like this

public void testAddPlusOne(){
   int v1 = 2 + 5 + 1;
   int v2 = 7 + 13 + 1;
}
  • The compiler does this to save the overhead of actually making a function call, which would involve pushing each parameter on to the stack.
  • This can be done for non-virtual functions because you won’t know which code it contains if virtual function is overridden by others until runtime. Since java methods are virtual by default. You can make methods static or final to make non-virtual.

Pipe & Redirection

Bash-script-21

Difference between Pipe & Redirection

Pipe is used to pass output to another Program or Utility.

  • thing1 | thing2
    • thing1’s output s will be passed to things2 Program or Utility.

Redirect is used to pass output to either a File or Stream.

  • thing1 > thing2
    • thing1’s outputs will be placed in a file called thing2.

 

Pipe

With Pipe, the standard output of one command is fed into the standard input of another.

$ ls | grep "java"

It will find all the file names from ls that contain “java” string.

 

Redirection

Standard output directs its contents to the display. To redirect standard output to a file, the “>” character is used.

$ ls > file_list.txt

In this example, all the file names from ls command will be written in an file named file_list.txt. Since the output of ls was redirected to the file, no results on the display. Each time the command above is repeated, file_list.txt is overwritten (from the beginning) with the output of the command ls. If you want the new results to be appended to the file instead, use “>>“.

$ ls >> file_list.txt

 

Standard Input gets its contents from the keyboard, but like standard output, it can be redirected. To redirect standard input from a file instead of the keyboard, the “<” character is used.

$ sort < file_list.txt

Sort command print the contents of file_list.txt. We could redirect standard output to another file.

$ sort < file_list.txt > sorted_file_list.txt

As you can see, a command can have both its input and output redirected. The redirection operators (the “<” and “>”) must appear after the other options and arguments in the command.

 

Filters

  • sort: Sorts standard input then outputs the sorted result on standard output.
  • uniq: Given a sorted stream of data from standard input, it removes duplicate lines of data (i.e., it makes sure that every line is unique).
  • grep: Examines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters.
  • fmt: Reads text from standard input, then outputs formatted text on standard output.
  • pr: Takes text input from standard input and splits the data into pages with page breaks, headers and footers in preparation for printing.
  • head: Outputs the first few lines of its input. Useful for getting the header of a file.
  • tail: Outputs the last few lines of its input. Useful for things like getting the most recent entries from a log file.
  • tr: Translates characters. Can be used to perform tasks such as upper/lowercase conversions or changing line termination characters from one type to another (for example, converting DOS text files into Unix style text files).
  • sed: Stream editor. Can perform more sophisticated text translations than tr.
  • awk: An entire programming language designed for constructing filters. Extremely powerful.

 

Soot

soot-logo

What is Soot

A framework for analyzing and transforming Java and Android Applications.

  • Soot can process and produce code from the following sources.
    • Java bytecode
    • Android bytecode
    • Jimple
    • Jasmin
  • This is a powerful framework that many research group use.
  • For example, Soot can transform application file(apk) to Jimple code which developers can see actual code.
  • To run Soot, you will also need the projects Jasmin and Heros.

Phantom Reference in Soot

Phantom

Definition

Phantom classes are classes that are neither in the process directory nor on the Soot classpath, but that are referenced by some class / method body that Soot loads.

  • Process Directory: contains Application classes.
  • Soot Classpath: contains Library Classes.

There are several scenarios that reference classes don’t exist. A developer might leave the code to improve it later, or he got rid of some jar files that was in use but not executable. Sometimes people left such code because it will not be reachable on runtime anyway.

However there is another case that is significant. The actual reference classes can be inside of the actual device, and can be not showing until it runs. So in this case, it will be bad idea to touch or to handle this phantom classes.

 

–allow-phantom-refs

  • Soot option that can handle phantom classes. (not recommended by Soot)
  • Only use this option if you know what you are doing.
  • Soot will not just abort and fail on such an unresolvable reference, but create an empty stub called a phantom class which in turn contains phanom methods to make up for the missing bits and pieces.

 

Garbage Collection

GCDuke

How Garbage Collection Really works

All objects are allocated on the heap area managed by the JVM. Every item that the developer uses is treated this way, including class objects, static variables, and even the code itself. As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.

allocation

Garbage Collector root

  • Local variables in the main method
  • The main thread
  • Static variables of the main class

gc-roots

Mark and Sweep Algorithm

  • The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.
  • All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.
  • When objects are no longer referenced directly or indirectly by a GC root, they will be removed. There are no classic memory leaks.

 

 

Java Memory Structure

types of memory area

memory21

Static memory (Method area)

  • Method information and Field information.
  • Global constants and other data generated by the compiler(e.g. info to support garbage collection) are allocated static storage.
  • Static variables are bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. E.g., static variables.

Stack

  • Names local to a procedure and Parameters are allocated space on a stack. The size of stack can not be determined at compile time.

Heap

  • Data that may outlive the call to the procedure that created it is usually allocated on a heap. E.g. new to create objects that may be passed from procedure to procedure.
  • The size of heap can not be determined at compile time. Referenced only through pointers or references, e.g., dynamic objects in C++, all objects in Java
  • Garbage collection cleans up data(not using) on heap.

 

Example

class Point {
   int x, y;

   void move(int x, int y) {
      this.x = x;
      this.y = y;
   }

   int getX() {
      return x;
   }

   int getY() {
      return y;
   }

   void setX(int x) {
      this.x = x;
   }

   void setY(int y) {
      this.y = y;
   }

   void draw() {
      System.out.println("Point(" + x + ", " + y + ")");
   }
}

class PointTest {
   public static void main(String args[]) {
      Point p1, p2 = null;

      p1 = new Point();
      p1.move(4, 4);
      // Point 1
      p2 = new Point();
      p2.move(2, 4);

      // Point 2
   }
}

[ PointTest class loaded ]

m1

 

[ JVM calls Main method ]

m2

 

[ At Point 1 in Main method ]

m3

 

 [ At Point 2 in Main method ]

m4

 

Java features

java_logo

Simple

  • Java is Easy to write and more readable and eye catching.
  • Java has a concise, cohesive set of features that makes it easy to learn and use.
  • Most of the concepts are drew from C++ this making Java learning simpler.

Secure

  • Java program cannot harm other system thus making it secure.
  • Java provides a secure means of creating Internet applications.
  • Java provides secure way to access web applications.

Portable

  • Java programs can execute in any environment for which there is a Java run-time-system.(JVM)
  • Java programs can be run on any platform (Linux, Window, Mac)
  • Java programs can be transferred over world wide web (e.g applets)

Object-oriented

  • Java programming is pure object-oriented programming language. (while C++ is semi object oriented)
  • Like C++, java provides most of the object oriented features.

Robust

  • Java encourages error-free programming by being strictly typed and performing run-time check.

Multithreaded

  • Java provides integrated support for multithreaded programming.

Architecture-neutral

  • Java is not tied to a specific machine or operating system architecture.
  • Machine independent i.e Java is independent of hardware.

Interpreted

  • Java supports cross-platform code through the use of Java bytecode.
  • Bytecode can be interpreted on any platform by JVM.

High Performance

  • Bytecodes are highly optimized.
  • JVM can be executed them much faster.

Distributed

  • Java was designed with the distributed environment.
  • Java can be transmit, run over internet.

Dynamic

  • Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve access to objects at run time.