Bug 23875 - support string map keys in foreach iteration
Summary: support string map keys in foreach iteration
Status: RESOLVED FIXED
Alias: None
Product: systemtap
Classification: Unclassified
Component: bpf (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-09 17:00 UTC by Serhei Makarov
Modified: 2019-03-26 17:16 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Serhei Makarov 2018-11-09 17:00:45 UTC
global t

probe kernel.function("vfs_read") {
  t["key"] = 6
  exit()
}

probe end {
  c = 0
  foreach (k in t)
    c++
  printf("%d\n", c)
}

Pass 1: parsed user script and 49 library scripts using 120048virt/21872res/8376shr/13356data kb, in 0usr/0sys/7real ms.
Pass 2: analyzed script: 2 probes, 2 functions, 0 embeds, 1 global using 175628virt/78164res/9264shr/68936data kb, in 590usr/20sys/602real ms.
Pass 4: compiled BPF into "stap_12352.bo" in 0usr/0sys/3real ms.
Pass 5: starting run.
1
*** stack smashing detected ***: <unknown> terminated
WARNING: /opt/systemtap/bin/stapbpf exited with signal: 6 (Aborted)
Pass 5: run completed in 0usr/0sys/113real ms.
Pass 5: run failed.  [man error::pass5]
Comment 1 Serhei Makarov 2018-11-13 18:15:57 UTC
The foreach loop functionality needs to be extended to support iteration of string keys.

Current code assumes keys are int64_t, which is what causes the 'stack smashing' error (attempt to copy 64-byte string into 8-byte stack variable). There are other problems e.g. potential to infinite-loop, segfault and so forth.

Wrote testcases to cover the known issues and (for the time being) added commit 0eaf4f196 to cleanly reject foreach loops with string keys in the translator. Keeping the PR open to track work on foreach string key support.
Comment 2 Serhei Makarov 2019-03-25 16:53:56 UTC
Have some code for this, but there are regressions I need to fix. The parts that needed to be changed so far:

- in bpf-translate.cxx, visit_foreach_loop() needs to create correctly sized stack slots for string keys
- in bpfinterp.cxx, create a separate map_str_keys vector and use the appropriate vectors depending on type. Some unavoidable code duplication as the string handling code is similar but not quite the same as the code for ints.
Comment 3 Serhei Makarov 2019-03-26 17:16:31 UTC
should be fixed in commit ceed4889 apart from issues to be fixed in PR23858