Tuesday, April 12, 2011

File.list() issue in Java

Maybe some of you have encountered problems with filenames that are incorrectly treated by the JVM. To be more precise, File.list() and any other method that uses a file name can give a FileNotFoundException() if the file name contains characters that are encoded with another charset as JVM thinks.


What happens?
Each file system has a so-called "io charset" setting used to transform the chars from the file name in bytes. Most Linux file systems use ISO-8859-1 or ISO-8859-15 by default. The JVM tries to figure out at startup the encoding used on the operating system. If some system variables are not set (like LC_ALL, LANG, etc), it will choose ISO-8859-1 by default. Sometimes however it may guess a wrong charset and then what happens is that it will transform the bytes corresponding to the file name (which it receives from the file system) to wrong chars! :) Of course this happens *only* for some, "special" chars: the chars that have different char points in these 2 charsets; for 'a'-'Z', 0-9 (all the ASCII chars) the char points are the same in all charsets. Not the same thing can be said about German umlauts for instance.


How can this be fixed?
Don't forget to set -Dos.encoding as system property.


file.encoding system property doesn't have any effect on this issue (contrary to what you'll see in some posts). file.encoding affects only the charset of the stream reader.

System calls (part 1)

Definition: a controlled entry point into the kernel. A way by which user programs can execute functions that require a greater privilege.


A few words about privileges... In general, each CPU / microcontroller has a set of operating modes. Among this set of modes, some of them concern security; without going to deep into details, security means:
- what memory areas can be read/written (depending on the memory map, some ranges from the addressing space might point to I/O devices - this happens for memory-mapped I/O)
- what instructions can be executed (I'm referring to microinstructions)
The current IA-32 architecture has 4 so-called privilege or protection rings. Ring0 is the level with the most privileges, ring1 is next, and ring3 is the last. 
[Terminology: rings are also called current protection level (CPL) => sometimes we see the term ring0 to 4, sometimes we see the term CPL-0 to CPL-4.]
Software runs in one of these 4 rings. 
Operating systems manage the switching from one ring to another, by calling CPU instructions that effectively do this. Kernel runs in ring0, device drivers run in ring1, user applications usually run in ring3, which restricts access certain functions (like memory map for instance) that would impact the correct behavior of other applications. Since the kernel is the only code to run in ring0, it can control which application can run in which ring. An application who runs in a low-privileged ring cannot force the CPU to switch to a higher-privilege ring, because it simply hasn't the right to execute the instructions that change the CPU state.


Now let's see what happens when a user application makes a system call.
Each OS has an API that can be accessed by user applications. Among the provided functions are: I/O functions, processes management, IPC, etc. I will talk what happens when making a Linux system call:
1. the application program makes a system call by calling a wrapper function in the C library (glibc, etc), like for instance, fopen.
2. the wrapper function must make all of the system call parameters available to the system function. It receives the parameters in the stack (user process' stack), but must copy them in some registers, in order to pass them to the kernel. These registers are %ecx (counter register), %edx (data register) and %ebp (base pointer) are saved on the user stack and %esp (stack pointer) is copied to %ebp before executing sysenter (it helps in restoring the user stack).
3. After executing sysenter instruction, processor starts execution at sysenter_entry. sysenter_entry is defined in/usr/src/linux/arch/i386/kernel/entry.S
4. wrapper calls __kernel_vsyscall function. This is the function that Address of __kernel_vsyscall is not fixed. The kernel passes this address to user processes using AT_SYSINFO elf parameter.


Of course, user programs can make directly a system call, but wrapper functions provide a more user-friendly way of making these calls.

Saturday, December 11, 2010

Passing parameters to methods

A. Introduction: passing parameters through value. Languages: Java, C# and other
In Java and C# (and other languages) all parameters are transmitted through value, without exception. Let's see what that means:
1. all primitive types are copied
2. for non-primitive types, the reference variables are copied. That means that the objects themselves are not copied.
3. the methods work only with these copies. That means that the initial variables remain unchanged.


Example ( Java ):




import java.util.HashMap;
import java.util.Map;


public class ParamPassingTest {


public static void main (String... args){
// params passing for primitive types
int sum = 1000;
add(sum, 500);
System.out.println(sum);
sum = add(sum, 500);
System.out.println(sum);

// params passing for non-primitive types
Map map = new HashMap();
map.put(10, "Ten");
System.out.println(map);
change(map);
System.out.println(map);
}

private static void change(Map map){
map.put(1, "One");
map.put(2, "Two");
map = new HashMap();
map.put(3, "Three");
System.out.println("Map inside change method: " + map);
}

private static int add(int sum, int addedValue){
sum += addedValue;
return sum;
}
}

The output:


1000
1500
{10=Ten}
Map inside change method: {3=Three}
{1=One, 2=Two, 10=Ten}

Conclusions:
1. The objects themselves are not copied. Only the references to these objects (the reference variables) are copied. The reference variables change only by invoking the new operator; since both references point to the same object, the changes to the object that happen inside the method by invoking some methods on the object are seen also outside the method, right during the method execution.


2. There is some performance penalty: each time we call a method memory is allocated for the copies of the method parameters. However, the footprint is not big. I only want to point out that this is a little penalty when relating to other programming languages, where params are sent through reference (memory address) and no copies are made.




B. Types of parameter passing

Terms:
- formal parameter = the parameter inside the method
- actual parameter = the parameter outside the method (the parameter that is sent when calling the method)
- L-value = (simple definition) the left part of an expression. An expression produces a result and returns a value; ex: "x = x*2" is an expression. It's a method of saying at what address is a variable saved. We use this in order to express what happens to a parameter.

The problem: Inside a method, the value of the formal parameter can be changed. The question is whether AND when the formal parameter is copied to the L-value of the actual parameter => the following types of parameter passing:

1. value (described above): actual parameter is copied into formal parameter; changes to formal parameter are not returned to the actual parameter. Languages: Java, C#.
2. result: the value of the formal parameter is copied to the actual parameter at the return of the method. During the method execution the value of the formal parameter is not found in the actual parameter. Language: CORBA.
3. value result: a combination between value and result. Language: CORBA, the inout parameter.
4. reference: the L-value of the formal parameter is copied to the L-value of the actual parameter => the method can modify the value and address of the actual parameter right during the method's execution. This happens in C for instance when we pass the pointer to a variable to a function.
5. readonly: can use value or reference passing type; the changes of the actual parameter are seen in the formal parameter (for instance in multi-threaded applications, changes outside the method are seen in the formal parameter), but changing the value of the formal parameter is forbidden by the compiler
6. macro and name: not used anymore. I personally don't know too much about them :)