Repository: pixie-io/pixie (language: cpp)

Issue:
[ARM] Support Java Symbolization on ARM.
Java symbolization is currently disabled on aarch64 because the java agent is not built for aarch64.

- [ ] Add musl toolchain targeting ARM


Discussion / hints:

Fix patch:
diff --git a/bazel/external/musl.BUILD b/bazel/external/musl.BUILD
new file mode 100644
index 00000000000..e7cf0ebd7c4
--- /dev/null
+++ b/bazel/external/musl.BUILD
@@ -0,0 +1,79 @@
+# Copyright 2018- The Pixie Authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make")
+
+filegroup(
+    name = "all",
+    srcs = glob(["**"]),
+)
+
+configure_make(
+    name = "musl",
+    configure_options = ["--disable-shared"] + select({
+        "@platforms//cpu:aarch64": [
+            "--target",
+            "aarch64",
+        ],
+        "//conditions:default": [],
+    }),
+    lib_source = ":all",
+    out_bin_dir = "lib",
+    out_binaries = [
+        "Scrt1.o",
+        "crt1.o",
+        "crti.o",
+        "crtn.o",
+        "rcrt1.o",
+    ],
+    out_static_libs = ["libc.a"],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "Scrt1.o",
+    srcs = [":musl"],
+    output_group = "Scrt1.o",
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "crt1.o",
+    srcs = [":musl"],
+    output_group = "crt1.o",
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "crti.o",
+    srcs = [":musl"],
+    output_group = "crti.o",
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "crtn.o",
+    srcs = [":musl"],
+    output_group = "crtn.o",
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "rcrt1.o",
+    srcs = [":musl"],
+    output_group = "rcrt1.o",
+    visibility = ["//visibility:public"],
+)
diff --git a/bazel/pl_build_system.bzl b/bazel/pl_build_system.bzl
index 401b0bc43da..d0f3d386f50 100644
--- a/bazel/pl_build_system.bzl
+++ b/bazel/pl_build_system.bzl
@@ -213,6 +213,40 @@ def pl_cc_binary(
         features = pl_default_features(),
     )
 
