1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-12-21 13:38:01 +03:00

Improvements to the design of the /proc lock analysis.

FossilOrigin-Name: f2809dd3b266c0a6a6eb4ed62812a907493fb4006d26f8905df7ff2b1ca01a5e
This commit is contained in:
drh
2025-10-24 23:12:33 +00:00
parent aeb886bc53
commit 003737d1dc
3 changed files with 113 additions and 120 deletions

View File

@@ -1,5 +1,5 @@
C Easier\sto\sread\s/proc\slock\sinterpretation.
D 2025-10-24T20:26:13.562
C Improvements\sto\sthe\sdesign\sof\sthe\s/proc\slock\sanalysis.
D 2025-10-24T23:12:33.738
F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -720,7 +720,7 @@ F src/os.h 1ff5ae51d339d0e30d8a9d814f4b8f8e448169304d83a7ed9db66a65732f3e63
F src/os_common.h 6c0eb8dd40ef3e12fe585a13e709710267a258e2c8dd1c40b1948a1d14582e06
F src/os_kv.c fb7ba8d6204197357f1eb7e1c7450d09c10043bf7e99aba602f4aa46b8fb11a3
F src/os_setup.h 8efc64eda6a6c2f221387eefc2e7e45fd5a3d5c8337a7a83519ba4fbd2957ae2
F src/os_unix.c b5f955d80854ffb836dc61aab2e3207cbe75fb1f75abe465af1e0fd6e4185c0c
F src/os_unix.c ef2cab7e9adf8a9123fedf9570e517bee90afd5c28e0670665077cdcb1ead0eb
F src/os_win.c 5b14841f9c3ab76841c06a4eca20ba3e7747f44253eba9dfd3d918797e753d49
F src/os_win.h 4c247cdb6d407c75186c94a1e84d5a22cbae4adcec93fcae8d2bc1f956fd1f19
F src/pager.c 113f9149092ccff6cf90e97c2611200e5a237f13d26c394bc9fd933377852764
@@ -2171,8 +2171,8 @@ F tool/version-info.c 33d0390ef484b3b1cb685d59362be891ea162123cea181cb8e6d2cf6dd
F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
F tool/warnings.sh 1ad0169b022b280bcaaf94a7fa231591be96b514230ab5c98fbf15cd7df842dd
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P ce75111cced8a03f05cf47ef809b9f017f2dac3c64c6008d595f28dbfec32057
R e81b72d89ea720b63d81165aacbbddaf
P d7bcc54beb6463501c0559333455bd2a378798b58cb48f35590e9026d07acd2f
R 8d33f28289c9578897478104576c4047
U drh
Z 2d238be892f323f949a2bd6e1256d798
Z d0703c0dd028be4d90d4623b7127b76b
# Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
d7bcc54beb6463501c0559333455bd2a378798b58cb48f35590e9026d07acd2f
f2809dd3b266c0a6a6eb4ed62812a907493fb4006d26f8905df7ff2b1ca01a5e

View File

