Mr Pang
Mr Pang

Reputation: 1181

Can java code cause segmentation fault in linux?

It is said that java is more safer in memory handling than C. In C it is very easy to cause a segmentation fault by accessing invalid pointer. Now I would like to know whether java code can also cause segmentation fault. Can someone give me an example?

Upvotes: 4

Views: 3042

Answers (3)

Level_Up
Level_Up

Reputation: 824

Typically in normal Java program you do not have this problem. But yes if you use thing like sun.misc.Unsafe you can cause segmentation fault. But that why Unsafe is called Unsafe. Normally you don't have to use it so you do not have this problems in your code.

For more information: Where is sun.misc.Unsafe documented?

Bug reported based with Segmentation error with sun.misc.Unsafe https://github.com/eclipse/openj9/issues/4153

Here one example how you can test:

import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class MainClass {

public static void main(String[] args)
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {

    Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
    theUnsafe.setAccessible(true);
    Unsafe unsafe = (Unsafe) theUnsafe.get(null);


     long ten = 10;
     byte size = 1;
     long mem = unsafe.allocateMemory(size);
     //Put here the wrong address!!!
     unsafe.putAddress(1, ten);
     //With this will work:
     //unsafe.putAddress(mem, ten);
     long readValue = unsafe.getAddress(mem);
     System.out.println("result: " + readValue);
}

}

When I execute on Ubuntu 18.04 I get this output: A fatal error has been detected by the Java Runtime Environment:

SIGSEGV (0xb) at pc=0x00007f05bdf04d27, pid=4145, tid=4147

JRE version: OpenJDK Runtime Environment (10.0.2+13) (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)

On Windows I think it will be similar output with fatal error description.

Good luck to all!

Upvotes: 5

Raedwald
Raedwald

Reputation: 48654

Neither the Java Language Specification nor the Java Virtual Machine specification mention segmentation fault as the behaviour of a Java program or the JVM. This is because "segmentation fault" is a system specific thing (specific to Unix operating systems), and Java is meant to be portable. Nor do those specifications give anything like a segmentation fault as permitted behaviour. Furthermore, the specified semantics of Java byte code do not have anything like the "undefined behaviour" of C or C++. Therefore the JVM specification does not allow pure Java code to result in a segmentation fault.

If a pure Java program resulted in a segmentation fault, that would therefore indicate a bug in the JVM itself. Can a JVM on Linux have a bug that could cause a segmentation fault? Of course. But in that case would it be right to say that the Java code caused the segmentation fault? I think not; I would say that "the bug in the JVM" caused the segmentation fault.

So the answer to your question is, pedantically, no.

Java can also call "native code" (which means C code, in practice). That C code could have bugs so it performed operations that have undefined behaviour, and thus subsequently result in a segmentation fault. Again, pedantically, the Java code did not cause the segmentation fault; I would say "the bug in the native code" caused the segmentation fault.

Upvotes: 2

Torben
Torben

Reputation: 3913

Java has an interface for executing native code (JNI). With that it is possible to call code written in C, making segmentation faults possible.

Bugs in the JVM code itself can cause segmentation faults, but those are fairly rare.

With pure Java, no. The language is designed to only allow references that point to valid addresses or null. Following the latter you get a NullPointerException.

Upvotes: 1

Related Questions