Wednesday, January 21, 2015

Bandit Level 24 → Level 25

A daemon is listening on port 30002 and will give you the password for bandit25 if given the password for bandit24 and a secret numeric 4-digit pincode. There is no way to retrieve the pincode except by going through all of the 10000 combinaties, called brute-forcing.

# create a bash script like following, let's name it test.sh
for i in {1..10000}
do
  echo "UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ $i" >> ./f
done
# bash test.sh, you can see a new file named "f" created
# cat f | nc localhost 30002
.....
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
Correct!

The password of user bandit25 is uNG9O58gUE7snukf3bvZ0rxhtnjzSGzG

Monday, September 22, 2014

My CUDA debug tour

1. Copy array from device to host and print it.


-------------------------------------------------------------------------------

2. Cuda-gdb

When fundamental functions like cudaMalloc failed,usually it shouldn't be its problem. So, first check if there is cuda device available now (driver problem?), then check if problems happen before cudaMalloc.

Tips: you have to be very careful about memory reading and writing. For example, you read from shared memory with the right index. But if you didn't write to it before, it can cause a hidden error! Usually you would think it should be a random value. But in CUDA, your fundamental functions can fail because of this hidden error!

Before you read, you write. Before you write, you check.

Thursday, September 11, 2014

My Github SSH Conguration

1. Generate my keys both public and private
ssh-keygen -t rsa -C "your_email@example.com"

2. Start ssh agent in the background and add ssh private key
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/github

3. See the public key content
cat github.pub

4. Go to github website and add public key there.

5. Go to repository and use the SSH version "git@github.com:USERNAME/REPOSITORY.git"

6. Set origin url with repository address
Git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

7. Check if it is all right.
Git remote -v

--------------------------------------------------------------------------------------------------------
 Tips:
1. Clone specific branch
git clone -b <branch> <remote_repo>

2. See history (make sure you are in a folder with ".git")
git log

Monday, August 25, 2014

CUDA Parallel Programming

Recently I have been studying "Heterogeneous Parallel Programming" founded on Coursera. Take notes would be useful and I haven't written any blog for a long time. So these are notes about my study.

1. CPU = Latency Oriented Cores. Its design is to reduce latency, including operation latency, access latency, branch latency, data latency and so on. CPU is for sequential parts where latency matters.

2. GPU = Throughput Oriented Cores. Its design is to increase throughput, including memory throughput, operation throughput and so on. GPU is for parallel parts where throughput wins.

3. Scalability means the same application runs efficiently on new generation of cores and on more of the same cores. The "Heterogeneous Parallel Programming" course supports scalability.

4. Portbility means the same application runs efficiently on different types of cores and on systems with different organizations and interfaces.

5. A thread is a visualized or abstract Von-Neumann Processor.

6. A CUDA kernel is executed by a grid (array) of threads 
– All threads in a grid run the same kernel code (SPMD)
– Each thread has indexes that it uses to compute memory addresses (decide what data to work on) and make control decisions (Simplifies memory addressing when processing multidimensional data)

7. Divide thread array into multiple blocks
• Threads within a block cooperate via shared memory, atomic operations and barrier synchronization
• Threads in different blocks do not interact

8. Heterogeneous host+device application C program
• Serial parts in host C code
• Parallel parts in device SPMD kernel C code


9. Device code can:
• R/W per-thread registers
• R/W all-shared global memory

10. Host code can
• Transfer data to/from per grid global memory 

11. Compiling procedure

12. There is no dependence between any of the thread blocks. Hardware is free to assign blocks to any processor at any time.

13. All the threads in the same block would be assigned to the same Streaming Multiprocessors (SM, where threads are executed). In the current generation of the CUDA definition, we can assign up to eight thread blocks to each SM. This is a language level constraint.

14. Each block is executed as 32-thread warps. All threads in a warp executes the same instruction when selected. Warps are scheduling units in SM. It is an implementation decision not part of the CUDA programming model.

15. Thread blocks are partitioned into warps and the partition is always the same. DO NOT RELY ON ANY ORDERING WITHIN OR BETWEEN WARPS.

16. Common Programming Strategy:
a.Partition data into subsets or tiles that fit into shared memory.
b.Use one thread block to handle each tile by: loading tiles from global memory to shared memory by multiple threads, performing computations on subsets from shared memory to reduce the traffic to global memory, writing results from shared memory to global memory on completion.

------------------------------------------

Following are notes for CUDA's programming guide:

1. substantial performance improvements can be realized by taking care that the code seldom requires threads in a warp to diverge.

2.









































