<commit_before>/*
 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * NaCl Simple/secure ELF loader (NaCl SEL).
 *
 * This loader can only process NaCl object files as produced using
 * the NaCl toolchain.  Other ELF files will be rejected.
 *
 * The primary function, NaClAppLoadFile, parses an ELF file,
 * allocates memory, loads the relocatable image from the ELF file
 * into memory, and performs relocation.  NaClAppRun runs the
 * resultant program.
 *
 * This loader is written in C so that it can be used by C-only as
 * well as C++ applications.  Other languages should also be able to
 * use their foreign-function interfaces to invoke C code.
 *
 * This loader must be part of the NaCl TCB, since it directly handles
 * externally supplied input (the ELF file).  Any security
 * vulnerabilities in handling the ELF image, e.g., buffer or integer
 * overflows, can put the application at risk.
 */

#ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_LDR_H_
#define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_LDR_H_ 1

#include <signal.h>
#include <stdbool.h>

#include "native_client/src/include/atomic_ops.h"
#include "native_client/src/include/nacl_base.h"
#include "native_client/src/include/portability.h"
#include "native_client/src/include/elf.h"

#include "native_client/src/shared/platform/nacl_host_desc.h"
#include "native_client/src/shared/platform/nacl_log.h"
#include "native_client/src/shared/platform/nacl_sync_checked.h"
#include "native_client/src/shared/platform/nacl_threads.h"

#include "native_client/src/shared/srpc/nacl_srpc.h"

#include "native_client/src/trusted/interval_multiset/nacl_interval_multiset.h"
#include "native_client/src/trusted/interval_multiset/nacl_interval_range_tree.h"

#include "native_client/src/trusted/service_runtime/dyn_array.h"
#include "native_client/src/trusted/service_runtime/nacl_error_code.h"
#include "native_client/src/trusted/service_runtime/nacl_kernel_service.h"
#include "native_client/src/trusted/service_runtime/nacl_resource.h"

#include "native_client/src/trusted/service_runtime/nacl_secure_service.h"

#include "native_client/src/trusted/service_runtime/sel_addrspace.h"
#include "native_client/src/trusted/service_runtime/sel_mem.h"
#include "native_client/src/trusted/service_runtime/sel_util.h"
#include "native_client/src/trusted/service_runtime/sel_rt.h"

#include "native_client/src/trusted/service_runtime/name_service/name_service.h"

#include "native_client/src/trusted/validator/ncvalidate.h"
#include "native_client/src/trusted/service_runtime/nacl_globals.h"

EXTERN_C_BEGIN

#define NACL_SERVICE_PORT_DESCRIPTOR    3
#define NACL_SERVICE_ADDRESS_DESCRIPTOR 4

#define NACL_DEFAULT_STACK_MAX  (16u << 20)  /* main thread stack */

#define NACL_BAD_FD                     -1

struct NaClAppThread;
struct NaClDesc;  /* see native_client/src/trusted/desc/nacl_desc_base.h */
struct NaClDynamicRegion;
struct NaClManifestProxy;
struct NaClReverseQuotaInterface;
struct NaClSignalContext;
struct NaClThreadInterface;  /* see sel_ldr_thread_interface.h */
struct NaClValidationCache;
struct NaClValidationMetadata;

extern volatile sig_atomic_t fork_num;
extern int fd_cage_table[CAGE_MAX][FILE_DESC_MAX];

struct NaClDebugCallbacks {
  void (*thread_create_hook)(struct NaClAppThread *natp);
  void (*thread_exit_hook)(struct NaClAppThread *natp);
  void (*process_exit_hook)(void);
};

enum NaClResourcePhase {
  NACL_RESOURCE_PHASE_START,
  NACL_RESOURCE_PHASE_REV_CHAN
};

#if NACL_WINDOWS
enum NaClDebugExceptionHandlerState {
  NACL_DEBUG_EXCEPTION_HANDLER_NOT_STARTED,
  NACL_DEBUG_EXCEPTION_HANDLER_STARTED,
  NACL_DEBUG_EXCEPTION_HANDLER_FAILED
};
/*
 * Callback function used to request that an exception handler be
 * attached using the Windows debug API.  See sel_main_chrome.h.
 */
