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

Fix tracking references of delayed methods and make generating backtraces safer #3455

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions scripted-tests/run/backtrace/src/main/scala/Hello.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ object Hello {
val expectedHello =
if (isMac) {
List(
"Hello$.error(Hello.scala:10)",
"Hello$.g(Hello.scala:8)",
"Hello$.f(Hello.scala:6)",
"Hello$.main(Hello.scala:4)",
"Hello.main(Hello.scala:4)"
"Hello$.error(Hello.scala:11)",
"Hello$.g(Hello.scala:9)",
"Hello$.f(Hello.scala:7)",
"Hello$.main(Hello.scala:5)",
"Hello.main(Hello.scala:5)"
)
} else {
List(
Expand Down
32 changes: 18 additions & 14 deletions tools/src/main/scala/scala/scalanative/linker/Reach.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Reach(
loader
.load(owner)
.fold[Unit] {
if (!ignoreIfUnavailable) addMissing(owner)
if (!ignoreIfUnavailable) addMissing(global)
} { defns =>
val scope = mutable.Map.empty[Global, Defn]
defns.foreach { defn => scope(defn.name) = defn }
Expand Down Expand Up @@ -279,11 +279,7 @@ class Reach(
def reachGlobal(name: Global)(implicit srcPosition: nir.Position): Unit =
if (!enqueued.contains(name) && name.ne(Global.None)) {
enqueued += name
from.getOrElseUpdate(
name,
if (stack.isEmpty) ReferencedFrom.Root
else ReferencedFrom(stack.head, srcPosition)
)
track(name)
todo ::= name
}

Expand All @@ -292,11 +288,7 @@ class Reach(
()
} else if (!stack.contains(name)) {
enqueued += name
from.getOrElseUpdate(
name,
if (stack.isEmpty) ReferencedFrom.Root
else ReferencedFrom(stack.head, srcPosition)
)
track(name)
reachDefn(name)
} else {
val lines = (s"cyclic reference to ${name.show}:" +:
Expand Down Expand Up @@ -840,7 +832,9 @@ class Reach(
()
}

def reachMethodTargets(ty: Type, sig: Sig)(implicit pos: Position): Unit =
def reachMethodTargets(ty: Type, sig: Sig)(implicit
srcPosition: Position
): Unit =
ty match {
case Type.Array(ty, _) =>
reachMethodTargets(Type.Ref(Type.toArrayClass(ty)), sig)
Expand All @@ -853,7 +847,8 @@ class Reach(
else {
// At this stage we cannot tell if method target is not defined or not yet reached
// We're delaying resolving targets to the end of Reach phase to check if this method is never defined in NIR
delayedMethods += DelayedMethod(name.top, sig, pos)
track(name.member(sig))
delayedMethods += DelayedMethod(name, sig, srcPosition)
}
}
}
Expand Down Expand Up @@ -951,7 +946,9 @@ class Reach(

val buf = List.newBuilder[BackTraceElement]
def getBackTrace(name: Global): List[BackTraceElement] = {
val current = from(name)
// orElse just in case if we messed something up and failed to correctly track references
// Accept possibly empty backtrace instead of crashing
val current = from.getOrElse(name, ReferencedFrom.Root)
if (current == ReferencedFrom.Root) buf.result()
else {
val file = current.srcPosition.filename.getOrElse("unknown")
Expand Down Expand Up @@ -981,6 +978,13 @@ class Reach(
private def fail(msg: => String): Nothing = {
throw new LinkingException(msg)
}

private def track(name: Global)(implicit srcPosition: nir.Position) =
from.getOrElseUpdate(
name,
if (stack.isEmpty) ReferencedFrom.Root
else ReferencedFrom(stack.head, srcPosition)
)
}

object Reach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,30 @@ class MissingSymbolsTest extends LinkerSpec {
}
}

// Methods of allocated classess have a special delayed handling needed to correctly
// distinguish unimplemented methods from not yet reached
@Test def unreachableDelayedMethod(): Unit = {
doesNotLink(
entry = mainClass,
Map(sourceFile -> s"""
|object $mainClass{
| def main(args: Array[String]): Unit = {
| val theFields = this.getClass().getDeclaredFields
| println(theFields)
| }
|}
""".stripMargin)
) {
case (config, result) =>
// Testing if is able to get non-empty backtrace.
// If reference tacking of delayed methods is invalid we would get empty list here
result.unreachable
.find(_.symbol == "java.lang.Class.getDeclaredFields")
.map { symbol =>
assertTrue("no-backtrace", symbol.backtrace.nonEmpty)
}
.getOrElse(fail("Not found required unreachable symbol"))
}
}

}