+def _default_empty_list(name, kwargs):
+    if not name in kwargs:
+        kwargs[name] = []
+
+def pl_cc_musl_binary(name, **kwargs):
+    _default_empty_list("copts", kwargs)
+    kwargs["copts"] = kwargs["copts"] + ["-nostdlib", "-nostdinc"]
+
+    _default_empty_list("additional_linker_inputs", kwargs)
+    kwargs["additional_linker_inputs"] = kwargs["additional_linker_inputs"] + [
+        "@org_libc_musl//:crt1.o",
+        "@org_libc_musl//:crti.o",
+        "@org_libc_musl//:crtn.o",
+    ]
+
+    linkshared = "linkshared" in kwargs and kwargs["linkshared"]
+    start_libs = []
+    if not linkshared:
+        start_libs = start_libs + ["$(location @org_libc_musl//:crt1.o)"]
+    start_libs = start_libs + ["$(location @org_libc_musl//:crti.o)"]
+    end_libs = ["$(location @org_libc_musl//:crtn.o)"]
+    linkopts = [
+        "-nostdlib",
+        "-nodefaultlibs",
+        "-nostartfiles",
+        "-lgcc",
+    ]
+    _default_empty_list("linkopts", kwargs)
+    kwargs["linkopts"] = start_libs + kwargs["linkopts"] + linkopts + end_libs
+
+    _default_empty_list("deps", kwargs)
+    kwargs["deps"] = kwargs["deps"] + ["@org_libc_musl//:musl"]
+    cc_binary(name = name, **kwargs)
+
 # PL C++ test targets should be specified with this function.
 def pl_cc_test(
         name,
diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl
index 5e862b6eefa..cd05bca3a9b 100644
--- a/bazel/repositories.bzl
+++ b/bazel/repositories.bzl
@@ -183,6 +183,8 @@ def _cc_deps():
     _include_all_repo("com_github_libuv_libuv", patches = ["//bazel/external:libuv.patch"], patch_args = ["-p1"])
     _include_all_repo("com_github_libarchive_libarchive", patches = ["//bazel/external:libarchive.patch"], patch_args = ["-p1"])
 
+    _bazel_repo("org_libc_musl", build_file = "//bazel/external:musl.BUILD")
+
 def _java_deps():
     _bazel_repo("com_oracle_openjdk_18", build_file = "//bazel/external:jdk_includes.BUILD")
     remote_java_repository(
diff --git a/bazel/repository_locations.bzl b/bazel/repository_locations.bzl
index fb87c800d38..da1aaf2eec1 100644
--- a/bazel/repository_locations.bzl
+++ b/bazel/repository_locations.bzl
@@ -401,6 +401,12 @@ REPOSITORY_LOCATIONS = dict(
         strip_prefix = "tensorflow-2.11.0",
         urls = ["https://github.com/tensorflow/tensorflow/archive/refs/tags/v2.11.0.tar.gz"],
     ),
+    org_libc_musl = dict(
+        sha256 = "7d5b0b6062521e4627e099e4c9dc8248d32a30285e959b7eecaa780cf8cfd4a4",
+        strip_prefix = "musl-1.2.3",
+        urls = ["http://musl.libc.org/releases/musl-1.2.3.tar.gz"],
+        manual_license_name = "libc/musl",
+    ),
     rules_foreign_cc = dict(
         sha256 = "6041f1374ff32ba711564374ad8e007aef77f71561a7ce784123b9b4b88614fc",
         strip_prefix = "rules_foreign_cc-0.8.0",
diff --git a/src/stirling/BUILD.bazel b/src/stirling/BUILD.bazel
index a9d2cdd48cd..3812f7a6caa 100644
--- a/src/stirling/BUILD.bazel
+++ b/src/stirling/BUILD.bazel
@@ -83,10 +83,8 @@ gen_timeconst(name = "stirling_linux_timeconst_files")
 # Used by stirling runtime to provide symbolization for Java application profiling.
 stirling_java_profiling_tools = [
     "//src/stirling/source_connectors/perf_profiler/java/px_jattach:px_jattach",
-] + select({
-    "@platforms//cpu:x86_64": ["//src/stirling/source_connectors/perf_profiler/java/agent:agent"],
-    "//conditions:default": [],
-})
+    "//src/stirling/source_connectors/perf_profiler/java/agent:agent",
+]
 
 # Use this binary to dynamically turn on/off Stirling runtime's debug logging.
 # See src/stirling/e2e_tests/stirling_signal_test.sh for its usage.
diff --git a/src/stirling/source_connectors/perf_profiler/java/agent/BUILD.bazel b/src/stirling/source_connectors/perf_profiler/java/agent/BUILD.bazel
index 739cd30333c..6276cdcf427 100644
--- a/src/stirling/source_connectors/perf_profiler/java/agent/BUILD.bazel
+++ b/src/stirling/source_connectors/perf_profiler/java/agent/BUILD.bazel
@@ -14,8 +14,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 
-load("//bazel:pl_build_system.bzl", "pl_cc_library", "pl_cc_test")
-load("//bazel:toolchain_transitions.bzl", "cc_static_musl_binary")
+load("//bazel:pl_build_system.bzl", "pl_cc_library", "pl_cc_musl_binary", "pl_cc_test")
 
 package(default_visibility = ["//src/stirling:__subpackages__"])
 
@@ -64,17 +63,15 @@ pl_cc_library(
 
 # If we don't statically link libc, then the .so file will fail to be loaded on systems with older libc versions than we built with.
 # glibc is a pain to link statically, so instead we link musl libc statically.
-cc_static_musl_binary(
+pl_cc_musl_binary(
     name = "px-java-agent",
     srcs = [
         "agent.c",
         "agent_hash.h",
         "raw_symbol_update.h",
     ],
+    features = ["fully_static_link"],
     linkshared = True,
-    target_compatible_with = [
-        "@platforms//cpu:x86_64",
-    ],
     deps = ["@com_oracle_openjdk_18//:jdk_includes"],
 )
 
diff --git a/tools/licenses/manual_licenses.json b/tools/licenses/manual_licenses.json
index 2e32521d854..0ac46e247e7 100644
--- a/tools/licenses/manual_licenses.json
+++ b/tools/licenses/manual_licenses.json
@@ -167,6 +167,13 @@
     "spdxID": "MIT and BSC-2-Clause and BSD-3-Clause and ISC",
     "licenseText": "libuv is licensed for use as follows:\n\n====\nCopyright (c) 2015-present libuv project contributors.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to\ndeal in the Software without restriction, including without limitation the\nrights to use, copy, modify, merge, publish, distribute, sublicense, and/or\nsell copies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\nIN THE SOFTWARE.\n====\n\nThis license applies to parts of libuv originating from the\nhttps://github.com/joyent/libuv repository:\n\n====\n\nCopyright Joyent, Inc. and other Node contributors. All rights reserved.\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to\ndeal in the Software without restriction, including without limitation the\nrights to use, copy, modify, merge, publish, distribute, sublicense, and/or\nsell copies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\nIN THE SOFTWARE.\n\n====\n\nThis license applies to all parts of libuv that are not externally\nmaintained libraries.\n\nThe externally maintained libraries used by libuv are:\n\n  - tree.h (from FreeBSD), copyright Niels Provos. Two clause BSD license.\n\n  - inet_pton and inet_ntop implementations, contained in src/inet.c, are\n    copyright the Internet Systems Consortium, Inc., and licensed under the ISC\n    license.\n\n  - stdint-msvc2008.h (from msinttypes), copyright Alexander Chemeris. Three\n    clause BSD license.\n\n  - pthread-fixes.c, copyright Google Inc. and Sony Mobile Communications AB.\n    Three clause BSD license.\n\n  - android-ifaddrs.h, android-ifaddrs.c, copyright Berkeley Software Design\n    Inc, Kenneth MacKay and Emergya (Cloud4all, FP7/2007-2013, grant agreement\n    n° 289016). Three clause BSD license.\n"
   },
+  {
+    "name": "libc/musl",
+    "package": "https://git.musl-libc.org/cgit/musl",
+    "url": "https://musl.libc.org",
+    "spdxID": "MIT",
+    "licenseText": "musl as a whole is licensed under the following standard MIT license:\n\n----------------------------------------------------------------------\nCopyright \u00a9 2005-2020 Rich Felker, et al.\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n----------------------------------------------------------------------\n\nAuthors/contributors include:\n\nA. Wilcox\nAda Worcester\nAlex Dowad\nAlex Suykov\nAlexander Monakov\nAndre McCurdy\nAndrew Kelley\nAnthony G. Basile\nAric Belsito\nArvid Picciani\nBartosz Brachaczek\nBenjamin Peterson\nBobby Bingham\nBoris Brezillon\nBrent Cook\nChris Spiegel\nCl\u00e9ment Vasseur\nDaniel Micay\nDaniel Sabogal\nDaurnimator\nDavid Carlier\nDavid Edelsohn\nDenys Vlasenko\nDmitry Ivanov\nDmitry V. Levin\nDrew DeVault\nEmil Renner Berthing\nFangrui Song\nFelix Fietkau\nFelix Janda\nGianluca Anzolin\nHauke Mehrtens\nHe X\nHiltjo Posthuma\nIsaac Dunham\nJaydeep Patil\nJens Gustedt\nJeremy Huntwork\nJo-Philipp Wich\nJoakim Sindholt\nJohn Spencer\nJulien Ramseier\nJustin Cormack\nKaarle Ritvanen\nKhem Raj\nKylie McClain\nLeah Neukirchen\nLuca Barbato\nLuka Perkov\nM Farkas-Dyck (Strake)\nMahesh Bodapati\nMarkus Wichmann\nMasanori Ogino\nMichael Clark\nMichael Forney\nMikhail Kremnyov\nNatanael Copa\nNicholas J. Kain\norc\nPascal Cuoq\nPatrick Oppenlander\nPetr Hosek\nPetr Skocik\nPierre Carrier\nReini Urban\nRich Felker\nRichard Pennington\nRyan Fairfax\nSamuel Holland\nSegev Finer\nShiz\nsin\nSolar Designer\nStefan Kristiansson\nStefan O'Rear\nSzabolcs Nagy\nTimo Ter\u00e4s\nTrutz Behn\nValentin Ochs\nWill Dietz\nWilliam Haddon\nWilliam Pitcock\n\nPortions of this software are derived from third-party works licensed\nunder terms compatible with the above MIT license:\n\nThe TRE regular expression implementation (src/regex/reg* and\nsrc/regex/tre*) is Copyright \u00a9 2001-2008 Ville Laurikari and licensed\nunder a 2-clause BSD license (license text in the source files). The\nincluded version has been heavily modified by Rich Felker in 2012, in\nthe interests of size, simplicity, and namespace cleanliness.\n\nMuch of the math library code (src/math/* and src/complex/*) is\nCopyright \u00a9 1993,2004 Sun Microsystems or\nCopyright \u00a9 2003-2011 David Schultz or\nCopyright \u00a9 2003-2009 Steven G. Kargl or\nCopyright \u00a9 2003-2009 Bruce D. Evans or\nCopyright \u00a9 2008 Stephen L. Moshier or\nCopyright \u00a9 2017-2018 Arm Limited\nand labelled as such in comments in the individual source files. All\nhave been licensed under extremely permissive terms.\n\nThe ARM memcpy code (src/string/arm/memcpy.S) is Copyright \u00a9 2008\nThe Android Open Source Project and is licensed under a two-clause BSD\nlicense. It was taken from Bionic libc, used on Android.\n\nThe AArch64 memcpy and memset code (src/string/aarch64/*) are\nCopyright \u00a9 1999-2019, Arm Limited.\n\nThe implementation of DES for crypt (src/crypt/crypt_des.c) is\nCopyright \u00a9 1994 David Burren. It is licensed under a BSD license.\n\nThe implementation of blowfish crypt (src/crypt/crypt_blowfish.c) was\noriginally written by Solar Designer and placed into the public\ndomain. The code also comes with a fallback permissive license for use\nin jurisdictions that may not recognize the public domain.\n\nThe smoothsort implementation (src/stdlib/qsort.c) is Copyright \u00a9 2011\nValentin Ochs and is licensed under an MIT-style license.\n\nThe x86_64 port was written by Nicholas J. Kain and is licensed under\nthe standard MIT terms.\n\nThe mips and microblaze ports were originally written by Richard\nPennington for use in the ellcc project. The original code was adapted\nby Rich Felker for build system and code conventions during upstream\nintegration. It is licensed under the standard MIT terms.\n\nThe mips64 port was contributed by Imagination Technologies and is\nlicensed under the standard MIT terms.\n\nThe powerpc port was also originally written by Richard Pennington,\nand later supplemented and integrated by John Spencer. It is licensed\nunder the standard MIT terms.\n\nAll other files which have no copyright comments are original works\nproduced specifically for use as part of this library, written either\nby Rich Felker, the main author of the library, or by one or more\ncontibutors listed above. Details on authorship of individual files\ncan be found in the git version control history of the project. The\nomission of copyright and license comments in each file is in the\ninterest of source tree size.\n\nIn addition, permission is hereby granted for all public header files\n(include/* and arch/*/bits/*) and crt files intended to be linked into\napplications (crt/*, ldso/dlstart.c, and arch/*/crt_arch.h) to omit\nthe copyright notice and permission notice otherwise required by the\nlicense, and to use these files without any requirement of\nattribution. These files include substantial contributions from:\n\nBobby Bingham\nJohn Spencer\nNicholas J. Kain\nRich Felker\nRichard Pennington\nStefan Kristiansson\nSzabolcs Nagy\n\nall of whom have explicitly granted such permission.\n\nThis file previously contained text expressing a belief that most of\nthe files covered by the above exception were sufficiently trivial not\nto be subject to copyright, resulting in confusion over whether it\nnegated the permissions granted in the license. In the spirit of\npermissive licensing, and of not having licensing issues being an\nobstacle to adoption, that text has been removed.\n"
+  },
   {
     "name": "llvm/llvm-project",
     "package": "github.com/llvm/llvm-project",


Test patch:
diff --git a/src/stirling/source_connectors/perf_profiler/testing/testing.cc b/src/stirling/source_connectors/perf_profiler/testing/testing.cc
index 32e448bf40b..bf5e2eca40e 100644
--- a/src/stirling/source_connectors/perf_profiler/testing/testing.cc
+++ b/src/stirling/source_connectors/perf_profiler/testing/testing.cc
@@ -35,8 +35,7 @@ const fs_path profiler_path_pfx = "src/stirling/source_connectors/perf_profiler"
 
 // Returns a string as the flag value for the --stirling_profiler_java_agent_libs.
 std::string GetAgentLibsFlagValueForTesting() {
-  const fs_path path =
-      profiler_path_pfx / "java" / "agent" / "px-java-agent" / "libpx-java-agent.so";
+  const fs_path path = profiler_path_pfx / "java" / "agent" / "libpx-java-agent.so";
   return BazelRunfilePath(path).string();
 }