@@ -596,6 +596,108 @@ static struct unix_syscall {
}; /* End of the overrideable system calls */
#if __linux__ && \
(defined(SQLITE_DEBUG) \
|| defined(SQLITE_ENABLE_FILE_INFO) \
|| defined(SQLITE_ENABLE_PAL_ASSERT))
/* ^^^--- Mnemonic: Posix Advisory Lock */
/*
** Examine locking information from the /proc/PID/fdinfo/FD pseudo-file.
**
** If pStr is not NULL, append text that describes the locks to pStr.
**
** if iAssert is not 0, then verify that the there exists an posix
** advisory lock (PAL) on byte iAssert of the file that fd is pointing
** to.
**
** This routine no-ops if /proc/PID/fdinfo/FD cannot be opened.
**
** This routine is intended for diagnostic purposes only and is not a part
** of ordinary builds.
*/
static void unixProcFSLocks(
int fd, /* The file descriptor to analyze */
sqlite3_str *pStr, /* Write a text description of PALs here */
sqlite3_uint64 iAssert /* Assert that this lock is held */
){
int in;
ssize_t n;
char *p, *pNext, *x;
int nLock = 0;
char z[2000];
sqlite3_snprintf(sizeof(z), z, "/proc/%d/fdinfo/%d", getpid(), fd);
in = open(z, O_RDONLY);
if( in<0 ){
if( pStr ) sqlite3_str_appendall(pStr,"(not-available)");
return;
}
n = read(in, z, sizeof(z)-1);
close(in);
if( n<=0 ) return;
z[n] = 0;
pNext = strstr(z, "lock:\t");
while( pNext ){
char cType = 0;
sqlite3_int64 iFirst, iLast;
p = pNext+6;
pNext = strstr(p, "lock:\t");
if( pNext ) pNext[-1] = 0;
if( strstr(p, " READ ")!=0 ){
cType = 'R';
}else if( strstr(p, " WRITE ")!=0 ){
cType = 'W';
}
if( cType==0 ) continue;
x = strrchr(p, ' ');
if( x==0 ) continue;
iLast = strtoll(x+1, 0, 10);
*x = 0;
x = strrchr(p, ' ');
if( x==0 ) continue;
iFirst = strtoll(x+1, 0, 10);
if( pStr ){
const int shmBase = (22+SQLITE_SHM_NLOCK)*4;
if( (nLock++)>0 ) sqlite3_str_append(pStr," ",1);
sqlite3_str_appendf(pStr, "%c ", cType);
if( iFirst>=PENDING_BYTE ){
/* "P+" stands for PENDING_BYTE+ */
sqlite3_str_appendf(pStr, "P+%lld", iFirst - PENDING_BYTE);
}else if( iFirst>=shmBase ){
/* "B+" stands for UNIX_SHM_BASE+ */
sqlite3_str_appendf(pStr, "B+%lld", iFirst - shmBase);
}else{
sqlite3_str_appendf(pStr, "%lld", iFirst);
}
if( iLast>iFirst ){
sqlite3_str_appendf(pStr,"(%lld)", iLast-iFirst+1);
}
}
if( iAssert>=iFirst && iAssert<=iLast ) iAssert = 0;
}
#if defined(SQLITE_ENABLE_PAL_ASSERT) || defined(SQLITE_DEBUG)
if( iAssert ){
sqlite3_snprintf(sizeof(z), z,
"a Posix-advisory lock is missing from byte %lld of fd %d\n",
iAssert, fd);
write(2, z, strlen(z));
abort();
}
#else
(void)iAssert;
#endif
}
#else
# define unixProcFSLocks(A,B,C) /* no-op */
#endif /* __linux__ && (SQLITE_DEBUG || SQLITE_ENABLE_<various>) */
/*
** If compiled with -DSQLITE_ENABLE_PAL_ASSERT, then the ASSERT_PAL_HELD(X,Y)
** macro will print an error on file descriptor 2 and abort if there is
** no PAL on byte Y of file-descriptor x.
*/
#define ASSERT_PAL_HELD(fd,loc) unixProcFSLocks(fd,0,loc)
/*
** On some systems, calls to fchown() will trigger a message in a security
** log if they come from non-root processes. So avoid calling fchown() if
@@ -2119,6 +2221,7 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
/* ASSERT_PAL_HELD(pFile->h, PENDING_BYTE+2); */
if( unixFileLock(pFile, &lock)==0 ){
pInode->eFileLock = NO_LOCK;
}else{
@@ -3981,119 +4084,6 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){
return SQLITE_OK;
}
#if __linux__ && \
(defined(SQLITE_DEBUG) \
|| defined(SQLITE_ENABLE_FILE_INFO) \
|| defined(SQLITE_ENABLE_PROC_ASSERT))
/*
** Examine locking information from the /proc/PID/fdinfo/FD pseudo-file.
**
** If pStr is not NULL, append text that describes the locks to pStr.
**
** if iAssert is not 0, then verify that the byte iAssert/4 is either:
**
** * No locks are held if iAssert%4==0
** * A read-lock is held if iAssert%4==1
** * A write-lock is held if iAssert%4==2
** * Any lock is held if iAssert%4==3
**
** This routine no-ops if /proc/PID/fdinfo/FD cannot be opened.
**
** This routine is intended for diagnostic purposes only and is not a part
** of ordinary builds.
*/
static void unixProcFSLocks(int fd, sqlite3_str *pStr, sqlite3_uint64 iAssert){
int in;
ssize_t n;
char *p, *pNext, *x;
int nLock = 0;
char z[2000];
sqlite3_snprintf(sizeof(z), z, "/proc/%d/fdinfo/%d", getpid(), fd);
in = open(z, O_RDONLY);
if( in<0 ){
if( pStr ) sqlite3_str_appendall(pStr,"(not-available)");
return;
}
n = read(in, z, sizeof(z)-1);
close(in);
if( n>0 ){
z[n] = 0;
pNext = strstr(z, "lock:\t");
while( pNext ){
char cType = 0;
sqlite3_int64 iFirst, iLast;
p = pNext+6;
pNext = strstr(p, "lock:\t");
if( pNext ) pNext[-1] = 0;
if( strstr(p, " READ ")!=0 ){
cType = 'R';
}else if( strstr(p, " WRITE ")!=0 ){
cType = 'W';
}
if( cType ){
x = strrchr(p, ' ');
if( x ){
iLast = strtoll(x+1, 0, 10);
*x = 0;
x = strrchr(p, ' ');
if( x ){
iFirst = strtoll(x+1, 0, 10);
if( pStr ){
const int shmBase = (22+SQLITE_SHM_NLOCK)*4;
if( (nLock++)>0 ) sqlite3_str_append(pStr," ",1);
sqlite3_str_appendf(pStr, "%c ", cType);
if( iFirst>=PENDING_BYTE ){
/* "P+" stands for PENDING_BYTE+ */
sqlite3_str_appendf(pStr, "P+%lld", iFirst - PENDING_BYTE);
}else if( iFirst>=shmBase ){
/* "B+" stands for UNIX_SHM_BASE+ */
sqlite3_str_appendf(pStr, "B+%lld", iFirst - shmBase);
}else{
sqlite3_str_appendf(pStr, "%lld", iFirst);
}
if( iLast>iFirst ){
sqlite3_str_appendf(pStr,"(%lld)", iLast-iFirst+1);
}
}
if( iAssert!=0 ){
if( iAssert/4>=iFirst && iAssert/4<=iLast ){
switch( iAssert%4 ){
case 0:
sqlite3_snprintf(sizeof(z), z,
"byte %lld for fd %d should be unlocked\n",
iAssert/4, fd);
write(2, z, strlen(z));
abort();
case 1:
if( cType=='R' ) iAssert = 0;
break;
case 2:
if( cType=='W' ) iAssert = 0;
break;
case 3:
iAssert = 0;
break;
}
}
}
}
}
}
}
if( iAssert ){
sqlite3_snprintf(sizeof(z), z,
"byte %lld of fd %d should be locked\n",
iAssert/4, fd);
write(2, z, strlen(z));
abort();
}
}
}
#else
# define verifyProcLocks(A,B,C) /* no-op */
#endif /* __linux__ && (SQLITE_DEBUG || SQLITE_ENABLE_<various>) */
/*
** If *pArg is initially negative then this is a query. Set *pArg to
** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
@@ -4635,6 +4625,9 @@ static int unixShmSystemLock(
|| (ofst>=UNIX_SHM_BASE && ofst+n<=(UNIX_SHM_BASE+SQLITE_SHM_NLOCK))
);
if( ofst==UNIX_SHM_DMS ){
if( lockType==F_UNLCK && pShmNode->hShm>=0 ){
ASSERT_PAL_HELD(pShmNode->hShm, ofst);
}
assert( pShmNode->nRef>0 || unixMutexHeld() );
assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
}else{