Sunday, August 24, 2014

Installing PostGIS on Fedora 20

I have never used GIS database before. That's why I decide to install PostGIS to play with. The installation is very easy on Fedora 20. Here are what I have met during my trial.

1. sudo yum install postgresql-server postgresql-contrib postgis
2. sudo postgresql-setup initdb
3. service postgresql start
4. sudo su - postgres (Then the terminal turns into "-base-4.2" in my case)
5. createuser --pwprompt --encrypted leo
6. createdb --encoding=utf-8 --owner=leo first
7. psql -d first -f /usr/share/pgsql/contrib/postgis-64.sql
8. exit

Tuesday, May 6, 2014

Programming for my graduation

With the coming of the graduation, it is time to start my programming work. I'm happy to work on it and the only sad thing is that you have to work yourself. Although I have been doing things only myself for several years, I'm still not used to it. OK, turn to the topic.

My goal is to build a web application for helping specialists monitoring public health and environment. And I found google flu trend working well. More importantly, when I was looking for indexes to help judge, EPI by Yale University seems much great! So, my work seems to repeat what already have been done. In another perspective, my work is an exercise.

I learned to submit my codes before I leave every day, and deploy it on Google App Engine. They can be easily done by PyCharm IDE, which is really excellent! And I made an important decision to give up the idea of building my graduation project google site, which could also be done in my programming.

Github: liaocs2008/GraduationProject
Google App Engine: graduation-thesis-leo.appspot.com



Saturday, March 8, 2014

#3 Linkers, Loaders and Libraries

I'm recently reading the book "Linkers, Loaders and Libraries". It really opens my mind. The process of the generation of a program always confuses me a lot. To understand and put my learning into practice, this paper is to record my study on Chapter 3 of the book.

My programming environment is Fedora 20, 64bit.

1. gcc -c SimpleSection.c
2. objdump -h SimpleSection.o
SimpleSection.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000054  0000000000000000  0000000000000000  00000040  2**2
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 .data         00000008  0000000000000000  0000000000000000  00000094  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000004  0000000000000000  0000000000000000  0000009c  2**2
                  ALLOC
  3 .rodata       00000004  0000000000000000  0000000000000000  0000009c  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .comment      0000002d  0000000000000000  0000000000000000  000000a0  2**0
                  CONTENTS, READONLY
  5 .note.GNU-stack 00000000  0000000000000000  0000000000000000  000000cd  2**0
                  CONTENTS, READONLY
  6 .eh_frame     00000058  0000000000000000  0000000000000000  000000d0  2**3
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA

3. size SimpleSection.o
   text   data    bss    dec    hex filename
    176      8      4    188     bc SimpleSection.o
4. objdump -x -s -d SimpleSection.o
SimpleSection.o:     file format elf64-x86-64
SimpleSection.o
architecture: i386:x86-64, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x0000000000000000

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000054  0000000000000000  0000000000000000  00000040  2**2
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 .data         00000008  0000000000000000  0000000000000000  00000094  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000004  0000000000000000  0000000000000000  0000009c  2**2
                  ALLOC
  3 .rodata       00000004  0000000000000000  0000000000000000  0000009c  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .comment      0000002d  0000000000000000  0000000000000000  000000a0  2**0
                  CONTENTS, READONLY
  5 .note.GNU-stack 00000000  0000000000000000  0000000000000000  000000cd  2**0
                  CONTENTS, READONLY
  6 .eh_frame     00000058  0000000000000000  0000000000000000  000000d0  2**3
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
SYMBOL TABLE:
0000000000000000 l    df *ABS* 0000000000000000 SimpleSection.c
0000000000000000 l    d  .text 0000000000000000 .text
0000000000000000 l    d  .data 0000000000000000 .data
0000000000000000 l    d  .bss 0000000000000000 .bss
0000000000000000 l    d  .rodata 0000000000000000 .rodata
0000000000000004 l     O .data 0000000000000004 static_var.1728
0000000000000000 l     O .bss 0000000000000004 static_var2.1729
0000000000000000 l    d  .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 l    d  .eh_frame 0000000000000000 .eh_frame
0000000000000000 l    d  .comment 0000000000000000 .comment
0000000000000000 g     O .data 0000000000000004 global_init_var
0000000000000004       O *COM* 0000000000000004 global_uninit_var
0000000000000000 g     F .text 0000000000000021 func1
0000000000000000         *UND* 0000000000000000 printf
0000000000000021 g     F .text 0000000000000033 main


