Context: I want to take a Java project that uses a native library via JNA and run its JUnit tests under address sanitizer, suppressing all memory leaks reported from JNA or other Java libraries. I don't care if they're false positive or even true positive leaks, I only need to suppress chosen libraries.
Consider the following JUnit test class:
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import java.lang.Integer;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
public class MyTest {
private void dummyTest() {
new Memory(1024 * 9);
}
@Test
public void find() {
List<Integer> list = new ArrayList<>();
list.add(new Integer(1));
list.stream().map(arg -> {
Pointer ptr = new Memory(1024 * 1024);
return ptr;
}).toArray(Pointer[]::new);
}
@Test
public void test001() {
dummyTest();
}
// [91 more identical tests named test002...test092 follow]
I run it on Docker Ubuntu 24.04 with packages openjdk-8-jdk clang-17 using Gradle, with address sanitizer preloaded. JNA 5.12.1, JUnit 5.8.2, host system is an up-to-date Arch Linux. See repo for full reproduction - run.sh to build and run docker, it executes "./run_with_asan.sh ./gradlew clean build test".
It consistently produces the following leak report:
Direct leak of 1048576 byte(s) in 1 object(s) allocated from:
#0 0x759245cfb372 in malloc (/usr/lib/llvm-17/lib/clang/17/lib/linux/libclang_rt.asan-x86_64.so+0xfb372) (BuildId: 91f375f2a48c6b133a56d8cc059d017ae5de4982)
#1 0x7592316185e6 (<unknown module>)
#2 0x759231607bcf (<unknown module>)
#3 0x759231607bcf (<unknown module>)
#4 0x7592316080f5 (<unknown module>)
#5 0x759231607e3f (<unknown module>)
#6 0x75923197befb (<unknown module>)
SUMMARY: AddressSanitizer: 1048576 byte(s) leaked in 1 allocation(s).
This clearly refers to the native memory allocated on line 19. Could this be a JNA bug?
Expected correct behaviour: a library other than <unknown module> (or asan) appears in this leak report (therefore, no leak report appears if at least one such library is in the suppressions list).
The test class has to be large - this leak report disappears or appears depending on number of JUnit tests and even their exact names. It could be just an effect of changing order of tests. That's part of why I'm very unsure if it's an issue with JNA specifically, or something general with unloading/symbolizer/etc.
--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jna-users/af78fe45-af3a-44f3-86f2-93cd3d29ad63n%40googlegroups.com.
(What's up with deleting messages? I just wanted to ask...)