Skip to content

Commit

Permalink
Added test for env sanitizing and removed the old test code from the VM
Browse files Browse the repository at this point in the history
  • Loading branch information
schmelter-sap committed Oct 5, 2023
1 parent 5984cbc commit f12bb9b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 45 deletions.
42 changes: 8 additions & 34 deletions src/hotspot/os/posix/malloctrace/mallocTrace2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,10 @@ static void print_needed_preload_env(outputStream* st) {
st->print_cr("Its current value is %s", getenv(LD_PRELOAD));
}

static void remove_malloc_hooks_from_env(char const* env) {
bool debug = DEBUG_ONLY(MallocTraceTestHooks) NOT_DEBUG(false);
static void remove_malloc_hooks_from_env() {
char const* env = ::getenv(LD_PRELOAD);

if ((env == NULL) || (env[0] == '\0')) {
if (debug) {
printf("No env\n");
}

return;
}

Expand All @@ -357,6 +353,7 @@ static void remove_malloc_hooks_from_env(char const* env) {
while ((pos = strstr(pos, LIB_MALLOC_HOOKS)) != NULL) {
if (pos[len] != ':') {
pos += 1;

continue;
}

Expand All @@ -372,29 +369,19 @@ static void remove_malloc_hooks_from_env(char const* env) {
new_env.print("%.*s%s", (int) (c - base + 1), base, pos + len + 1);
} else {
pos += 1;

continue;
}

if (new_env.size() <= 2) {
::unsetenv(LD_PRELOAD);

if (debug) {
printf("Removing LD_PRELOAD=%s\n", env);
}
} else {
stringStream ss;
ss.print("%s=%.*s", LD_PRELOAD, MAX(0, (int) (new_env.size() - 2)), new_env.base() + 1);
ss.print("%.*s", MAX(0, (int) (new_env.size() - 2)), new_env.base() + 1);
::setenv(LD_PRELOAD, ss.base(), 1);

if (debug) {
printf("%s=%s -> %s\n", LD_PRELOAD, env, ss.base());
}
}
return;
}

if (debug) {
printf("Nothing to do for %s=%s\n", LD_PRELOAD, env);
return;
}
}

Expand Down Expand Up @@ -1804,27 +1791,14 @@ void MallocStatistic::initialize() {
}
#endif

char const* preload_env = ::getenv(LD_PRELOAD);

#if defined(ASSERT)
if (MallocTraceTestHooks) {
mallocStatImpl::remove_malloc_hooks_from_env("");
mallocStatImpl::remove_malloc_hooks_from_env(LIB_MALLOC_HOOKS);
mallocStatImpl::remove_malloc_hooks_from_env(LIB_MALLOC_HOOKS ":dummy.so");
mallocStatImpl::remove_malloc_hooks_from_env("dummy.so:" LIB_MALLOC_HOOKS ":dummy.so");
mallocStatImpl::remove_malloc_hooks_from_env("dummy.so:/a/b/" LIB_MALLOC_HOOKS ":dummy.so");
mallocStatImpl::remove_malloc_hooks_from_env("dummy.so:/a/b/" LIB_MALLOC_HOOKS "s:dummy.so");
mallocStatImpl::remove_malloc_hooks_from_env("dosy.so:l" LIB_MALLOC_HOOKS ":" LIB_MALLOC_HOOKS);
}
#endif

// Remove the hooks from the preload env, so we don't
// preload mallochooks for spawned programs.
mallocStatImpl::remove_malloc_hooks_from_env(preload_env);
mallocStatImpl::remove_malloc_hooks_from_env();

// We have to make sure the child process of a fork doesn't run with
// enabled malloc hooks before forking.
pthread_atfork(NULL, NULL, mallocStatImpl::after_child_fork);

mallocStatImpl::MallocStatisticImpl::initialize();

if (MallocTraceAtStartup) {
Expand Down
3 changes: 0 additions & 3 deletions src/hotspot/share/runtime/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,6 @@ const int ObjectAlignmentInBytes = 8;
"the first occurrance of '@pid' is replaced by the pid of the " \
"process).") \
\
develop(bool, MallocTraceTestHooks, false, \
"If set we test the malloc hooks at startup.") \
\
develop(bool, PrintMiscellaneous, false, \
"Print uncategorized debugging information (requires +Verbose)") \
\
Expand Down
76 changes: 68 additions & 8 deletions test/hotspot/jtreg/runtime/malloctrace/MallocHooksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,85 @@
* @run main/native MallocHooksTest
*/

import java.io.File;

import jdk.test.lib.Platform;
import jdk.test.lib.Utils;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;

public class MallocHooksTest {

public static void main(String args[]) throws Exception {
String libDir = System.getProperty("sun.boot.library.path") + "/";
ProcessBuilder pb = ProcessTools.createNativeTestProcessBuilder("testmallochooks");
private static final String LD_PRELOAD = Platform.isOSX() ? "DYLD_INSERT_LIBRARIES" : "LD_PRELOAD";
private static final String LIB_SUFFIX = Platform.isOSX() ? ".dylib" : ".so";
private static final String LIB_DIR = System.getProperty("sun.boot.library.path") + "/";
private static final String NATIVE_DIR = System.getProperty("test.nativepath") + "/";
private static final String LIB_MALLOC_HOOKS = LIB_DIR + "libmallochooks" + LIB_SUFFIX;
private static final String LIB_FAKE_MALLOC_HOOKS = NATIVE_DIR + "libfakemallochooks" + LIB_SUFFIX;

if (Platform.isLinux()) {
pb.environment().put("LD_PRELOAD", libDir + "libmallochooks.so");
} else if (Platform.isOSX()) {
pb.environment().put("DYLD_INSERT_LIBRARIES", libDir + "libmallochooks.dylib");
} else {
public static void main(String args[]) throws Exception {
if (!Platform.isLinux() && !Platform.isOSX()) {
return;
}

if ((args.length > 0) && (args[0].equals("checkEnv"))) {
if (args[1].equals(getLdPrelodEnv())) {
return;
}

throw new Exception("Expected " + LD_PRELOAD + "=\"" + args[1] + "\", but got " +
LD_PRELOAD + "=\"" + getLdPrelodEnv() + "\"");
}

testNoRecursiveCallsForFallbacks();
testEnvSanitizing();
}

private static void testNoRecursiveCallsForFallbacks() throws Exception {
ProcessBuilder pb = ProcessTools.createNativeTestProcessBuilder("testmallochooks");
pb.environment().put(LD_PRELOAD, LIB_MALLOC_HOOKS);
new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
}

private static void testEnvSanitizing() throws Exception {
ProcessBuilder pb = checkEnvProc("");
pb.environment().put(LD_PRELOAD, LIB_MALLOC_HOOKS);
new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
pb = checkEnvProc(LIB_FAKE_MALLOC_HOOKS);
pb.environment().put(LD_PRELOAD, LIB_MALLOC_HOOKS + ":" + LIB_FAKE_MALLOC_HOOKS);
new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
pb = checkEnvProc(LIB_FAKE_MALLOC_HOOKS);
pb.environment().put(LD_PRELOAD, LIB_FAKE_MALLOC_HOOKS + ":" + LIB_MALLOC_HOOKS);
new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
pb = checkEnvProcWithHooks("");
new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
pb = checkEnvProcWithHooks("");
pb.environment().put(LD_PRELOAD, LIB_MALLOC_HOOKS);
new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
pb = checkEnvProcWithHooks(LIB_FAKE_MALLOC_HOOKS);
pb.environment().put(LD_PRELOAD, LIB_MALLOC_HOOKS + ":" + LIB_FAKE_MALLOC_HOOKS);
new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
pb = checkEnvProcWithHooks(LIB_FAKE_MALLOC_HOOKS);
pb.environment().put(LD_PRELOAD, LIB_FAKE_MALLOC_HOOKS + ":" + LIB_MALLOC_HOOKS);
new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
}

private static ProcessBuilder checkEnvProc(String env) {
return ProcessTools.createJavaProcessBuilder(MallocHooksTest.class.getName(), "checkEnv", env);
}

private static ProcessBuilder checkEnvProcWithHooks(String env) {
return ProcessTools.createJavaProcessBuilder("-XX:+UseMallocHooks", MallocHooksTest.class.getName(),
"checkEnv", env);
}

private static String getLdPrelodEnv() {
String env = System.getenv(LD_PRELOAD);

return env == null ? "" : env;
}

private static String absPath(String file) {
return new File(file).getAbsolutePath().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* This is just a dummy library. */

0 comments on commit f12bb9b

Please sign in to comment.