typedef int (*NaClAttachDebugExceptionHandlerFunc)(const void *info,
                                                   size_t size);
#endif

struct NaClSpringboardInfo {
  /* These are addresses in untrusted address space (relative to mem_start). */
  uint32_t start_addr;
  uint32_t end_addr;
};

enum NaClThreadLaunchType {
  THREAD_LAUNCH_MAIN,
  THREAD_LAUNCH_FORK,
  THREAD_LAUNCH_EXEC
};

struct NaClApp {
  /*
   * children table lock children_mu is higher in the locking order than
   * the thread locks, i.e., children_mu must be acqured w/o holding
   * any thread table or per-thread lock (threads_mu or natp->mu).
   *
   * -jp
   */
  struct NaClMutex          children_mu;
  struct NaClCondVar        children_cv;
  struct DynArray           children;
  struct NaClApp            *parent;
  struct NaClApp            *master;
  /* mappings of `int fd` numbers to `NaClDesc *` */
  struct NaClDesc           *fd_maps[FILE_DESC_MAX];
  volatile sig_atomic_t     children_ids[CHILD_NUM_MAX];
  volatile sig_atomic_t     num_children;
  volatile sig_atomic_t     cage_id;
  volatile sig_atomic_t     num_lib;
  volatile sig_atomic_t     parent_id;
  enum NaClThreadLaunchType tl_type;

  /* yiwen: store the path of the execuable running inside this cage(as the main thread) */
  int                       argc;
  char                      **argv;
  char                      *binary;
  char                      *nacl_file;
  char const *const         *clean_environ;
  volatile int              in_fork;


