Lines Matching refs:task
218 static bool task_check_xpm(struct loadtask *task);
219 static bool map_library_header(struct loadtask *task);
220 static bool task_map_library(struct loadtask *task, struct reserved_address_params *reserved_params);
221 static bool resolve_fd_to_realpath(struct loadtask *task);
222 static bool load_library_header(struct loadtask *task);
223 static void task_load_library(struct loadtask *task, struct reserved_address_params *reserved_params);
230 static void open_library_by_path(const char *name, const char *s, struct loadtask *task, struct zip_info *z_info);
231 static void handle_asan_path_open_by_task(int fd, const char *name, ns_t *namespace, struct loadtask *task, struct zip_info *z_info);
3573 struct loadtask *task = NULL;
3692 task = create_loadtask(file, head, ns, true);
3693 if (!task) {
3699 if (!load_library_header(task)) {
3704 LD_LOGE("dlopen_impl load library header failed for %{public}s", task->name);
3709 reserved_params.target = task->p;
3712 if (!task->p) {
3713 LD_LOGE("dlopen_impl load library failed for %{public}s", task->name);
3724 if (!task->isloaded) {
3725 is_task_appended = append_loadtasks(tasks, task);
3728 preload_deps(task->p, tasks);
3741 p = task->p;
3742 if (!task->isloaded) {
3746 free_task(task);
3747 task = NULL;
3842 free_task(task);
4869 static void open_library_by_path(const char *name, const char *s, struct loadtask *task, struct zip_info *z_info)
4871 char *buf = task->buf;
4872 size_t buf_size = sizeof task->buf;
4883 task->fd = z_info->fd;
4884 task->file_offset = z_info->file_offset;
4890 if ((task->fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) break;
4898 static void handle_asan_path_open_by_task(int fd, const char *name, ns_t *namespace, struct loadtask *task,
4913 open_library_by_path(name, new_lib_paths, task, z_info);
4916 task->fd);
4919 open_library_by_path(name, namespace->asan_lib_paths, task, z_info);
4922 task->fd);
4924 open_library_by_path(name, namespace->lib_paths, task, z_info);
4927 task->fd);
5057 static bool task_check_xpm(struct loadtask *task)
5060 void *map = mmap(0, mapLen, PROT_READ, MAP_PRIVATE | MAP_XPM, task->fd, task->file_offset);
5062 LD_LOGE("Xpm check failed for %{public}s, errno for mmap is: %{public}d", task->name, errno);
5069 static bool map_library_header(struct loadtask *task)
5076 if (!task_check_xpm(task)) {
5080 ssize_t l = pread(task->fd, task->ehdr_buf, sizeof task->ehdr_buf, task->file_offset);
5081 task->eh = task->ehdr_buf;
5083 LD_LOGE("Error mapping header %{public}s: failed to read fd errno: %{public}d", task->name, errno);
5086 if (l < sizeof(Ehdr) || (task->eh->e_type != ET_DYN && task->eh->e_type != ET_EXEC)) {
5088 task->name, l, task->eh->e_type);
5091 task->phsize = task->eh->e_phentsize * task->eh->e_phnum;
5092 if (task->phsize > sizeof task->ehdr_buf - sizeof(Ehdr)) {
5093 task->allocated_buf = malloc(task->phsize);
5094 if (!task->allocated_buf) {
5095 LD_LOGE("Error mapping header %{public}s: failed to alloc memory errno: %{public}d", task->name, errno);
5098 l = pread(task->fd, task->allocated_buf, task->phsize, task->eh->e_phoff + task->file_offset);
5100 LD_LOGE("Error mapping header %{public}s: failed to pread errno: %{public}d", task->name, errno);
5103 if (l != task->phsize) {
5104 LD_LOGE("Error mapping header %{public}s: unmatched phsize errno: %{public}d", task->name, errno);
5107 ph = task->ph0 = task->allocated_buf;
5108 } else if (task->eh->e_phoff + task->phsize > l) {
5109 l = pread(task->fd, task->ehdr_buf + 1, task->phsize, task->eh->e_phoff + task->file_offset);
5111 LD_LOGE("Error mapping header %{public}s: failed to pread errno: %{public}d", task->name, errno);
5114 if (l != task->phsize) {
5115 LD_LOGE("Error mapping header %{public}s: unmatched phsize", task->name);
5118 ph = task->ph0 = (void *)(task->ehdr_buf + 1);
5120 ph = task->ph0 = (void *)((char *)task->ehdr_buf + task->eh->e_phoff);
5123 for (i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsize)) {
5125 task->dyn = ph->p_vaddr;
5127 task->tls_image = ph->p_vaddr;
5128 task->tls.align = ph->p_align;
5129 task->tls.len = ph->p_filesz;
5130 task->tls.size = ph->p_memsz;
5139 task->dyn_map_len = ph->p_memsz + (ph->p_offset - off_start);
5143 task->dyn_map = mmap(0, task->dyn_map_len, PROT_READ, MAP_PRIVATE, task->fd, off_start + task->file_offset);
5144 if (task->dyn_map == MAP_FAILED) {
5145 LD_LOGE("Error mapping header %{public}s: failed to map dynamic section errno: %{public}d", task->name, errno);
5148 task->dyn_addr = (size_t *)((unsigned char *)task->dyn_map + (ph->p_offset - off_start));
5150 if (search_vec(task->dyn_addr, &dyn_tmp, DT_STRTAB)) {
5153 LD_LOGE("Error mapping header %{public}s: DT_STRTAB not found", task->name);
5156 if (search_vec(task->dyn_addr, &dyn_tmp, DT_STRSZ)) {
5159 LD_LOGE("Error mapping header %{public}s: DT_STRSZ not found", task->name);
5164 task->shsize = task->eh->e_shentsize * task->eh->e_shnum;
5165 off_start = task->eh->e_shoff;
5167 task->shsize += task->eh->e_shoff - off_start;
5168 task->shdr_allocated_buf = mmap(0, task->shsize, PROT_READ, MAP_PRIVATE, task->fd, off_start + task->file_offset);
5169 if (task->shdr_allocated_buf == MAP_FAILED) {
5171 task->name, errno);
5174 Shdr *sh = (char *)task->shdr_allocated_buf + task->eh->e_shoff - off_start;
5175 for (i = task->eh->e_shnum; i; i--, sh = (void *)((char *)sh + task->eh->e_shentsize)) {
5181 task->str_map_len = sh->sh_size + (sh->sh_offset - off_start);
5182 task->str_map = mmap(0, task->str_map_len, PROT_READ, MAP_PRIVATE, task->fd, off_start + task->file_offset);
5183 if (task->str_map == MAP_FAILED) {
5185 task->name, errno);
5188 task->str_addr = (char *)task->str_map + sh->sh_offset - off_start;
5191 if (!task->dyn) {
5192 LD_LOGE("Error mapping header %{public}s: dynamic section not found", task->name);
5195 if (task->shdr_allocated_buf != MAP_FAILED) {
5196 munmap(task->shdr_allocated_buf, task->shsize);
5197 task->shdr_allocated_buf = MAP_FAILED;
5203 free(task->allocated_buf);
5204 task->allocated_buf = NULL;
5205 if (task->shdr_allocated_buf != MAP_FAILED) {
5206 munmap(task->shdr_allocated_buf, task->shsize);
5207 task->shdr_allocated_buf = MAP_FAILED;
5212 static bool task_map_library(struct loadtask *task, struct reserved_address_params *reserved_params)
5218 Phdr *ph = task->ph0;
5227 for (i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsize)) {
5229 task->p->relro_start = ph->p_vaddr & -PAGE_SIZE;
5230 task->p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
5253 if (!task->dyn) {
5254 LD_LOGE("Error mapping library: !task->dyn dynamic section not found task->name=%{public}s", task->name);
5257 if (DL_FDPIC && !(task->eh->e_flags & FDPIC_CONSTDISP_FLAG)) {
5258 task->p->loadmap = calloc(1, sizeof(struct fdpic_loadmap) + nsegs * sizeof(struct fdpic_loadseg));
5259 if (!task->p->loadmap) {
5263 task->p->loadmap->nsegs = nsegs;
5264 for (ph = task->ph0, i = 0; i < nsegs; ph = (void *)((char *)ph + task->eh->e_phentsize)) {
5273 task->fd, ph->p_offset & -PAGE_SIZE + task->file_offset);
5275 unmap_library(task->p);
5276 LD_LOGE("Error mapping library: PT_LOAD mmap failed task->name=%{public}s errno=%{public}d map_len=%{public}d",
5277 task->name, errno, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE - 1));
5280 task->p->loadmap->segs[i].addr = (size_t)map +
5282 task->p->loadmap->segs[i].p_vaddr = ph->p_vaddr;
5283 task->p->loadmap->segs[i].p_memsz = ph->p_memsz;
5298 map = (void *)task->p->loadmap->segs[0].addr;
5311 size_t maxinum_alignment = phdr_table_get_maxinum_alignment(task->ph0, task->eh->e_phnum);
5319 LD_LOGE("Error mapping library: map len is larger than reserved address task->name=%{public}s", task->name);
5341 : mmap((void *)start_addr, map_len, prot, map_flags, task->fd, off_start + task->file_offset);
5344 " task->fd=%{public}d task->name=%{public}s map_len=%{public}d",
5345 errno, DL_NOMMU_SUPPORT, task->fd, task->name, map_len);
5368 : mmap(real_map, map_len, prot, map_flags | MAP_FIXED, task->fd, off_start + task->file_offset);
5371 "task->fd=%{public}d task->name=%{public}s map_len=%{public}d",
5372 errno, DL_NOMMU_SUPPORT, task->fd, task->name, map_len);
5399 task->p->map = map;
5400 task->p->map_len = map_len;
5403 if (task->eh->e_type != ET_DYN && addr_min && map != (void *)addr_min) {
5404 LD_LOGE("Error mapping library: ET_DYN task->name=%{public}s", task->name);
5409 task->p->phdr = 0;
5410 task->p->phnum = 0;
5411 for (ph = task->ph0, i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsize)) {
5421 if (!task->p->phdr && task->eh->e_phoff >= ph->p_offset
5422 && task->eh->e_phoff + task->phsize <= ph->p_offset + ph->p_filesz) {
5423 task->p->phdr = (void *)(base + ph->p_vaddr + (task->eh->e_phoff - ph->p_offset));
5424 task->p->phnum = task->eh->e_phnum;
5425 task->p->phentsize = task->eh->e_phentsize;
5438 task->fd,
5439 off_start + task->file_offset) == MAP_FAILED) {
5440 LD_LOGE("Error mapping library: mmap fix failed task->name=%{public}s errno=%{public}d", task->name, errno);
5457 LD_LOGE("Error mapping library: PF_W mmap fix failed errno=%{public}d task->name=%{public}s zeromap_size=%{public}d",
5458 errno, task->name, zeromap_size);
5461 set_bss_vma_name(task->p->name, (void *)pgbrk, zeromap_size);
5464 for (i = 0; ((size_t *)(base + task->dyn))[i]; i += NEXT_DYNAMIC_INDEX) {
5465 if (((size_t *)(base + task->dyn))[i] == DT_TEXTREL) {
5467 LD_LOGE("Error mapping library: mprotect failed task->name=%{public}s errno=%{public}d", task->name, errno);
5476 (void)is_section_exist(task->eh, sizeof(Ehdr), task->fd, ".kernelpermission");
5481 task->p->base = base;
5482 task->p->dynv = laddr(task->p, task->dyn);
5483 if (task->p->tls.size) {
5484 task->p->tls.image = laddr(task->p, task->tls_image);
5486 free(task->allocated_buf);
5487 task->allocated_buf = NULL;
5493 unmap_library(task->p);
5495 free(task->allocated_buf);
5496 task->allocated_buf = NULL;
5500 static bool resolve_fd_to_realpath(struct loadtask *task)
5505 int ret = snprintf(proc_self_fd, sizeof(proc_self_fd), "/proc/self/fd/%d", task->fd);
5514 strncpy(task->buf, resolved_path, PATH_MAX);
5519 static bool load_library_header(struct loadtask *task)
5521 const char *name = task->name;
5522 struct dso *needed_by = task->needed_by;
5523 ns_t *namespace = task->namespace;
5524 bool check_inherited = task->check_inherited;
5576 task->isloaded = true;
5577 task->p = &ldso;
5585 task->pathname = name;
5586 if (!is_accessible(namespace, task->pathname, g_is_asan, check_inherited)) {
5588 task->pathname, namespace ? namespace->ns_name : "NULL");
5589 task->fd = -1;
5591 task->fd = z_info.fd;
5592 task->file_offset = z_info.file_offset;
5599 task->pathname = name;
5600 if (!is_accessible(namespace, task->pathname, g_is_asan, check_inherited)) {
5602 task->pathname, namespace ? namespace->ns_name : "NULL");
5603 task->fd = -1;
5605 task->fd = open(name, O_RDONLY | O_CLOEXEC);
5611 task->p = find_library_by_name(name, namespace, check_inherited);
5612 if (task->p) {
5613 task->isloaded = true;
5615 name, namespace->ns_name, task->p->name, task->p->namespace->ns_name);
5622 task->fd = -1;
5624 open_library_by_path(name, namespace->env_paths, task, &z_info);
5626 for (task->p = needed_by; task->fd == -1 && task->p; task->p = task->p->needed_by) {
5627 if (fixup_rpath(task->p, task->buf, sizeof task->buf) < 0) {
5628 task->fd = INVALID_FD_INHIBIT_FURTHER_SEARCH; /* Inhibit further search. */
5630 if (task->p->rpath) {
5631 open_library_by_path(name, task->p->rpath, task, &z_info);
5632 if (task->fd != -1 && resolve_fd_to_realpath(task)) {
5633 if (!is_accessible(namespace, task->buf, g_is_asan, check_inherited)) {
5636 close(task->fd);
5637 task->fd = -1;
5643 handle_asan_path_open_by_task(task->fd, name, namespace, task, &z_info);
5644 LD_LOGD("load_library handle_asan_path_open_by_task fd:%{public}d.", task->fd);
5646 if (task->fd == -1 && namespace->lib_paths) {
5647 open_library_by_path(name, namespace->lib_paths, task, &z_info);
5648 LD_LOGD("load_library no asan lib_paths path_open fd:%{public}d.", task->fd);
5651 task->pathname = task->buf;
5653 if (task->fd < 0) {
5656 task->name, namespace->ns_name, errno);
5665 task->namespace = inherit->inherited_ns;
5666 task->check_inherited = false;
5667 if (load_library_header(task)) {
5672 task->name, namespace->ns_name);
5676 if (fstat(task->fd, &st) < 0) {
5677 LD_LOGE("Error loading header %{public}s: failed to get file state errno=%{public}d", task->name, errno);
5678 close(task->fd);
5679 task->fd = -1;
5683 task->p = find_library_by_fstat(&st, namespace, check_inherited, task->file_offset);
5684 if (task->p) {
5688 if (!task->p->shortname && task->pathname != name) {
5689 task->p->shortname = strrchr(task->p->name, '/') + 1;
5691 close(task->fd);
5692 task->fd = -1;
5693 task->isloaded = true;
5695 name, namespace->ns_name, task->p->name, task->p->namespace->ns_name);
5699 map = noload ? 0 : map_library_header(task);
5701 LD_LOGE("Error loading header %{public}s: failed to map header", task->name);
5702 close(task->fd);
5703 task->fd = -1;
5712 alloc_size = sizeof(struct dso) + strlen(task->pathname) + 1;
5713 if (runtime && task->tls.size) {
5714 size_t per_th = task->tls.size + task->tls.align + sizeof(void *) * (tls_cnt + TLS_CNT_INCREASE);
5722 task->p = calloc(1, alloc_size);
5723 if (!task->p) {
5724 LD_LOGE("Error loading header %{public}s: failed to allocate dso", task->name);
5725 close(task->fd);
5726 task->fd = -1;
5729 task->p->dev = st.st_dev;
5730 task->p->ino = st.st_ino;
5731 task->p->file_offset = task->file_offset;
5732 task->p->needed_by = needed_by;
5733 task->p->name = task->p->buf;
5734 strcpy(task->p->name, task->pathname);
5735 task->p->tls = task->tls;
5736 task->p->dynv = task->dyn_addr;
5737 task->p->strings = task->str_addr;
5740 if (search_vec(task->p->dynv, &rpath_offset, DT_RPATH))
5741 task->p->rpath_orig = task->p->strings + rpath_offset;
5742 if (search_vec(task->p->dynv, &runpath_offset, DT_RUNPATH))
5743 task->p->rpath_orig = task->p->strings + runpath_offset;
5746 if (task->pathname != name) {
5747 task->p->shortname = strrchr(task->p->name, '/') + 1;
5750 if (task->p->tls.size) {
5751 task->p->tls_id = ++tls_cnt;
5752 task->p->new_dtv = (void *)(-sizeof(size_t) &
5753 (uintptr_t)(task->p->name + strlen(task->p->name) + sizeof(size_t)));
5754 task->p->new_tls = (void *)(task->p->new_dtv + n_th * (tls_cnt + 1));
5757 tail->next = task->p;
5758 task->p->prev = tail;
5759 tail = task->p;
5762 task->p->namespace = namespace;
5763 ns_add_dso(namespace, task->p);
5767 static void task_load_library(struct loadtask *task, struct reserved_address_params *reserved_params)
5769 LD_LOGD("load_library loading ns=%{public}s name=%{public}s by_dlopen=%{public}d", task->namespace->ns_name, task->p->name, runtime);
5770 bool map = noload ? 0 : task_map_library(task, reserved_params);
5771 __close(task->fd);
5772 task->fd = -1;
5775 task->name, noload, errno);
5776 error("Error loading library %s: failed to map library noload=%d errno=%d", task->name, noload, errno);
5787 decode_dyn(task->p);
5788 if (find_sym(task->p, "__libc_start_main", 1).sym &&
5789 find_sym(task->p, "stdin", 1).sym) {
5790 do_dlclose(task->p, 0);
5791 task->p = NULL;
5792 free((void*)task->name);
5793 task->name = ld_strdup("libc.so");
5794 task->check_inherited = true;
5795 if (!load_library_header(task)) {
5796 LD_LOGE("Error loading library %{public}s: failed to load libc.so", task->name);
5797 error("Error loading library %s: failed to load libc.so", task->name);
5808 reclaim_gaps(task->p);
5810 task->p->runtime_loaded = runtime;
5812 task->p->by_dlopen = 1;
5817 makefuncdescs(task->p);
5821 dprintf(1, "\t%s => %s (%p)\n", task->name, task->pathname, task->p->base);
5826 libc.load_hook((long unsigned int)task->p->base, task->p->phdr, task->p->phnum);
5871 LD_LOGD("load_library %{public}s adding DT_NEEDED task %{public}s namespace(%{public}s)", p->name, dtneed_name, namespace->ns_name);
5872 struct loadtask *task = create_loadtask(dtneed_name, p, namespace, true);
5873 if (!task) {
5874 LD_LOGE("Error loading dependencies %{public}s : create load task failed", p->name);
5875 error("Error loading dependencies for %s : create load task failed", p->name);
5882 if (!load_library_header(task)) {
5883 free_task(task);
5884 task = NULL;
5895 p->deps[cnt++] = task->p;
5896 if (task->isloaded) {
5897 free_task(task);
5898 task = NULL;
5900 append_loadtasks(tasks, task);
5912 struct loadtask *task = NULL;
5914 task = get_loadtask(tasks, i);
5915 if (!task) {
5918 if (task->dyn_map_len) {
5919 munmap(task->dyn_map, task->dyn_map_len);
5920 task->dyn_map = NULL;
5921 task->dyn_map_len = 0;
5922 if (task->p) {
5923 task->p->dynv = NULL;
5926 if (task->str_map_len) {
5927 munmap(task->str_map, task->str_map_len);
5928 task->str_map = NULL;
5929 task->str_map_len = 0;
5930 if (task->p) {
5931 task->p->strings = NULL;
5949 struct loadtask *task = NULL;
5952 task = get_loadtask(tasks, i);
5953 if (task) {
5955 reserved_address = reserved_params->reserved_address_recursive || (reserved_params->target == task->p);
5957 task_load_library(task, reserved_address ? reserved_params : NULL);
5994 struct loadtask *task = NULL;
6004 task = create_loadtask(s, NULL, ns, true);
6005 if (!task) {
6008 if (load_library_header(task)) {
6009 if (!task->isloaded) {
6010 append_loadtasks(tasks, task);
6011 task = NULL;
6014 if (task) {
6015 free_task(task);