protected int loadLibraryFrom()

in java/com/facebook/soloader/DirectorySoSource.java [67:132]


  protected int loadLibraryFrom(
      String soName, int loadFlags, File libDir, StrictMode.ThreadPolicy threadPolicy)
      throws IOException {
    if (SoLoader.sSoFileLoader == null) {
      throw new IllegalStateException("SoLoader.init() not yet called");
    }

    if (denyList.contains(soName)) {
      Log.d(
          SoLoader.TAG,
          soName + " is on the denyList, skip loading from " + libDir.getCanonicalPath());
      return LOAD_RESULT_NOT_FOUND;
    }

    File soFile = getSoFileByName(soName);
    if (soFile == null) {
      Log.d(SoLoader.TAG, soName + " not found on " + libDir.getCanonicalPath());
      return LOAD_RESULT_NOT_FOUND;
    } else {
      Log.d(SoLoader.TAG, soName + " found on " + libDir.getCanonicalPath());
    }
    if ((loadFlags & LOAD_FLAG_ALLOW_IMPLICIT_PROVISION) != 0
        && (flags & ON_LD_LIBRARY_PATH) != 0) {
      Log.d(SoLoader.TAG, soName + " loaded implicitly");
      return LOAD_RESULT_IMPLICITLY_PROVIDED;
    }

    ElfByteChannel bc = null;
    boolean shouldLoadDependencies = (flags & RESOLVE_DEPENDENCIES) != 0;
    boolean shouldLoadFromFile = soFile.getName().equals(soName);
    try {
      if (shouldLoadDependencies || !shouldLoadFromFile) {
        bc = getChannel(soFile);
      }

      if (shouldLoadDependencies) {
        loadDependencies(soName, bc, loadFlags, threadPolicy);
      } else {
        Log.d(SoLoader.TAG, "Not resolving dependencies for " + soName);
      }

      try {
        if (shouldLoadFromFile) {
          SoLoader.sSoFileLoader.load(soFile.getAbsolutePath(), loadFlags);
        } else {
          // The shared object does not exist in the file system, only in memory
          SoLoader.sSoFileLoader.loadBytes(soFile.getAbsolutePath(), bc, loadFlags);
        }

      } catch (UnsatisfiedLinkError e) {
        if (e.getMessage().contains("bad ELF magic")) {
          Log.d(SoLoader.TAG, "Corrupted lib file detected");
          // Swallow exception. Higher layers will try again from a backup source
          return LOAD_RESULT_CORRUPTED_LIB_FILE;
        } else {
          throw e;
        }
      }
    } finally {
      if (bc != null) {
        bc.close();
      }
    }

    return LOAD_RESULT_LOADED;
  }