[make] fix javac capture

Summary: This deteriorated over time.

Reviewed By: dulmarod

Differential Revision: D5406274

fbshipit-source-id: 286f122
master
Jules Villard 8 years ago committed by Facebook Github Bot
parent 6bbb186ce2
commit 9a2746c143

@ -23,7 +23,6 @@ BUILD_SYSTEMS_TESTS += \
fail_on_issue \ fail_on_issue \
j1 \ j1 \
linters \ linters \
make \
project_root_rel \ project_root_rel \
reactive \ reactive \
run_hidden_linters \ run_hidden_linters \
@ -84,7 +83,7 @@ endif
endif endif
ifeq ($(BUILD_C_ANALYZERS)+$(BUILD_JAVA_ANALYZERS),yes+yes) ifeq ($(BUILD_C_ANALYZERS)+$(BUILD_JAVA_ANALYZERS),yes+yes)
BUILD_SYSTEMS_TESTS += utf8_in_pwd BUILD_SYSTEMS_TESTS += make utf8_in_pwd
endif endif
.PHONY: all .PHONY: all

@ -0,0 +1,17 @@
# Copyright (c) 2017 - present Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
SOURCES = Resources.java Pointers.java Hello.java
OBJECTS = $(SOURCES:.java=.class)
all: Hello.class
%.class: $(SOURCES)
javac $(SOURCES)
clean:
rm -rf $(OBJECTS)

@ -2,28 +2,23 @@
# This is a wrapper for javac # This is a wrapper for javac
if [ -z "$INFER_RESULTS_DIR" ]; then if [ -z "$INFER_OLD_PATH" ]; then
echo '$INFER_RESULTS_DIR with a path to the results dir not provided.' > /dev/stderr
exit 1
elif [ -z "$INFER_OLD_PATH" ]; then
echo '$INFER_OLD_PATH with a copy of $PATH not provided.' > /dev/stderr echo '$INFER_OLD_PATH with a copy of $PATH not provided.' > /dev/stderr
exit 1 exit 1
fi fi
HOST_COMPILER=(`PATH=$INFER_OLD_PATH which javac`) HOST_COMPILER=(`PATH="$INFER_OLD_PATH" which javac`)
COMPILER_ARGS=("$@") COMPILER_ARGS=("$@")
HOST_COMPILER_COMMAND=("$HOST_COMPILER" "${COMPILER_ARGS[@]}") HOST_COMPILER_COMMAND=("$HOST_COMPILER" "${COMPILER_ARGS[@]}")
FRONTEND_COMMAND=("infer" "-a" "capture" "-o" "$INFER_RESULTS_DIR" "--" "javac" "${COMPILER_ARGS[@]}") FRONTEND_COMMAND=("infer" "capture" "--continue" "--" "javac" "${COMPILER_ARGS[@]}")
if [[ "$*" == *-version* ]]; then if [[ "$*" == *-version* ]]; then
"${HOST_COMPILER_COMMAND[@]}" exec "${HOST_COMPILER_COMMAND[@]}"
elif [ -n "$INFER_COMPILER_WRAPPER_IN_RECURSION" ]; then elif [ "$INFER_COMPILER_WRAPPER_IN_RECURSION" != "1" ]; then
if [ -z "$INFER_LISTENER" ]; then export INFER_COMPILER_WRAPPER_IN_RECURSION="1"
"${HOST_COMPILER_COMMAND[@]}" PATH="$INFER_OLD_PATH" exec "${FRONTEND_COMMAND[@]}"
fi
else else
export INFER_COMPILER_WRAPPER_IN_RECURSION="Y" echo 'ERROR: the javac wrapper called itself!' > /dev/stderr
"${FRONTEND_COMMAND[@]}" # internal error
exit 3
fi fi
exit $?

@ -49,8 +49,9 @@ let capture compiler ~prog ~args =
-> ClangWrapper.exe ~prog ~args -> ClangWrapper.exe ~prog ~args
| Make | Make
-> let path_var = "PATH" in -> let path_var = "PATH" in
let new_path = Config.wrappers_dir ^ ":" ^ Sys.getenv_exn path_var in let old_path = Option.value ~default:"" (Sys.getenv path_var) in
let extended_env = `Extend [(path_var, new_path)] in let new_path = Config.wrappers_dir ^ ":" ^ old_path in
let extended_env = `Extend [(path_var, new_path); ("INFER_OLD_PATH", old_path)] in
L.environment_info "Running command %s with env:@\n%a@\n@." prog pp_extended_env extended_env ; L.environment_info "Running command %s with env:@\n%a@\n@." prog pp_extended_env extended_env ;
Unix.fork_exec ~prog ~argv:(prog :: args) ~env:extended_env () |> Unix.waitpid Unix.fork_exec ~prog ~argv:(prog :: args) ~env:extended_env () |> Unix.waitpid
|> function |> function

