Commit graph

189 commits

Author SHA1 Message Date
Christopher Ferris
cadacdf474 Fix usage of Read instead of ReadFully.
Found by inspection.

Test: Builds and unit tests passes.
Change-Id: I10b63534ae0a3bff9ce56e821c542fa69c15822d
2019-01-15 19:10:49 -08:00
Martin Stjernholm
2677b802bf Remove dependency on libdexfile internal header.
Also remove some tests for implementation details in the libdexfile API that
are now covered by unit tests there (http://r.android.com/867370).

Test: mmma system/core/{libunwindstack,libbacktrace} and run host gtests
Bug: 119632407
Change-Id: Ie731bb929a564de56ce1c78385da12a43d0954ff
2019-01-11 21:05:50 +00:00
Martin Stjernholm
b49289b43a Some minor updates in the libdexfile external API.
Test: m
Test: mmma system/core/{libunwindstack,libbacktrace} and run host gtests
Bug: 119632407
Bug: 120978655
Change-Id: I919586ab503be3617e8a0604dfe48db331e7af94
2019-01-09 23:25:25 +00:00
Christopher Ferris
d76bd4c8d0 Merge "Remove CHECK in AdjustEncodedValue." 2019-01-08 16:47:06 +00:00
Treehugger Robot
a7d2ffa821 Merge "Move DwarfCfaInfo::kTable from data.rel.ro to rodata" 2018-12-21 12:58:26 +00:00
Christopher Ferris
a39aaf91eb Remove CHECK in AdjustEncodedValue.
The CHECK(encoding != DW_EH_PE_aligned) can trip given the right
arguments. This check isn't necessary, since the code will return
false in that case.

Add new unit test that tries all values to make sure no CHECK fires.

Bug: 120968571

Test: Passes new unit test, and passes fuzzing that failed before.
Change-Id: I062bcd18508c75cd3a4ca9dd12f922e25aafda8a
2018-12-20 08:40:58 -08:00
Martin Stjernholm
bb4f2b440a Revert^2 "Use libdexfile external API in libunwindstack."
This reverts commit cacf5bf6bc.

Reason for revert: Re-apply with proper fix for VNDK visibility on marlin and sailfish.

Test: Manual repro of http://b/121110092#comment1 on reported branch
Test: atest CtsRenderscriptTestCases
Test: mmma system/core/{libunwindstack,libbacktrace}, run host gtests
Test: Make image, flash, and reboot device.
Test: Forrest cts/art/gce-all: https://android-build.googleplex.com/builds/forrest/run/L00300000240828791
Test: Forrest cts/bionic/gce-all: https://android-build.googleplex.com/builds/forrest/run/L05600000240682947 (shows 27/2958 failed, but it doesn't pass on Blackbox either: http://screen/xbjioEf6UgR)
Test: Forrest cts/renderscript/gce-all: https://android-build.googleplex.com/builds/forrest/run/L66200000240680523
Bug: 119632407
Change-Id: I601aa97eac8127e30d753405f8bc1fc4ae7f849f
2018-12-19 21:04:35 +00:00
Treehugger Robot
1baa19b1a6 Merge "Fix offsets when shared lib split across maps." 2018-12-19 17:11:53 +00:00
Martin Stjernholm
cacf5bf6bc Revert "Use libdexfile external API in libunwindstack."
This reverts commit 444e23d2fc.

The rest of the topic doesn't need to be reverted.

Reason for revert: Breaks renderscript on marlin and sailfish.

Test: Manual repro of http://b/121110092#comment1 on reported branch
Test: "atest CtsRenderscriptTestCases" on that branch
Test: mmma system/core/{libunwindstack,libbacktrace}, run host gtests
Test: Make image, flash, and reboot device.
Bug: 121110092, 119632407
Change-Id: If1976b19ce386c95bc5bd4fd6d523745c167de18
2018-12-19 00:11:04 +00:00
Christopher Ferris
a09c4a6ff2 Fix offsets when shared lib split across maps.
The linker was modified so that a shared library has a read-only
map, then a read-execute map to represent the whole shared library.
When backtraces are created, then the offsets are set incorrectly
for backtraces. For example, all backtraces wind up with an offset now,
and a shared library loaded out of an apk shows the wrong offset.

Changes:
- Fix the FormatFrame function which was putting the offset before the
  map name.
- Refactor the Maps and MapInfo classes to keep track of the previous map
  instead of all maps. This was the only map that was ever needed.
- Modify the unwind_for_offline tool to capture both the read-only and
  read-execute map when using the read-only segment option.

Bug: 120981155

Test: Updated unit tests pass.
Test: Unwinds on device don't show the offsets everywhere any more.
Change-Id: I75b3727221be9c20316dfdcd7a1c6d242d7ce948
2018-12-18 11:22:14 -08:00
Vic Yang
80aba5494a Move DwarfCfaInfo::kTable from data.rel.ro to rodata
Pages in .data.rel.ro are always dirty.  Move whatever we can to
.rodata so that we reduce memory pressure.

The size of rodata, text, and data.rel.ro  sections of linker64 on
cuttlefish before/after this change:
  rodata: 0x170e4 -> 0x17964 (+2176 bytes)
  text: 0xec919 -> 0xec919 (no change)
  data.rel.ro: 0x9cf0 -> 0x98f0 (-1024 bytes)

Test: Boot cuttlefish
Change-Id: Ib2fd2bb0afb50208d441731d8c31bc8f287272c3
2018-12-14 06:03:09 -08:00
Martin Stjernholm
1d6969ccb9 Merge "Use libdexfile external API in libunwindstack." 2018-12-14 13:36:30 +00:00
Christopher Ferris
065f156195 Do not remove speculative frames in all cases.
If the first frame of an unwind is a totally invalid pc that's not in
any map, a speculative frame is added. Rather than deleting this frame
if no more unwinding is possible, leave it. This fixes a case where
the only frame you get is an invalid one, but the speculative frame
winds up in a shared library or somewhere else and gets removed.

Bug: 120505086

Test: New unit tests to catch this case pass.
Test: Verified original crashing program now emits two backtrace lines.
Change-Id: I088dff21c057386dcdaeb3fc2578b24322683bd0
2018-12-13 10:41:08 -08:00
Martin Stjernholm
444e23d2fc Use libdexfile external API in libunwindstack.
Test: mmma system/core/{libunwindstack,libbacktrace}, run host gtests
Test: Make image, flash, and reboot device.
Bug: 119632407
Change-Id: I370f089a1b20ba432e136818b4325d46f99df708
2018-12-13 17:58:23 +00:00
Christopher Ferris
583ce2deff Merge "Fix handling of ro segments for embedded libs." 2018-12-13 03:18:58 +00:00
Christopher Ferris
01040b10b2 Fix handling of ro segments for embedded libs.
When a shared library is loaded directly from an apk, the new way the
linker splits a shared library into a read-only and execute segment
broke unwinding. Modify the code to handle this case.

Other changes:
- Modify the algorithm for finding read-only map entries. Before, the code
  would search the entire map for the closest offset. Now it simply looks
  at the previous map. I did this because the old code was too lenient and
  might still work even if the linker changes. I want this to break if the
  linker behavior changes so that I can analyze the change.
- Update the tools to use PTRACE_SEIZE instead of PTRACE_ATTACH since
  PTRACE_ATTACH doesn't work in all cases.
- Small refactor of the GetFileMemory function.
- Add new unit test cases and new offline unwind test cases.

Bug: 120618231

Test: Ran new unit tests, ran original failing test.
Change-Id: I4bade55cf33220d52f1d5e9b0cbbbcc8419669d4
2018-12-11 19:16:29 -08:00
Chih-Hung Hsieh
1b7b7979af Fix performance-for-range-copy warnings
Bug: 30413223
Test: make with WITH_TIDY=1 DEFAULT_GLOBAL_TIDY_CHECKS=-*,performance*
Change-Id: I3ad102f2b0f971266d57488a3bd57d312f7ee3e6
2018-12-11 10:51:13 -08:00
Florian Mayer
3f1f2e07ab Add missing includes.
Test: Builds.
Change-Id: Ia8b79154902503cdd774dc3b4523466123b4324c
2018-12-06 10:09:20 -08:00
Florian Mayer
a5913bdd07 Merge "Read .note.gnu.build-id." 2018-12-06 10:57:26 +00:00
Florian Mayer
da459e56f7 Read .note.gnu.build-id.
This will be used by heapprofd to allow us to correlate build ids with
memory leaks in libraries and binaries.

Test: m
Test: host libunwindstack_test
Test: run unwind_info against my phone's libc.so (32/64) and compare
      to readelf Build ID output.

Bug: 120186412

Change-Id: I3cefd6cce9a8733509bf35b7175eb0f967783477
2018-12-05 14:13:30 +00:00
Christopher Ferris
ef6401091f Add support for caching small reads.
Add benchmarking to verify this is faster.

Test: Ran unit tests.
Change-Id: I1487114331f4581ec2368e56c4f18c6e3e6bcc7d
2018-11-28 13:04:58 -08:00
Christopher Ferris
5565906732 Remove overly restrictive check for memory.
Remove unit test that is not needed any more.

Slightly reorganize the code around the changed check.

Bug: 77958880

Test: Ran backtrace tests with new linker options.
Test: Ran libunwindstack unit tests.
Change-Id: Ie95707323fb51616a093cafb83e0343fa24e7e61
2018-11-15 14:06:26 -08:00
Mark Salyzyn
9f1cf25332 switch to using android-base/file.h instead of android-base/test_utils.h
Test: compile
Bug: 119313545
Change-Id: I4f7ad84743e974b4b4d1d7256088f6c8b749a237
2018-11-14 09:35:34 -08:00
Christopher Ferris
4568f4bc0f Verify that the elf matches the expected arch.
To avoid a case where a malicious app might try and trick the system to
create an elf and register object that mismatches, always verify that they
are the same arch.

Test: Ran unit tests.
Change-Id: I66978e9e02f8e4f396856912e7019528ead4838e
2018-10-29 18:08:09 -07:00
Treehugger Robot
551efd11f7 Merge "Fix off by one error." 2018-10-24 00:52:15 +00:00
Christopher Ferris
e8c4ecf49e Always init gnu_debugdata interface if it exists.
Every real call always sets the value to true to init the gnu_debugdata
interface, so remove this parameter.

Test: Builds, unit tests pass.
Change-Id: I07fb3adbbd5b65b51c0dc7608561e820a5095051
2018-10-23 12:13:54 -07:00
Florian Mayer
9c36a99ebc Fix off by one error.
Change-Id: I18a2d177f3212626b7091150f42dfe151e47570e
2018-10-23 18:10:41 +01:00
Christopher Ferris
56d0e07d70 Fix which maps to search for globals.
If multiple threads are unwinding at the same time, new maps that contain
the global variables for dex files and jit information are created. This
leads to threads creating more new maps that then get searched, then
more maps, then more searching until virtual address space exhaustion.

Fix this so that we only search maps that have a corresponding rw map that
could contain the global memory.

Small refactor to combine the code to search for global variables into
one class that both classes inherit from.

Modify unit tests for the new pattern checking.

Bug: 117761427

Test: Ran unit tests for libunwindstack/libbacktrace/simpleperf.
Test: Ran art 004-ThreadStress that used to fail.
Change-Id: I837ca6b9d0383100079de090bc7d019598e0cdfe
2018-10-17 14:23:10 -07:00
Christopher Ferris
6853a187e3 Merge "Fix up the definition of Elf::GetInfo." 2018-10-13 17:47:18 +00:00
Christopher Ferris
9d0ad238ca Fix problem adding too many frames.
When adding a frame with a dex pc, two frames will be added total. However,
if there is only enough room for a single frame, two get added any way.

Only add a single frame in this case, and add a unit test for this case.

Test: Passes unit tests.
Change-Id: If320584b126967a042c623d8fdf3f51dbc1c2251
2018-10-12 16:36:35 -07:00
Christopher Ferris
a2f38f1bde Fix up the definition of Elf::GetInfo.
The function should have returned a bool, so now it does.
Also use a different lighter weight function for validating the elf
in one place that didn't need to keep the max size value.

Test: Unit tests pass.
Change-Id: Ibde674e608091ba04abf22fad2fdc4dbdb2c6e73
2018-10-12 11:29:06 -07:00
Christopher Ferris
1f34c0e7a4 Fix handling of globals for new linker.
The new linker base map is read-only, but the global handling code
for jit and dex information assumed the base map was a read-execute
one. Relax that requirement to search read-only maps.

Adjust the unit tests for this case.

Bug: 117293117
Test: Passes unit tests.
Test: Passes art cfi tests with and without llvm linker.
Change-Id: I31a765d51847d0b8d778ace9cbaa29f42073f4d9
2018-10-08 19:47:33 -07:00
Christopher Ferris
9d5712c123 Implement support for linker rosegment option.
The rosegment linker option results in two maps containing the elf data
existing. One is an execute map where the code lives, and the other is the
read-only segment which contains the elf header information. If the file
backing a shared library in memory is not readable, then the new code
will attempt to find the read-only map that has the same name as the
current execute segment, and that is at offest zero in the file.

Add new unit tests for this functionality.

Add the missing MapInfoCreateMemoryTest.cpp to the list of tests.

Bug: 109657296

Test: Pass new unit tests.
Test: All unit libbacktrace/libunwindstack tests pass with rosegment enabled.
Change-Id: If8f69e4a067d77b3f2a7c31e2e5cd989a0702a8c
2018-10-03 20:48:45 -07:00
Christopher Ferris
3f9582f432 Fix implicit fall through switch cases.
Bug: 116020901

Test: Builds with -Werror=implicit-fallthrough.
Change-Id: I401f37585dae6cbf8995389a8a189abc2e102987
2018-09-19 14:12:59 -07:00
Chih-Hung Hsieh
502f4864d6 Suppress implicit-fallthrough warnings.
Add FALLTHROUGH_INTENDED for clang compiler.

Bug: 112564944
Test: build with global -Wimplicit-fallthrough.
Change-Id: I40f8bbf94e207c9dd90921e9b762ba51abab5777
2018-09-17 16:50:11 +00:00
Florian Mayer
ec004eb1b3 Merge "Allow to get raw register data on all platforms." 2018-08-08 21:37:46 +00:00
Mathieu Chartier
c2277fc216 Move to using ClassAccessor
In preparation for removal of ClassDataItemAccessor.

Bug: 79758018
Test: make
Test: test/run-test --host 137
Test: out/host/linux-x86/nativetest/libunwindstack_test/libunwindstack_test

Change-Id: I61a20fe7200a0d669b84573d3853b55877922602
2018-08-08 11:04:15 -07:00
Florian Mayer
932247e900 Allow to get raw register data on all platforms.
No new test failures introduced.

atest libunwindstack_test on aosp_walleye-userdebug:

Without patch:
Total: 800, Passed: 781, Failed: 19
With patch:
Total: 800, Passed: 781, Failed: 19

Change-Id: I8853c3e1594799761b481464b75e2904b38c4830
2018-08-08 11:19:41 +01:00
Christopher Ferris
5acf069a7a Be permissive about badly formed elf files.
Here is the allowable issues with an elf file that will not result in an error:
- The program headers/section headers offset points to unreadable memory.
- Allow missing program header and/or section headers.
- Allow a symbol table section header to point to invalid symbol table values.

There is no real reason to require the elf file be perfect. Everything in
the code has sane defaults, so any missing information won't cause any
problems.

This gets rid of the warning that occurs any time an elf is loaded
from memory. In memory elf files never contain all of the section headers,
and do not contain the symbol table data.

Update tests to test these new cases.

Test: Builds and unit tests all pass.
Change-Id: Iaefe2cd6b6c965a01ed425a112d6afae339f3b78
2018-08-03 22:02:05 +00:00
Christopher Ferris
2786608743 Fix incorrect left shifts.
Bug: 112142060

Test: New unit tests pass.
Change-Id: I0c47b22582f0bf75ab503364a337c7de4de3ec43
2018-08-02 18:01:02 -07:00
David Srbecky
02d0f7962d Create lookup table of DEX symbols.
Create fast lookup table instead of iterating every single time.
This will create the cache as methods are searched for.

Test: 137-cfi
Change-Id: I4be190bb1a637fef5d385b993be6a7e2203a6814
2018-07-20 10:48:59 -07:00
Christopher Ferris
5afddb0637 Remove Memory::ReadField.
In almost all cases, it is faster to read the entire structure rather
than do multiple reads using ReadField. The only case where it would be
slower is if doing a remote unwind and ptrace is the only way to read. In
all other cases, it's a single system call. In the ptrace call, it will be
multiple calls. Given that it is unusual to be forced to use ptrace,
it's better to avoid it.

It also reduces the code complexity to do a single read, and avoids
issues where the code forgets to read the field it needs.

Test: Unit tests pass on host and target.
Change-Id: I7b3875b2c85d0d88115b1776e1be28521dc0b932
2018-07-12 12:45:31 -07:00
Elliott Hughes
38488907a0 Move libbacktrace off cutils.
There's still <cutils/atomic.h> in a test, but I don't understand why
that isn't just std::atomic.

Also add a shared tgkill wrapper to libbase.

Bug: N/A
Test: ran tests
Change-Id: Idd4baa1e1670a84b3a8f35803cc5ffe5aae008a6
2018-07-11 13:55:07 -07:00
Christopher Ferris
92acaac8c7 Refactor the DwarfSection classes.
Modify the code for the no header sections because it turns out that
it is not okay to assume that the fdes are non-overlapping. It's necessary
to read the fdes in order and match as you go.

Modify the code so that it only reads until it finds the given pc rather than
reading all of the cie/fde entries at once.

Rewrote the tests to verify the new behavior.

Bug: 68998033
Bug: 110235461

Test: Ran libbacktrace/libunwindstack unit tests.
Test: Unwind the mediaserver process on a walleye and verify it
Test: unwinds properly.

Change-Id: I7bb59d1db72c13fa34caa9735ec34c1a60e20ed2
2018-06-27 14:52:21 -07:00
Christopher Ferris
f882a38864 Fix ARM program header values used for exidx.
Before, I was using p_vaddr to get the offset into the elf file where
the exidx frame starts. I changed that to use p_offset since this already
has the load bias offset in it and some elf files do not set p_vaddr
properly.

Also, use p_filesz instead of p_memsz, since again, some elf files do
not set p_memsz to the same as p_filesz.

Bug: 110704153

Test: All libbacktrace/libunwindstack unit tests pass.
Test: Randomly unwind process on a walleye.
Test: Verified that this properly dumps and unwinds the shared
Test: library that sets p_vaddr and p_memsz differently.
Change-Id: Ic7b1e5d07439f4636fa02cd884a8727a5737372b
2018-06-22 16:48:02 -07:00
Christopher Ferris
4cc36d2b43 Fix handling of load bias values.
It turns out that for the dwarf information, if a FDE indicates it's pc
relative, then pc has to be incremented by the load bias. If not, then
it should not be incremented.

Previously, the code always subtracted load bias values from pcs, and assumed
that all fdes were incremented by load bias values. The new code actually
reads the fdes and adjusted the pcs in the fde and in the eh frame hdr so
that load bias values are already handled properly.

In addition, add dumping of arm exidx values in unwind_reg_info. This allowed
verifying that the debug frame in those elf files was being handled properly.

Added a new unit test that only has a debug frame that has a non-zero load
bias and has fde entries that do not have pc relative encoding.

Fix a couple of other small bugs.

Bug: 109824792

Test: All libbacktrace/libunwindstack unit tests pass.
Test: Ran ART 137-cfi test and 004-ThreadStress.
Test: Verify that displaying the fde start and end pc actually match the
Test: real data for fde that have pc relative set, and that don't.
Test: Verified that the unwind information for arm exidx matches the
Test: debug frame data.

Change-Id: I707555286b5cb05df9f25489e8c5ede753cfe0fb
2018-06-11 18:53:55 -07:00
Elliott Hughes
f6d7594d7f Try to fix LocalUnwinderTest.unwind_after_dlopen in APCT.
Failure:

  system/core/libunwindstack/tests/LocalUnwinderTest.cpp:181: Failure
  Value of: handle != nullptr
  Actual: false
  Expected: true

Bug: N/A
Test: N/A
Change-Id: I89b3228d51961f7bf952dd01f12f4ee61a97fe91
2018-06-06 10:48:41 -07:00
Treehugger Robot
6c9bb058c5 Merge "Build adbd for recovery" 2018-05-25 05:58:01 +00:00
Jiyong Park
a0e75045e6 Build adbd for recovery
adbd (and its dependencies) are marked as recovery_available:true so
that recovery version of the binary is built separately from the one for
system partition. This allows us to stop copying the system version to
the recovery partition and also opens up the way to enable shared
libraries in the recovery partition. Then we can also build adbd as a
dynamic executable.

Bug: 79146551
Test: m -j adbd.recovery
Change-Id: Ib95614c7435f9d0afc02a0c7d5ae1a94e439e32a
2018-05-24 14:11:11 +09:00
Christopher Ferris
ca9a54b2c0 Add a specialized LocalUnwinder object.
This object is for doing many local unwinds across different threads
at any point during a program's execution.

Also add LocalUpdatableMaps that will re-read the maps data under certain
circumstances.

This first version does not support jit or dex pc data.

Bug: 74361929

Test: Ran unit tests.
Change-Id: I790662366d3fed677f31b3288182950c494de9ad
2018-05-23 20:02:30 -07:00