<commit_before>/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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.
 */

package com.facebook.buck.cxx.toolchain.objectfile;

import com.facebook.buck.util.ObjectFileCommonModificationDate;
import com.facebook.buck.util.nio.ByteBufferUnmapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class Machos {

  // http://www.opensource.apple.com/source/xnu/xnu-1699.32.7/EXTERNAL_HEADERS/mach-o/loader.h
  // File magic
  static final byte[] MH_MAGIC = Ints.toByteArray(0xFEEDFACE);
  static final byte[] MH_MAGIC_64 = Ints.toByteArray(0xFEEDFACF);
  static final byte[] MH_CIGAM = Ints.toByteArray(0xCEFAEDFE);
  static final byte[] MH_CIGAM_64 = Ints.toByteArray(0xCFFAEDFE);
  // Map segment load command
  static final int LC_SEGMENT = 0x1;
  // Symbol table load command
  public static final int LC_SYMTAB = 0x2;
  // UUID load command
  static final int LC_UUID = 0x1B;
  // Map 64 bit segment load command
  static final int LC_SEGMENT_64 = 0x19;

  static final int LC_REQ_DYLD = 0x80000000;
  static final int LC_DYLD_INFO = 0x22;
  public static final int LC_DYLD_INFO_ONLY = (0x22 | LC_REQ_DYLD);

  // http://www.opensource.apple.com/source/xnu/xnu-1699.32.7/EXTERNAL_HEADERS/mach-o/stab.h
  // Description of object file STAB entries
  static final short N_OSO = (short) 0x66;

  static final String LINKEDIT = "__LINKEDIT";

  private static final int NO_VALUE_MARKER = -1;

  private Machos() {}

  static void setUuidIfPresent(ByteBuffer map, byte[] uuid) throws MachoException {
    int commandsCount = getHeader(map).getCommandsCount();

    for (int i = 0; i < commandsCount; i++) {
      int command = ObjectFileScrubbers.getLittleEndianInt(map);
      int commandSize = ObjectFileScrubbers.getLittleEndianInt(map);
      if (LC_UUID == command) {
        ObjectFileScrubbers.putBytes(map, uuid);
        return;
      } else {
        /* Command body */ ObjectFileScrubbers.getBytes(map, commandSize - 8);
      }
    }
  }

  static boolean isMacho(FileChannel file) throws IOException {
    try (ByteBufferUnmapper unmapper =
        ByteBufferUnmapper.createUnsafe(
            file.map(FileChannel.MapMode.READ_ONLY, 0, MH_MAGIC.length))) {
      ByteBuffer map = unmapper.getByteBuffer();

      byte[] magic = ObjectFileScrubbers.getBytes(map, MH_MAGIC.length);
      return Arrays.equals(MH_MAGIC, magic)
          || Arrays.equals(MH_CIGAM, magic)
          || Arrays.equals(MH_MAGIC_64, magic)
          || Arrays.equals(MH_CIGAM_64, magic);
    }
  }

  /**
   * Relativize paths in OSO entries.
   *
   * <p>OSO entries point to other files containing debug information. These are generated by the
   * linker as absolute paths.
   */
  static void relativizeOsoSymbols(FileChannel file, ImmutableMap<Path, Path> cellRoots)
      throws IOException, MachoException {
    cellRoots.forEach(
        (from, to) -> {
          Preconditions.checkArgument(from.isAbsolute());
          Preconditions.checkArgument(!to.isAbsolute());
        });

    long size = file.size();
    try (ByteBufferUnmapper unmapper =
        ByteBufferUnmapper.createUnsafe(file.map(FileChannel.MapMode.READ_WRITE, 0, size))) {
      ByteBuffer map = unmapper.getByteBuffer();

      MachoHeader header = getHeader(map);

      int symbolTableOffset = 0;
      int symbolTableCount = 0;
      int stringTableOffset = 0;<commit_msg>Simpler and faster Mach-O file detection<commit_after>/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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.
 */

package com.facebook.buck.cxx.toolchain.objectfile;

import com.facebook.buck.util.ObjectFileCommonModificationDate;
import com.facebook.buck.util.nio.ByteBufferUnmapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class Machos {

  // http://www.opensource.apple.com/source/xnu/xnu-1699.32.7/EXTERNAL_HEADERS/mach-o/loader.h
  // File magic
  static final byte[] MH_MAGIC = Ints.toByteArray(0xFEEDFACE);
  static final byte[] MH_MAGIC_64 = Ints.toByteArray(0xFEEDFACF);
  static final byte[] MH_CIGAM = Ints.toByteArray(0xCEFAEDFE);
  static final byte[] MH_CIGAM_64 = Ints.toByteArray(0xCFFAEDFE);
  // Map segment load command
  static final int LC_SEGMENT = 0x1;
  // Symbol table load command
  public static final int LC_SYMTAB = 0x2;
  // UUID load command
  static final int LC_UUID = 0x1B;
  // Map 64 bit segment load command
  static final int LC_SEGMENT_64 = 0x19;

  static final int LC_REQ_DYLD = 0x80000000;
  static final int LC_DYLD_INFO = 0x22;
  public static final int LC_DYLD_INFO_ONLY = (0x22 | LC_REQ_DYLD);

  // http://www.opensource.apple.com/source/xnu/xnu-1699.32.7/EXTERNAL_HEADERS/mach-o/stab.h
  // Description of object file STAB entries
  static final short N_OSO = (short) 0x66;

  static final String LINKEDIT = "__LINKEDIT";

  private static final int NO_VALUE_MARKER = -1;

  private Machos() {}

  static void setUuidIfPresent(ByteBuffer map, byte[] uuid) throws MachoException {
    int commandsCount = getHeader(map).getCommandsCount();

    for (int i = 0; i < commandsCount; i++) {
      int command = ObjectFileScrubbers.getLittleEndianInt(map);
      int commandSize = ObjectFileScrubbers.getLittleEndianInt(map);
      if (LC_UUID == command) {
        ObjectFileScrubbers.putBytes(map, uuid);
        return;
      } else {
        /* Command body */ ObjectFileScrubbers.getBytes(map, commandSize - 8);
      }
    }
  }

  static boolean isMacho(FileChannel file) throws IOException {
    if (file.size() < MH_MAGIC.length) {
      return false;
    }

    byte[] magic = new byte[MH_MAGIC.length];
    ByteBuffer buffer = ByteBuffer.wrap(magic);
    file.read(buffer, 0);

    return Arrays.equals(MH_MAGIC, magic)
        || Arrays.equals(MH_CIGAM, magic)
        || Arrays.equals(MH_MAGIC_64, magic)
        || Arrays.equals(MH_CIGAM_64, magic);
  }

  /**
   * Relativize paths in OSO entries.
   *
   * <p>OSO entries point to other files containing debug information. These are generated by the
   * linker as absolute paths.
   */
  static void relativizeOsoSymbols(FileChannel file, ImmutableMap<Path, Path> cellRoots)
      throws IOException, MachoException {
    cellRoots.forEach(
        (from, to) -> {
          Preconditions.checkArgument(from.isAbsolute());
          Preconditions.checkArgument(!to.isAbsolute());
        });

    long size = file.size();
    try (ByteBufferUnmapper unmapper =
        ByteBufferUnmapper.createUnsafe(file.map(FileChannel.MapMode.READ_WRITE, 0, size))) {
      ByteBuffer map = unmapper.getByteBuffer();

      MachoHeader header = getHeader(map);

      int symbolTableOffset = 0;
      int symbolTableCount = 0;
      int stringTableOffset = 0;
{'fraction_non_alphanumeric': 0.0991108476585655, 'fraction_numerical': 0.016360403082394784, 'mean_word_length': 3.5872756933115824, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4041484', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2653, 'n_words': 704}