Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Run tests with valgrind #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN set -x \
g++ openjdk-8-jdk-headless sbt cmake make curl git \
zlib1g-dev \
libgc-dev libunwind8-dev libre2-dev \
valgrind \
&& rm -rf /var/lib/apt/lists/*

ARG LLVM_VERSION=6.0
Expand Down
1 change: 1 addition & 0 deletions bindgen/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ int main(int argc, const char *argv[]) {

char *resolved = realpath(op.getSourcePathList()[0].c_str(), nullptr);
LocationManager locationManager(resolved);
delete [] resolved;

IR ir(libName, linkName, objectName, Package.getValue(), locationManager);

Expand Down
17 changes: 17 additions & 0 deletions bindgen/valgrind-suppressions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
syscall_param_suppression
Memcheck:Param
__pthread_sigmask(set)
fun:__pthread_sigmask
fun:pthread_sigmask
fun:_ZN4llvm3sys7Process25SafelyCloseFileDescriptorEi
fun:_ZN12_GLOBAL__N_18RealFileD1Ev
fun:_ZN12_GLOBAL__N_18RealFileD0Ev
fun:_ZN5clang3vfs10FileSystem16getBufferForFileERKN4llvm5TwineExbb
fun:_ZN5clang11FileManager16getBufferForFileEPKNS_9FileEntryEbb
fun:_ZNK5clang6SrcMgr12ContentCache9getBufferERNS_17DiagnosticsEngineERKNS_13SourceManagerENS_14SourceLocationEPb
fun:_ZNK5clang13SourceManager9getBufferENS_6FileIDENS_14SourceLocationEPb
fun:_ZN5clang12Preprocessor15EnterSourceFileENS_6FileIDEPKNS_15DirectoryLookupENS_14SourceLocationE
fun:_ZN5clang12Preprocessor19EnterMainSourceFileEv
fun:_ZN5clang8ParseASTERNS_4SemaEbb
}
4 changes: 3 additions & 1 deletion bindgen/visitor/TreeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ bool TreeVisitor::VisitVarDecl(clang::VarDecl *varDecl) {
std::shared_ptr<Location> TreeVisitor::getLocation(clang::Decl *decl) {
clang::SourceManager &sm = astContext->getSourceManager();
std::string filename = std::string(sm.getFilename(decl->getLocation()));
std::string path = realpath(filename.c_str(), nullptr);
char *resolved = realpath(filename.c_str(), nullptr);
std::string path = resolved;
delete [] resolved;

unsigned lineNumber = sm.getSpellingLineNumber(decl->getLocation());
return std::make_shared<Location>(path, lineNumber);
Expand Down
6 changes: 6 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import scala.sys.process._

addCommandAlias("verify", "; ^test:compile ; ^test ; ^scripted ; docs/makeSite")
addCommandAlias(
"verifyWithoutMemoryChecks",
"; ^test:compile ; " +
"^testOnly -- -z \"should exist\" -z \"correct bindings\" ; " +
"^scripted ; " +
"docs/makeSite")

val Versions = new {
val scala210 = "2.10.6"
Expand Down
6 changes: 5 additions & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ if [[ ! -e bindgen/target/.llvm-version ]] || [[ "$(<bindgen/target/.llvm-versio
fi

make -C bindgen/target
sbt "${@:-verify}"
if [[ "${LLVM_VERSION}" == "5.0" ]]; then
sbt "${@:-verify}"
else
sbt "${@:-verifyWithoutMemoryChecks}"
fi
27 changes: 26 additions & 1 deletion tests/src/test/scala/org/scalanative/bindgen/BindgenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.scalanative.bindgen
import java.io.File
import org.scalatest.FunSpec
import scala.io.Source
import scala.sys.process.{Process, ProcessLogger}

class BindgenSpec extends FunSpec {
describe("Bindgen") {
Expand Down Expand Up @@ -32,8 +33,28 @@ class BindgenSpec extends FunSpec {
def contentOf(file: File) =
Source.fromFile(file).getLines.mkString("\n").trim()

/**
* @return valgrind exit code
*/
def checkMemoryErrors(inputFile: File): Int = {
val cmd = Seq(
"valgrind",
"--leak-check=full",
"--error-exitcode=1",
"--suppressions=",
new File("bindgen/valgrind-suppressions.txt").getAbsolutePath,
"--show-leak-kinds=definite",
bindgenPath,
inputFile.getAbsolutePath,
"--name",
"lib",
"--"
)
Process(cmd).run(ProcessLogger(_ => ())).exitValue()
}

for (input <- inputDirectory.listFiles() if input.getName.endsWith(".h")) {
it(s"should generate bindings for ${input.getName}") {
it(s"should generate correct bindings for ${input.getName}") {
val testName = input.getName.replace(".h", "")
val expected = new File(inputDirectory, testName + ".scala")
val output = new File(outputDir, testName + ".scala")
Expand All @@ -43,6 +64,10 @@ class BindgenSpec extends FunSpec {
assert(output.exists())
assert(contentOf(output) == contentOf(expected))
}

it(s"should generate bindings for ${input.getName} without memory errors") {
assert(0 == checkMemoryErrors(input))
}
}
}
}