Contents of section .text:
 0000 554889e5 4883ec10 897dfc8b 45fc89c6  UH..H....}..E...
 0010 bf000000 00b80000 0000e800 000000c9  ................
 0020 c3554889 e54883ec 10c745fc 01000000  .UH..H....E.....
 0030 8b150000 00008b05 00000000 01c28b45  ...............E
 0040 fc01c28b 45f801d0 89c7e800 0000008b  ....E...........
 0050 45fcc9c3                             E...          
Contents of section .data:
 0000 54000000 55000000                    T...U...      
Contents of section .rodata:
 0000 25640a00                             %d..          
Contents of section .comment:
 0000 00474343 3a202847 4e552920 342e382e  .GCC: (GNU) 4.8.
 0010 32203230 31333132 31322028 52656420  2 20131212 (Red
 0020 48617420 342e382e 322d3729 00        Hat 4.8.2-7).
Contents of section .eh_frame:
 0000 14000000 00000000 017a5200 01781001  .........zR..x..
 0010 1b0c0708 90010000 1c000000 1c000000  ................
 0020 00000000 21000000 00410e10 8602430d  ....!....A....C.
 0030 065c0c07 08000000 1c000000 3c000000  .\..........<...
 0040 00000000 33000000 00410e10 8602430d  ....3....A....C.
 0050 066e0c07 08000000                    .n......      

Disassembly of section .text:

0000000000000000 <func1>:
   0: 55                   push   %rbp
   1: 48 89 e5             mov    %rsp,%rbp
   4: 48 83 ec 10           sub    $0x10,%rsp
   8: 89 7d fc             mov    %edi,-0x4(%rbp)
   b: 8b 45 fc             mov    -0x4(%rbp),%eax
   e: 89 c6                 mov    %eax,%esi
  10: bf 00 00 00 00       mov    $0x0,%edi
11: R_X86_64_32 .rodata
  15: b8 00 00 00 00       mov    $0x0,%eax
  1a: e8 00 00 00 00       callq  1f <func1+0x1f>
1b: R_X86_64_PC32 printf-0x4
  1f: c9                   leaveq
  20: c3                   retq

0000000000000021 <main>:
  21: 55                   push   %rbp
  22: 48 89 e5             mov    %rsp,%rbp
  25: 48 83 ec 10           sub    $0x10,%rsp
  29: c7 45 fc 01 00 00 00 movl   $0x1,-0x4(%rbp)
  30: 8b 15 00 00 00 00     mov    0x0(%rip),%edx        # 36 <main+0x15>
32: R_X86_64_PC32 .data
  36: 8b 05 00 00 00 00     mov    0x0(%rip),%eax        # 3c <main+0x1b>
38: R_X86_64_PC32 .bss-0x4
  3c: 01 c2                 add    %eax,%edx
  3e: 8b 45 fc             mov    -0x4(%rbp),%eax
  41: 01 c2                 add    %eax,%edx
  43: 8b 45 f8             mov    -0x8(%rbp),%eax
  46: 01 d0                 add    %edx,%eax
  48: 89 c7                 mov    %eax,%edi
  4a: e8 00 00 00 00       callq  4f <main+0x2e>
4b: R_X86_64_PC32 func1-0x4
  4f: 8b 45 fc             mov    -0x4(%rbp),%eax
  52: c9                   leaveq
  53: c3                   retq

6. readelf -h SimpleSection.o
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          400 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           64 (bytes)
  Number of section headers:         13
  Section header string table index: 10

7. readelf -s SimpleSection.o
Symbol table '.symtab' contains 16 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS SimpleSection.c
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    4
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5
     6: 0000000000000004     4 OBJECT  LOCAL  DEFAULT    3 static_var.1728
     7: 0000000000000000     4 OBJECT  LOCAL  DEFAULT    4 static_var2.1729
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT    7
     9: 0000000000000000     0 SECTION LOCAL  DEFAULT    8
    10: 0000000000000000     0 SECTION LOCAL  DEFAULT    6
    11: 0000000000000000     4 OBJECT  GLOBAL DEFAULT    3 global_init_var
    12: 0000000000000004     4 OBJECT  GLOBAL DEFAULT  COM global_uninit_var
    13: 0000000000000000    33 FUNC    GLOBAL DEFAULT    1 func1
    14: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND printf
    15: 0000000000000021    51 FUNC    GLOBAL DEFAULT    1 main
8. nm SimpleSection.o
0000000000000000 T func1
0000000000000000 D global_init_var
0000000000000004 C global_uninit_var
0000000000000021 T main
                 U printf
0000000000000004 d static_var.1728
0000000000000000 b static_var2.1729