  /*
   * public, user settable prior to app start.
   */
  uint8_t                   addr_bits;
  uintptr_t                 stack_size;
  uint32_t                  initial_nexe_max_code_bytes;
  /*
   * stack_size is the maximum size of the (main) stack.  The stack
   * memory is eager allocated (mapped in w/o MAP_NORESERVE) so<commit_msg>removing unused libnum and fdmaps from nap<commit_after>/*
 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * NaCl Simple/secure ELF loader (NaCl SEL).
 *
 * This loader can only process NaCl object files as produced using
 * the NaCl toolchain.  Other ELF files will be rejected.
 *
 * The primary function, NaClAppLoadFile, parses an ELF file,
 * allocates memory, loads the relocatable image from the ELF file
 * into memory, and performs relocation.  NaClAppRun runs the
 * resultant program.
 *
 * This loader is written in C so that it can be used by C-only as
 * well as C++ applications.  Other languages should also be able to
 * use their foreign-function interfaces to invoke C code.
 *
 * This loader must be part of the NaCl TCB, since it directly handles
 * externally supplied input (the ELF file).  Any security
 * vulnerabilities in handling the ELF image, e.g., buffer or integer
 * overflows, can put the application at risk.
 */

#ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_LDR_H_
#define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_LDR_H_ 1

#include <signal.h>
#include <stdbool.h>

#include "native_client/src/include/atomic_ops.h"
#include "native_client/src/include/nacl_base.h"
#include "native_client/src/include/portability.h"
#include "native_client/src/include/elf.h"

#include "native_client/src/shared/platform/nacl_host_desc.h"
#include "native_client/src/shared/platform/nacl_log.h"
#include "native_client/src/shared/platform/nacl_sync_checked.h"
#include "native_client/src/shared/platform/nacl_threads.h"

#include "native_client/src/shared/srpc/nacl_srpc.h"

#include "native_client/src/trusted/interval_multiset/nacl_interval_multiset.h"
#include "native_client/src/trusted/interval_multiset/nacl_interval_range_tree.h"

#include "native_client/src/trusted/service_runtime/dyn_array.h"
#include "native_client/src/trusted/service_runtime/nacl_error_code.h"
#include "native_client/src/trusted/service_runtime/nacl_kernel_service.h"
#include "native_client/src/trusted/service_runtime/nacl_resource.h"

#include "native_client/src/trusted/service_runtime/nacl_secure_service.h"

#include "native_client/src/trusted/service_runtime/sel_addrspace.h"
#include "native_client/src/trusted/service_runtime/sel_mem.h"
#include "native_client/src/trusted/service_runtime/sel_util.h"
#include "native_client/src/trusted/service_runtime/sel_rt.h"

#include "native_client/src/trusted/service_runtime/name_service/name_service.h"

#include "native_client/src/trusted/validator/ncvalidate.h"
#include "native_client/src/trusted/service_runtime/nacl_globals.h"

EXTERN_C_BEGIN

#define NACL_SERVICE_PORT_DESCRIPTOR    3
#define NACL_SERVICE_ADDRESS_DESCRIPTOR 4

#define NACL_DEFAULT_STACK_MAX  (16u << 20)  /* main thread stack */

#define NACL_BAD_FD                     -1

struct NaClAppThread;
struct NaClDesc;  /* see native_client/src/trusted/desc/nacl_desc_base.h */
struct NaClDynamicRegion;
struct NaClManifestProxy;
struct NaClReverseQuotaInterface;
struct NaClSignalContext;
struct NaClThreadInterface;  /* see sel_ldr_thread_interface.h */
struct NaClValidationCache;
struct NaClValidationMetadata;

extern volatile sig_atomic_t fork_num;
extern int fd_cage_table[CAGE_MAX][FILE_DESC_MAX];

struct NaClDebugCallbacks {
  void (*thread_create_hook)(struct NaClAppThread *natp);
  void (*thread_exit_hook)(struct NaClAppThread *natp);
  void (*process_exit_hook)(void);
};

enum NaClResourcePhase {
  NACL_RESOURCE_PHASE_START,
  NACL_RESOURCE_PHASE_REV_CHAN
};

#if NACL_WINDOWS
enum NaClDebugExceptionHandlerState {
  NACL_DEBUG_EXCEPTION_HANDLER_NOT_STARTED,
  NACL_DEBUG_EXCEPTION_HANDLER_STARTED,
  NACL_DEBUG_EXCEPTION_HANDLER_FAILED
};
/*
 * Callback function used to request that an exception handler be
 * attached using the Windows debug API.  See sel_main_chrome.h.
 */
typedef int (*NaClAttachDebugExceptionHandlerFunc)(const void *info,
                                                   size_t size);
#endif

struct NaClSpringboardInfo {
  /* These are addresses in untrusted address space (relative to mem_start). */
  uint32_t start_addr;
  uint32_t end_addr;
};

enum NaClThreadLaunchType {
  THREAD_LAUNCH_MAIN,
  THREAD_LAUNCH_FORK,
  THREAD_LAUNCH_EXEC
};

struct NaClApp {
  /*
   * children table lock children_mu is higher in the locking order than
   * the thread locks, i.e., children_mu must be acqured w/o holding
   * any thread table or per-thread lock (threads_mu or natp->mu).
   *
   * -jp
   */
  struct NaClMutex          children_mu;
  struct NaClCondVar        children_cv;
  struct DynArray           children;
  struct NaClApp            *parent;
  struct NaClApp            *master;
  /* mappings of `int fd` numbers to `NaClDesc *` */
  volatile sig_atomic_t     children_ids[CHILD_NUM_MAX];
  volatile sig_atomic_t     num_children;
  volatile sig_atomic_t     cage_id;
  volatile sig_atomic_t     parent_id;
  enum NaClThreadLaunchType tl_type;

  /* yiwen: store the path of the execuable running inside this cage(as the main thread) */
  int                       argc;
  char                      **argv;
  char                      *binary;
  char                      *nacl_file;
  char const *const         *clean_environ;
  volatile int              in_fork;


  /*
   * public, user settable prior to app start.
   */
  uint8_t                   addr_bits;
  uintptr_t                 stack_size;
  uint32_t                  initial_nexe_max_code_bytes;
  /*
   * stack_size is the maximum size of the (main) stack.  The stack
   * memory is eager allocated (mapped in w/o MAP_NORESERVE) so
{'fraction_non_alphanumeric': 0.07900912646675358, 'fraction_numerical': 0.0033029117774880487, 'mean_word_length': 3.877490462060195, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4610554', 'n_tokens_mistral': 3818, 'n_tokens_neox': 3672, 'n_words': 929}