From 77402bdbc877746eacb5a9e2f4d4ea422bd50eff Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 6 Feb 2021 18:12:47 -0500 Subject: [PATCH] avoid overflows when computing memory offsets and allocating memory --- src/KSPACE/remap.cpp | 2 +- src/OPT/pair_eam_opt.cpp | 4 ++-- src/USER-DPD/pair_multi_lucy.cpp | 6 +++--- src/USER-DPD/pair_multi_lucy_rx.cpp | 6 +++--- src/USER-MISC/fix_imd.cpp | 4 ++-- src/USER-MISC/fix_ttm_mod.cpp | 2 +- src/USER-PHONON/fix_phonon.cpp | 2 +- src/USER-REAXC/reaxc_allocate.cpp | 4 ++-- src/angle_hybrid.cpp | 2 +- src/bond_hybrid.cpp | 2 +- src/comm.cpp | 9 +++++---- src/compute_orientorder_atom.cpp | 2 +- src/memory.h | 10 +++++----- 13 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/KSPACE/remap.cpp b/src/KSPACE/remap.cpp index c3c3703d1d..ab2ec687d7 100644 --- a/src/KSPACE/remap.cpp +++ b/src/KSPACE/remap.cpp @@ -597,7 +597,7 @@ struct remap_plan_3d *remap_3d_create_plan( if (memory == 1) { if (nrecv > 0) { plan->scratch = - (FFT_SCALAR *) malloc(nqty*out.isize*out.jsize*out.ksize * + (FFT_SCALAR *) malloc((size_t)nqty*out.isize*out.jsize*out.ksize * sizeof(FFT_SCALAR)); if (plan->scratch == nullptr) return nullptr; } diff --git a/src/OPT/pair_eam_opt.cpp b/src/OPT/pair_eam_opt.cpp index c7d3541e67..67923515c9 100644 --- a/src/OPT/pair_eam_opt.cpp +++ b/src/OPT/pair_eam_opt.cpp @@ -112,7 +112,7 @@ void PairEAMOpt::eval() int ntypes2 = ntypes*ntypes; fast_alpha_t* _noalias fast_alpha = - (fast_alpha_t*) malloc(ntypes2*(nr+1)*sizeof(fast_alpha_t)); + (fast_alpha_t*) malloc((size_t)ntypes2*(nr+1)*sizeof(fast_alpha_t)); for (i = 0; i < ntypes; i++) for (j = 0; j < ntypes; j++) { fast_alpha_t* _noalias tab = &fast_alpha[i*ntypes*nr+j*nr]; if (type2rhor[i+1][j+1] >= 0) { @@ -135,7 +135,7 @@ void PairEAMOpt::eval() fast_alpha_t* _noalias tabeight = fast_alpha; fast_gamma_t* _noalias fast_gamma = - (fast_gamma_t*) malloc(ntypes2*(nr+1)*sizeof(fast_gamma_t)); + (fast_gamma_t*) malloc((size_t)ntypes2*(nr+1)*sizeof(fast_gamma_t)); for (i = 0; i < ntypes; i++) for (j = 0; j < ntypes; j++) { fast_gamma_t* _noalias tab = &fast_gamma[i*ntypes*nr+j*nr]; if (type2rhor[i+1][j+1] >= 0) { diff --git a/src/USER-DPD/pair_multi_lucy.cpp b/src/USER-DPD/pair_multi_lucy.cpp index 11bc1c7ba5..70ea8b40ad 100644 --- a/src/USER-DPD/pair_multi_lucy.cpp +++ b/src/USER-DPD/pair_multi_lucy.cpp @@ -221,9 +221,9 @@ void PairMultiLucy::allocate() memory->create(cutsq,nt,nt,"pair:cutsq"); memory->create(tabindex,nt,nt,"pair:tabindex"); - memset(&setflag[0][0],0,nt*nt*sizeof(int)); - memset(&cutsq[0][0],0,nt*nt*sizeof(double)); - memset(&tabindex[0][0],0,nt*nt*sizeof(int)); + memset(&setflag[0][0],0,sizeof(int)*nt*nt); + memset(&cutsq[0][0],0,sizeof(double)*nt*nt); + memset(&tabindex[0][0],0,sizeof(int)*nt*nt); } /* ---------------------------------------------------------------------- diff --git a/src/USER-DPD/pair_multi_lucy_rx.cpp b/src/USER-DPD/pair_multi_lucy_rx.cpp index e1a263b7dc..bd720ae138 100644 --- a/src/USER-DPD/pair_multi_lucy_rx.cpp +++ b/src/USER-DPD/pair_multi_lucy_rx.cpp @@ -310,9 +310,9 @@ void PairMultiLucyRX::allocate() memory->create(cutsq,nt,nt,"pair:cutsq"); memory->create(tabindex,nt,nt,"pair:tabindex"); - memset(&setflag[0][0],0,nt*nt*sizeof(int)); - memset(&cutsq[0][0],0,nt*nt*sizeof(double)); - memset(&tabindex[0][0],0,nt*nt*sizeof(int)); + memset(&setflag[0][0],0,sizeof(int)*nt*nt); + memset(&cutsq[0][0],0,sizeof(double)*nt*nt); + memset(&tabindex[0][0],0,sizeof(int)*nt*nt); } /* ---------------------------------------------------------------------- diff --git a/src/USER-MISC/fix_imd.cpp b/src/USER-MISC/fix_imd.cpp index 0da5b29b62..8796de289e 100644 --- a/src/USER-MISC/fix_imd.cpp +++ b/src/USER-MISC/fix_imd.cpp @@ -919,7 +919,7 @@ void FixIMD::post_force(int /*vflag*/) if (imd_forces < length) { /* grow holding space for forces, if needed. */ memory->destroy(force_buf); - force_buf = (void *) memory->smalloc(length*size_one, + force_buf = (void *) memory->smalloc((bigint)length*size_one, "imd:force_buf"); } imd_forces = length; @@ -960,7 +960,7 @@ void FixIMD::post_force(int /*vflag*/) if (old_imd_forces < imd_forces) { /* grow holding space for forces, if needed. */ if (force_buf != nullptr) memory->sfree(force_buf); - force_buf = memory->smalloc(imd_forces*size_one, "imd:force_buf"); + force_buf = memory->smalloc((bigint)imd_forces*size_one, "imd:force_buf"); } } MPI_Bcast(force_buf, imd_forces*size_one, MPI_BYTE, 0, world); diff --git a/src/USER-MISC/fix_ttm_mod.cpp b/src/USER-MISC/fix_ttm_mod.cpp index 9cac1c02b2..7056ad6a00 100644 --- a/src/USER-MISC/fix_ttm_mod.cpp +++ b/src/USER-MISC/fix_ttm_mod.cpp @@ -136,7 +136,7 @@ FixTTMMod::FixTTMMod(LAMMPS *lmp, int narg, char **arg) : gfactor1 = new double[atom->ntypes+1]; gfactor2 = new double[atom->ntypes+1]; // allocate 3d grid variables - total_nnodes = nxnodes*nynodes*nznodes; + total_nnodes = (bigint)nxnodes*nynodes*nznodes; memory->create(nsum,nxnodes,nynodes,nznodes,"ttm/mod:nsum"); memory->create(nsum_all,nxnodes,nynodes,nznodes,"ttm/mod:nsum_all"); memory->create(sum_vsq,nxnodes,nynodes,nznodes,"ttm/mod:sum_vsq"); diff --git a/src/USER-PHONON/fix_phonon.cpp b/src/USER-PHONON/fix_phonon.cpp index 455ca80b12..30fb7baad5 100644 --- a/src/USER-PHONON/fix_phonon.cpp +++ b/src/USER-PHONON/fix_phonon.cpp @@ -723,7 +723,7 @@ void FixPhonon::postprocess( ) fwrite(&nucell, sizeof(int), 1, fp_bin); fwrite(&boltz, sizeof(double), 1, fp_bin); - fwrite(Phi_all[0],sizeof(double),ntotal*fft_dim2*2,fp_bin); + fwrite(Phi_all[0],sizeof(double),(bigint)ntotal*fft_dim2*2,fp_bin); fwrite(&TempAve, sizeof(double),1, fp_bin); fwrite(&basevec[0], sizeof(double),9, fp_bin); diff --git a/src/USER-REAXC/reaxc_allocate.cpp b/src/USER-REAXC/reaxc_allocate.cpp index f043dc85d6..9ba92b9dfa 100644 --- a/src/USER-REAXC/reaxc_allocate.cpp +++ b/src/USER-REAXC/reaxc_allocate.cpp @@ -302,10 +302,10 @@ int Allocate_Workspace( reax_system * /*system*/, control_params * control, // storage for reductions with multiple threads #ifdef LMP_USER_OMP - workspace->CdDeltaReduction = (double *) scalloc(control->error_ptr, sizeof(double), total_cap*control->nthreads, + workspace->CdDeltaReduction = (double *) scalloc(control->error_ptr, sizeof(double), (rc_bigint)total_cap*control->nthreads, "cddelta_reduce"); - workspace->forceReduction = (rvec *) scalloc(control->error_ptr, sizeof(rvec), total_cap*control->nthreads, + workspace->forceReduction = (rvec *) scalloc(control->error_ptr, sizeof(rvec), (rc_bigint)total_cap*control->nthreads, "forceReduction"); workspace->valence_angle_atom_myoffset = (int *) scalloc(control->error_ptr, sizeof(int), total_cap, "valence_angle_atom_myoffset"); diff --git a/src/angle_hybrid.cpp b/src/angle_hybrid.cpp index 1e4ade3ca8..d7e3051b90 100644 --- a/src/angle_hybrid.cpp +++ b/src/angle_hybrid.cpp @@ -113,7 +113,7 @@ void AngleHybrid::compute(int eflag, int vflag) const int nthreads = comm->nthreads; if (comm->nthreads > 1) { - const int nall = atom->nlocal + atom->nghost; + const bigint nall = atom->nlocal + atom->nghost; if (eflag_atom) memset(&eatom[0],0,nall*nthreads*sizeof(double)); if (vflag_atom) diff --git a/src/bond_hybrid.cpp b/src/bond_hybrid.cpp index 3a6bb4b7de..f1debc0676 100644 --- a/src/bond_hybrid.cpp +++ b/src/bond_hybrid.cpp @@ -113,7 +113,7 @@ void BondHybrid::compute(int eflag, int vflag) const int nthreads = comm->nthreads; if (nthreads > 1) { - const int nall = atom->nlocal + atom->nghost; + const bigint nall = atom->nlocal + atom->nghost; if (eflag_atom) memset(&eatom[0],0,nall*nthreads*sizeof(double)); if (vflag_atom) diff --git a/src/comm.cpp b/src/comm.cpp index 77d1ade148..8e119744fc 100644 --- a/src/comm.cpp +++ b/src/comm.cpp @@ -979,7 +979,7 @@ rendezvous_all2all(int n, char *inbuf, int insize, int inorder, int *procs, offsets[0] = 0; for (int i = 1; i < nprocs; i++) - offsets[i] = offsets[i-1] + insize*procs_a2a[i-1]; + offsets[i] = offsets[i-1] + (bigint)insize*procs_a2a[i-1]; bigint offset = 0; for (int i = 0; i < n; i++) { @@ -989,7 +989,8 @@ rendezvous_all2all(int n, char *inbuf, int insize, int inorder, int *procs, offset += insize; } - all2all1_bytes = nprocs*sizeof(int) + nprocs*sizeof(bigint) + n*insize; + all2all1_bytes = nprocs*sizeof(int) + nprocs*sizeof(bigint) + + (bigint)n*insize; } else { procs_a2a = procs; @@ -1085,7 +1086,7 @@ rendezvous_all2all(int n, char *inbuf, int insize, int inorder, int *procs, offsets[0] = 0; for (int i = 1; i < nprocs; i++) - offsets[i] = offsets[i-1] + outsize*procs_a2a[i-1]; + offsets[i] = offsets[i-1] + (bigint)outsize*procs_a2a[i-1]; bigint offset = 0; for (int i = 0; i < nrvous_out; i++) { @@ -1096,7 +1097,7 @@ rendezvous_all2all(int n, char *inbuf, int insize, int inorder, int *procs, } all2all2_bytes = nprocs*sizeof(int) + nprocs*sizeof(bigint) + - nrvous_out*outsize; + (bigint)nrvous_out*outsize; } else { procs_a2a = procs_rvous; diff --git a/src/compute_orientorder_atom.cpp b/src/compute_orientorder_atom.cpp index eb5a96b706..8a836a97b8 100644 --- a/src/compute_orientorder_atom.cpp +++ b/src/compute_orientorder_atom.cpp @@ -258,7 +258,7 @@ void ComputeOrientOrderAtom::compute_peratom() double **x = atom->x; int *mask = atom->mask; - memset(&qnarray[0][0],0,nmax*ncol*sizeof(double)); + memset(&qnarray[0][0],0,sizeof(double)*nmax*ncol); for (ii = 0; ii < inum; ii++) { i = ilist[ii]; diff --git a/src/memory.h b/src/memory.h index 64685feac0..d85eb64a36 100644 --- a/src/memory.h +++ b/src/memory.h @@ -410,18 +410,18 @@ class Memory : protected Pointers { nbytes = ((bigint) sizeof(TYPE ***)) * n1; array = (TYPE ****) smalloc(nbytes,name); - int i,j,k; + bigint i,j,k; bigint m1,m2; bigint n = 0; for (i = 0; i < n1; i++) { - m2 = ((bigint) i) * n2; + m2 = i * n2; array[i] = &plane[m2]; for (j = 0; j < n2; j++) { - m1 = ((bigint) i) * n2 + j; - m2 = ((bigint) i) * n2*n3 + j*n3; + m1 = i * n2 + j; + m2 = i * n2*n3 + j*n3; plane[m1] = &cube[m2]; for (k = 0; k < n3; k++) { - m1 = ((bigint) i) * n2*n3 + j*n3 + k; + m1 = i * n2*n3 + j*n3 + k; cube[m1] = &data[n]; n += n4; }