@ -0,0 +1,59 @@
/*
* Copyright (c) 2015 - present Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package hello;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
class Hello {
void doesNotCauseNPE() {
Pointers.A a = Pointers.mayReturnNull(10);
a.method();
}
void mayCauseNPE() {
Random rng = new Random();
Pointers.A a = Pointers.mayReturnNull(rng.nextInt());
// NPE
a.method();
}
void mayLeakResource() throws IOException {
OutputStream stream = Resources.allocateResource();
if (stream == null) {
return;
}
try {
stream.write(12);
} finally {
// Resource leak
}
}
void twoResources() throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("whatever.txt"));
fos = new FileOutputStream(new File("everwhat.txt"));
fos.write(fis.read());
} finally {
if (fis != null) { fis.close(); } // Resource leak
if (fos != null) { fos.close(); }
}
}
}

@ -5,13 +5,20 @@
# LICENSE file in the root directory of this source tree. An additional grant # LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory. # of patent rights can be found in the PATENTS file in the same directory.
SOURCES = $(shell ls *.c) C_SOURCES = $(shell ls *.c)
OBJECTS = $(SOURCES:.c=.o) JAVA_SOURCES = Resources.java Pointers.java Hello.java
SOURCES = $(C_SOURCES) $(JAVA_SOURCES)
C_OBJECTS = $(C_SOURCES:.c=.o)
JAVA_OBJECTS = $(JAVA_SOURCES:.java=.class)
OBJECTS = $(C_OBJECTS) $(JAVA_OBJECTS)
all: $(OBJECTS) all: $(C_OBJECTS) Hello.class
.c.o: .c.o:
$(CC) -c $< $(CC) -c $<
%.class: $(JAVA_SOURCES)
javac $(JAVA_SOURCES)
clean: clean:
rm -rf $(OBJECTS) rm -rf $(OBJECTS)

@ -0,0 +1,26 @@
/*
* Copyright (c) 2015 - present Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package hello;
public class Pointers {
public static class A {
public void method() {
}
}
public static A mayReturnNull(int i) {
if (i > 0) {
return new A();
}
return null;
}
}

@ -0,0 +1,27 @@
/*
* Copyright (c) 2015 - present Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package hello;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Resources {
public static FileOutputStream allocateResource() {
try {
File file = new File("foo.txt");
return new FileOutputStream(file);
} catch (IOException e) {
return null;
}
}
}

@ -17,7 +17,7 @@ SOURCES = $(wildcard ../codetoanalyze/make/*.c)
include $(TESTS_DIR)/clang.make include $(TESTS_DIR)/clang.make
infer-out/report.json: $(CLANG_DEPS) $(SOURCES) $(HEADERS) infer-out/report.json: $(CLANG_DEPS) $(SOURCES) $(HEADERS)
$(QUIET)$(call silent_on_success,Testing make clang integration,\ $(QUIET)$(call silent_on_success,Testing make clang and javac integration,\
$(INFER_BIN) --dump-duplicate-symbols --project-root $(TESTS_DIR) -a $(ANALYZER) -- \ $(INFER_BIN) --dump-duplicate-symbols --project-root $(TESTS_DIR) -a $(ANALYZER) -- \
make -C ../codetoanalyze/make clean all) make -C ../codetoanalyze/make clean all)
$(QUIET)$(call check_no_duplicates,infer-out/duplicates.txt) $(QUIET)$(call check_no_duplicates,infer-out/duplicates.txt)

@ -1 +1,4 @@
build_systems/codetoanalyze/make/Hello.java, void Hello.mayCauseNPE(), 4, NULL_DEREFERENCE, [start of procedure mayCauseNPE(),Skipped call: function or method not found,start of procedure mayReturnNull(...),Taking false branch,return from a call to Pointers$A Pointers.mayReturnNull(int)]
build_systems/codetoanalyze/make/Hello.java, void Hello.mayLeakResource(), 7, RESOURCE_LEAK, [start of procedure mayLeakResource(),start of procedure allocateResource(),Skipped call: function or method not found,return from a call to FileOutputStream Resources.allocateResource(),Taking false branch]
build_systems/codetoanalyze/make/Hello.java, void Hello.twoResources(), 11, RESOURCE_LEAK, [start of procedure twoResources(),Taking true branch,exception java.io.IOException]
build_systems/codetoanalyze/make/utf8_in_function_names.c, test_성공, 2, NULL_DEREFERENCE, [start of procedure test_성공()] build_systems/codetoanalyze/make/utf8_in_function_names.c, test_성공, 2, NULL_DEREFERENCE, [start of procedure test_성공()]

@ -31,7 +31,7 @@ JAVAC_SOURCES = $(ROOT_DIR)/examples/Hello.java
MAKE_DIR = $(UTF8_DIR)/make MAKE_DIR = $(UTF8_DIR)/make
MAKE_SOURCES_DIR = ../codetoanalyze/make MAKE_SOURCES_DIR = ../codetoanalyze/make
MAKE_SOURCES = $(wildcard $(MAKE_SOURCES_DIR)/*.c) MAKE_SOURCES = $(wildcard $(MAKE_SOURCES_DIR)/*.c $(MAKE_SOURCES_DIR)/*.java)
ANALYZER = infer ANALYZER = infer
CLEAN_EXTRA = $(CMAKE_BUILD_DIR) $(CMAKE_ANALYZE_DIR) infer-out-cmake CLEAN_EXTRA = $(CMAKE_BUILD_DIR) $(CMAKE_ANALYZE_DIR) infer-out-cmake

@ -3,4 +3,7 @@ Hello.java, void Hello.mayCauseNPE(), 4, NULL_DEREFERENCE, [start of procedure m
Hello.java, void Hello.mayLeakResource(), 7, RESOURCE_LEAK, [start of procedure mayLeakResource(),start of procedure allocateResource(),Skipped call: function or method not found,return from a call to FileOutputStream Resources.allocateResource(),Taking false branch] Hello.java, void Hello.mayLeakResource(), 7, RESOURCE_LEAK, [start of procedure mayLeakResource(),start of procedure allocateResource(),Skipped call: function or method not found,return from a call to FileOutputStream Resources.allocateResource(),Taking false branch]
Hello.java, void Hello.twoResources(), 11, RESOURCE_LEAK, [start of procedure twoResources(),Taking true branch,exception java.io.IOException] Hello.java, void Hello.twoResources(), 11, RESOURCE_LEAK, [start of procedure twoResources(),Taking true branch,exception java.io.IOException]
Hello.java, int Hello.test(), 2, NULL_DEREFERENCE, [start of procedure test()] Hello.java, int Hello.test(), 2, NULL_DEREFERENCE, [start of procedure test()]
Hello.java, void Hello.mayCauseNPE(), 4, NULL_DEREFERENCE, [start of procedure mayCauseNPE(),Skipped call: function or method not found,start of procedure mayReturnNull(...),Taking false branch,return from a call to Pointers$A Pointers.mayReturnNull(int)]
Hello.java, void Hello.mayLeakResource(), 7, RESOURCE_LEAK, [start of procedure mayLeakResource(),start of procedure allocateResource(),Skipped call: function or method not found,return from a call to FileOutputStream Resources.allocateResource(),Taking false branch]
Hello.java, void Hello.twoResources(), 11, RESOURCE_LEAK, [start of procedure twoResources(),Taking true branch,exception java.io.IOException]
utf8_in_function_names.c, test_성공, 2, NULL_DEREFERENCE, [start of procedure test_성공()] utf8_in_function_names.c, test_성공, 2, NULL_DEREFERENCE, [start of procedure test_성공()]

@ -1 +1,4 @@
Hello.java, void Hello.mayCauseNPE(), 4, NULL_DEREFERENCE, [start of procedure mayCauseNPE(),Skipped call: function or method not found,start of procedure mayReturnNull(...),Taking false branch,return from a call to Pointers$A Pointers.mayReturnNull(int)]
Hello.java, void Hello.mayLeakResource(), 7, RESOURCE_LEAK, [start of procedure mayLeakResource(),start of procedure allocateResource(),Skipped call: function or method not found,return from a call to FileOutputStream Resources.allocateResource(),Taking false branch]
Hello.java, void Hello.twoResources(), 11, RESOURCE_LEAK, [start of procedure twoResources(),Taking true branch,exception java.io.IOException]
utf8_in_function_names.c, test_성공, 2, NULL_DEREFERENCE, [start of procedure test_성공()] utf8_in_function_names.c, test_성공, 2, NULL_DEREFERENCE, [start of procedure test_성공()]

Loading…
Cancel
Save